Commit ·
f7ca1dc
1
Parent(s): 2e81b4d
시간이 오래걸리는 web_search 제거
Browse files- .env.example +1 -0
- data/collect_data.py +22 -21
- llm/generator.py +24 -147
- pipeline.py +79 -209
.env.example
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
LLM_MODEL_NAME=gpt-5-mini
|
| 2 |
LLM_MODEL_API_KEY=
|
|
|
|
|
|
| 1 |
LLM_MODEL_NAME=gpt-5-mini
|
| 2 |
LLM_MODEL_API_KEY=
|
| 3 |
+
LOG_FILE=analysis_results.jsonl
|
data/collect_data.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
|
| 3 |
from data.earnings import fetch_earnings_data
|
| 4 |
from data.finance_fetchers import fetch_fundamentals, fetch_news, fetch_price_data
|
|
@@ -7,7 +9,7 @@ from llm.web_search import fetch_web_search
|
|
| 7 |
from utils.data_types import MarketData
|
| 8 |
|
| 9 |
|
| 10 |
-
|
| 11 |
# 인텐트와 선택된 도구 목록을 기반으로 모든 시장 데이터 수집
|
| 12 |
ticker = intent.get("ticker", "")
|
| 13 |
period = intent.get("time_range", "1y")
|
|
@@ -23,40 +25,39 @@ async def collect_data(client, intent, tools):
|
|
| 23 |
|
| 24 |
if ticker:
|
| 25 |
if "price" in tools:
|
| 26 |
-
price_data =
|
| 27 |
print(f" → 가격 데이터: {len(price_data)}개 지표")
|
| 28 |
|
| 29 |
if "fundamentals" in tools:
|
| 30 |
-
fundamentals =
|
| 31 |
print(f" → 펀더멘털: {len(fundamentals)}개 지표")
|
| 32 |
|
| 33 |
if "technicals" in tools:
|
| 34 |
-
technicals =
|
| 35 |
print(f" → 기술지표: {len(technicals)}개 지표")
|
| 36 |
|
| 37 |
if "news" in tools:
|
| 38 |
-
news_snippets =
|
| 39 |
print(f" → 뉴스: {len(news_snippets)}개 헤드라인")
|
| 40 |
|
| 41 |
if "earnings" in tools:
|
| 42 |
-
earnings_data =
|
| 43 |
-
fetch_earnings_data,
|
| 44 |
ticker,
|
| 45 |
target_year=intent.get("target_year"),
|
| 46 |
-
target_quarter=intent.get("target_quarter")
|
| 47 |
)
|
| 48 |
-
|
| 49 |
-
if "web_search" in tools:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
return MarketData(
|
| 62 |
ticker=ticker,
|
|
|
|
| 1 |
+
from typing import Any
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import yfinance as yf
|
| 4 |
|
| 5 |
from data.earnings import fetch_earnings_data
|
| 6 |
from data.finance_fetchers import fetch_fundamentals, fetch_news, fetch_price_data
|
|
|
|
| 9 |
from utils.data_types import MarketData
|
| 10 |
|
| 11 |
|
| 12 |
+
def collect_data(client, intent, tools):
|
| 13 |
# 인텐트와 선택된 도구 목록을 기반으로 모든 시장 데이터 수집
|
| 14 |
ticker = intent.get("ticker", "")
|
| 15 |
period = intent.get("time_range", "1y")
|
|
|
|
| 25 |
|
| 26 |
if ticker:
|
| 27 |
if "price" in tools:
|
| 28 |
+
price_data = fetch_price_data(ticker, period)
|
| 29 |
print(f" → 가격 데이터: {len(price_data)}개 지표")
|
| 30 |
|
| 31 |
if "fundamentals" in tools:
|
| 32 |
+
fundamentals = fetch_fundamentals(ticker)
|
| 33 |
print(f" → 펀더멘털: {len(fundamentals)}개 지표")
|
| 34 |
|
| 35 |
if "technicals" in tools:
|
| 36 |
+
technicals = fetch_technicals(ticker, "6mo")
|
| 37 |
print(f" → 기술지표: {len(technicals)}개 지표")
|
| 38 |
|
| 39 |
if "news" in tools:
|
| 40 |
+
news_snippets = fetch_news(ticker)
|
| 41 |
print(f" → 뉴스: {len(news_snippets)}개 헤드라인")
|
| 42 |
|
| 43 |
if "earnings" in tools:
|
| 44 |
+
earnings_data = fetch_earnings_data(
|
|
|
|
| 45 |
ticker,
|
| 46 |
target_year=intent.get("target_year"),
|
| 47 |
+
target_quarter=intent.get("target_quarter")
|
| 48 |
)
|
| 49 |
+
# fetch_web_search는 너무 오래 걸린다.
|
| 50 |
+
# if "web_search" in tools:
|
| 51 |
+
# company_name = fundamentals.get("company_name", ticker) if fundamentals else ticker
|
| 52 |
+
# web_search_results = fetch_web_search(
|
| 53 |
+
# client=client,
|
| 54 |
+
# ticker=ticker,
|
| 55 |
+
# company_name=company_name,
|
| 56 |
+
# analysis_type=intent.get("analysis_type", "general"),
|
| 57 |
+
# language=intent.get("language", "ko"),
|
| 58 |
+
# target_year=intent.get("target_year"),
|
| 59 |
+
# target_quarter=intent.get("target_quarter"),
|
| 60 |
+
# )
|
| 61 |
|
| 62 |
return MarketData(
|
| 63 |
ticker=ticker,
|
llm/generator.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import time
|
| 4 |
from .prompts import SYSTEM_PROMPTS
|
| 5 |
from data.news import search_google_news, format_news_list
|
| 6 |
|
|
@@ -8,7 +7,6 @@ from data.news import search_google_news, format_news_list
|
|
| 8 |
# ⑤ LLM 분석 생성 (Responses API + web_search 상시 활성)
|
| 9 |
# ─────────────────────────────────────────────
|
| 10 |
|
| 11 |
-
|
| 12 |
def extract_response_text(resp):
|
| 13 |
# Responses API 출력에서 텍스트 블록만 추출하여 합산
|
| 14 |
texts = []
|
|
@@ -23,7 +21,7 @@ def extract_response_text(resp):
|
|
| 23 |
return "\n".join(texts)
|
| 24 |
|
| 25 |
|
| 26 |
-
|
| 27 |
"""LLM을 통해 구글 뉴스 검색어 리스트 생성"""
|
| 28 |
language = intent.get("language", "ko")
|
| 29 |
prompt = f"""
|
|
@@ -32,56 +30,37 @@ async def generate_search_keywords(client, user_query, intent):
|
|
| 32 |
[사용자 질의]
|
| 33 |
{user_query}
|
| 34 |
"""
|
| 35 |
-
LLM_MODEL_NAME = os.environ.get(
|
| 36 |
-
resp =
|
| 37 |
model=LLM_MODEL_NAME,
|
| 38 |
-
messages=[
|
| 39 |
-
|
| 40 |
-
{"role": "user", "content": prompt},
|
| 41 |
-
],
|
| 42 |
)
|
|
|
|
| 43 |
content = resp.choices[0].message.content
|
| 44 |
try:
|
| 45 |
keywords = json.loads(content)
|
| 46 |
if isinstance(keywords, list):
|
| 47 |
return keywords
|
| 48 |
except Exception:
|
| 49 |
-
|
|
|
|
| 50 |
return []
|
| 51 |
|
| 52 |
|
| 53 |
-
|
| 54 |
-
"""LLM 키워드 생성 + 구글 뉴스 검색 + 포맷까지 한 번에 처리
|
| 55 |
language = intent.get("language", "ko")
|
| 56 |
-
keywords =
|
| 57 |
news_list = search_google_news(keywords, language=language)
|
| 58 |
news_str = format_news_list(news_list)
|
| 59 |
return news_str
|
| 60 |
|
| 61 |
|
| 62 |
-
def
|
| 63 |
-
#
|
| 64 |
-
delta = getattr(event, "delta", None)
|
| 65 |
-
if delta:
|
| 66 |
-
return delta
|
| 67 |
-
if isinstance(event, dict):
|
| 68 |
-
return event.get("delta", "")
|
| 69 |
-
return ""
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
async def generate_analysis(
|
| 73 |
-
client,
|
| 74 |
-
user_query,
|
| 75 |
-
context,
|
| 76 |
-
intent,
|
| 77 |
-
news_str,
|
| 78 |
-
stream_output=False,
|
| 79 |
-
stream_callback=None,
|
| 80 |
-
log_callback=None,
|
| 81 |
-
):
|
| 82 |
-
# Async Responses API로 최종 투자 분석 리포트 생성
|
| 83 |
analysis_type = intent.get("analysis_type", "general")
|
| 84 |
-
language
|
| 85 |
system_prompt = SYSTEM_PROMPTS.get(analysis_type, SYSTEM_PROMPTS["general"])
|
| 86 |
system_prompt += f"\n\n반드시 {language} 언어로 답변하세요. 투자 조언이 아닌 정보 제공임을 명시하세요."
|
| 87 |
|
|
@@ -96,122 +75,20 @@ async def generate_analysis(
|
|
| 96 |
[최신 구글 뉴스]
|
| 97 |
{news_str} """
|
| 98 |
|
| 99 |
-
LLM_MODEL_NAME = os.environ.get(
|
| 100 |
-
|
| 101 |
-
delta_log_preview_chars = int(os.environ.get("DELTA_LOG_PREVIEW_CHARS", "80"))
|
| 102 |
-
start_msg = f"[⑤] LLM 분석 생성 중 (Responses API, 모델: {LLM_MODEL_NAME})..."
|
| 103 |
-
if log_callback:
|
| 104 |
-
log_callback(start_msg)
|
| 105 |
-
else:
|
| 106 |
-
print(start_msg)
|
| 107 |
|
| 108 |
-
|
| 109 |
model=LLM_MODEL_NAME,
|
| 110 |
-
tools=[
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
],
|
| 116 |
input=full_input,
|
| 117 |
)
|
| 118 |
|
| 119 |
-
if stream_output:
|
| 120 |
-
try:
|
| 121 |
-
all_deltas = []
|
| 122 |
-
first_event_logged = False
|
| 123 |
-
delta_count = 0
|
| 124 |
-
delta_total_chars = 0
|
| 125 |
-
response_created_at = None
|
| 126 |
-
first_delta_latency_logged = False
|
| 127 |
-
|
| 128 |
-
async with client.responses.stream(**request_kwargs) as stream:
|
| 129 |
-
if log_callback:
|
| 130 |
-
log_callback("[⑤][stream] stream context 진입 완료")
|
| 131 |
-
else:
|
| 132 |
-
print("[⑤][stream] stream context 진입 완료")
|
| 133 |
-
|
| 134 |
-
async for event in stream:
|
| 135 |
-
if not first_event_logged:
|
| 136 |
-
event_type = getattr(event, "type", None)
|
| 137 |
-
msg = f"[⑤][stream] 첫 이벤트 수신 type={event_type}"
|
| 138 |
-
if log_callback:
|
| 139 |
-
log_callback(msg)
|
| 140 |
-
else:
|
| 141 |
-
print(msg)
|
| 142 |
-
first_event_logged = True
|
| 143 |
-
|
| 144 |
-
if getattr(event, "type", None) == "response.created" and response_created_at is None:
|
| 145 |
-
response_created_at = time.perf_counter()
|
| 146 |
-
|
| 147 |
-
if getattr(event, "type", None) == "response.output_text.delta":
|
| 148 |
-
delta = _extract_delta_text(event)
|
| 149 |
-
if delta:
|
| 150 |
-
all_deltas.append(delta)
|
| 151 |
-
delta_count += 1
|
| 152 |
-
delta_total_chars += len(delta)
|
| 153 |
-
|
| 154 |
-
if response_created_at is not None and not first_delta_latency_logged:
|
| 155 |
-
latency_s = time.perf_counter() - response_created_at
|
| 156 |
-
latency_msg = f"[⑤][stream] response.created -> 첫 delta 지연: {latency_s:.3f}초"
|
| 157 |
-
if log_callback:
|
| 158 |
-
log_callback(latency_msg)
|
| 159 |
-
else:
|
| 160 |
-
print(latency_msg)
|
| 161 |
-
first_delta_latency_logged = True
|
| 162 |
-
|
| 163 |
-
should_log_delta = delta_count <= 3 or (delta_log_every > 0 and delta_count % delta_log_every == 0)
|
| 164 |
-
if should_log_delta:
|
| 165 |
-
preview = delta.replace("\n", "\\n")[:delta_log_preview_chars]
|
| 166 |
-
msg = f"[⑤][stream] delta#{delta_count} len={len(delta)} total_chars={delta_total_chars} preview='{preview}'"
|
| 167 |
-
if log_callback:
|
| 168 |
-
log_callback(msg)
|
| 169 |
-
else:
|
| 170 |
-
print(msg)
|
| 171 |
-
|
| 172 |
-
if stream_callback:
|
| 173 |
-
stream_callback(delta)
|
| 174 |
-
else:
|
| 175 |
-
print(delta, end="", flush=True)
|
| 176 |
-
|
| 177 |
-
if not stream_callback:
|
| 178 |
-
print()
|
| 179 |
-
|
| 180 |
-
if response_created_at is not None and not first_delta_latency_logged:
|
| 181 |
-
no_delta_msg = "[⑤][stream] response.created 이후 delta 미수신"
|
| 182 |
-
if log_callback:
|
| 183 |
-
log_callback(no_delta_msg)
|
| 184 |
-
else:
|
| 185 |
-
print(no_delta_msg)
|
| 186 |
-
|
| 187 |
-
summary_msg = f"[⑤][stream] delta 수집 요약: chunks={delta_count}, total_chars={delta_total_chars}"
|
| 188 |
-
if log_callback:
|
| 189 |
-
log_callback(summary_msg)
|
| 190 |
-
else:
|
| 191 |
-
print(summary_msg)
|
| 192 |
-
|
| 193 |
-
if log_callback:
|
| 194 |
-
log_callback("[⑤][stream] 최종 응답 수집 시작")
|
| 195 |
-
else:
|
| 196 |
-
print("[⑤][stream] 최종 응답 수집 시작")
|
| 197 |
-
final_resp = await stream.get_final_response()
|
| 198 |
-
if log_callback:
|
| 199 |
-
log_callback("[⑤][stream] 최종 응답 수집 완료")
|
| 200 |
-
else:
|
| 201 |
-
print("[⑤][stream] 최종 응답 수집 완료")
|
| 202 |
-
|
| 203 |
-
result = extract_response_text(final_resp)
|
| 204 |
-
if result:
|
| 205 |
-
return result
|
| 206 |
-
joined = "".join(all_deltas).strip()
|
| 207 |
-
return joined or "(분석 결과를 가져오지 못했습니다)"
|
| 208 |
-
except Exception as e:
|
| 209 |
-
err_msg = f"[⑤] 스트리밍 출력 실패, 일반 모드로 재시도: {e}"
|
| 210 |
-
if log_callback:
|
| 211 |
-
log_callback(err_msg)
|
| 212 |
-
else:
|
| 213 |
-
print(err_msg)
|
| 214 |
-
|
| 215 |
-
resp = await client.responses.create(**request_kwargs)
|
| 216 |
result = extract_response_text(resp)
|
| 217 |
return result or "(분석 결과를 가져오지 못했습니다)"
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
from .prompts import SYSTEM_PROMPTS
|
| 4 |
from data.news import search_google_news, format_news_list
|
| 5 |
|
|
|
|
| 7 |
# ⑤ LLM 분석 생성 (Responses API + web_search 상시 활성)
|
| 8 |
# ─────────────────────────────────────────────
|
| 9 |
|
|
|
|
| 10 |
def extract_response_text(resp):
|
| 11 |
# Responses API 출력에서 텍스트 블록만 추출하여 합산
|
| 12 |
texts = []
|
|
|
|
| 21 |
return "\n".join(texts)
|
| 22 |
|
| 23 |
|
| 24 |
+
def generate_search_keywords(client, user_query, intent):
|
| 25 |
"""LLM을 통해 구글 뉴스 검색어 리스트 생성"""
|
| 26 |
language = intent.get("language", "ko")
|
| 27 |
prompt = f"""
|
|
|
|
| 30 |
[사용자 질의]
|
| 31 |
{user_query}
|
| 32 |
"""
|
| 33 |
+
LLM_MODEL_NAME = os.environ.get('LLM_MODEL_NAME')
|
| 34 |
+
resp = client.chat.completions.create(
|
| 35 |
model=LLM_MODEL_NAME,
|
| 36 |
+
messages=[{"role": "system", "content": "뉴스 검색 키워드 생성"},
|
| 37 |
+
{"role": "user", "content": prompt}],
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
+
# 키워드 리스트 추출 (예: ['삼성전자', '반도체 전망', ...])
|
| 40 |
content = resp.choices[0].message.content
|
| 41 |
try:
|
| 42 |
keywords = json.loads(content)
|
| 43 |
if isinstance(keywords, list):
|
| 44 |
return keywords
|
| 45 |
except Exception:
|
| 46 |
+
# 리스트 형태가 아니면 줄바꿈/쉼표로 분리
|
| 47 |
+
return [k.strip() for k in content.replace('\n', ',').split(',') if k.strip()]
|
| 48 |
return []
|
| 49 |
|
| 50 |
|
| 51 |
+
def generate_news_info(client, user_query, intent):
|
| 52 |
+
"""LLM 키워드 생성 + 구글 뉴스 검색 + 포맷까지 한 번에 처리"""
|
| 53 |
language = intent.get("language", "ko")
|
| 54 |
+
keywords = generate_search_keywords(client, user_query, intent)
|
| 55 |
news_list = search_google_news(keywords, language=language)
|
| 56 |
news_str = format_news_list(news_list)
|
| 57 |
return news_str
|
| 58 |
|
| 59 |
|
| 60 |
+
def generate_analysis(client, user_query, context, intent, news_str):
|
| 61 |
+
# Responses API + web_search로 최종 투자 분석 리포트 생성
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
analysis_type = intent.get("analysis_type", "general")
|
| 63 |
+
language = intent.get("language", "ko")
|
| 64 |
system_prompt = SYSTEM_PROMPTS.get(analysis_type, SYSTEM_PROMPTS["general"])
|
| 65 |
system_prompt += f"\n\n반드시 {language} 언어로 답변하세요. 투자 조언이 아닌 정보 제공임을 명시하세요."
|
| 66 |
|
|
|
|
| 75 |
[최신 구글 뉴스]
|
| 76 |
{news_str} """
|
| 77 |
|
| 78 |
+
LLM_MODEL_NAME = os.environ.get('LLM_MODEL_NAME')
|
| 79 |
+
print(f"[⑤] LLM 분석 생성 중 (Responses API, 모델: {LLM_MODEL_NAME})...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
resp = client.responses.create(
|
| 82 |
model=LLM_MODEL_NAME,
|
| 83 |
+
# tools=[
|
| 84 |
+
# {
|
| 85 |
+
# "type": "web_search",
|
| 86 |
+
# "user_location": {"type": "approximate", "country": "KR"}
|
| 87 |
+
# }
|
| 88 |
+
# ],
|
| 89 |
input=full_input,
|
| 90 |
)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
result = extract_response_text(resp)
|
| 93 |
return result or "(분석 결과를 가져오지 못했습니다)"
|
| 94 |
+
|
pipeline.py
CHANGED
|
@@ -1,20 +1,22 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import asyncio
|
| 4 |
import textwrap
|
| 5 |
-
|
| 6 |
from datetime import datetime
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
from openai import AsyncOpenAI
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
from llm.generator import generate_analysis, generate_news_info
|
|
|
|
| 16 |
from data.collect_data import collect_data
|
|
|
|
| 17 |
from context.context_builder import build_context
|
|
|
|
| 18 |
from utils.data_types import AnalysisResult
|
| 19 |
|
| 20 |
load_dotenv()
|
|
@@ -22,104 +24,40 @@ load_dotenv()
|
|
| 22 |
|
| 23 |
api_key = os.environ.get("LLM_MODEL_API_KEY")
|
| 24 |
if api_key:
|
| 25 |
-
|
| 26 |
else:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
PIPELINE_LOG_PATH = Path("results/pipeline.jsonl")
|
| 30 |
-
WAITING_STATUS_MESSAGES = [
|
| 31 |
-
"답변 구조를 정리하고 있어요...",
|
| 32 |
-
"핵심 포인트를 우선순위로 정리 중이에요...",
|
| 33 |
-
"시장 데이터와 맥락을 교차 검증하고 있어요...",
|
| 34 |
-
"근거를 확인하면서 답변을 다듬고 있어요...",
|
| 35 |
-
"요약과 리스크 포인트를 함께 정리하고 있어요...",
|
| 36 |
-
"답변 완성도를 높이고 있어요...",
|
| 37 |
-
]
|
| 38 |
-
WAITING_STATUS_INTERVAL_RANGE_SECONDS = 12
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def append_pipeline_jsonl_log(result, executed_at, request_id, process_timings=None):
|
| 42 |
-
PIPELINE_LOG_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 43 |
-
|
| 44 |
-
log_record = {
|
| 45 |
-
"request_id": request_id,
|
| 46 |
-
"executed_at": executed_at,
|
| 47 |
-
"query": result.query,
|
| 48 |
-
"ticker": result.ticker,
|
| 49 |
-
"analysis_type": result.analysis_type,
|
| 50 |
-
"timestamp": result.timestamp,
|
| 51 |
-
"data_context": result.data_context,
|
| 52 |
-
"llm_response": result.llm_response,
|
| 53 |
-
"process_timings": process_timings or {},
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
with PIPELINE_LOG_PATH.open("a", encoding="utf-8") as f:
|
| 57 |
-
f.write(json.dumps(log_record, ensure_ascii=False) + "\n")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def _emit_status(message, status_callback=None):
|
| 61 |
-
if status_callback:
|
| 62 |
-
status_callback(message)
|
| 63 |
-
|
| 64 |
|
| 65 |
-
def _now_str():
|
| 66 |
-
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
def _req_prefix(request_id):
|
| 70 |
-
return f"[req:{request_id}]"
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def _req_log_and_status_callback(request_id, on_latency_marker=None):
|
| 74 |
-
def _log(message):
|
| 75 |
-
print(f"{_req_prefix(request_id)} {message}")
|
| 76 |
-
if on_latency_marker and "response.created -> 첫 delta 지연:" in message:
|
| 77 |
-
on_latency_marker()
|
| 78 |
-
|
| 79 |
-
return _log
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
async def _emit_waiting_statuses_in_order(status_callback, stop_event):
|
| 83 |
-
if not status_callback:
|
| 84 |
-
return
|
| 85 |
-
|
| 86 |
-
index = 0
|
| 87 |
-
message_count = len(WAITING_STATUS_MESSAGES)
|
| 88 |
-
|
| 89 |
-
while not stop_event.is_set():
|
| 90 |
-
await asyncio.sleep(WAITING_STATUS_INTERVAL_RANGE_SECONDS)
|
| 91 |
-
if stop_event.is_set():
|
| 92 |
-
break
|
| 93 |
-
_emit_status(WAITING_STATUS_MESSAGES[index], status_callback)
|
| 94 |
-
index = (index + 1) % message_count
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
async def _run_stage(label, coro, stream_output, process_timings, request_id):
|
| 98 |
-
started_at_dt = datetime.now()
|
| 99 |
-
started_at = started_at_dt.strftime("%Y-%m-%d %H:%M:%S")
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
ended_at = ended_at_dt.strftime("%Y-%m-%d %H:%M:%S")
|
| 108 |
-
elapsed_seconds = round((ended_at_dt - started_at_dt).total_seconds(), 3)
|
| 109 |
-
|
| 110 |
-
process_timings[label] = {
|
| 111 |
-
"started_at": started_at,
|
| 112 |
-
"ended_at": ended_at,
|
| 113 |
-
"elapsed_seconds": elapsed_seconds,
|
| 114 |
}
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
return value
|
| 120 |
-
|
| 121 |
|
| 122 |
-
|
| 123 |
"""
|
| 124 |
파이프라인:
|
| 125 |
① 인텐트 파싱 (Chat Completions + Function Calling)
|
|
@@ -128,112 +66,45 @@ async def pipeline(query, stream_output=False, stream_callback=None, status_call
|
|
| 128 |
④ 컨텍스트 조립
|
| 129 |
⑤ 분석 생성 (Responses API + web_search 상시 활성)
|
| 130 |
"""
|
| 131 |
-
global
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
process_timings = {}
|
| 135 |
-
|
| 136 |
print(f"\n{'='*60}")
|
| 137 |
-
print(f"
|
| 138 |
print('='*60)
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
"① 인텐트 파싱",
|
| 143 |
-
parse_intent(async_client, query),
|
| 144 |
-
stream_output,
|
| 145 |
-
process_timings,
|
| 146 |
-
request_id,
|
| 147 |
-
)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
"② Tool 라우팅",
|
| 152 |
-
asyncio.to_thread(route_tools, intent),
|
| 153 |
-
stream_output,
|
| 154 |
-
process_timings,
|
| 155 |
-
request_id,
|
| 156 |
-
)
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
"③ 데이터 수집",
|
| 161 |
-
collect_data(async_client, intent, tools),
|
| 162 |
-
stream_output,
|
| 163 |
-
process_timings,
|
| 164 |
-
request_id,
|
| 165 |
-
)
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
)
|
| 176 |
-
print(f"{_req_prefix(request_id)} [④] 컨텍스트 빌드 완료 ({len(context)} 문자)")
|
| 177 |
-
|
| 178 |
-
_emit_status("AI가 답변을 작성하고 있어요...", status_callback)
|
| 179 |
-
waiting_stop_event = asyncio.Event()
|
| 180 |
-
waiting_task = None
|
| 181 |
-
if status_callback and stream_output:
|
| 182 |
-
waiting_task = asyncio.create_task(
|
| 183 |
-
_emit_waiting_statuses_in_order(status_callback, waiting_stop_event)
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
try:
|
| 187 |
-
response = await _run_stage(
|
| 188 |
-
"⑤ 분석 생성",
|
| 189 |
-
generate_analysis(
|
| 190 |
-
async_client,
|
| 191 |
-
query,
|
| 192 |
-
context,
|
| 193 |
-
intent,
|
| 194 |
-
news_str=news_str,
|
| 195 |
-
stream_output=stream_output,
|
| 196 |
-
stream_callback=stream_callback,
|
| 197 |
-
log_callback=_req_log_and_status_callback(
|
| 198 |
-
request_id,
|
| 199 |
-
on_latency_marker=waiting_stop_event.set,
|
| 200 |
-
),
|
| 201 |
-
),
|
| 202 |
-
stream_output,
|
| 203 |
-
process_timings,
|
| 204 |
-
request_id,
|
| 205 |
-
)
|
| 206 |
-
finally:
|
| 207 |
-
waiting_stop_event.set()
|
| 208 |
-
if waiting_task:
|
| 209 |
-
waiting_task.cancel()
|
| 210 |
-
with suppress(asyncio.CancelledError):
|
| 211 |
-
await waiting_task
|
| 212 |
|
| 213 |
result = AnalysisResult(
|
| 214 |
query=query,
|
| 215 |
ticker=intent.get("ticker", ""),
|
| 216 |
analysis_type=intent.get("analysis_type", "general"),
|
| 217 |
-
data_context=
|
| 218 |
-
|
| 219 |
-
"fundamentals": market_data.fundamentals,
|
| 220 |
-
"technicals": market_data.technicals,
|
| 221 |
-
"news_count": len(market_data.news_snippets),
|
| 222 |
-
"earnings": market_data.earnings_data,
|
| 223 |
-
"web_search_blocks": len(market_data.web_search_results),
|
| 224 |
-
"google_news": news_str,
|
| 225 |
-
},
|
| 226 |
-
llm_response=response,
|
| 227 |
)
|
| 228 |
|
| 229 |
-
|
| 230 |
-
print(f"{_req_prefix(request_id)} [로그] JSONL 저장 완료: {PIPELINE_LOG_PATH}")
|
| 231 |
-
|
| 232 |
-
print(f"\n{_req_prefix(request_id)} [완료] 분석 완료 ✓")
|
| 233 |
-
print(f"{_req_prefix(request_id)} context info")
|
| 234 |
-
print(context)
|
| 235 |
|
| 236 |
-
|
|
|
|
|
|
|
| 237 |
return result
|
| 238 |
|
| 239 |
|
|
@@ -241,42 +112,41 @@ async def pipeline(query, stream_output=False, stream_callback=None, status_call
|
|
| 241 |
# CLI 출력 + 진입점
|
| 242 |
# ─────────────────────────────────────────────
|
| 243 |
|
| 244 |
-
def print_result(result
|
| 245 |
print(f"\n{'='*60}")
|
| 246 |
print(f"분석 결과 | {result.ticker} | {result.analysis_type.upper()}")
|
| 247 |
print(f"생성 시각: {result.timestamp}")
|
| 248 |
print('='*60)
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
print(textwrap.fill(line, width=80) if len(line) > 80 else line)
|
| 253 |
-
print()
|
| 254 |
|
| 255 |
ctx = result.data_context
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
print("[수집 데이터 요약]")
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
print(f"
|
| 260 |
-
print(f" 뉴스 헤드라인 수: {ctx.get('news_count', 0)}")
|
| 261 |
-
print(f" 분기 실적 수: {len(ctx.get('earnings', {}).get('quarterly_results', []))}")
|
| 262 |
-
print(f" 연간 실적 수: {len(ctx.get('earnings', {}).get('annual_results', []))}")
|
| 263 |
-
print(f" 웹 검색 블록 수: {ctx.get('web_search_blocks', 0)}")
|
| 264 |
-
print(f" 구글 뉴스 요약 포함: {'예' if ctx.get('google_news') else '아니오'}")
|
| 265 |
|
| 266 |
-
|
| 267 |
-
async def main():
|
| 268 |
while True:
|
| 269 |
text = input("\n질문> ").strip()
|
| 270 |
if text.lower() in ("exit", "quit", "종료"):
|
| 271 |
break
|
| 272 |
if not text:
|
| 273 |
continue
|
| 274 |
-
result =
|
| 275 |
-
print_result(result
|
| 276 |
|
| 277 |
|
| 278 |
if __name__ == "__main__":
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
# FastAPI 실행 진입점
|
| 282 |
-
# (uvicorn으로 실행: uvicorn api_server:app --reload)
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
import textwrap
|
| 4 |
+
import time
|
| 5 |
from datetime import datetime
|
| 6 |
+
from typing import Any
|
| 7 |
+
from dataclasses import dataclass, field
|
| 8 |
+
from openai import OpenAI
|
| 9 |
+
from dataclasses import asdict
|
|
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
+
|
| 13 |
+
from intent.intent_parser import parse_intent
|
| 14 |
from llm.generator import generate_analysis, generate_news_info
|
| 15 |
+
|
| 16 |
from data.collect_data import collect_data
|
| 17 |
+
|
| 18 |
from context.context_builder import build_context
|
| 19 |
+
from intent.intent_parser import route_tools
|
| 20 |
from utils.data_types import AnalysisResult
|
| 21 |
|
| 22 |
load_dotenv()
|
|
|
|
| 24 |
|
| 25 |
api_key = os.environ.get("LLM_MODEL_API_KEY")
|
| 26 |
if api_key:
|
| 27 |
+
client = OpenAI(api_key=api_key)
|
| 28 |
else:
|
| 29 |
+
client = OpenAI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
def timed(timings, key):
|
| 33 |
+
"""컨텍스트 매니저: 블록 실행 시간을 timings[key]에 기록."""
|
| 34 |
+
class _Timer:
|
| 35 |
+
def __enter__(self):
|
| 36 |
+
self._t = time.time()
|
| 37 |
+
def __exit__(self, *_):
|
| 38 |
+
timings[key] = time.time() - self._t
|
| 39 |
+
return _Timer()
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
def print_timings(timings):
|
| 43 |
+
print("\n[소요시간(sec)]:")
|
| 44 |
+
for k, v in timings.items():
|
| 45 |
+
print(f" {k:20s}: {v:.2f}s")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
def save_result_jsonl(result):
|
| 49 |
+
file_name = os.environ.get("LOG_FILE", "log_file.jsonl")
|
| 50 |
+
data = asdict(result)
|
| 51 |
|
| 52 |
+
ordered_data = {
|
| 53 |
+
"timestamp": data.get("timestamp"),
|
| 54 |
+
**{k: v for k, v in data.items() if k != "timestamp"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
}
|
| 56 |
|
| 57 |
+
with open(file_name, "a", encoding="utf-8") as f:
|
| 58 |
+
f.write(json.dumps(ordered_data, ensure_ascii=False) + "\n")
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
def pipeline(query):
|
| 61 |
"""
|
| 62 |
파이프라인:
|
| 63 |
① 인텐트 파싱 (Chat Completions + Function Calling)
|
|
|
|
| 66 |
④ 컨텍스트 조립
|
| 67 |
⑤ 분석 생성 (Responses API + web_search 상시 활성)
|
| 68 |
"""
|
| 69 |
+
global client
|
| 70 |
+
timings = {}
|
| 71 |
+
start_total = time.time()
|
|
|
|
|
|
|
| 72 |
print(f"\n{'='*60}")
|
| 73 |
+
print(f"Wallstreet-AI 분석 시작: {query}")
|
| 74 |
print('='*60)
|
| 75 |
|
| 76 |
+
with timed(timings, 'intent_parse'):
|
| 77 |
+
intent = parse_intent(client, query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
with timed(timings, 'tool_route'):
|
| 80 |
+
tools = route_tools(intent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
with timed(timings, 'data_collect'):
|
| 83 |
+
market_data = collect_data(client, intent, tools)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
with timed(timings, 'context_build'):
|
| 86 |
+
news_str = generate_news_info(client, query, intent)
|
| 87 |
+
context = build_context(market_data, intent, news_str=news_str)
|
| 88 |
+
print(f"[④] 컨텍스트 빌드 완료 ({len(context)} 문자)")
|
| 89 |
+
|
| 90 |
+
with timed(timings, 'analysis_generate'):
|
| 91 |
+
response = generate_analysis(client, query, context, intent, news_str=None)
|
| 92 |
+
|
| 93 |
+
timings['total'] = time.time() - start_total
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
result = AnalysisResult(
|
| 96 |
query=query,
|
| 97 |
ticker=intent.get("ticker", ""),
|
| 98 |
analysis_type=intent.get("analysis_type", "general"),
|
| 99 |
+
data_context=market_data,
|
| 100 |
+
llm_response=response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
+
save_result_jsonl(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
print("\n[완료] 분석 완료 ✓")
|
| 106 |
+
print_timings(timings)
|
| 107 |
+
|
| 108 |
return result
|
| 109 |
|
| 110 |
|
|
|
|
| 112 |
# CLI 출력 + 진입점
|
| 113 |
# ─────────────────────────────────────────────
|
| 114 |
|
| 115 |
+
def print_result(result):
|
| 116 |
print(f"\n{'='*60}")
|
| 117 |
print(f"분석 결과 | {result.ticker} | {result.analysis_type.upper()}")
|
| 118 |
print(f"생성 시각: {result.timestamp}")
|
| 119 |
print('='*60)
|
| 120 |
+
for line in result.llm_response.split("\n"):
|
| 121 |
+
print(textwrap.fill(line, width=80) if len(line) > 80 else line)
|
| 122 |
+
print()
|
|
|
|
|
|
|
| 123 |
|
| 124 |
ctx = result.data_context
|
| 125 |
+
earnings = getattr(ctx, 'earnings_data', None) or {}
|
| 126 |
+
stats = {
|
| 127 |
+
"펀더멘털 지표 수": len(getattr(ctx, 'fundamentals', {}) or {}),
|
| 128 |
+
"가격 데이터 지표 수": len(getattr(ctx, 'price_data', {}) or {}),
|
| 129 |
+
"기술적 지표 수": len(getattr(ctx, 'technicals', {}) or {}),
|
| 130 |
+
"뉴스 헤드라인 수": len(getattr(ctx, 'news_snippets', []) or []),
|
| 131 |
+
"분기 실적 수": len(earnings.get('quarterly_results', [])),
|
| 132 |
+
"연간 실적 수": len(earnings.get('annual_results', [])),
|
| 133 |
+
"웹 검색 블록 수": len(getattr(ctx, 'web_search_results', []) or []),
|
| 134 |
+
}
|
| 135 |
print("[수집 데이터 요약]")
|
| 136 |
+
for label, val in stats.items():
|
| 137 |
+
print(f" {label:20s}: {val}")
|
| 138 |
+
print(f" {'구글 뉴스 요약 포함':20s}: {'예' if getattr(ctx, 'google_news', None) else '아니오'}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
def main():
|
|
|
|
| 141 |
while True:
|
| 142 |
text = input("\n질문> ").strip()
|
| 143 |
if text.lower() in ("exit", "quit", "종료"):
|
| 144 |
break
|
| 145 |
if not text:
|
| 146 |
continue
|
| 147 |
+
result = pipeline(text)
|
| 148 |
+
print_result(result)
|
| 149 |
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
+
main()
|
|
|
|
|
|
|
|
|