1package main
2
3import (
4 "context"
5 "fmt"
6 "os"
7
8 "github.com/charmbracelet/ai"
9 "github.com/charmbracelet/ai/providers/openai"
10)
11
12func main() {
13 provider := openai.New(
14 openai.WithAPIKey(os.Getenv("OPENAI_API_KEY")),
15 )
16 model, err := provider.LanguageModel("gpt-4o")
17 if err != nil {
18 fmt.Println(err)
19 os.Exit(1)
20 }
21
22 // Create weather tool using the new type-safe API
23 type WeatherInput struct {
24 Location string `json:"location" description:"the city"`
25 }
26
27 weatherTool := ai.NewAgentTool(
28 "weather",
29 "Get weather information for a location",
30 func(ctx context.Context, input WeatherInput, _ ai.ToolCall) (ai.ToolResponse, error) {
31 return ai.NewTextResponse("40 C"), nil
32 },
33 )
34
35 agent := ai.NewAgent(
36 model,
37 ai.WithSystemPrompt("You are a helpful assistant"),
38 ai.WithTools(weatherTool),
39 )
40
41 result, err := agent.Generate(context.Background(), ai.AgentCall{
42 Prompt: "What's the weather in pristina",
43 })
44 if err != nil {
45 fmt.Println(err)
46 os.Exit(1)
47 }
48
49 fmt.Println("Steps: ", len(result.Steps))
50 for _, s := range result.Steps {
51 for _, c := range s.Content {
52 if c.GetType() == ai.ContentTypeToolCall {
53 tc, _ := ai.AsContentType[ai.ToolCallContent](c)
54 fmt.Println("ToolCall: ", tc.ToolName)
55 }
56 }
57 }
58
59 fmt.Println("Final Response: ", result.Response.Content.Text())
60 fmt.Println("Total Usage: ", result.TotalUsage)
61}