internal/llm/llm.go (view raw)
1package llm
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "strings"
8 "time"
9
10 "llm_aggregator/internal/defaults"
11 "llm_aggregator/internal/progress"
12
13 "github.com/openai/openai-go/v3"
14 "github.com/openai/openai-go/v3/option"
15)
16
17// LLMClient is a client for interacting with LLM API.
18type LLMClient struct {
19 client openai.Client
20 model string
21 maxTokens int
22 temperature float64
23 logger *progress.Context
24}
25
26// NewLLMClient creates a new LLM client.
27// apiKey: LLM API key (or read from LLM_AGGREGATOR_API_KEY env var)
28// baseURL: API base URL (defaults to "https://api.deepseek.com")
29// model: Model to use (defaults to "deepseek-chat")
30// maxTokens: Maximum tokens in response (defaults to 4000)
31// temperature: Sampling temperature (0.0 to 1.0, defaults to 0.7)
32func NewLLMClient(apiKey, baseURL, model string, maxTokens int, temperature float64) (*LLMClient, error) {
33 // Get API key from parameter or environment variable
34 if apiKey == "" {
35 apiKey = os.Getenv("LLM_AGGREGATOR_API_KEY")
36 }
37 if apiKey == "" || strings.TrimSpace(apiKey) == "" {
38 return nil, fmt.Errorf(
39 "LLM API key is required. " +
40 "Set LLM_AGGREGATOR_API_KEY environment variable or pass apiKey parameter",
41 )
42 }
43
44 // Set defaults using central constants
45 if baseURL == "" {
46 baseURL = defaults.DefaultBaseURL
47 }
48 if model == "" {
49 model = defaults.DefaultModel
50 }
51 if maxTokens == 0 {
52 maxTokens = defaults.DefaultMaxTokens
53 }
54 if temperature == 0 {
55 temperature = defaults.DefaultTemperature
56 }
57
58 // Create OpenAI client configured for LLM
59 client := openai.NewClient(
60 option.WithAPIKey(apiKey),
61 option.WithBaseURL(baseURL),
62 )
63
64 return &LLMClient{
65 client: client,
66 model: model,
67 maxTokens: maxTokens,
68 temperature: temperature,
69 }, nil
70}
71
72// SetLogger sets the logger for the LLM client
73func (dc *LLMClient) SetLogger(logger *progress.Context) {
74 dc.logger = logger
75}
76
77// SummariseArticles summarises a list of articles based on user prompt.
78// articles: List of article maps
79// userPrompt: User's query/summarisation request
80// systemPrompt: Optional system prompt (defaults to helpful assistant)
81func (dc *LLMClient) SummariseArticles(
82 articles []map[string]any,
83 userPrompt string,
84 systemPrompt string,
85) (string, error) {
86 if len(articles) == 0 {
87 return "No articles to summarise.", nil
88 }
89
90 // Prepare context from articles
91 context := dc.prepareContext(articles)
92
93 // Create messages for chat completion API
94 messages := dc.createMessages(context, userPrompt, systemPrompt)
95
96 // Call API with messages
97 return dc.callAPIWithMessages(messages)
98}
99
100func (dc *LLMClient) prepareContext(articles []map[string]any) string {
101 contextParts := []string{}
102
103 for i, article := range articles {
104 contextParts = append(contextParts, fmt.Sprintf("--- ARTICLE %d ---", i+1))
105 contextParts = append(contextParts, fmt.Sprintf("Title: %s", article["title"]))
106
107 if source, ok := article["source_feed"].(string); ok && source != "" {
108 contextParts = append(contextParts, fmt.Sprintf("Source: %s", source))
109 }
110
111 if published, ok := article["published"]; ok {
112 switch pub := published.(type) {
113 case time.Time:
114 if !pub.IsZero() {
115 contextParts = append(contextParts, fmt.Sprintf("Published: %s", pub.Format(time.RFC3339)))
116 }
117 case string:
118 contextParts = append(contextParts, fmt.Sprintf("Published: %s", pub))
119 default:
120 contextParts = append(contextParts, fmt.Sprintf("Published: %v", pub))
121 }
122 }
123
124 if author, ok := article["author"].(string); ok && author != "" {
125 contextParts = append(contextParts, fmt.Sprintf("Author: %s", author))
126 }
127
128 if link, ok := article["link"].(string); ok && link != "" {
129 contextParts = append(contextParts, fmt.Sprintf("Link: %s", link))
130 }
131
132 if content, ok := article["content"].(string); ok && content != "" {
133 // Truncate very long content
134 maxContentLen := 3000
135 if len(content) > maxContentLen {
136 content = content[:maxContentLen] + "... [truncated]"
137 }
138 contextParts = append(contextParts, fmt.Sprintf("Content: %s", content))
139 }
140
141 contextParts = append(contextParts, "") // Empty line between articles
142 }
143
144 return strings.Join(contextParts, "\n")
145}
146
147func (dc *LLMClient) createMessages(context, userPrompt, systemPrompt string) []openai.ChatCompletionMessageParamUnion {
148 if systemPrompt == "" {
149 systemPrompt = `You are an expert analyst and summariser.
150You analyse content from multiple sources and provide
151concise, insightful summaries based on user requests.
152Focus on key points, trends, and important information.`
153 }
154
155 // Combine context with user prompt
156 fullUserContent := fmt.Sprintf(`Here are articles from various RSS feeds:
157
158%s
159
160User request: %s
161
162Please provide a comprehensive summary/analysis addressing the user's request.
163Focus on key insights, trends, and important information from the articles.
164If relevant, note any patterns, contradictions, or notable developments.`,
165 context, userPrompt)
166
167 messages := []openai.ChatCompletionMessageParamUnion{
168 openai.SystemMessage(systemPrompt),
169 openai.UserMessage(fullUserContent),
170 }
171
172 return messages
173}
174
175func (dc *LLMClient) callAPIWithMessages(messages []openai.ChatCompletionMessageParamUnion) (string, error) {
176 ctx := context.Background()
177
178 if dc.logger != nil {
179 dc.logger.Logf("Calling LLM API with model: %s", dc.model)
180 }
181
182 response, err := dc.client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
183 Model: dc.model,
184 Messages: messages,
185 MaxTokens: openai.Int(int64(dc.maxTokens)),
186 Temperature: openai.Float(dc.temperature),
187 })
188
189 if err != nil {
190 // Provide more specific error messages
191 errStr := err.Error()
192 if strings.Contains(errStr, "401") {
193 return "", fmt.Errorf("invalid API key. Please check your LLM API key")
194 } else if strings.Contains(errStr, "429") {
195 return "", fmt.Errorf("rate limit exceeded. Please try again later")
196 } else if strings.Contains(errStr, "500") {
197 return "", fmt.Errorf("LLM API server error. Please try again later")
198 } else if strings.Contains(errStr, "404") {
199 return "", fmt.Errorf("API endpoint not found. Please check the base URL and endpoint. OpenAI API uses /chat/completions")
200 }
201 return "", fmt.Errorf("failed to connect to LLM API: %w", err)
202 }
203
204 // Extract text from response
205 if len(response.Choices) == 0 {
206 return "", fmt.Errorf("no response choices returned from API")
207 }
208
209 outputText := response.Choices[0].Message.Content
210
211 // Print token usage
212 if dc.logger != nil {
213 dc.logger.Logf(
214 "LLM API response: %d prompt tokens, %d completion tokens",
215 response.Usage.PromptTokens,
216 response.Usage.CompletionTokens,
217 )
218 }
219
220 return outputText, nil
221}
222
223// AnalyseWithCustomPrompt analyses articles with custom system and user prompts.
224func (dc *LLMClient) AnalyseWithCustomPrompt(
225 articles []map[string]any,
226 systemPrompt string,
227 userPrompt string,
228) (string, error) {
229 context := dc.prepareContext(articles)
230
231 // Create messages for chat completion API
232 messages := []openai.ChatCompletionMessageParamUnion{
233 openai.SystemMessage(systemPrompt),
234 openai.UserMessage(fmt.Sprintf(`Here are articles from various RSS feeds:
235
236%s
237
238%s`, context, userPrompt)),
239 }
240
241 return dc.callAPIWithMessages(messages)
242}