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 := provider.LanguageModel("gpt-4o")
16
17	stream, err := model.Stream(context.Background(), ai.Call{
18		Prompt: ai.Prompt{
19			ai.NewUserMessage("Whats the weather in pristina."),
20		},
21		Temperature: ai.FloatOption(0.7),
22		Tools: []ai.Tool{
23			ai.FunctionTool{
24				Name:        "weather",
25				Description: "Gets the weather for a location",
26				InputSchema: map[string]any{
27					"type": "object",
28					"properties": map[string]any{
29						"location": map[string]string{
30							"type":        "string",
31							"description": "the city",
32						},
33					},
34					"required": []string{
35						"location",
36					},
37				},
38			},
39		},
40	})
41	if err != nil {
42		fmt.Println(err)
43		return
44	}
45
46	for chunk := range stream {
47		data, _ := json.Marshal(chunk)
48		fmt.Println(string(data))
49	}
50}