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 support for Functions #25

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type CreateCompletionParams struct {
Stop []string `json:"stop,omitempty"`
Stream bool `json:"stream,omitempty"`

Functions []Function `json:"functions,omitempty"`
FunctionCall string `json:"function_call,omitempty"`

N int `json:"n,omitempty"`
TopP float64 `json:"top_p,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
Expand Down Expand Up @@ -70,9 +73,10 @@ type Choice struct {
}

type Message struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
Name string `json:"name,omitempty"`
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
Name string `json:"name,omitempty"`
FunctionCall *FunctionCall `json:"function_call,omitempty"`
}

func (c *Client) CreateCompletion(ctx context.Context, p *CreateCompletionParams) (*CreateCompletionResponse, error) {
Expand Down
67 changes: 67 additions & 0 deletions chat/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package chat

import "encoding/json"

// EmptyParameters is an empty parameter object used for a Function that accepts no arguments.
var EmptyParameters = Schema{
Type: "object",
Properties: map[string]Schema{},
}

const (
// FunctionCallAuto indicates the model should decide whether to call the function.
// This is default when Functions are provided.
FunctionCallAuto = "auto"

// FunctionCallNone indicates the mode should not call any functions.
// This is the default when no Functions are provided.
FunctionCallNone = "none"
)

// Schema defines a schema for a JSON object.
//
// See https://json-schema.org/understanding-json-schema/
type Schema struct {
Type string `json:"type"`
Description string `json:"description,omitempty"`
Properties map[string]Schema `json:"properties"`
Required []string `json:"required,omitempty"`

// NOTE adding new fields may require updating the MarshalJSON method.
}

func (s Schema) MarshalJSON() ([]byte, error) {
type Alias Schema

// If Properties is nil marshal it without the Properties field. This,
// rather than relying on omitempty, allows us to distinguish between an
// empty map (which is needed for valid API requests) and a nil map (which
// is needed for the default value).
if s.Properties == nil {
return json.Marshal(&struct {
Type string `json:"type"`
Description string `json:"description,omitempty"`
Required []string `json:"required,omitempty"`
}{
Type: s.Type,
Description: s.Description,
Required: s.Required,
})
}

return json.Marshal((*Alias)(&s))
}

// Function is a function the model may generate JSON inputs for
type Function struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters Schema `json:"parameters"`
}

// FunctionCall is the name and arguments of a function that should be called,
// as generated by the model.
type FunctionCall struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}