main.go

 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, err := provider.LanguageModel("gpt-4o")
17	if err != nil {
18		fmt.Println(err)
19		return
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, _ := agent.Generate(context.Background(), ai.AgentCall{
42		Prompt: "What's the weather in pristina",
43	})
44
45	fmt.Println("Steps: ", len(result.Steps))
46	for _, s := range result.Steps {
47		for _, c := range s.Content {
48			if c.GetType() == ai.ContentTypeToolCall {
49				tc, _ := ai.AsContentType[ai.ToolCallContent](c)
50				fmt.Println("ToolCall: ", tc.ToolName)
51			}
52		}
53	}
54
55	fmt.Println("Final Response: ", result.Response.Content.Text())
56	fmt.Println("Total Usage: ", result.TotalUsage)
57}