|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/sashabaranov/go-openai" |
| 9 | + "github.com/sashabaranov/go-openai/jsonschema" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + ctx := context.Background() |
| 14 | + client := openai.NewClient(os.Getenv("OPENAI_API_KEY")) |
| 15 | + |
| 16 | + // describe the function & its inputs |
| 17 | + params := jsonschema.Definition{ |
| 18 | + Type: jsonschema.Object, |
| 19 | + Properties: map[string]jsonschema.Definition{ |
| 20 | + "location": { |
| 21 | + Type: jsonschema.String, |
| 22 | + Description: "The city and state, e.g. San Francisco, CA", |
| 23 | + }, |
| 24 | + "unit": { |
| 25 | + Type: jsonschema.String, |
| 26 | + Enum: []string{"celsius", "fahrenheit"}, |
| 27 | + }, |
| 28 | + }, |
| 29 | + Required: []string{"location"}, |
| 30 | + } |
| 31 | + f := openai.FunctionDefinition{ |
| 32 | + Name: "get_current_weather", |
| 33 | + Description: "Get the current weather in a given location", |
| 34 | + Parameters: params, |
| 35 | + } |
| 36 | + t := openai.Tool{ |
| 37 | + Type: openai.ToolTypeFunction, |
| 38 | + Function: f, |
| 39 | + } |
| 40 | + |
| 41 | + // simulate user asking a question that requires the function |
| 42 | + dialogue := []openai.ChatCompletionMessage{ |
| 43 | + {Role: openai.ChatMessageRoleUser, Content: "What is the weather in Boston today?"}, |
| 44 | + } |
| 45 | + fmt.Printf("Asking OpenAI '%v' and providing it a '%v()' function...\n", |
| 46 | + dialogue[0].Content, f.Name) |
| 47 | + resp, err := client.CreateChatCompletion(ctx, |
| 48 | + openai.ChatCompletionRequest{ |
| 49 | + Model: openai.GPT4TurboPreview, |
| 50 | + Messages: dialogue, |
| 51 | + Tools: []openai.Tool{t}, |
| 52 | + }, |
| 53 | + ) |
| 54 | + if err != nil || len(resp.Choices) != 1 { |
| 55 | + fmt.Printf("Completion error: err:%v len(choices):%v\n", err, |
| 56 | + len(resp.Choices)) |
| 57 | + return |
| 58 | + } |
| 59 | + msg := resp.Choices[0].Message |
| 60 | + if len(msg.ToolCalls) != 1 { |
| 61 | + fmt.Printf("Completion error: len(toolcalls): %v\n", len(msg.ToolCalls)) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // simulate calling the function & responding to OpenAI |
| 66 | + dialogue = append(dialogue, msg) |
| 67 | + fmt.Printf("OpenAI called us back wanting to invoke our function '%v' with params '%v'\n", |
| 68 | + msg.ToolCalls[0].Function.Name, msg.ToolCalls[0].Function.Arguments) |
| 69 | + dialogue = append(dialogue, openai.ChatCompletionMessage{ |
| 70 | + Role: openai.ChatMessageRoleTool, |
| 71 | + Content: "Sunny and 80 degrees.", |
| 72 | + Name: msg.ToolCalls[0].Function.Name, |
| 73 | + ToolCallID: msg.ToolCalls[0].ID, |
| 74 | + }) |
| 75 | + fmt.Printf("Sending OpenAI our '%v()' function's response and requesting the reply to the original question...\n", |
| 76 | + f.Name) |
| 77 | + resp, err = client.CreateChatCompletion(ctx, |
| 78 | + openai.ChatCompletionRequest{ |
| 79 | + Model: openai.GPT4TurboPreview, |
| 80 | + Messages: dialogue, |
| 81 | + Tools: []openai.Tool{t}, |
| 82 | + }, |
| 83 | + ) |
| 84 | + if err != nil || len(resp.Choices) != 1 { |
| 85 | + fmt.Printf("2nd completion error: err:%v len(choices):%v\n", err, |
| 86 | + len(resp.Choices)) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + // display OpenAI's response to the original question utilizing our function |
| 91 | + msg = resp.Choices[0].Message |
| 92 | + fmt.Printf("OpenAI answered the original request with: %v\n", |
| 93 | + msg.Content) |
| 94 | +} |
0 commit comments