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(tools): support Tool calls in the API #1715

Merged
merged 9 commits into from
Feb 17, 2024
10 changes: 10 additions & 0 deletions api/openai/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ func updateRequestConfig(config *config.Config, input *schema.OpenAIRequest) {
}
}

if len(input.Tools) > 0 {
for _, tool := range input.Tools {
input.Functions = append(input.Functions, tool.Function)
}
}

if input.ToolsChoice != nil {
input.FunctionCall = input.ToolsChoice
mudler marked this conversation as resolved.
Show resolved Hide resolved
}

// Decode each request's message content
index := 0
for i, m := range input.Messages {
Expand Down
3 changes: 3 additions & 0 deletions api/schema/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ type OpenAIRequest struct {
Functions []grammar.Function `json:"functions" yaml:"functions"`
FunctionCall interface{} `json:"function_call" yaml:"function_call"` // might be a string or an object

Tools []grammar.Tool `json:"tools,omitempty" yaml:"tools"`
ToolsChoice interface{} `json:"tool_choice,omitempty" yaml:"tool_choice"`

Stream bool `json:"stream"`

// Image (not supported by OpenAI)
Expand Down
6 changes: 6 additions & 0 deletions pkg/grammar/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ type Function struct {
}
type Functions []Function

type Tool struct {
Type string `json:"type"`
Function Function `json:"function,omitempty"`
}
type Tools []Tool

func (f Functions) ToJSONStructure() JSONFunctionStructure {
js := JSONFunctionStructure{}
for _, function := range f {
Expand Down
Loading