66 lines
2.5 KiB
TypeScript
66 lines
2.5 KiB
TypeScript
|
|
import { GoogleGenAI } from "@google/genai";
|
|
import { AiConfig } from "../types";
|
|
|
|
export class AiService {
|
|
/**
|
|
* 뉴스 기사들을 바탕으로 시장 심리 및 인사이트 분석
|
|
*/
|
|
static async analyzeNewsSentiment(config: AiConfig, newsHeadlines: string[]): Promise<string> {
|
|
const prompt = `당신은 전문 주식 분석가입니다. 다음 뉴스 헤드라인들을 분석하여 시장의 심리(상승/하락/중립)와 투자자가 주목해야 할 핵심 포인트 3가지를 한국어로 요약해 주세요.
|
|
|
|
뉴스 헤드라인:
|
|
${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<string> {
|
|
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<string> {
|
|
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)}`;
|
|
}
|
|
}
|
|
}
|