main.go

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