Skip to content

Commit

Permalink
llms/openai: Optionally supply ResponseFormat (#690)
Browse files Browse the repository at this point in the history
* fix: ResponseFormat cause openai api response err - is not one of ['json_object', 'text'] - 'response_format.type'
  • Loading branch information
shawti authored Mar 19, 2024
1 parent 4065971 commit 8b54683
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion llms/openai/internal/openaiclient/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
6 changes: 3 additions & 3 deletions llms/openai/openaillm_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 8b54683

Please sign in to comment.