Feat Support tools and tools choice new fileds (#526)

* feat: support tools and tools choice new fileds

* fix: use value not pointers
This commit is contained in:
Simone Vellei
2023-11-07 16:53:24 +01:00
committed by GitHub
parent 3063e676bf
commit 1ad6b6f53d
2 changed files with 38 additions and 4 deletions

41
chat.go
View File

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

View File

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