-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgemini_model.go
More file actions
304 lines (260 loc) · 7.84 KB
/
gemini_model.go
File metadata and controls
304 lines (260 loc) · 7.84 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
package contextwindow
import (
"context"
"encoding/json"
"fmt"
"os"
"google.golang.org/genai"
)
const (
ModelGemini20Flash = "gemini-2.0-flash"
ModelGemini25Flash = "gemini-2.5-flash"
ModelGemini20FlashExp = "gemini-2.0-flash-exp"
ModelGemini25Pro = "gemini-2.5-pro"
ModelGemini25FlashLite = "gemini-2.5-flash-lite"
)
var AllGeminiModels = []string{
ModelGemini20Flash,
ModelGemini20FlashExp,
ModelGemini25Flash,
ModelGemini25Pro,
ModelGemini25FlashLite,
}
type GeminiModel struct {
client *genai.Client
model string
middleware []Middleware
toolExecutor ToolExecutor
}
func NewGeminiModel(model string) (*GeminiModel, error) {
apiKey := os.Getenv("GOOGLE_GENAI_API_KEY")
if apiKey == "" {
apiKey = os.Getenv("GEMINI_API_KEY")
}
if apiKey == "" {
return nil, fmt.Errorf("GOOGLE_GENAI_API_KEY or GEMINI_API_KEY not set")
}
ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
APIKey: apiKey,
Backend: genai.BackendGeminiAPI,
})
if err != nil {
return nil, fmt.Errorf("failed to create Gemini client: %w", err)
}
return &GeminiModel{
client: client,
model: model,
}, nil
}
func (g *GeminiModel) MaxTokens() int {
return 1_048_576
}
// SetMiddleware sets the middleware for the Gemini model
func (g *GeminiModel) SetMiddleware(middleware []Middleware) {
g.middleware = middleware
}
// SetToolExecutor sets the tool executor for the Gemini model
func (g *GeminiModel) SetToolExecutor(executor ToolExecutor) {
g.toolExecutor = executor
}
func (g *GeminiModel) Call(
ctx context.Context,
inputs []Record,
) ([]Record, int, error) {
return g.CallWithOpts(ctx, inputs, CallModelOpts{})
}
func (g *GeminiModel) CallWithOpts(
ctx context.Context,
inputs []Record,
opts CallModelOpts,
) ([]Record, int, error) {
var availableTools []ToolDefinition
if g.toolExecutor != nil && !opts.DisableTools {
availableTools = g.toolExecutor.GetRegisteredTools()
}
// Convert Records to Gemini Content
var contents []*genai.Content
var systemInstruction string
for _, rec := range inputs {
switch rec.Source {
case SystemPrompt:
// Gemini supports system instructions separately
if systemInstruction != "" {
systemInstruction += "\n\n" + rec.Content
} else {
systemInstruction = rec.Content
}
case Prompt:
contents = append(contents, &genai.Content{
Parts: []*genai.Part{genai.NewPartFromText(rec.Content)},
Role: "user",
})
case ModelResp:
contents = append(contents, &genai.Content{
Parts: []*genai.Part{genai.NewPartFromText(rec.Content)},
Role: "model",
})
case ToolCall:
// Parse the tool call format: "functionName({...})"
// For now, append as text to user message (will be revisited)
contents = append(contents, &genai.Content{
Parts: []*genai.Part{genai.NewPartFromText(rec.Content)},
Role: "user",
})
case ToolOutput:
contents = append(contents, &genai.Content{
Parts: []*genai.Part{genai.NewPartFromText(rec.Content)},
Role: "user",
})
}
}
// Build config
config := &genai.GenerateContentConfig{
Temperature: genai.Ptr(float32(1.0)),
}
if systemInstruction != "" {
config.SystemInstruction = &genai.Content{
Parts: []*genai.Part{genai.NewPartFromText(systemInstruction)},
Role: "user",
}
}
if len(availableTools) > 0 {
tools := getGeminiToolParams(availableTools)
config.Tools = tools
}
// Make initial request
resp, err := g.client.Models.GenerateContent(ctx, g.model, contents, config)
if err != nil {
return nil, 0, fmt.Errorf("Gemini API: %w", err)
}
var events []Record
totalTokens := 0
if resp.UsageMetadata != nil {
totalTokens = int(resp.UsageMetadata.TotalTokenCount)
}
// Tool calling loop
for len(resp.FunctionCalls()) > 0 {
var modelParts []*genai.Part
// Collect all parts from the response (text + function calls)
for _, part := range resp.Candidates[0].Content.Parts {
if part.Text != "" {
modelParts = append(modelParts, genai.NewPartFromText(part.Text))
}
if part.FunctionCall != nil {
modelParts = append(modelParts, genai.NewPartFromFunctionCall(
part.FunctionCall.Name,
part.FunctionCall.Args,
))
}
}
// Add model's response to conversation
contents = append(contents, &genai.Content{
Parts: modelParts,
Role: "model",
})
// Execute tools and collect responses
var toolResponseParts []*genai.Part
for _, funcCall := range resp.FunctionCalls() {
// Marshal args to JSON for middleware and execution
argsJSON, err := json.Marshal(funcCall.Args)
if err != nil {
argsJSON = []byte("{}")
}
for _, m := range g.middleware {
m.OnToolCall(ctx, funcCall.Name, string(argsJSON))
}
out, err := g.toolExecutor.ExecuteTool(ctx, funcCall.Name, json.RawMessage(argsJSON))
if err != nil {
out = fmt.Sprintf("error: %s", err)
}
for _, m := range g.middleware {
m.OnToolResult(ctx, funcCall.Name, out, err)
}
// Record the tool call and output
call := fmt.Sprintf("%s(%s)", funcCall.Name, string(argsJSON))
events = append(events, Record{
Source: ToolCall,
Content: call,
Live: true,
EstTokens: tokenCount(call),
})
events = append(events, Record{
Source: ToolOutput,
Content: out,
Live: true,
EstTokens: tokenCount(out),
})
// Parse the output as the function response
var response map[string]interface{}
if err := json.Unmarshal([]byte(out), &response); err != nil {
// If not JSON, wrap in a simple response
response = map[string]interface{}{"result": out}
}
toolResponseParts = append(toolResponseParts, genai.NewPartFromFunctionResponse(
funcCall.Name,
response,
))
}
// Add tool responses to conversation
contents = append(contents, &genai.Content{
Parts: toolResponseParts,
Role: "user",
})
// Continue the conversation
resp, err = g.client.Models.GenerateContent(ctx, g.model, contents, config)
if err != nil {
return nil, 0, fmt.Errorf("Gemini API (tool continuation): %w", err)
}
if resp.UsageMetadata != nil {
totalTokens += int(resp.UsageMetadata.TotalTokenCount)
}
}
// Extract final text response
responseText := resp.Text()
events = append(events, Record{
Source: ModelResp,
Content: responseText,
Live: true,
EstTokens: tokenCount(responseText),
})
return events, totalTokens, nil
}
// CallWithThreading implements ServerSideThreadingCapable interface
func (g *GeminiModel) CallWithThreading(
ctx context.Context,
useServerSideThreading bool,
lastResponseID *string,
inputs []Record,
) ([]Record, *string, int, error) {
return g.CallWithThreadingAndOpts(ctx, useServerSideThreading, lastResponseID, inputs, CallModelOpts{})
}
// CallWithThreadingAndOpts implements CallOptsCapable interface
func (g *GeminiModel) CallWithThreadingAndOpts(
ctx context.Context,
useServerSideThreading bool,
lastResponseID *string,
inputs []Record,
opts CallModelOpts,
) ([]Record, *string, int, error) {
// Gemini doesn't support server-side threading, so we always use client-side
events, tokensUsed, err := g.CallWithOpts(ctx, inputs, opts)
return events, nil, tokensUsed, err
}
// getGeminiToolParams converts ToolDefinitions to Gemini tool parameters
func getGeminiToolParams(availableTools []ToolDefinition) []*genai.Tool {
var functionDeclarations []*genai.FunctionDeclaration
for _, tool := range availableTools {
if geminiFunc, ok := tool.Definition.(*genai.FunctionDeclaration); ok {
functionDeclarations = append(functionDeclarations, geminiFunc)
continue
}
if builder, ok := tool.Definition.(*ToolBuilder); ok {
geminiFunc := builder.ToGemini()
functionDeclarations = append(functionDeclarations, geminiFunc)
continue
}
panic(fmt.Sprintf("can't convert tool definition for %s to Gemini format (type: %T)", tool.Name, tool.Definition))
}
return []*genai.Tool{{FunctionDeclarations: functionDeclarations}}
}