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