Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Items to JSONSchemaDefine for JsonTypeArray. #375

Closed
wants to merge 1 commit into from

Conversation

ryomak
Copy link
Contributor

@ryomak ryomak commented Jun 15, 2023

In the current version (v1.11.0), when trying to use the JsonTypeArray in FunctionCall, it is not possible to define the array type, resulting in a 400 error.

I propose adding Items to JSONSchemaDefine. This would allow the array type to be specified and would eliminate the aforementioned error.

Could you please have a moment to take a look at it?

example

func main() {
	ctx := context.Background()
	c := openai.NewClient("key")
	response, err := c.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
		Model: openai.GPT40613,
		Messages: []openai.ChatCompletionMessage{
			{
				Role:    openai.ChatMessageRoleUser,
				Content: "「What is the weather like in Boston?」Break down the above sentence into words",
			},
		},
		N:                0,
		Stream:           false,
		Stop:             nil,
		MaxTokens:        1024,
		PresencePenalty:  0,
		FrequencyPenalty: 0,
		LogitBias:        nil,
		Functions: []*openai.FunctionDefine{
			{
				Name:        "split_word",
				Description: "Break sentences into words",
				Parameters: &openai.FunctionParams{
					Type: openai.JSONSchemaTypeObject,
					Properties: map[string]*openai.JSONSchemaDefine{
						"list": {
							Type: openai.JSONSchemaTypeArray,
							Items: &openai.JSONSchemaDefine{
								Type: openai.JSONSchemaTypeString,
							},
							Description: "word list",
						},
						"count": {
							Type:        openai.JSONSchemaTypeNumber,
							Description: "word count",
						},
					},
					Required: []string{"list", "count"},
				},
			},
		},
	})
	if err != nil {
		panic(err)
	}
	if response.Choices[0].FinishReason != openai.FinishReasonFunctionCall {
		panic(err)
	}
	fmt.Println(response.Choices[0].Message.FunctionCall.Arguments)
}

/*
output: 
{
  "count": 7,
  "list": ["What", "is", "the", "weather", "like", "in", "Boston?"]
}
*/

referrence

https://json-schema.org/understanding-json-schema/reference/array.html#id6

@codecov
Copy link

codecov bot commented Jun 15, 2023

Codecov Report

Merging #375 (ffbdff2) into master (0bd14f9) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #375   +/-   ##
=======================================
  Coverage   95.22%   95.22%           
=======================================
  Files          17       17           
  Lines         670      670           
=======================================
  Hits          638      638           
  Misses         22       22           
  Partials       10       10           
Impacted Files Coverage Δ
chat.go 100.00% <ø> (ø)

@vvatanabe
Copy link
Collaborator

duplicate of #373

@ryomak ryomak closed this Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants