-
-
Notifications
You must be signed in to change notification settings - Fork 710
/
Copy pathopenai_function_call_example.go
52 lines (46 loc) · 1.34 KB
/
openai_function_call_example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/openai"
"github.com/tmc/langchaingo/schema"
)
func main() {
llm, err := openai.NewChat(openai.WithModel("gpt-3.5-turbo-0613"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
completion, err := llm.Call(ctx, []schema.ChatMessage{
schema.HumanChatMessage{Content: "What is the weather like in Boston?"},
}, llms.WithFunctions(functions))
if err != nil {
log.Fatal(err)
}
if completion.FunctionCall != nil {
fmt.Printf("Function call: %v\n", completion.FunctionCall)
}
}
func getCurrentWeather(location string, unit string) (string, error) {
weatherInfo := map[string]interface{}{
"location": location,
"temperature": "72",
"unit": unit,
"forecast": []string{"sunny", "windy"},
}
b, err := json.Marshal(weatherInfo)
if err != nil {
return "", err
}
return string(b), nil
}
var functions = []llms.FunctionDefinition{
{
Name: "getCurrentWeather",
Description: "Get the current weather in a given location",
Parameters: json.RawMessage(`{"type": "object", "properties": {"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["location"]}`),
},
}