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 bytecode 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("cute_dog_tool", "Provide up-to-date info on cute dogs.", fetchCuteDogInfo)
41
42// Equip your agent.
43agent := fantasy.NewAgent(model, fantasy.WithTools(cuteDogTool))
44
45// Put that agent to work!
46const prompt = "Find all the cute dogs in Silver Lake, Los Angeles."
47result, err := agent.Generate(context.Background(), fantasy.AgentCall{Prompt: prompt})
48if err != nil {
49 fmt.Fprintln(os.Stderr, "Oof:", err)
50 os.Exit(1)
51}
52fmt.Println(result.Response.Content.Text())
53```
54
55🍔 For the full implementation and more [see the examples directory](https://github.com/charmbracelet/fantasy/tree/main/examples).
56
57## Multi-model? Multi-provider?
58
59Yeah! 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).
60
61## Work in Progress
62
63We 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:
64
65- Image models
66- Audio models
67- PDF uploads
68
69For things you’d like to see supported, PRs are welcome.
70
71## Whatcha think?
72
73We’d love to hear your thoughts on this project. Need help? We gotchu. You can find us on:
74
75- [Slack](https://charm.land/slack)
76- [Discord][discord]
77- [Twitter](https://twitter.com/charmcli)
78- [The Fediverse](https://mastodon.social/@charmcli)
79- [Bluesky](https://bsky.app/profile/charm.land)
80
81[discord]: https://charm.land/discord
82
83---
84
85Part of [Charm](https://charm.land).
86
87<a href="https://charm.land/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-banner-next.jpg" width="400"></a>
88
89Charm热爱开源 • Charm loves open source