main.go

 1package main
 2
 3import (
 4	"context"
 5	"encoding/json"
 6	"fmt"
 7	"os"
 8
 9	"github.com/charmbracelet/crush/internal/ai"
10	"github.com/charmbracelet/crush/internal/ai/providers"
11)
12
13func main() {
14	provider := providers.NewOpenAIProvider(providers.WithOpenAIApiKey(os.Getenv("OPENAI_API_KEY")))
15	model, err := provider.LanguageModel("gpt-4o")
16	if err != nil {
17		fmt.Println(err)
18		return
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.FloatOption(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		return
48	}
49
50	for chunk := range stream {
51		data, _ := json.Marshal(chunk)
52		fmt.Println(string(data))
53	}
54}