-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (52 loc) · 1.33 KB
/
main.go
File metadata and controls
61 lines (52 loc) · 1.33 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
59
60
61
package main
import (
"context"
"fmt"
"os"
"github.com/charmbracelet/ai/ai"
"github.com/charmbracelet/ai/providers/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)
}
// Create weather tool using the new type-safe API
type WeatherInput struct {
Location string `json:"location" description:"the city"`
}
weatherTool := ai.NewAgentTool(
"weather",
"Get weather information for a location",
func(ctx context.Context, input WeatherInput, _ ai.ToolCall) (ai.ToolResponse, error) {
return ai.NewTextResponse("40 C"), nil
},
)
agent := ai.NewAgent(
model,
ai.WithSystemPrompt("You are a helpful assistant"),
ai.WithTools(weatherTool),
)
result, err := agent.Generate(context.Background(), ai.AgentCall{
Prompt: "What's the weather in pristina",
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Steps: ", len(result.Steps))
for _, s := range result.Steps {
for _, c := range s.Content {
if c.GetType() == ai.ContentTypeToolCall {
tc, _ := ai.AsContentType[ai.ToolCallContent](c)
fmt.Println("ToolCall: ", tc.ToolName)
}
}
}
fmt.Println("Final Response: ", result.Response.Content.Text())
fmt.Println("Total Usage: ", result.TotalUsage)
}