diff --git a/chat.go b/chat.go index 88db8cf1d..04303184a 100644 --- a/chat.go +++ b/chat.go @@ -12,6 +12,7 @@ const ( ChatMessageRoleUser = "user" ChatMessageRoleAssistant = "assistant" ChatMessageRoleFunction = "function" + ChatMessageRoleTool = "tool" ) const chatCompletionsSuffix = "/chat/completions" @@ -61,6 +62,12 @@ type ChatCompletionMessage struct { Name string `json:"name,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` +} + +type ToolCall struct { + ID string `json:"id"` + Function FunctionCall `json:"function"` } type FunctionCall struct { @@ -97,10 +104,35 @@ type ChatCompletionRequest struct { // LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string. // incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}` // refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias - LogitBias map[string]int `json:"logit_bias,omitempty"` - User string `json:"user,omitempty"` - Functions []FunctionDefinition `json:"functions,omitempty"` - FunctionCall any `json:"function_call,omitempty"` + LogitBias map[string]int `json:"logit_bias,omitempty"` + User string `json:"user,omitempty"` + // Deprecated: use Tools instead. + Functions []FunctionDefinition `json:"functions,omitempty"` + // Deprecated: use ToolChoice instead. + 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"` +} + +type ToolType string + +const ( + ToolTypeFunction ToolType = "function" +) + +type Tool struct { + Type ToolType `json:"type"` + Function FunctionDefinition `json:"function,omitempty"` +} + +type ToolChoiche struct { + Type ToolType `json:"type"` + Function ToolFunction `json:"function,omitempty"` +} + +type ToolFunction struct { + Name string `json:"name"` } type FunctionDefinition struct { @@ -123,6 +155,7 @@ const ( FinishReasonStop FinishReason = "stop" FinishReasonLength FinishReason = "length" FinishReasonFunctionCall FinishReason = "function_call" + FinishReasonToolCalls FinishReason = "tool_calls" FinishReasonContentFilter FinishReason = "content_filter" FinishReasonNull FinishReason = "null" ) diff --git a/chat_stream.go b/chat_stream.go index f1faa3964..57cfa789f 100644 --- a/chat_stream.go +++ b/chat_stream.go @@ -9,6 +9,7 @@ type ChatCompletionStreamChoiceDelta struct { Content string `json:"content,omitempty"` Role string `json:"role,omitempty"` FunctionCall *FunctionCall `json:"function_call,omitempty"` + ToolCalls []ToolCall `json:"tool_calls,omitempty"` } type ChatCompletionStreamChoice struct {