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

fix error 400 during function call #433

Conversation

mehulgohil
Copy link
Contributor

@mehulgohil mehulgohil commented Jul 10, 2023

Describe the change
Added omitempty as json tag for properties field

Describe your solution
Was getting error 400 while calling open api function call. So have added omitempty json tag. As properties was going as null

Tests
Fixed tests accordingly

Issue: #432

@codecov
Copy link

codecov bot commented Jul 10, 2023

Codecov Report

Merging #433 (127c95d) into master (f028c28) will not change coverage.
The diff coverage is n/a.

❗ Current head 127c95d differs from pull request most recent head 6f7ec02. Consider uploading reports for the commit 6f7ec02 to get more accurate results

@@           Coverage Diff           @@
##           master     #433   +/-   ##
=======================================
  Coverage   97.02%   97.02%           
=======================================
  Files          17       17           
  Lines         705      705           
=======================================
  Hits          684      684           
  Misses         15       15           
  Partials        6        6           

@vvatanabe
Copy link
Collaborator

@mehulgohil Thanks for PR. This issue has already been Fixed in #431. This commit has not yet been tagged for release.

@vvatanabe
Copy link
Collaborator

@mehulgohil Please try running the following code using the latest go-openai. (Commit ID: f028c28).

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/sashabaranov/go-openai"
	"github.com/sashabaranov/go-openai/jsonschema"
)

func main() {
	client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
	resp, err := client.CreateChatCompletion(
		context.Background(),
		openai.ChatCompletionRequest{
			Model: openai.GPT3Dot5Turbo,
			Messages: []openai.ChatCompletionMessage{
				{
					Role:    openai.ChatMessageRoleUser,
					Content: "What is the weather like in Boston?",
				},
			},
			Functions: []openai.FunctionDefinition{{
				Name: "get_current_weather",
				Parameters: &jsonschema.Definition{
					Type: jsonschema.Object,
					Properties: map[string]jsonschema.Definition{
						"location": {
							Type:        jsonschema.String,
							Description: "The city and state, e.g. San Francisco, CA",
						},
						"unit": {
							Type: jsonschema.String,
							Enum: []string{"celsius", "fahrenheit"},
						},
					},
					Required: []string{"location"},
				},
			}},
		},
	)
	if err != nil {
		fmt.Printf("Completion error: %v\n", err)
		return
	}
	respJson, _ := json.Marshal(resp)
	fmt.Println(string(respJson))
}

@mehul-gohil
Copy link

@vvatanabe I did try with latest commit only

@mehulgohil
Copy link
Contributor Author

@vvatanabe I see the difference here
I was not using the &operator

Parameters: &jsonschema.Definition{}

@mehulgohil
Copy link
Contributor Author

@vvatanabe I have updated the readme file example for function call

@vvatanabe
Copy link
Collaborator

@mehulgohil I get it, MarshalJSON() is a receiver, so it looks like MarshalJSON() won't execute unless you pass it a pointer.

func (d *Definition) MarshalJSON() ([]byte, error)

@vvatanabe
Copy link
Collaborator

@mehulgohil I made a PR to support both pointers and values. Please check #434

@mehulgohil
Copy link
Contributor Author

This makes sense, thanks @vvatanabe

@vvatanabe
Copy link
Collaborator

@mehulgohil Merged #434

@vvatanabe vvatanabe closed this Jul 11, 2023
@ttdias25
Copy link

ttdias25 commented Oct 2, 2023

@mehulgohil Please try running the following code using the latest go-openai. (Commit ID: f028c28).

package main

import (
	"context"
	"encoding/json"
	"fmt"
	"os"

	"github.com/sashabaranov/go-openai"
	"github.com/sashabaranov/go-openai/jsonschema"
)

func main() {
	client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
	resp, err := client.CreateChatCompletion(
		context.Background(),
		openai.ChatCompletionRequest{
			Model: openai.GPT3Dot5Turbo,
			Messages: []openai.ChatCompletionMessage{
				{
					Role:    openai.ChatMessageRoleUser,
					Content: "What is the weather like in Boston?",
				},
			},
			Functions: []openai.FunctionDefinition{{
				Name: "get_current_weather",
				Parameters: &jsonschema.Definition{
					Type: jsonschema.Object,
					Properties: map[string]jsonschema.Definition{
						"location": {
							Type:        jsonschema.String,
							Description: "The city and state, e.g. San Francisco, CA",
						},
						"unit": {
							Type: jsonschema.String,
							Enum: []string{"celsius", "fahrenheit"},
						},
					},
					Required: []string{"location"},
				},
			}},
		},
	)
	if err != nil {
		fmt.Printf("Completion error: %v\n", err)
		return
	}
	respJson, _ := json.Marshal(resp)
	fmt.Println(string(respJson))
}

I tried this exact example and the response (response.Choices[0].Message.Content) was empty, any idea?

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.

4 participants