1# Fantasy
2
3<p>
4 <img width="475" alt="The Charm Fantasy logo" src="https://github.com/user-attachments/assets/b22c5862-792a-44c1-bc98-55a2e46c8fb9" /><br>
5 <a href="https://github.com/charmbracelet/fantasy/releases"><img src="https://img.shields.io/github/release/charmbracelet/fantasy.svg" alt="Latest Release"></a>
6 <a href="https://pkg.go.dev/charm.land/fantasy?tab=doc"><img src="https://godoc.org/charm.land/fantasy?status.svg" alt="GoDoc"></a>
7 <a href="https://github.com/charmbracelet/fantasy/actions"><img src="https://github.com/charmbracelet/fantasy/actions/workflows/build.yml/badge.svg?branch=main" alt="Build Status"></a>
8</p>
9
10Build AI agents with Go. Multi-provider, multi-model, one API.
11
121. Choose a model and provider
132. Add some tools
143. Compile to native machine code and let it rip
15
16> [!NOTE]
17> Fantasy is currently a preview. Expect API changes.
18
19```go
20import "charm.land/fantasy"
21import "charm.land/fantasy/providers/openrouter"
22
23// Choose your fave provider.
24provider, err := openrouter.New(openrouter.WithAPIKey(myHotKey))
25if err != nil {
26 fmt.Fprintln(os.Stderr, "Whoops:", err)
27 os.Exit(1)
28}
29
30ctx := context.Background()
31
32// Pick your fave model.
33model, err := provider.LanguageModel(ctx, "moonshotai/kimi-k2")
34if err != nil {
35 fmt.Fprintln(os.Stderr, "Dang:", err)
36 os.Exit(1)
37}
38
39// Make your own tools.
40cuteDogTool := fantasy.NewAgentTool(
41 "cute_dog_tool",
42 "Provide up-to-date info on cute dogs.",
43 fetchCuteDogInfoFunc,
44)
45
46// Equip your agent.
47agent := fantasy.NewAgent(model, fantasy.WithTools(cuteDogTool))
48
49// Put that agent to work!
50const prompt = "Find all the cute dogs in Silver Lake, Los Angeles."
51result, err := agent.Generate(context.Background(), fantasy.AgentCall{Prompt: prompt})
52if err != nil {
53 fmt.Fprintln(os.Stderr, "Oof:", err)
54 os.Exit(1)
55}
56fmt.Println(result.Response.Content.Text())
57```
58
59🍔 For the full implementation and more [see the examples directory](https://github.com/charmbracelet/fantasy/tree/main/examples).
60
61## Multi-model? Multi-provider?
62
63Yeah! Fantasy is designed to support a wide variety of providers and models under a single API. While many providers such as Microsoft Azure, Amazon Bedrock, and OpenRouter have dedicated package, many other work just fine with `openaicompat`, the generic OpenAI-compatible layer. That said, if you find a provider that’s not compatible and needs special treatment, please let us know in an issue (or open a PR).
64
65## Work in Progress
66
67We built Fantasy to power [Crush](https://github.com/charmbracelet/crush), a hot coding agent for glamourously invincible development. Given that, Fantasy does not yet support things like:
68
69- Image models
70- Audio models
71- PDF uploads
72- Provider tools (e.g. web_search)
73
74For things you’d like to see supported, PRs are welcome.
75
76## Whatcha think?
77
78We’d love to hear your thoughts on this project. Need help? We gotchu. You can find us on:
79
80- [Slack](https://charm.land/slack)
81- [Discord][discord]
82- [Twitter](https://twitter.com/charmcli)
83- [The Fediverse](https://mastodon.social/@charmcli)
84- [Bluesky](https://bsky.app/profile/charm.land)
85
86[discord]: https://charm.land/discord
87
88---
89
90Part of [Charm](https://charm.land).
91
92<a href="https://charm.land/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-banner-next.jpg" width="400"></a>
93
94Charm热爱开源 • Charm loves open source