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:
13
chat.go
13
chat.go
@@ -62,11 +62,17 @@ type ChatCompletionMessage struct {
|
|||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
|
|
||||||
FunctionCall *FunctionCall `json:"function_call,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"`
|
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 {
|
type ToolCall struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
|
Type ToolType `json:"type"`
|
||||||
Function FunctionCall `json:"function"`
|
Function FunctionCall `json:"function"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +90,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ChatCompletionResponseFormat struct {
|
type ChatCompletionResponseFormat struct {
|
||||||
Type ChatCompletionResponseFormatType `json:"type"`
|
Type ChatCompletionResponseFormatType `json:"type,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChatCompletionRequest represents a request structure for chat completion API.
|
// ChatCompletionRequest represents a request structure for chat completion API.
|
||||||
@@ -112,7 +118,7 @@ type ChatCompletionRequest struct {
|
|||||||
FunctionCall any `json:"function_call,omitempty"`
|
FunctionCall any `json:"function_call,omitempty"`
|
||||||
Tools []Tool `json:"tools,omitempty"`
|
Tools []Tool `json:"tools,omitempty"`
|
||||||
// This can be either a string or an ToolChoice object.
|
// 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
|
type ToolType string
|
||||||
@@ -126,7 +132,7 @@ type Tool struct {
|
|||||||
Function FunctionDefinition `json:"function,omitempty"`
|
Function FunctionDefinition `json:"function,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ToolChoiche struct {
|
type ToolChoice struct {
|
||||||
Type ToolType `json:"type"`
|
Type ToolType `json:"type"`
|
||||||
Function ToolFunction `json:"function,omitempty"`
|
Function ToolFunction `json:"function,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -188,6 +194,7 @@ type ChatCompletionResponse struct {
|
|||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Choices []ChatCompletionChoice `json:"choices"`
|
Choices []ChatCompletionChoice `json:"choices"`
|
||||||
Usage Usage `json:"usage"`
|
Usage Usage `json:"usage"`
|
||||||
|
SystemFingerprint string `json:"system_fingerprint"`
|
||||||
|
|
||||||
httpHeader
|
httpHeader
|
||||||
}
|
}
|
||||||
|
|||||||
14
chat_test.go
14
chat_test.go
@@ -51,6 +51,20 @@ func TestChatCompletionsWrongModel(t *testing.T) {
|
|||||||
checks.ErrorIs(t, err, openai.ErrChatCompletionInvalidModel, msg)
|
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) {
|
func TestChatCompletionsWithStream(t *testing.T) {
|
||||||
config := openai.DefaultConfig("whatever")
|
config := openai.DefaultConfig("whatever")
|
||||||
config.BaseURL = "http://localhost/v1"
|
config.BaseURL = "http://localhost/v1"
|
||||||
|
|||||||
Reference in New Issue
Block a user