import { GoogleGenAI } from "@google/genai"; import { AiConfig } from "../types"; export class AiService { /** * 뉴스 기사들을 바탕으로 시장 심리 및 인사이트 분석 */ static async analyzeNewsSentiment(config: AiConfig, newsHeadlines: string[]): Promise { const isBatch = newsHeadlines.length > 1; const prompt = isBatch ? `당신은 전문 주식 분석가입니다. 다음 뉴스 헤드라인들을 분석하여 시장의 전반적인 심리와 투자 핵심 포인트를 한국어로 리포트 형식으로 작성해 주세요. 또한, 반드시 리포트 내용이 끝난 뒤에 "---METADATA---" 라는 구분선을 넣고, 그 바로 뒤에 각 뉴스 인덱스별로 연관된 테마와 종목 정보를 JSON 배열 형식으로 포함해 주세요. JSON 형식 예시: [{"index": 0, "themes": ["반도체", "AI"], "stocks": ["삼성전자"], "sentiment": "POSITIVE"}, ...] 분석할 뉴스 헤드라인: ${newsHeadlines.map((h, i) => `${i}. ${h}`).join('\n')} ` : `당신은 전문 주식 분석가입니다. 다음 뉴스 헤드라인을 분석하여 시장의 심리(상승/하락/중립)와 투자자가 주목해야 할 핵심 포인트를 한국어로 요약해 주세요. 분석할 뉴스 헤드라인: ${newsHeadlines.join('\n')} `; if (config.providerType === 'gemini') { return this.callGemini(config.modelName, prompt); } else { return this.callOpenAiCompatible(config, prompt); } } private static async callGemini(modelName: string, prompt: string): Promise { try { const ai = new GoogleGenAI({ apiKey: process.env.API_KEY }); const response = await ai.models.generateContent({ model: modelName || 'gemini-3-flash-preview', contents: prompt, }); return response.text || "분석 결과를 생성할 수 없습니다."; } catch (error) { console.error("Gemini 분석 오류:", error); return `Gemini 분석 중 오류가 발생했습니다: ${error instanceof Error ? error.message : String(error)}`; } } private static async callOpenAiCompatible(config: AiConfig, prompt: string): Promise { if (!config.baseUrl) return "API 베이스 URL이 설정되지 않았습니다."; try { const response = await fetch(`${config.baseUrl}/chat/completions`, { method: 'POST', headers: { 'Content-Type': 'application/json', // Ollama나 로컬 엔진은 보통 키가 필요 없거나 커스텀하게 처리하므로 일단 비워둡니다. }, body: JSON.stringify({ model: config.modelName, messages: [{ role: 'user', content: prompt }], temperature: 0.7 }) }); if (!response.ok) { throw new Error(`HTTP 오류! 상태 코드: ${response.status}`); } const data = await response.json(); return data.choices?.[0]?.message?.content || "분석 결과를 생성할 수 없습니다."; } catch (error) { console.error("OpenAI 호환 API 분석 오류:", error); return `AI 엔진(${config.name}) 분석 중 오류가 발생했습니다: ${error instanceof Error ? error.message : String(error)}`; } } }