From 8b5468327f1ed01bdf1080b6c732111c92356da4 Mon Sep 17 00:00:00 2001 From: Shawn Tian Date: Wed, 20 Mar 2024 05:47:01 +0800 Subject: [PATCH] llms/openai: Optionally supply ResponseFormat (#690) * fix: ResponseFormat cause openai api response err - is not one of ['json_object', 'text'] - 'response_format.type' --- llms/openai/internal/openaiclient/chat.go | 2 +- llms/openai/openaillm_option.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/llms/openai/internal/openaiclient/chat.go b/llms/openai/internal/openaiclient/chat.go index 527ecf4a4..8db82dc02 100644 --- a/llms/openai/internal/openaiclient/chat.go +++ b/llms/openai/internal/openaiclient/chat.go @@ -33,7 +33,7 @@ type ChatRequest struct { FrequencyPenalty float64 `json:"frequency_penalty,omitempty"` PresencePenalty float64 `json:"presence_penalty,omitempty"` - ResponseFormat ResponseFormat `json:"response_format,omitempty"` + ResponseFormat *ResponseFormat `json:"response_format,omitempty"` // Function definitions to include in the request. Functions []FunctionDefinition `json:"functions,omitempty"` diff --git a/llms/openai/openaillm_option.go b/llms/openai/openaillm_option.go index 1a4603421..ba4771ca2 100644 --- a/llms/openai/openaillm_option.go +++ b/llms/openai/openaillm_option.go @@ -33,7 +33,7 @@ type options struct { apiType APIType httpClient openaiclient.Doer - responseFormat ResponseFormat + responseFormat *ResponseFormat // required when APIType is APITypeAzure or APITypeAzureAD apiVersion string @@ -49,7 +49,7 @@ type Option func(*options) type ResponseFormat = openaiclient.ResponseFormat // ResponseFormatJSON is the JSON response format. -var ResponseFormatJSON = ResponseFormat{Type: "json_object"} //nolint:gochecknoglobals +var ResponseFormatJSON = &ResponseFormat{Type: "json_object"} //nolint:gochecknoglobals // WithToken passes the OpenAI API token to the client. If not set, the token // is read from the OPENAI_API_KEY environment variable. @@ -124,7 +124,7 @@ func WithCallback(callbackHandler callbacks.Handler) Option { } // WithResponseFormat allows setting a custom response format. -func WithResponseFormat(responseFormat ResponseFormat) Option { +func WithResponseFormat(responseFormat *ResponseFormat) Option { return func(opts *options) { opts.responseFormat = responseFormat }