main.go

 1package main
 2
 3import (
 4	"context"
 5	"fmt"
 6	"os"
 7
 8	"charm.land/fantasy"
 9	"charm.land/fantasy/openrouter"
10)
11
12func main() {
13	provider := openrouter.New(
14		openrouter.WithAPIKey(os.Getenv("OPENROUTER_API_KEY")),
15	)
16	model, err := provider.LanguageModel("moonshotai/kimi-k2-0905")
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 := fantasy.NewAgentTool(
28		"weather",
29		"Get weather information for a location",
30		func(ctx context.Context, input WeatherInput, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
31			return fantasy.NewTextResponse("40 C"), nil
32		},
33	)
34
35	agent := fantasy.NewAgent(
36		model,
37		fantasy.WithSystemPrompt("You are a helpful assistant"),
38		fantasy.WithTools(weatherTool),
39	)
40
41	result, err := agent.Generate(context.Background(), fantasy.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() == fantasy.ContentTypeToolCall {
53				tc, _ := fantasy.AsContentType[fantasy.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}