all repos — llm_aggregator @ e6f508220e5dbe8a9333b4ad603b5add4b13af7b

A CLI tool to aggregate RSS feeds and summarise them with LLMs

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	clientOpts := []option.RequestOption{
 60		option.WithAPIKey(apiKey),
 61		option.WithBaseURL(baseURL),
 62	}
 63
 64	client := openai.NewClient(clientOpts...)
 65
 66	return &LLMClient{
 67		client:      client,
 68		model:       model,
 69		maxTokens:   maxTokens,
 70		temperature: temperature,
 71	}, nil
 72}
 73
 74// SetLogger sets the logger for the LLM client
 75func (dc *LLMClient) SetLogger(logger *progress.Context) {
 76	dc.logger = logger
 77}
 78
 79// TokenUsage holds token usage information from the API
 80type TokenUsage struct {
 81	PromptTokens     int
 82	CompletionTokens int
 83}
 84
 85// SummariseArticles summarises a list of articles based on user prompt.
 86// articles: List of article maps
 87// userPrompt: User's query/summarisation request
 88// systemPrompt: Optional system prompt (defaults to helpful assistant)
 89func (dc *LLMClient) SummariseArticles(
 90	articles []map[string]any,
 91	userPrompt string,
 92	systemPrompt string,
 93) (string, *TokenUsage, error) {
 94	if len(articles) == 0 {
 95		return "No articles to summarise.", nil, nil
 96	}
 97
 98	// Prepare context from articles
 99	context := dc.prepareContext(articles)
100
101	// Create messages for chat completion API
102	messages := dc.createMessages(context, userPrompt, systemPrompt)
103
104	// Call API with messages
105	return dc.callAPIWithMessages(messages)
106}
107
108func (dc *LLMClient) prepareContext(articles []map[string]any) string {
109	contextParts := []string{}
110
111	for i, article := range articles {
112		contextParts = append(contextParts, fmt.Sprintf("--- ARTICLE %d ---", i+1))
113		contextParts = append(contextParts, fmt.Sprintf("Title: %s", article["title"]))
114
115		if source, ok := article["source_feed"].(string); ok && source != "" {
116			contextParts = append(contextParts, fmt.Sprintf("Source: %s", source))
117		}
118
119		if published, ok := article["published"]; ok {
120			switch pub := published.(type) {
121			case time.Time:
122				if !pub.IsZero() {
123					contextParts = append(contextParts, fmt.Sprintf("Published: %s", pub.Format(time.RFC3339)))
124				}
125			case string:
126				contextParts = append(contextParts, fmt.Sprintf("Published: %s", pub))
127			default:
128				contextParts = append(contextParts, fmt.Sprintf("Published: %v", pub))
129			}
130		}
131
132		if author, ok := article["author"].(string); ok && author != "" {
133			contextParts = append(contextParts, fmt.Sprintf("Author: %s", author))
134		}
135
136		if link, ok := article["link"].(string); ok && link != "" {
137			contextParts = append(contextParts, fmt.Sprintf("Link: %s", link))
138		}
139
140		if content, ok := article["content"].(string); ok && content != "" {
141			// Truncate very long content
142			maxContentLen := 3000
143			if len(content) > maxContentLen {
144				content = content[:maxContentLen] + "... [truncated]"
145			}
146			contextParts = append(contextParts, fmt.Sprintf("Content: %s", content))
147		}
148
149		contextParts = append(contextParts, "") // Empty line between articles
150	}
151
152	return strings.Join(contextParts, "\n")
153}
154
155func (dc *LLMClient) createMessages(context, userPrompt, systemPrompt string) []openai.ChatCompletionMessageParamUnion {
156	if systemPrompt == "" {
157		systemPrompt = `You are an expert analyst and summariser.
158You analyse content from multiple sources and provide
159concise, insightful summaries based on user requests.
160Focus on key points, trends, and important information.`
161	}
162
163	// Combine context with user prompt
164	fullUserContent := fmt.Sprintf(`Here are articles from various RSS feeds:
165
166%s
167
168User request: %s
169
170Please provide a comprehensive summary/analysis addressing the user's request.
171Focus on key insights, trends, and important information from the articles.
172If relevant, note any patterns, contradictions, or notable developments.`,
173		context, userPrompt)
174
175	messages := []openai.ChatCompletionMessageParamUnion{
176		openai.SystemMessage(systemPrompt),
177		openai.UserMessage(fullUserContent),
178	}
179
180	return messages
181}
182
183func (dc *LLMClient) callAPIWithMessages(messages []openai.ChatCompletionMessageParamUnion) (string, *TokenUsage, error) {
184	ctx := context.Background()
185
186	if dc.logger != nil {
187		dc.logger.Logf("Calling LLM API with model: %s", dc.model)
188		dc.logger.Logf("Max tokens: %d, Temperature: %.2f", dc.maxTokens, dc.temperature)
189		dc.logger.Logf("Messages count: %d", len(messages))
190	}
191
192	response, err := dc.client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
193		Model:       dc.model,
194		Messages:    messages,
195		MaxTokens:   openai.Int(int64(dc.maxTokens)),
196		Temperature: openai.Float(dc.temperature),
197	})
198
199	if err != nil {
200		errStr := err.Error()
201
202		if dc.logger != nil {
203			dc.logger.Logf("API error: %s", errStr)
204		}
205
206		if strings.Contains(errStr, "401") {
207			return "", nil, fmt.Errorf("invalid API key. Please check your LLM API key")
208		} else if strings.Contains(errStr, "429") {
209			return "", nil, fmt.Errorf("rate limit exceeded. Please try again later")
210		} else if strings.Contains(errStr, "500") {
211			return "", nil, fmt.Errorf("LLM API server error. Please try again later")
212		} else if strings.Contains(errStr, "404") {
213			return "", nil, fmt.Errorf("API endpoint not found. Please check the base URL and endpoint. OpenAI API uses /chat/completions")
214		}
215		return "", nil, fmt.Errorf("failed to connect to LLM API: %w", err)
216	}
217
218	if len(response.Choices) == 0 {
219		return "", nil, fmt.Errorf("no response choices returned from API")
220	}
221
222	outputText := response.Choices[0].Message.Content
223
224	usage := &TokenUsage{
225		PromptTokens:     int(response.Usage.PromptTokens),
226		CompletionTokens: int(response.Usage.CompletionTokens),
227	}
228
229	if dc.logger != nil {
230		dc.logger.Logf(
231			"LLM API response: %d prompt tokens, %d completion tokens",
232			usage.PromptTokens,
233			usage.CompletionTokens,
234		)
235	}
236
237	return outputText, usage, nil
238}