Skip to content

Commit e3e065d

Browse files
authored
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
1 parent bc89139 commit e3e065d

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

chat.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,17 @@ type ChatCompletionMessage struct {
6262
Name string `json:"name,omitempty"`
6363

6464
FunctionCall *FunctionCall `json:"function_call,omitempty"`
65-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
65+
66+
// For Role=assistant prompts this may be set to the tool calls generated by the model, such as function calls.
67+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
68+
69+
// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
70+
ToolCallID string `json:"tool_call_id,omitempty"`
6671
}
6772

6873
type ToolCall struct {
6974
ID string `json:"id"`
75+
Type ToolType `json:"type"`
7076
Function FunctionCall `json:"function"`
7177
}
7278

@@ -84,7 +90,7 @@ const (
8490
)
8591

8692
type ChatCompletionResponseFormat struct {
87-
Type ChatCompletionResponseFormatType `json:"type"`
93+
Type ChatCompletionResponseFormatType `json:"type,omitempty"`
8894
}
8995

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

118124
type ToolType string
@@ -126,7 +132,7 @@ type Tool struct {
126132
Function FunctionDefinition `json:"function,omitempty"`
127133
}
128134

129-
type ToolChoiche struct {
135+
type ToolChoice struct {
130136
Type ToolType `json:"type"`
131137
Function ToolFunction `json:"function,omitempty"`
132138
}
@@ -182,12 +188,13 @@ type ChatCompletionChoice struct {
182188

183189
// ChatCompletionResponse represents a response structure for chat completion API.
184190
type ChatCompletionResponse struct {
185-
ID string `json:"id"`
186-
Object string `json:"object"`
187-
Created int64 `json:"created"`
188-
Model string `json:"model"`
189-
Choices []ChatCompletionChoice `json:"choices"`
190-
Usage Usage `json:"usage"`
191+
ID string `json:"id"`
192+
Object string `json:"object"`
193+
Created int64 `json:"created"`
194+
Model string `json:"model"`
195+
Choices []ChatCompletionChoice `json:"choices"`
196+
Usage Usage `json:"usage"`
197+
SystemFingerprint string `json:"system_fingerprint"`
191198

192199
httpHeader
193200
}

chat_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ func TestChatCompletionsWrongModel(t *testing.T) {
5151
checks.ErrorIs(t, err, openai.ErrChatCompletionInvalidModel, msg)
5252
}
5353

54+
func TestChatRequestOmitEmpty(t *testing.T) {
55+
data, err := json.Marshal(openai.ChatCompletionRequest{
56+
// We set model b/c it's required, so omitempty doesn't make sense
57+
Model: "gpt-4",
58+
})
59+
checks.NoError(t, err)
60+
61+
// messages is also required so isn't omitted
62+
const expected = `{"model":"gpt-4","messages":null}`
63+
if string(data) != expected {
64+
t.Errorf("expected JSON with all empty fields to be %v but was %v", expected, string(data))
65+
}
66+
}
67+
5468
func TestChatCompletionsWithStream(t *testing.T) {
5569
config := openai.DefaultConfig("whatever")
5670
config.BaseURL = "http://localhost/v1"

0 commit comments

Comments
 (0)