Skip to content

Commit 1ad6b6f

Browse files
authored
Feat Support tools and tools choice new fileds (#526)
* feat: support tools and tools choice new fileds * fix: use value not pointers
1 parent 3063e67 commit 1ad6b6f

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

chat.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const (
1212
ChatMessageRoleUser = "user"
1313
ChatMessageRoleAssistant = "assistant"
1414
ChatMessageRoleFunction = "function"
15+
ChatMessageRoleTool = "tool"
1516
)
1617

1718
const chatCompletionsSuffix = "/chat/completions"
@@ -61,6 +62,12 @@ type ChatCompletionMessage struct {
6162
Name string `json:"name,omitempty"`
6263

6364
FunctionCall *FunctionCall `json:"function_call,omitempty"`
65+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
66+
}
67+
68+
type ToolCall struct {
69+
ID string `json:"id"`
70+
Function FunctionCall `json:"function"`
6471
}
6572

6673
type FunctionCall struct {
@@ -97,10 +104,35 @@ type ChatCompletionRequest struct {
97104
// LogitBias is must be a token id string (specified by their token ID in the tokenizer), not a word string.
98105
// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
99106
// refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
100-
LogitBias map[string]int `json:"logit_bias,omitempty"`
101-
User string `json:"user,omitempty"`
102-
Functions []FunctionDefinition `json:"functions,omitempty"`
103-
FunctionCall any `json:"function_call,omitempty"`
107+
LogitBias map[string]int `json:"logit_bias,omitempty"`
108+
User string `json:"user,omitempty"`
109+
// Deprecated: use Tools instead.
110+
Functions []FunctionDefinition `json:"functions,omitempty"`
111+
// Deprecated: use ToolChoice instead.
112+
FunctionCall any `json:"function_call,omitempty"`
113+
Tools []Tool `json:"tools,omitempty"`
114+
// This can be either a string or an ToolChoice object.
115+
ToolChoiche any `json:"tool_choice,omitempty"`
116+
}
117+
118+
type ToolType string
119+
120+
const (
121+
ToolTypeFunction ToolType = "function"
122+
)
123+
124+
type Tool struct {
125+
Type ToolType `json:"type"`
126+
Function FunctionDefinition `json:"function,omitempty"`
127+
}
128+
129+
type ToolChoiche struct {
130+
Type ToolType `json:"type"`
131+
Function ToolFunction `json:"function,omitempty"`
132+
}
133+
134+
type ToolFunction struct {
135+
Name string `json:"name"`
104136
}
105137

106138
type FunctionDefinition struct {
@@ -123,6 +155,7 @@ const (
123155
FinishReasonStop FinishReason = "stop"
124156
FinishReasonLength FinishReason = "length"
125157
FinishReasonFunctionCall FinishReason = "function_call"
158+
FinishReasonToolCalls FinishReason = "tool_calls"
126159
FinishReasonContentFilter FinishReason = "content_filter"
127160
FinishReasonNull FinishReason = "null"
128161
)

chat_stream.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type ChatCompletionStreamChoiceDelta struct {
99
Content string `json:"content,omitempty"`
1010
Role string `json:"role,omitempty"`
1111
FunctionCall *FunctionCall `json:"function_call,omitempty"`
12+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
1213
}
1314

1415
type ChatCompletionStreamChoice struct {

0 commit comments

Comments
 (0)