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	"github.com/charmbracelet/crush/internal/llm/tools"
11)
12
13type weatherTool struct{}
14
15// Info implements tools.BaseTool.
16func (w *weatherTool) Info() tools.ToolInfo {
17	return tools.ToolInfo{
18		Name: "weather",
19		Parameters: map[string]any{
20			"location": map[string]string{
21				"type":        "string",
22				"description": "the city",
23			},
24		},
25		Required: []string{"location"},
26	}
27}
28
29// Name implements tools.BaseTool.
30func (w *weatherTool) Name() string {
31	return "weather"
32}
33
34// Run implements tools.BaseTool.
35func (w *weatherTool) Run(ctx context.Context, params tools.ToolCall) (tools.ToolResponse, error) {
36	return tools.NewTextResponse("40 C"), nil
37}
38
39func newWeatherTool() tools.BaseTool {
40	return &weatherTool{}
41}
42
43func main() {
44	provider := providers.NewOpenAIProvider(
45		providers.WithOpenAIApiKey(os.Getenv("OPENAI_API_KEY")),
46	)
47	model := provider.LanguageModel("gpt-4o")
48
49	agent := ai.NewAgent(
50		model,
51		ai.WithSystemPrompt("You are a helpful assistant"),
52		ai.WithTools(newWeatherTool()),
53	)
54
55	result, _ := agent.Generate(context.Background(), ai.AgentCall{
56		Prompt: "What's the weather in pristina",
57	})
58
59	fmt.Println("Steps: ", len(result.Steps))
60	for _, s := range result.Steps {
61		for _, c := range s.Content {
62			if c.GetType() == ai.ContentTypeToolCall {
63				tc, _ := ai.AsContentType[ai.ToolCallContent](c)
64				fmt.Println("ToolCall: ", tc.ToolName)
65
66			}
67		}
68	}
69
70	fmt.Println("Final Response: ", result.Response.Content.Text())
71	fmt.Println("Total Usage: ", result.TotalUsage)
72}