1// Package main provides a streaming example of using the fantasy AI SDK.
2package main
3
4import (
5 "context"
6 "encoding/json"
7 "fmt"
8 "os"
9
10 "charm.land/fantasy"
11 "charm.land/fantasy/providers/openai"
12)
13
14func main() {
15 provider := openai.New(openai.WithAPIKey(os.Getenv("OPENAI_API_KEY")))
16 model, err := provider.LanguageModel("gpt-4o")
17 if err != nil {
18 fmt.Println(err)
19 os.Exit(1)
20 }
21
22 stream, err := model.Stream(context.Background(), fantasy.Call{
23 Prompt: fantasy.Prompt{
24 fantasy.NewUserMessage("Whats the weather in pristina."),
25 },
26 Temperature: fantasy.Opt(0.7),
27 Tools: []fantasy.Tool{
28 fantasy.FunctionTool{
29 Name: "weather",
30 Description: "Gets the weather for a location",
31 InputSchema: map[string]any{
32 "type": "object",
33 "properties": map[string]any{
34 "location": map[string]string{
35 "type": "string",
36 "description": "the city",
37 },
38 },
39 "required": []string{
40 "location",
41 },
42 },
43 },
44 },
45 })
46 if err != nil {
47 fmt.Println(err)
48 os.Exit(1)
49 }
50
51 for chunk := range stream {
52 data, err := json.Marshal(chunk)
53 if err != nil {
54 fmt.Println(err)
55 continue
56 }
57 fmt.Println(string(data))
58 }
59}