1package main
2
3// This is a basic example illustrting how to create an agent with a custom
4// tool call.
5
6import (
7 "context"
8 "fmt"
9 "os"
10
11 "charm.land/fantasy"
12 "charm.land/fantasy/providers/openrouter"
13)
14
15func main() {
16 // Choose your fave provider.
17 provider, err := openrouter.New(openrouter.WithAPIKey(os.Getenv("OPENROUTER_API_KEY")))
18 if err != nil {
19 fmt.Fprintln(os.Stderr, "Whoops:", err)
20 os.Exit(1)
21 }
22
23 ctx := context.Background()
24
25 // Pick your fave model.
26 model, err := provider.LanguageModel(ctx, "moonshotai/kimi-k2")
27 if err != nil {
28 fmt.Fprintln(os.Stderr, "Dang:", err)
29 os.Exit(1)
30 }
31
32 // Let's make a tool that fetches info about cute dogs. Here's a schema
33 // for the tool's input.
34 type cuteDogQuery struct {
35 Location string `json:"location" description:"The location to search for cute dogs."`
36 }
37
38 // And here's the implementation of that tool.
39 fetchCuteDogInfo := func(ctx context.Context, input cuteDogQuery, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
40 if input.Location == "Silver Lake, Los Angeles" {
41 return fantasy.NewTextResponse("Cute dogs are everywhere!"), nil
42 }
43 return fantasy.NewTextResponse("No cute dogs found."), nil
44 }
45
46 // Add the tool.
47 cuteDogTool := fantasy.NewAgentTool("cute_dog_tool", "Provide up-to-date info on cute dogs.", fetchCuteDogInfo)
48
49 // Equip your agent.
50 agent := fantasy.NewAgent(model, fantasy.WithTools(cuteDogTool))
51
52 // Put that agent to work!
53 const prompt = "Find all the cute dogs in Silver Lake, Los Angeles."
54 result, err := agent.Generate(ctx, fantasy.AgentCall{Prompt: prompt})
55 if err != nil {
56 fmt.Fprintln(os.Stderr, "Oof:", err)
57 os.Exit(1)
58 }
59 fmt.Println(result.Response.Content.Text())
60}