Skip to content

Commit 159985b

Browse files
committed
feat(request): add Thinking flag to innerRequest and corresponding WithThinking method; update tests for new functionality
1 parent 821fccc commit 159985b

3 files changed

Lines changed: 81 additions & 6 deletions

File tree

llms/aistudio/aistudio.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,16 @@ func (p *AiStudio) adaptRequest(_ internal.Adapter, requester llmberjack.Request
122122
}}
123123
}
124124

125-
if opts.Thinking != nil {
125+
if r.Thinking != nil && !lo.FromPtr(r.Thinking) {
126126
cfg.ThinkingConfig = &genai.ThinkingConfig{
127-
IncludeThoughts: opts.Thinking.IncludeThoughts,
128-
ThinkingBudget: internal.MaybeIntToInt32(opts.Thinking.Budget),
127+
ThinkingBudget: internal.MaybeIntToInt32(lo.ToPtr(int(0))),
128+
}
129+
} else {
130+
if opts.Thinking != nil {
131+
cfg.ThinkingConfig = &genai.ThinkingConfig{
132+
IncludeThoughts: opts.Thinking.IncludeThoughts,
133+
ThinkingBudget: internal.MaybeIntToInt32(opts.Thinking.Budget),
134+
}
129135
}
130136
}
131137

llms/aistudio/aistudio_test.go

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,13 @@ func TestGoogleAiRequestWithThinking(t *testing.T) {
128128

129129
tests := []struct {
130130
name string
131+
thinking *bool
131132
requestOptions *aistudio.RequestOptions
132133
expectedMatcher func(body []byte) bool
133134
}{
134135
{
135136
name: "Without requestOption",
137+
thinking: nil,
136138
requestOptions: nil,
137139
expectedMatcher: func(body []byte) bool {
138140
// When no thinking config is provided, these fields should not be present
@@ -142,7 +144,8 @@ func TestGoogleAiRequestWithThinking(t *testing.T) {
142144
},
143145
},
144146
{
145-
name: "With requestOption - only IncludeThoughts",
147+
name: "With requestOption - only IncludeThoughts",
148+
thinking: nil,
146149
requestOptions: &aistudio.RequestOptions{
147150
Thinking: &aistudio.ThinkingConfig{
148151
IncludeThoughts: true,
@@ -155,7 +158,8 @@ func TestGoogleAiRequestWithThinking(t *testing.T) {
155158
},
156159
},
157160
{
158-
name: "With requestOption - only Budget",
161+
name: "With requestOption - only Budget",
162+
thinking: nil,
159163
requestOptions: &aistudio.RequestOptions{
160164
Thinking: &aistudio.ThinkingConfig{
161165
Budget: lo.ToPtr(int(50)),
@@ -168,7 +172,8 @@ func TestGoogleAiRequestWithThinking(t *testing.T) {
168172
},
169173
},
170174
{
171-
name: "With requestOption - both fields set",
175+
name: "With requestOption - both fields set",
176+
thinking: nil,
172177
requestOptions: &aistudio.RequestOptions{
173178
Thinking: &aistudio.ThinkingConfig{
174179
IncludeThoughts: true,
@@ -195,6 +200,57 @@ func TestGoogleAiRequestWithThinking(t *testing.T) {
195200
return true
196201
},
197202
},
203+
{
204+
name: "With Request - Disable thinking",
205+
thinking: lo.ToPtr(false),
206+
requestOptions: nil,
207+
expectedMatcher: func(body []byte) bool {
208+
assert.False(t, gjson.GetBytes(body, "generationConfig.thinkingConfig.includeThoughts").Exists())
209+
assert.True(t, gjson.GetBytes(body, "generationConfig.thinkingConfig.thinkingBudget").Exists())
210+
assert.EqualValues(t, 0, gjson.GetBytes(body, "generationConfig.thinkingConfig.thinkingBudget").Int())
211+
return true
212+
},
213+
},
214+
{
215+
name: "With Request - Disable thinking with request option",
216+
thinking: lo.ToPtr(false),
217+
requestOptions: &aistudio.RequestOptions{
218+
Thinking: &aistudio.ThinkingConfig{
219+
Budget: lo.ToPtr(int(100)),
220+
},
221+
},
222+
expectedMatcher: func(body []byte) bool {
223+
assert.False(t, gjson.GetBytes(body, "generationConfig.thinkingConfig.includeThoughts").Exists())
224+
assert.True(t, gjson.GetBytes(body, "generationConfig.thinkingConfig.thinkingBudget").Exists())
225+
assert.EqualValues(t, 0, gjson.GetBytes(body, "generationConfig.thinkingConfig.thinkingBudget").Int())
226+
return true
227+
},
228+
},
229+
{
230+
name: "With Request - Enable thinking",
231+
thinking: lo.ToPtr(true),
232+
requestOptions: nil,
233+
expectedMatcher: func(body []byte) bool {
234+
assert.False(t, gjson.GetBytes(body, "generationConfig.thinkingConfig.includeThoughts").Exists())
235+
assert.False(t, gjson.GetBytes(body, "generationConfig.thinkingConfig.thinkingBudget").Exists())
236+
return true
237+
},
238+
},
239+
{
240+
name: "With Request - Enable thinking with request option",
241+
thinking: lo.ToPtr(true),
242+
requestOptions: &aistudio.RequestOptions{
243+
Thinking: &aistudio.ThinkingConfig{
244+
IncludeThoughts: true,
245+
Budget: lo.ToPtr(int(100)),
246+
},
247+
},
248+
expectedMatcher: func(body []byte) bool {
249+
assert.True(t, gjson.GetBytes(body, "generationConfig.thinkingConfig.includeThoughts").Bool())
250+
assert.EqualValues(t, 100, gjson.GetBytes(body, "generationConfig.thinkingConfig.thinkingBudget").Int())
251+
return true
252+
},
253+
},
198254
}
199255

200256
for _, tt := range tests {
@@ -203,6 +259,10 @@ func TestGoogleAiRequestWithThinking(t *testing.T) {
203259
WithModel("themodel").
204260
WithText(llmberjack.RoleUser, "user text")
205261

262+
if tt.thinking != nil {
263+
req = req.WithThinking(*tt.thinking)
264+
}
265+
206266
// Only add provider options if they exist
207267
if tt.requestOptions != nil {
208268
req = req.WithProviderOptions(*tt.requestOptions)

request.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ type innerRequest struct {
8181
Temperature *float64
8282
TopP *float64
8383

84+
// Thinking is a flag to enable/disable thinking. If not provided, the provider will use its default behavior.
85+
Thinking *bool
86+
8487
ProviderOptions map[reflect.Type]internal.ProviderRequestOptions
8588
}
8689

@@ -501,6 +504,12 @@ func (r Request[T]) WithTopP(topp float64) Request[T] {
501504
return r
502505
}
503506

507+
func (r Request[T]) WithThinking(thinking bool) Request[T] {
508+
r.Thinking = &thinking
509+
510+
return r
511+
}
512+
504513
// Request[T] implementation of Requester.
505514

506515
func (r Request[T]) ToRequest() innerRequest {

0 commit comments

Comments
 (0)