Skip to content

Commit

Permalink
Feat Support tools and tools choice new fileds (#526)
Browse files Browse the repository at this point in the history
* feat: support tools and tools choice new fileds

* fix: use value not pointers
  • Loading branch information
henomis authored Nov 7, 2023
1 parent 3063e67 commit 1ad6b6f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
41 changes: 37 additions & 4 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
ChatMessageRoleUser = "user"
ChatMessageRoleAssistant = "assistant"
ChatMessageRoleFunction = "function"
ChatMessageRoleTool = "tool"
)

const chatCompletionsSuffix = "/chat/completions"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
)
Expand Down
1 change: 1 addition & 0 deletions chat_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1ad6b6f

Please sign in to comment.