-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_google_genai_tools_roundtrip_test.go
More file actions
228 lines (209 loc) · 7.02 KB
/
example_google_genai_tools_roundtrip_test.go
File metadata and controls
228 lines (209 loc) · 7.02 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
package integration
import (
"context"
"fmt"
"log/slog"
"os"
"time"
"github.com/flexigpt/inference-go"
"github.com/flexigpt/inference-go/spec"
)
const (
googleToolsRoundTripProviderName = "google-tools"
googleToolsRoundTripModelName = "gemini-2.5-flash"
googleToolsRoundTripToolID = "echo-tool"
googleToolsRoundTripToolName = "echo_text"
googleToolsRoundTripToolDescription = "Echo the provided text back in a deterministic tool result."
googleToolsRoundTripJSONKeyType = "type"
googleToolsRoundTripJSONValueObject = "object"
googleToolsRoundTripJSONKeyProperties = "properties"
googleToolsRoundTripJSONKeyText = "text"
googleToolsRoundTripJSONValueString = "string"
googleToolsRoundTripJSONKeyRequired = "required"
googleToolsRoundTripJSONKeyAdditionalProperties = "additionalProperties"
)
// Example_googleGenerateContent_functionToolRoundTrip demonstrates a full
// Gemini function-tool round trip:
//
// 1. user message + tool definition
// 2. model emits a function tool call
// 3. caller executes the tool locally
// 4. next request sends:
// - original user turn
// - assistant function tool call
// - user function tool output
// - extra user text
// 5. model returns the final answer
func Example_googleGenerateContent_functionToolRoundTrip() {
// Deadline greater than default timeout to simulate the sdk bug of context cancellation.
ctx, cancel := context.WithTimeout(context.Background(), 600*time.Second)
defer cancel()
ps, err := newProviderSetWithDebug(slog.LevelDebug)
if err != nil {
fmt.Fprintln(os.Stderr, "error creating ProviderSetAPI:", err)
return
}
_, err = ps.AddProvider(ctx, googleToolsRoundTripProviderName, &inference.AddProviderConfig{
SDKType: spec.ProviderSDKTypeGoogleGenerateContent,
Origin: spec.DefaultGoogleGenerateContentOrigin,
})
if err != nil {
fmt.Fprintln(os.Stderr, "error adding Google GenAI provider:", err)
return
}
apiKey := os.Getenv("GEMINI_API_KEY")
if apiKey == "" {
apiKey = os.Getenv("GOOGLE_API_KEY")
}
if apiKey == "" {
fmt.Fprintln(os.Stderr, "GEMINI_API_KEY/GOOGLE_API_KEY not set; skipping live Google GenAI tool example")
fmt.Println("OK")
return
}
if err := ps.SetProviderAPIKey(ctx, googleToolsRoundTripProviderName, apiKey); err != nil {
fmt.Fprintln(os.Stderr, "error setting Google GenAI API key:", err)
return
}
tool := spec.ToolChoice{
Type: spec.ToolTypeFunction,
ID: googleToolsRoundTripToolID,
Name: googleToolsRoundTripToolName,
Description: googleToolsRoundTripToolDescription,
Arguments: map[string]any{
googleToolsRoundTripJSONKeyType: googleToolsRoundTripJSONValueObject,
googleToolsRoundTripJSONKeyProperties: map[string]any{
googleToolsRoundTripJSONKeyText: map[string]any{
googleToolsRoundTripJSONKeyType: googleToolsRoundTripJSONValueString,
},
},
googleToolsRoundTripJSONKeyRequired: []any{googleToolsRoundTripJSONKeyText},
googleToolsRoundTripJSONKeyAdditionalProperties: false,
},
}
initialUser := newUserTextInput(
fmt.Sprintf(
`this is a test. think about what 20 new words you can say and say that in text. then call the %s tool with that text.`,
googleToolsRoundTripToolName,
),
)
firstReq := &spec.FetchCompletionRequest{
ModelParam: spec.ModelParam{
Name: googleToolsRoundTripModelName,
MaxOutputLength: 512,
Stream: true,
Reasoning: &spec.ReasoningParam{
Type: spec.ReasoningTypeHybridWithTokens,
Tokens: 256,
},
SystemPrompt: "You are validating a Gemini function tool round trip. " +
"When the tool is forced, emit only the tool call in the first response.",
},
Inputs: []spec.InputUnion{initialUser},
ToolChoices: []spec.ToolChoice{tool},
ToolPolicy: &spec.ToolPolicy{
Mode: spec.ToolPolicyModeTool,
AllowedTools: []spec.AllowedTool{
{ToolChoiceID: tool.ID},
},
DisableParallel: true,
},
}
firstResp, err := ps.FetchCompletion(ctx, googleToolsRoundTripProviderName, firstReq, &spec.FetchCompletionOptions{
CompletionKey: googleToolsRoundTripModelName,
StreamHandler: func(ev spec.StreamEvent) error {
switch ev.Kind {
case spec.StreamContentKindThinking:
if ev.Thinking != nil {
fmt.Fprintf(os.Stderr, "\n\n#######[thinking 1] %s\n", ev.Thinking.Text)
}
case spec.StreamContentKindText:
if ev.Text != nil {
fmt.Fprintf(os.Stderr, "\n\n#######[text 1] %s\n", ev.Text.Text)
}
}
return nil
},
})
if err != nil {
fmt.Fprintln(os.Stderr, "first FetchCompletion error:", err)
return
}
firstText := responseText(firstResp)
if firstText != "" {
fmt.Fprintln(os.Stderr, "FIRST assistant text:", firstText)
}
call, err := firstFunctionToolCall(firstResp)
if err != nil {
fmt.Fprintln(os.Stderr, "expected a function tool call, got error:", err)
return
}
fmt.Fprintf(
os.Stderr,
"\ntool call 1: name=%s id=%s args=%s\n",
call.Name,
nonEmpty(call.CallID, call.ID),
call.Arguments,
)
toolOutput, err := runEchoTool(call)
if err != nil {
fmt.Fprintln(os.Stderr, "error executing local tool:", err)
return
}
fmt.Fprintf(os.Stderr, "\ntool result 1 for %s: %s\n", toolOutput.CallID, firstToolOutputText(toolOutput))
history := make([]spec.InputUnion, 0, len(firstResp.Outputs)+2)
history = append(history, initialUser)
history = append(history, outputUnionsToInputs(firstResp.Outputs)...)
history = append(history, spec.InputUnion{
Kind: spec.InputKindFunctionToolOutput,
FunctionToolOutput: toolOutput,
})
secondReq := &spec.FetchCompletionRequest{
ModelParam: spec.ModelParam{
Name: googleToolsRoundTripModelName,
MaxOutputLength: 256,
Stream: true,
Reasoning: &spec.ReasoningParam{
Type: spec.ReasoningTypeHybridWithTokens,
Tokens: 256,
},
SystemPrompt: "You have now received the tool result. " +
"Answer with a sonnet of it. Do not call any tool again.",
},
Inputs: append(history, newUserTextInput("Now finish in one short sentence.")),
ToolChoices: []spec.ToolChoice{tool},
ToolPolicy: &spec.ToolPolicy{
Mode: spec.ToolPolicyModeNone,
},
}
secondResp, err := ps.FetchCompletion(
ctx,
googleToolsRoundTripProviderName,
secondReq,
&spec.FetchCompletionOptions{
CompletionKey: googleToolsRoundTripModelName,
StreamHandler: func(ev spec.StreamEvent) error {
switch ev.Kind {
case spec.StreamContentKindThinking:
if ev.Thinking != nil {
fmt.Fprintf(os.Stderr, "\n\n#######[thinking 2] %s\n", ev.Thinking.Text)
}
case spec.StreamContentKindText:
if ev.Text != nil {
fmt.Fprintf(os.Stderr, "\n\n#######[text 2] %s\n", ev.Text.Text)
}
}
return nil
},
},
)
if err != nil {
fmt.Fprintln(os.Stderr, "second FetchCompletion error:", err)
return
}
finalText := responseText(secondResp)
if finalText != "" {
fmt.Fprintln(os.Stderr, "\nFINAL assistant text:", finalText)
}
fmt.Println("OK")
// Output: OK
}