Skip to content

Commit b36dd6f

Browse files
committed
cleanup response format handling in GenerateContent
Signed-off-by: Jet Chiang <[email protected]>
1 parent a533cfc commit b36dd6f

1 file changed

Lines changed: 42 additions & 24 deletions

File tree

‎go/adk/pkg/models/openai_adk.go‎

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ func (m *OpenAIModel) Name() string {
125125
return m.Config.Model
126126
}
127127

128+
func (m *OpenAIModel) apiFormat() string {
129+
if m.Config != nil && m.Config.APIFormat != "" {
130+
return m.Config.APIFormat
131+
}
132+
return OpenAIAPIFormatChatCompletions
133+
}
134+
128135
// GenerateContent implements model.LLM. Uses only ADK/genai types.
129136
func (m *OpenAIModel) GenerateContent(ctx context.Context, req *model.LLMRequest, stream bool) iter.Seq2[*model.LLMResponse, error] {
130137
return func(yield func(*model.LLMResponse, error) bool) {
@@ -137,35 +144,46 @@ func (m *OpenAIModel) GenerateContent(ctx context.Context, req *model.LLMRequest
137144
}
138145
telemetry.SetLLMRequestAttributes(ctx, modelName, req)
139146

140-
if m.Config != nil && m.Config.APIFormat == OpenAIAPIFormatResponses {
147+
switch m.apiFormat() {
148+
case OpenAIAPIFormatResponses:
141149
generateContentResponses(ctx, m, req, modelName, stream, yield)
142-
return
143-
}
144-
145-
messages, systemInstruction := genaiContentsToOpenAIMessages(req.Contents, req.Config)
146-
params := openai.ChatCompletionNewParams{
147-
Model: shared.ChatModel(modelName),
148-
Messages: messages,
149-
}
150-
if systemInstruction != "" {
151-
params.Messages = append([]openai.ChatCompletionMessageParamUnion{
152-
openai.SystemMessage(systemInstruction),
153-
}, params.Messages...)
150+
default:
151+
generateContentChatCompletions(ctx, m, req, modelName, stream, yield)
154152
}
155-
applyOpenAIConfig(&params, m.Config)
153+
}
154+
}
156155

157-
if req.Config != nil && len(req.Config.Tools) > 0 {
158-
params.Tools = genaiToolsToOpenAITools(req.Config.Tools)
159-
params.ToolChoice = openai.ChatCompletionToolChoiceOptionUnionParam{
160-
OfAuto: openai.String("auto"),
161-
}
156+
func generateContentChatCompletions(
157+
ctx context.Context,
158+
m *OpenAIModel,
159+
req *model.LLMRequest,
160+
modelName string,
161+
stream bool,
162+
yield func(*model.LLMResponse, error) bool,
163+
) {
164+
messages, systemInstruction := genaiContentsToOpenAIMessages(req.Contents, req.Config)
165+
params := openai.ChatCompletionNewParams{
166+
Model: shared.ChatModel(modelName),
167+
Messages: messages,
168+
}
169+
if systemInstruction != "" {
170+
params.Messages = append([]openai.ChatCompletionMessageParamUnion{
171+
openai.SystemMessage(systemInstruction),
172+
}, params.Messages...)
173+
}
174+
applyOpenAIConfig(&params, m.Config)
175+
176+
if req.Config != nil && len(req.Config.Tools) > 0 {
177+
params.Tools = genaiToolsToOpenAITools(req.Config.Tools)
178+
params.ToolChoice = openai.ChatCompletionToolChoiceOptionUnionParam{
179+
OfAuto: openai.String("auto"),
162180
}
181+
}
163182

164-
if stream {
165-
runStreaming(ctx, m, params, yield)
166-
} else {
167-
runNonStreaming(ctx, m, params, yield)
168-
}
183+
if stream {
184+
runStreaming(ctx, m, params, yield)
185+
} else {
186+
runNonStreaming(ctx, m, params, yield)
169187
}
170188
}
171189

0 commit comments

Comments
 (0)