forked from gonka-ai/ai-reviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.go
More file actions
383 lines (340 loc) · 10.4 KB
/
Copy pathmodels.go
File metadata and controls
383 lines (340 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"context"
"fmt"
"os"
"strings"
"sync"
"github.com/anthropics/anthropic-sdk-go"
"github.com/anthropics/anthropic-sdk-go/option"
"github.com/sashabaranov/go-openai"
"google.golang.org/genai"
)
var (
envCache map[string]string
envOnce sync.Once
)
func getEnv(key string) string {
envOnce.Do(func() {
envCache = make(map[string]string)
data, err := os.ReadFile("KEYS.env")
if err == nil {
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
envCache[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
}
}
})
if val, ok := envCache[key]; ok {
return val
}
return os.Getenv(key)
}
type ModelClient interface {
Generate(ctx context.Context, prompt string, maxTokens int) (ModelResult, error)
GenerateJSON(ctx context.Context, prompt string, maxTokens int) (ModelResult, error)
}
type ModelResult struct {
Text string
TokensIn int
TokensOut int
TokensReasoning int
Provider string
Model string
FinishReason string
}
type ModelCategory string
const (
FastestGood ModelCategory = "fastest_good"
Balanced ModelCategory = "balanced"
BestCode ModelCategory = "best_code"
FrontierBest ModelCategory = "frontier_best"
)
// OpenAI Client
type OpenAIClient struct {
client *openai.Client
model string
reasoningLevel string
}
func NewOpenAIClient(apiKey, model, reasoningLevel string) *OpenAIClient {
return &OpenAIClient{
client: openai.NewClient(apiKey),
model: model,
reasoningLevel: reasoningLevel,
}
}
func (c *OpenAIClient) Generate(ctx context.Context, prompt string, maxTokens int) (ModelResult, error) {
return c.generate(ctx, prompt, maxTokens, false)
}
func (c *OpenAIClient) GenerateJSON(ctx context.Context, prompt string, maxTokens int) (ModelResult, error) {
return c.generate(ctx, prompt, maxTokens, true)
}
func (c *OpenAIClient) generate(ctx context.Context, prompt string, maxTokens int, jsonMode bool) (ModelResult, error) {
modelDisplay := c.model
if c.reasoningLevel != "" && c.reasoningLevel != "none" {
modelDisplay = fmt.Sprintf("%s(%s)", c.model, c.reasoningLevel)
}
req := openai.ChatCompletionRequest{
Model: c.model,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
}
if jsonMode {
req.ResponseFormat = &openai.ChatCompletionResponseFormat{
Type: openai.ChatCompletionResponseFormatTypeJSONObject,
}
}
if maxTokens > 0 {
req.MaxCompletionTokens = maxTokens
}
if c.reasoningLevel != "" && c.reasoningLevel != "none" {
req.ReasoningEffort = c.reasoningLevel
}
resp, err := c.client.CreateChatCompletion(ctx, req)
if err != nil {
return ModelResult{}, err
}
tokensIn := resp.Usage.PromptTokens
tokensOut := resp.Usage.CompletionTokens
tokensReasoning := 0
if resp.Usage.CompletionTokensDetails != nil {
tokensReasoning = resp.Usage.CompletionTokensDetails.ReasoningTokens
}
return ModelResult{
Text: resp.Choices[0].Message.Content,
TokensIn: tokensIn,
TokensOut: tokensOut,
TokensReasoning: tokensReasoning,
Provider: "openai",
Model: modelDisplay,
FinishReason: string(resp.Choices[0].FinishReason),
}, nil
}
// Anthropic Client
type AnthropicClient struct {
client *anthropic.Client
model string
reasoningLevel string
}
func NewAnthropicClient(apiKey, model, reasoningLevel string) *AnthropicClient {
c := anthropic.NewClient(option.WithAPIKey(apiKey))
return &AnthropicClient{
client: &c,
model: model,
reasoningLevel: reasoningLevel,
}
}
func (c *AnthropicClient) Generate(ctx context.Context, prompt string, maxTokens int) (ModelResult, error) {
return c.generate(ctx, prompt, maxTokens, false)
}
func (c *AnthropicClient) GenerateJSON(ctx context.Context, prompt string, maxTokens int) (ModelResult, error) {
// Anthropic doesn't have a simple "JSON mode" flag in the same way OpenAI does,
// but we can ask for it in the prompt or use tool use.
// For now, we'll just append a JSON instruction if it's not already there and use regular Generate.
// Actually, newer Anthropic models support structured output via tools, but for simplicity
// here we will just rely on the system prompt for now, OR we could implement tool use.
// The prompt already asks for JSON.
return c.generate(ctx, prompt, maxTokens, true)
}
func (c *AnthropicClient) generate(ctx context.Context, prompt string, maxTokens int, jsonMode bool) (ModelResult, error) {
modelDisplay := c.model
if c.reasoningLevel != "" && c.reasoningLevel != "none" {
modelDisplay = fmt.Sprintf("%s(%s)", c.model, c.reasoningLevel)
}
params := anthropic.MessageNewParams{
Model: anthropic.Model(c.model),
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock(prompt)),
},
}
if maxTokens > 0 {
params.MaxTokens = int64(maxTokens)
} else {
// If 0, it means no limit, but Anthropic REQUIRES max_tokens.
// Use 64k tokens
params.MaxTokens = 65536
}
if c.reasoningLevel != "" && c.reasoningLevel != "none" {
budget := int64(2048)
if params.MaxTokens > 4096 {
budget = int64(params.MaxTokens / 2)
} else if params.MaxTokens <= 2048 {
// Budget must be < max_tokens and >= 1024
// If maxTokens is too low, we might need to increase it or skip thinking
if params.MaxTokens > 1024 {
budget = 1024
} else {
// Can't enable thinking if max_tokens <= 1024
budget = 0
}
}
if budget >= 1024 {
params.Thinking = anthropic.ThinkingConfigParamOfEnabled(budget)
// Anthropic requires max_tokens to be GREATER than budget_tokens.
// It should be enough to cover thinking + some output.
if params.MaxTokens <= budget {
params.MaxTokens = budget + 1024
}
}
}
stream := c.client.Messages.NewStreaming(ctx, params)
tokensReasoning := 0
text := ""
var inputTokens int64
var outputTokens int64
var stopReason string
for stream.Next() {
event := stream.Current()
switch event.Type {
case "message_start":
inputTokens = event.Message.Usage.InputTokens
outputTokens = event.Message.Usage.OutputTokens
case "content_block_delta":
if event.Delta.Type == "text_delta" {
text += event.Delta.Text
}
case "message_delta":
outputTokens = event.Usage.OutputTokens
stopReason = string(event.Delta.StopReason)
}
}
if err := stream.Err(); err != nil {
return ModelResult{}, err
}
return ModelResult{
Text: text,
TokensIn: int(inputTokens),
TokensOut: int(outputTokens),
TokensReasoning: tokensReasoning,
Provider: "anthropic",
Model: modelDisplay,
FinishReason: stopReason,
}, nil
}
// Gemini Client
type GeminiClient struct {
client *genai.Client
model string
reasoningLevel string
}
func NewGeminiClient(ctx context.Context, apiKey, model, reasoningLevel string) (*GeminiClient, error) {
client, err := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: apiKey,
Backend: genai.BackendGeminiAPI,
})
if err != nil {
return nil, err
}
return &GeminiClient{
client: client,
model: model,
reasoningLevel: reasoningLevel,
}, nil
}
func (c *GeminiClient) Generate(ctx context.Context, prompt string, maxTokens int) (ModelResult, error) {
return c.generate(ctx, prompt, maxTokens, false)
}
func (c *GeminiClient) GenerateJSON(ctx context.Context, prompt string, maxTokens int) (ModelResult, error) {
return c.generate(ctx, prompt, maxTokens, true)
}
func (c *GeminiClient) generate(ctx context.Context, prompt string, maxTokens int, jsonMode bool) (ModelResult, error) {
modelDisplay := c.model
if c.reasoningLevel != "" && c.reasoningLevel != "none" {
modelDisplay = fmt.Sprintf("%s(%s)", c.model, c.reasoningLevel)
}
config := &genai.GenerateContentConfig{}
if jsonMode {
config.ResponseMIMEType = "application/json"
}
if maxTokens > 0 {
config.MaxOutputTokens = int32(maxTokens)
}
if c.reasoningLevel != "" && c.reasoningLevel != "none" {
config.ThinkingConfig = &genai.ThinkingConfig{}
switch c.reasoningLevel {
case "low":
config.ThinkingConfig.ThinkingLevel = genai.ThinkingLevelLow
case "medium":
config.ThinkingConfig.ThinkingLevel = genai.ThinkingLevelMedium
case "high":
config.ThinkingConfig.ThinkingLevel = genai.ThinkingLevelHigh
}
config.ThinkingConfig.IncludeThoughts = false
}
resp, err := c.client.Models.GenerateContent(ctx, c.model, genai.Text(prompt), config)
if err != nil {
return ModelResult{}, err
}
if len(resp.Candidates) == 0 {
return ModelResult{}, fmt.Errorf("empty response from Gemini")
}
tokensIn := 0
tokensOut := 0
tokensReasoning := 0
if resp.UsageMetadata != nil {
tokensIn = int(resp.UsageMetadata.PromptTokenCount)
tokensOut = int(resp.UsageMetadata.CandidatesTokenCount)
tokensReasoning = int(resp.UsageMetadata.ThoughtsTokenCount)
}
finishReason := string(resp.Candidates[0].FinishReason)
if len(resp.Candidates[0].Content.Parts) == 0 {
return ModelResult{
TokensIn: tokensIn,
TokensOut: tokensOut,
TokensReasoning: tokensReasoning,
Provider: "gemini",
Model: c.model,
FinishReason: finishReason,
}, nil
}
text := ""
for _, part := range resp.Candidates[0].Content.Parts {
if part.Text != "" {
text += part.Text
}
}
return ModelResult{
Text: text,
TokensIn: tokensIn,
TokensOut: tokensOut,
TokensReasoning: tokensReasoning,
Provider: "gemini",
Model: modelDisplay,
FinishReason: finishReason,
}, nil
}
func GetModelClient(ctx context.Context, provider, model, reasoningLevel string) (ModelClient, error) {
switch provider {
case "openai":
apiKey := getEnv("OPENAI_API_KEY")
if apiKey == "" {
return nil, fmt.Errorf("OPENAI_API_KEY not set")
}
return NewOpenAIClient(apiKey, model, reasoningLevel), nil
case "anthropic":
apiKey := getEnv("ANTHROPIC_API_KEY")
if apiKey == "" {
return nil, fmt.Errorf("ANTHROPIC_API_KEY not set")
}
return NewAnthropicClient(apiKey, model, reasoningLevel), nil
case "gemini":
apiKey := getEnv("GEMINI_API_KEY")
if apiKey == "" {
return nil, fmt.Errorf("GEMINI_API_KEY not set")
}
return NewGeminiClient(ctx, apiKey, model, reasoningLevel)
default:
return nil, fmt.Errorf("unknown provider: %s", provider)
}
}