한국거래소(KRX) 주식 데이터 API 서버 - 자연어 질의 지원
FastAPI 기반 REST API 서버로, 자연어 질문을 분석하여 적절한 주식 데이터를 반환합니다.
| 기능 | 설명 |
|---|---|
| 자연어 API | "삼성전자 주가" → 자동으로 API 호출 |
| 하이브리드 분류 | 키워드 → 임베딩 → LLM 3단계 폴백 |
| KRX 로그인 | PER/PBR, 외국인 보유율 등 로그인 필요 API 지원 |
| pykrx 통합 | pykrx 라이브러리 기반 안정적 데이터 수집 |
git clone https://github.com/YOUR_USERNAME/pykrx-api-server.git
cd pykrx-api-server
# 가상환경 생성 (권장)
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# 의존성 설치
pip install -r requirements.txt# LLM 분류 사용 시 (선택)
export GEMINI_API_KEY=your_api_keypython main.py서버가 http://localhost:8000에서 실행됩니다.
# 시가총액 상위 종목
curl "http://localhost:8000/api/nl?query=코스피+시가총액+상위+10개"
# 특정 종목 주가
curl "http://localhost:8000/api/nl?query=삼성전자+주가"
# ETF 목록
curl "http://localhost:8000/api/nl?query=ETF+목록"
# 지수 조회
curl "http://localhost:8000/api/nl?query=코스닥150+지수"
# 외국인 보유율
curl "http://localhost:8000/api/nl?query=SK하이닉스+외국인+보유율"# 주가 조회
curl "http://localhost:8000/api/stocks/ohlcv?ticker=005930"
# 시가총액
curl "http://localhost:8000/api/stocks/market-cap?market=KOSPI&top_n=10"
# ETF 목록
curl "http://localhost:8000/api/etf/all"
# 지수 시세
curl "http://localhost:8000/api/index/ohlcv?index_code=1001"
# 서버 상태
curl "http://localhost:8000/api/status"{
"success": true,
"query": "코스피 시가총액 상위 10개",
"intent": "market_cap",
"confidence": 0.82,
"method": "embedding",
"data": [
{
"종목코드": "005930",
"종목명": "삼성전자",
"시가총액": 398456000000000,
"시가총액_조": 398.46
}
],
"count": 10,
"latency_ms": 245.3
}3단계 폴백 시스템으로 높은 정확도를 보장합니다:
| 단계 | 방식 | 속도 | 신뢰도 임계값 |
|---|---|---|---|
| 1 | 키워드 매칭 | ~1ms | 0.7 |
| 2 | 임베딩 유사도 | ~100ms | 0.6 |
| 3 | LLM (Gemini) | ~1s | - |
| 인텐트 | 키워드 예시 | API 엔드포인트 |
|---|---|---|
stock_price |
주가, 종가, 시세 | /api/stocks/ohlcv |
market_cap |
시가총액, 시총 | /api/stocks/market-cap |
etf_list |
ETF, 상장지수펀드 | /api/etf/all |
index_price |
지수, 코스피, 코스닥 | /api/index/ohlcv |
foreign_holding |
외국인 보유율, 외인 지분 | /api/stocks/foreign-holding |
fundamental |
PER, PBR, 배당 | /api/stocks/fundamental |
investor_trading |
외국인 매매, 기관 순매수 | /api/stocks/investor-trading |
short_selling |
공매도, 대차 | /api/short-selling/trading |
일부 API는 KRX 로그인이 필요합니다:
- PER/PBR/배당수익률
- 외국인 보유율
- 투자자별 거래
- 공매도 현황
- ETF/ETN/ELW (일부)
# krx_session.py 실행으로 쿠키 생성
python krx_session.py생성된 .krx_cookies.pkl 파일이 서버 시작 시 자동 로드됩니다.
pykrx-api-server/
├── main.py # FastAPI 서버 (2,300+ lines)
├── intent_classifier.py # 하이브리드 인텐트 분류기
├── krx_session.py # KRX 로그인 세션 관리
├── requirements.txt # 의존성
├── PYKRX_API_SPEC.md # API 상세 명세서
├── api_schema.json # API 스키마 (LLM용)
└── KRX_DATA_COLLECTION_GUIDE.md # 데이터 수집 가이드
fastapi>=0.100.0
uvicorn>=0.23.0
pykrx>=1.0.45
pandas>=2.0.0
requests>=2.28.0
sentence-transformers>=2.2.0 # 임베딩 기반 분류
numpy>=1.24.0
google-generativeai>=0.3.0 # LLM 폴백
# PowerShell
.\test-nl-api.ps1
# Python
python test_nl_api.pySwagger UI: http://localhost:8000/docs
MIT License
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request