-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider_google.go
More file actions
465 lines (399 loc) · 13.7 KB
/
Copy pathprovider_google.go
File metadata and controls
465 lines (399 loc) · 13.7 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package ai
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
// ═══════════════════════════════════════════════════════════════════════════
// Google / Gemini Provider
// ═══════════════════════════════════════════════════════════════════════════
const googleBaseURL = "https://generativelanguage.googleapis.com/v1beta"
// GoogleProvider implements Provider for Google's Gemini API.
type GoogleProvider struct {
config ProviderConfig
httpClient *http.Client
}
// NewGoogleProvider creates a GoogleProvider.
func NewGoogleProvider(config ProviderConfig) *GoogleProvider {
if config.BaseURL == "" {
config.BaseURL = googleBaseURL
}
if config.APIKey == "" {
config.APIKey = getEnvWithFallback("GOOGLE_API_KEY", "GEMINI_API_KEY")
}
client := http.DefaultClient
if config.Timeout > 0 {
client = &http.Client{Timeout: config.Timeout}
}
return &GoogleProvider{config: config, httpClient: client}
}
// Name returns the provider identifier ("google").
func (p *GoogleProvider) Name() string {
return "google"
}
// Capabilities reports the features supported by this provider implementation.
func (p *GoogleProvider) Capabilities() ProviderCapabilities {
return ProviderCapabilities{
Tools: true,
Vision: true,
Streaming: true,
JSON: true,
Thinking: true, // Gemini supports thinking mode
PDF: true, // Gemini supports PDF input
}
}
// ═══════════════════════════════════════════════════════════════════════════
// Send
// ═══════════════════════════════════════════════════════════════════════════
// Send executes a non-streaming request.
func (p *GoogleProvider) Send(ctx context.Context, req *ProviderRequest) (*ProviderResponse, error) {
if p.config.APIKey == "" {
return nil, &ProviderError{
Provider: p.Name(),
Message: "GOOGLE_API_KEY or GEMINI_API_KEY not set",
}
}
geminiReq := p.buildRequest(req)
model := resolveModel(ProviderGoogle, Model(req.Model))
body, err := json.Marshal(geminiReq)
if err != nil {
return nil, &ProviderError{Provider: p.Name(), Message: "failed to marshal request", Err: err}
}
url := fmt.Sprintf("%s/models/%s:generateContent?key=%s", p.config.BaseURL, model, p.config.APIKey)
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
if err != nil {
return nil, &ProviderError{Provider: p.Name(), Message: "failed to create request", Err: err}
}
p.setHeaders(httpReq)
if Debug {
fmt.Printf("%s [%s] POST /models/%s:generateContent\n", colorDim("→"), p.Name(), model)
}
resp, err := p.httpClient.Do(httpReq)
if err != nil {
return nil, &ProviderError{Provider: p.Name(), Message: "request failed", Err: err}
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, &ProviderError{Provider: p.Name(), Message: "failed to read response", Err: err}
}
return p.parseResponse(respBody)
}
// ═══════════════════════════════════════════════════════════════════════════
// SendStream
// ═══════════════════════════════════════════════════════════════════════════
// SendStream executes a streaming request and invokes callback for each chunk.
func (p *GoogleProvider) SendStream(ctx context.Context, req *ProviderRequest, callback StreamCallback) (*ProviderResponse, error) {
if p.config.APIKey == "" {
return nil, &ProviderError{
Provider: p.Name(),
Message: "GOOGLE_API_KEY or GEMINI_API_KEY not set",
}
}
geminiReq := p.buildRequest(req)
model := resolveModel(ProviderGoogle, Model(req.Model))
body, err := json.Marshal(geminiReq)
if err != nil {
return nil, &ProviderError{Provider: p.Name(), Message: "failed to marshal request", Err: err}
}
url := fmt.Sprintf("%s/models/%s:streamGenerateContent?alt=sse&key=%s", p.config.BaseURL, model, p.config.APIKey)
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
if err != nil {
return nil, &ProviderError{Provider: p.Name(), Message: "failed to create request", Err: err}
}
p.setHeaders(httpReq)
if Debug {
fmt.Printf("%s [%s] POST /models/%s:streamGenerateContent (stream)\n", colorDim("→"), p.Name(), model)
}
resp, err := p.httpClient.Do(httpReq)
if err != nil {
return nil, &ProviderError{Provider: p.Name(), Message: "request failed", Err: err}
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, &ProviderError{
Provider: p.Name(),
Code: fmt.Sprintf("%d", resp.StatusCode),
Message: string(body),
}
}
var fullContent strings.Builder
reader := bufio.NewReader(resp.Body)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
return nil, &ProviderError{Provider: p.Name(), Message: "stream read error", Err: err}
}
line = bytes.TrimSpace(line)
if !bytes.HasPrefix(line, []byte("data: ")) {
continue
}
data := bytes.TrimPrefix(line, []byte("data: "))
var chunk struct {
Candidates []struct {
Content struct {
Parts []struct {
Text string `json:"text"`
} `json:"parts"`
} `json:"content"`
} `json:"candidates"`
}
if err := json.Unmarshal(data, &chunk); err != nil {
continue
}
if len(chunk.Candidates) > 0 && len(chunk.Candidates[0].Content.Parts) > 0 {
text := chunk.Candidates[0].Content.Parts[0].Text
fullContent.WriteString(text)
callback(text)
}
}
completionTokens := len(fullContent.String()) / 4
return &ProviderResponse{
Content: fullContent.String(),
CompletionTokens: completionTokens,
TotalTokens: completionTokens,
}, nil
}
// ═══════════════════════════════════════════════════════════════════════════
// Internal helpers
// ═══════════════════════════════════════════════════════════════════════════
// geminiRequest is Gemini's API format
type geminiRequest struct {
Contents []geminiContent `json:"contents"`
SystemInstruct *geminiContent `json:"systemInstruction,omitempty"`
GenerationConfig *geminiGenerateConfig `json:"generationConfig,omitempty"`
Tools []geminiTool `json:"tools,omitempty"`
}
type geminiContent struct {
Role string `json:"role,omitempty"`
Parts []geminiPart `json:"parts"`
}
type geminiPart struct {
Text string `json:"text,omitempty"`
InlineData *geminiInline `json:"inlineData,omitempty"`
FileData *geminiFileData `json:"fileData,omitempty"`
}
type geminiInline struct {
MimeType string `json:"mimeType"`
Data string `json:"data"`
}
type geminiFileData struct {
MimeType string `json:"mimeType"`
FileURI string `json:"fileUri"`
}
type geminiGenerateConfig struct {
Temperature *float64 `json:"temperature,omitempty"`
ResponseMimeType string `json:"responseMimeType,omitempty"`
ThinkingConfig *geminiThinkingConfig `json:"thinkingConfig,omitempty"`
}
type geminiThinkingConfig struct {
// Gemini supports both a legacy "thinkingBudget" and the newer "thinkingLevel".
// Do not send both in the same request (Gemini 3 returns a 400).
ThinkingLevel string `json:"thinkingLevel,omitempty"`
ThinkingBudget int `json:"thinkingBudget,omitempty"`
}
type geminiTool struct {
FunctionDeclarations []geminiFunctionDecl `json:"functionDeclarations,omitempty"`
}
type geminiFunctionDecl struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters map[string]any `json:"parameters,omitempty"`
}
func (p *GoogleProvider) buildRequest(req *ProviderRequest) *geminiRequest {
geminiReq := &geminiRequest{
Contents: []geminiContent{},
}
// Process messages
for _, msg := range req.Messages {
if msg.Role == "system" {
// Gemini uses systemInstruction
if content, ok := msg.Content.(string); ok {
geminiReq.SystemInstruct = &geminiContent{
Parts: []geminiPart{{Text: content}},
}
}
continue
}
// Map roles: user -> user, assistant -> model
role := msg.Role
if role == "assistant" {
role = "model"
}
var parts []geminiPart
switch c := msg.Content.(type) {
case string:
parts = append(parts, geminiPart{Text: c})
case []ContentPart:
for _, part := range c {
switch part.Type {
case "text":
parts = append(parts, geminiPart{Text: part.Text})
case "image_url":
if part.ImageURL != nil {
url := part.ImageURL.URL
if strings.HasPrefix(url, "data:") {
// Parse data URI
dataParts := strings.SplitN(url, ",", 2)
if len(dataParts) == 2 {
mimeType := strings.TrimPrefix(strings.Split(dataParts[0], ";")[0], "data:")
parts = append(parts, geminiPart{
InlineData: &geminiInline{
MimeType: mimeType,
Data: dataParts[1],
},
})
}
}
}
case "document":
if part.Document != nil {
doc := part.Document
if doc.Data != "" {
// Inline base64 document
parts = append(parts, geminiPart{
InlineData: &geminiInline{
MimeType: doc.MimeType,
Data: doc.Data,
},
})
} else if doc.URL != "" {
// File URI (for Google Cloud Storage)
parts = append(parts, geminiPart{
FileData: &geminiFileData{
MimeType: doc.MimeType,
FileURI: doc.URL,
},
})
}
}
}
}
}
if len(parts) > 0 {
geminiReq.Contents = append(geminiReq.Contents, geminiContent{
Role: role,
Parts: parts,
})
}
}
// Generation config
geminiReq.GenerationConfig = &geminiGenerateConfig{}
if req.Temperature != nil {
geminiReq.GenerationConfig.Temperature = req.Temperature
}
if req.JSONMode {
geminiReq.GenerationConfig.ResponseMimeType = "application/json"
}
// Thinking/reasoning config
if req.Thinking != "" {
geminiReq.GenerationConfig.ThinkingConfig = &geminiThinkingConfig{
ThinkingLevel: string(req.Thinking),
}
}
// Tools
if len(req.Tools) > 0 {
var funcs []geminiFunctionDecl
for _, tool := range req.Tools {
funcs = append(funcs, geminiFunctionDecl{
Name: tool.Function.Name,
Description: tool.Function.Description,
Parameters: tool.Function.Parameters,
})
}
geminiReq.Tools = []geminiTool{{FunctionDeclarations: funcs}}
}
return geminiReq
}
func (p *GoogleProvider) setHeaders(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
for k, v := range p.config.Headers {
req.Header.Set(k, v)
}
}
func (p *GoogleProvider) parseResponse(body []byte) (*ProviderResponse, error) {
var result struct {
Candidates []struct {
Content struct {
Parts []struct {
Text string `json:"text,omitempty"`
FunctionCall *struct {
Name string `json:"name"`
Args map[string]any `json:"args"`
} `json:"functionCall,omitempty"`
} `json:"parts"`
Role string `json:"role"`
} `json:"content"`
FinishReason string `json:"finishReason"`
} `json:"candidates"`
UsageMetadata struct {
PromptTokenCount int `json:"promptTokenCount"`
CandidatesTokenCount int `json:"candidatesTokenCount"`
TotalTokenCount int `json:"totalTokenCount"`
} `json:"usageMetadata"`
Error *struct {
Code int `json:"code"`
Message string `json:"message"`
Status string `json:"status"`
} `json:"error,omitempty"`
}
if err := json.Unmarshal(body, &result); err != nil {
return nil, &ProviderError{
Provider: p.Name(),
Message: fmt.Sprintf("parse error: %v\nBody: %s", err, string(body)),
}
}
if result.Error != nil {
return nil, &ProviderError{
Provider: p.Name(),
Code: result.Error.Status,
Message: result.Error.Message,
}
}
if len(result.Candidates) == 0 {
return nil, &ProviderError{
Provider: p.Name(),
Message: "no response candidates",
}
}
// Extract text content and tool calls
var content strings.Builder
var toolCalls []ToolCall
candidate := result.Candidates[0]
for i, part := range candidate.Content.Parts {
if part.Text != "" {
content.WriteString(part.Text)
}
if part.FunctionCall != nil {
argsJSON, _ := json.Marshal(part.FunctionCall.Args)
toolCalls = append(toolCalls, ToolCall{
ID: fmt.Sprintf("call_%d", i),
Type: "function",
Function: struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}{
Name: part.FunctionCall.Name,
Arguments: string(argsJSON),
},
})
}
}
return &ProviderResponse{
Content: content.String(),
ToolCalls: toolCalls,
PromptTokens: result.UsageMetadata.PromptTokenCount,
CompletionTokens: result.UsageMetadata.CandidatesTokenCount,
TotalTokens: result.UsageMetadata.TotalTokenCount,
FinishReason: candidate.FinishReason,
}, nil
}