Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat Support tools and tools choice new fileds #526

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -84,10 +91,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"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix included here #543

}

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 @@ -110,6 +142,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"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this field in the docs, is it undocumented yet? https://platform.openai.com/docs/api-reference/chat/streaming

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes documented here: "The chat completion chunk object" -> choices -> delta -> tool_calls

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Also noted: lots of things are marked as deprecated now, we should probably reflect those too #540

}

type ChatCompletionStreamChoice struct {
Expand Down
Loading