1package main
2
3// This is a basic example illustrating how to create an agent with a custom
4// tool call.
5
6import (
7 "context"
8 "fmt"
9 "os"
10 "time"
11
12 "charm.land/fantasy"
13 "charm.land/fantasy/providers/kronk"
14)
15
16const modelURL = "Qwen/Qwen3-8B-GGUF/Qwen3-8B-Q8_0.gguf"
17
18func main() {
19 if err := run(); err != nil {
20 fmt.Printf("\nERROR: %s\n", err)
21 os.Exit(1)
22 }
23}
24
25func run() error {
26 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
27 defer cancel()
28
29 // Create the provider with optional logging.
30 provider, err := kronk.New(
31 kronk.WithName("kronk"),
32 kronk.WithLogger(kronk.FmtLogger),
33 )
34 if err != nil {
35 return fmt.Errorf("unable to create provider: %w", err)
36 }
37
38 // Clean up when done.
39 defer func() {
40 fmt.Println("\nUnloading Kronk")
41 if closer, ok := provider.(interface{ Close(context.Context) error }); ok {
42 if err := closer.Close(context.Background()); err != nil {
43 fmt.Printf("failed to close provider: %v\n", err)
44 }
45 }
46 }()
47
48 // Get a language model by providing the model URL.
49 // The provider will download and initialize the model automatically.
50 model, err := provider.LanguageModel(ctx, modelURL)
51 if err != nil {
52 return fmt.Errorf("unable to get language model: %w", err)
53 }
54
55 // -------------------------------------------------------------------------
56
57 // Let's make a tool that fetches info about cute dogs. Here's a schema
58 // for the tool's input.
59 type cuteDogQuery struct {
60 Location string `json:"location" description:"The location to search for cute dogs."`
61 }
62
63 // And here's the implementation of that tool.
64 fetchCuteDogInfo := func(ctx context.Context, input cuteDogQuery, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
65 if input.Location == "Silver Lake, Los Angeles" {
66 return fantasy.NewTextResponse("Cute dogs are everywhere!"), nil
67 }
68 return fantasy.NewTextResponse("No cute dogs found."), nil
69 }
70
71 // Add the tool.
72 cuteDogTool := fantasy.NewAgentTool("cute_dog_tool", "Provide up-to-date info on cute dogs.", fetchCuteDogInfo)
73
74 // Equip your agent.
75 agent := fantasy.NewAgent(model,
76 fantasy.WithSystemPrompt("You are a moderately helpful, dog-centric assistant."),
77 fantasy.WithTools(cuteDogTool),
78 fantasy.WithMaxOutputTokens(2048),
79 fantasy.WithTemperature(0.7),
80 fantasy.WithTopP(0.8),
81 fantasy.WithTopK(20),
82 )
83
84 // Put that agent to work!
85 const prompt = "Find all the cute dogs in Silver Lake, Los Angeles. Use the cute dog tool."
86 result, err := agent.Generate(ctx, fantasy.AgentCall{Prompt: prompt})
87 if err != nil {
88 return fmt.Errorf("agent generate failed: %w", err)
89 }
90 fmt.Println(result.Response.Content.Text())
91
92 return nil
93}