Add SystemFingerprint and chatMsg.ToolCallID field (#543)

* 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
This commit is contained in:
Gabriel Burt
2023-11-09 03:08:43 -05:00
committed by GitHub
parent bc89139c1d
commit e3e065deb0
2 changed files with 31 additions and 10 deletions

13
chat.go
View File

@@ -62,11 +62,17 @@ type ChatCompletionMessage struct {
Name string `json:"name,omitempty"`
FunctionCall *FunctionCall `json:"function_call,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"`
}
@@ -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.
@@ -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
@@ -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"`
}
@@ -188,6 +194,7 @@ type ChatCompletionResponse struct {
Model string `json:"model"`
Choices []ChatCompletionChoice `json:"choices"`
Usage Usage `json:"usage"`
SystemFingerprint string `json:"system_fingerprint"`
httpHeader
}

View File

@@ -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"