stock-lab: Gemini Pro AI 포트폴리오 분석 기능 추가
- ai_analyst.py 신규: Gemini Pro 연동 포트폴리오 분석 모듈 - 보유 종목 현재가 + 뉴스 기반 프롬프트 생성 - 종목별 매도/매수/분할매도 행동 지침 포함 - 5분 메모리 캐시 (force 파라미터로 강제 갱신 가능) - GET /api/stock/ai-analysis 엔드포인트 추가 - requirements.txt: google-generativeai>=0.8.0 추가 환경변수 필요: GEMINI_API_KEY (Google AI Studio) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,7 @@ from .db import (
|
||||
)
|
||||
from .scraper import fetch_market_news, fetch_major_indices, fetch_overseas_news
|
||||
from .price_fetcher import get_current_prices
|
||||
from .ai_analyst import analyze_portfolio as ai_analyze_portfolio
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@@ -410,3 +411,47 @@ def remove_sell_history(record_id: int):
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
# AI 포트폴리오 분석 (Gemini Pro)
|
||||
# ═══════════════════════════════════════════════════════════════
|
||||
|
||||
@app.get("/api/stock/ai-analysis")
|
||||
def get_ai_analysis(force: bool = False):
|
||||
"""AI 전문가 포트폴리오 분석 (Gemini Pro).
|
||||
|
||||
- 캐시: 5분 TTL (force=true 로 강제 갱신)
|
||||
- 보유 종목 현재가 + 최근 뉴스를 포함해 Gemini에 전달
|
||||
"""
|
||||
# 포트폴리오 + 현재가 조회
|
||||
items = get_all_portfolio()
|
||||
if items:
|
||||
tickers = list({item["ticker"] for item in items})
|
||||
prices = get_current_prices(tickers)
|
||||
else:
|
||||
prices = {}
|
||||
|
||||
holdings = []
|
||||
for item in items:
|
||||
cp = prices.get(item["ticker"])
|
||||
buy = item["avg_price"] * item["quantity"]
|
||||
eval_amt = cp * item["quantity"] if cp is not None else None
|
||||
profit = (eval_amt - buy) if eval_amt is not None else None
|
||||
rate = round((profit / buy) * 100, 2) if (profit is not None and buy) else None
|
||||
holdings.append({
|
||||
**item,
|
||||
"current_price": cp,
|
||||
"profit_amount": profit,
|
||||
"profit_rate": rate,
|
||||
})
|
||||
|
||||
# 최근 뉴스 (국내 20건)
|
||||
news = get_latest_articles(20)
|
||||
if not isinstance(news, list):
|
||||
news = []
|
||||
|
||||
try:
|
||||
return ai_analyze_portfolio(holdings, news, force=force)
|
||||
except RuntimeError as exc:
|
||||
return JSONResponse(status_code=500, content={"error": str(exc)})
|
||||
|
||||
Reference in New Issue
Block a user