1package main
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os"
8
9 "github.com/charmbracelet/fantasy/ai"
10 "github.com/charmbracelet/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(), ai.Call{
22 Prompt: ai.Prompt{
23 ai.NewUserMessage("Whats the weather in pristina."),
24 },
25 Temperature: ai.Opt(0.7),
26 Tools: []ai.Tool{
27 ai.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}