-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathmain.go
More file actions
58 lines (53 loc) · 1.09 KB
/
main.go
File metadata and controls
58 lines (53 loc) · 1.09 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/charmbracelet/ai/ai"
"github.com/charmbracelet/ai/openai"
)
func main() {
provider := openai.New(openai.WithAPIKey(os.Getenv("OPENAI_API_KEY")))
model, err := provider.LanguageModel("gpt-4o")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
stream, err := model.Stream(context.Background(), ai.Call{
Prompt: ai.Prompt{
ai.NewUserMessage("Whats the weather in pristina."),
},
Temperature: ai.FloatOption(0.7),
Tools: []ai.Tool{
ai.FunctionTool{
Name: "weather",
Description: "Gets the weather for a location",
InputSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"location": map[string]string{
"type": "string",
"description": "the city",
},
},
"required": []string{
"location",
},
},
},
},
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for chunk := range stream {
data, err := json.Marshal(chunk)
if err != nil {
fmt.Println(err)
continue
}
fmt.Println(string(data))
}
}