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

Add completion-with-tool example #598

Merged
merged 1 commit into from
Nov 26, 2023
Merged
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
94 changes: 94 additions & 0 deletions examples/completion-with-tool/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"context"
"fmt"
"os"

"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/jsonschema"
)

func main() {
ctx := context.Background()
client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))

// describe the function & its inputs
params := jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"location": {
Type: jsonschema.String,
Description: "The city and state, e.g. San Francisco, CA",
},
"unit": {
Type: jsonschema.String,
Enum: []string{"celsius", "fahrenheit"},
},
},
Required: []string{"location"},
}
f := openai.FunctionDefinition{
Name: "get_current_weather",
Description: "Get the current weather in a given location",
Parameters: params,
}
t := openai.Tool{
Type: openai.ToolTypeFunction,
Function: f,
}

// simulate user asking a question that requires the function
dialogue := []openai.ChatCompletionMessage{
{Role: openai.ChatMessageRoleUser, Content: "What is the weather in Boston today?"},
}
fmt.Printf("Asking OpenAI '%v' and providing it a '%v()' function...\n",
dialogue[0].Content, f.Name)
resp, err := client.CreateChatCompletion(ctx,
openai.ChatCompletionRequest{
Model: openai.GPT4TurboPreview,
Messages: dialogue,
Tools: []openai.Tool{t},
},
)
if err != nil || len(resp.Choices) != 1 {
fmt.Printf("Completion error: err:%v len(choices):%v\n", err,
len(resp.Choices))
return
}
msg := resp.Choices[0].Message
if len(msg.ToolCalls) != 1 {
fmt.Printf("Completion error: len(toolcalls): %v\n", len(msg.ToolCalls))
return
}

// simulate calling the function & responding to OpenAI
dialogue = append(dialogue, msg)
fmt.Printf("OpenAI called us back wanting to invoke our function '%v' with params '%v'\n",
msg.ToolCalls[0].Function.Name, msg.ToolCalls[0].Function.Arguments)
dialogue = append(dialogue, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleTool,
Content: "Sunny and 80 degrees.",
Name: msg.ToolCalls[0].Function.Name,
ToolCallID: msg.ToolCalls[0].ID,
})
fmt.Printf("Sending OpenAI our '%v()' function's response and requesting the reply to the original question...\n",
f.Name)
resp, err = client.CreateChatCompletion(ctx,
openai.ChatCompletionRequest{
Model: openai.GPT4TurboPreview,
Messages: dialogue,
Tools: []openai.Tool{t},
},
)
if err != nil || len(resp.Choices) != 1 {
fmt.Printf("2nd completion error: err:%v len(choices):%v\n", err,
len(resp.Choices))
return
}

// display OpenAI's response to the original question utilizing our function
msg = resp.Choices[0].Message
fmt.Printf("OpenAI answered the original request with: %v\n",
msg.Content)
}
Loading