Skip to content

Commit 3f47986

Browse files
authored
[ai] Add WithCallOptions functional option (#518)
## Summary Add a new functional option to pass an entire options struct. ## How was it tested? Added unit tests. ## Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 License](https://www.apache.org/licenses/LICENSE-2.0). By creating this pull request I represent that I have the right to license the contributions to the project maintainers under the Apache 2 License as stated in the [Community Contribution License](https://github.com/jetify-com/opensource/blob/main/CONTRIBUTING.md#community-contribution-license).
1 parent f2b2356 commit 3f47986

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

aisdk/ai/options.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ func WithProviderMetadata(providerName string, metadata any) GenerateOption {
127127
}
128128
}
129129

130+
// WithCallOptions sets the entire CallOptions struct.
131+
// This allows passing a pre-configured CallOptions instance.
132+
func WithCallOptions(callOptions api.CallOptions) GenerateOption {
133+
return func(o *GenerateOptions) {
134+
o.CallOptions = callOptions
135+
}
136+
}
137+
130138
// buildGenerateConfig combines multiple generate options into a single GenerateConfig struct.
131139
func buildGenerateConfig(opts []GenerateOption) GenerateOptions {
132140
config := GenerateOptions{

aisdk/ai/options_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ func TestCallOptionBuilders(t *testing.T) {
154154
Model: &mockLanguageModel{name: "test-model"},
155155
},
156156
},
157+
{
158+
name: "WithCallOptions",
159+
option: WithCallOptions(api.CallOptions{
160+
MaxOutputTokens: 500,
161+
Temperature: pointer.Float64(0.5),
162+
TopP: 0.8,
163+
StopSequences: []string{"END"},
164+
Seed: 123,
165+
PresencePenalty: 0.1,
166+
FrequencyPenalty: 0.2,
167+
}),
168+
expected: GenerateOptions{
169+
CallOptions: api.CallOptions{
170+
MaxOutputTokens: 500,
171+
Temperature: pointer.Float64(0.5),
172+
TopP: 0.8,
173+
StopSequences: []string{"END"},
174+
Seed: 123,
175+
PresencePenalty: 0.1,
176+
FrequencyPenalty: 0.2,
177+
},
178+
},
179+
},
157180
}
158181

159182
for _, tt := range tests {

0 commit comments

Comments
 (0)