1package main
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os"
8
9 "charm.land/fantasy"
10 "charm.land/fantasy/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.Println(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."),
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.Println(err)
47 os.Exit(1)
48 }
49
50 for chunk := range stream {
51 data, err := json.Marshal(chunk)
52 if err != nil {
53 fmt.Println(err)
54 continue
55 }
56 fmt.Println(string(data))
57 }
58}