Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Add SystemFingerprint and chatMsg.ToolCallID field (sashabaranov#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 authored and maerlyn5 committed Nov 22, 2023
1 parent 9ca1c18 commit 6f881c7
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 @@ -91,7 +91,12 @@ type ChatCompletionChoiceMessage 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"`
}

func (message ChatCompletionChoiceMessage) ToChatCompleteMessage() ChatCompletionMessage {
Expand All @@ -111,6 +116,7 @@ func (message ChatCompletionChoiceMessage) ToChatCompleteMessage() ChatCompletio

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

Expand All @@ -128,7 +134,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 @@ -156,7 +162,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 @@ -170,7 +176,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 @@ -226,12 +232,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 @@ -56,6 +56,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 6f881c7

Please sign in to comment.