-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.go
90 lines (73 loc) · 2.27 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"github.com/openai/openai-go"
)
// Mock function to simulate weather data retrieval
func getWeather(location string) string {
// In a real implementation, this function would call a weather API
return "Sunny, 25°C"
}
func main() {
client := openai.NewClient()
ctx := context.Background()
question := "Begin a very brief introduction of Greece, then incorporate the local weather of a few towns"
print("> ")
println(question)
println()
params := openai.ChatCompletionNewParams{
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
openai.UserMessage(question),
}),
Seed: openai.Int(0),
Model: openai.F(openai.ChatModelGPT4o),
Tools: openai.F([]openai.ChatCompletionToolParam{
{
Type: openai.F(openai.ChatCompletionToolTypeFunction),
Function: openai.F(openai.FunctionDefinitionParam{
Name: openai.String("get_live_weather"),
Description: openai.String("Get weather at the given location"),
Parameters: openai.F(openai.FunctionParameters{
"type": "object",
"properties": map[string]interface{}{
"location": map[string]string{
"type": "string",
},
},
"required": []string{"location"},
}),
}),
},
}),
}
stream := client.Chat.Completions.NewStreaming(ctx, params)
acc := openai.ChatCompletionAccumulator{}
for stream.Next() {
chunk := stream.Current()
acc.AddChunk(chunk)
// When this fires, the current chunk value will not contain content data
if content, ok := acc.JustFinishedContent(); ok {
println("Content stream finished:", content)
println()
}
if tool, ok := acc.JustFinishedToolCall(); ok {
println("Tool call stream finished:", tool.Index, tool.Name, tool.Arguments)
println()
}
if refusal, ok := acc.JustFinishedRefusal(); ok {
println("Refusal stream finished:", refusal)
println()
}
// It's best to use chunks after handling JustFinished events
if len(chunk.Choices) > 0 {
println(chunk.Choices[0].Delta.JSON.RawJSON())
}
}
if err := stream.Err(); err != nil {
panic(err)
}
// After the stream is finished, acc can be used like a ChatCompletion
_ = acc.Choices[0].Message.Content
println("Total Tokens:", acc.Usage.TotalTokens)
println("Finish Reason:", acc.Choices[0].FinishReason)
}