Skip to content

Commit

Permalink
Add SystemFingerprint and chatMsg.ToolCallID field (#543)
Browse files Browse the repository at this point in the history
* fix ToolChoiche typo

* add tool_call_id to ChatCompletionMessage

* add /chat system_fingerprint response field

* check empty ToolCallID JSON marshaling

and add omitempty for tool_call_id

* messages also required; don't omitempty

* add Type to ToolCall, required by the API

* fix test, omitempty for response_format ptr

* fix casing of role values in comments
  • Loading branch information
gburt committed Nov 9, 2023
1 parent bc89139 commit e3e065d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
27 changes: 17 additions & 10 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,17 @@ type ChatCompletionMessage struct {
Name string `json:"name,omitempty"`

FunctionCall *FunctionCall `json:"function_call,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`

// For Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls.
ToolCalls []ToolCall `json:"tool_calls,omitempty"`

// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
ToolCallID string `json:"tool_call_id,omitempty"`
}

type ToolCall struct {
ID string `json:"id"`
Type ToolType `json:"type"`
Function FunctionCall `json:"function"`
}

Expand All @@ -84,7 +90,7 @@ const (
)

type ChatCompletionResponseFormat struct {
Type ChatCompletionResponseFormatType `json:"type"`
Type ChatCompletionResponseFormatType `json:"type,omitempty"`
}

// ChatCompletionRequest represents a request structure for chat completion API.
Expand Down Expand Up @@ -112,7 +118,7 @@ type ChatCompletionRequest struct {
FunctionCall any `json:"function_call,omitempty"`
Tools []Tool `json:"tools,omitempty"`
// This can be either a string or an ToolChoice object.
ToolChoiche any `json:"tool_choice,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
}

type ToolType string
Expand All @@ -126,7 +132,7 @@ type Tool struct {
Function FunctionDefinition `json:"function,omitempty"`
}

type ToolChoiche struct {
type ToolChoice struct {
Type ToolType `json:"type"`
Function ToolFunction `json:"function,omitempty"`
}
Expand Down Expand Up @@ -182,12 +188,13 @@ type ChatCompletionChoice struct {

// ChatCompletionResponse represents a response structure for chat completion API.
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []ChatCompletionChoice `json:"choices"`
Usage Usage `json:"usage"`
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []ChatCompletionChoice `json:"choices"`
Usage Usage `json:"usage"`
SystemFingerprint string `json:"system_fingerprint"`

httpHeader
}
Expand Down
14 changes: 14 additions & 0 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ func TestChatCompletionsWrongModel(t *testing.T) {
checks.ErrorIs(t, err, openai.ErrChatCompletionInvalidModel, msg)
}

func TestChatRequestOmitEmpty(t *testing.T) {
data, err := json.Marshal(openai.ChatCompletionRequest{
// We set model b/c it's required, so omitempty doesn't make sense
Model: "gpt-4",
})
checks.NoError(t, err)

// messages is also required so isn't omitted
const expected = `{"model":"gpt-4","messages":null}`
if string(data) != expected {
t.Errorf("expected JSON with all empty fields to be %v but was %v", expected, string(data))
}
}

func TestChatCompletionsWithStream(t *testing.T) {
config := openai.DefaultConfig("whatever")
config.BaseURL = "http://localhost/v1"
Expand Down

0 comments on commit e3e065d

Please sign in to comment.