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