main.go

 1package main
 2
 3import (
 4	"context"
 5	"encoding/json"
 6	"fmt"
 7
 8	"github.com/charmbracelet/crush/internal/ai"
 9	"github.com/charmbracelet/crush/internal/ai/providers"
10)
11
12func main() {
13	provider := providers.NewOpenAIProvider(providers.WithOpenAIApiKey("$OPENAI_API_KEY"))
14	model := provider.LanguageModel("gpt-4o")
15
16	stream, err := model.Stream(context.Background(), ai.Call{
17		Prompt: ai.Prompt{
18			ai.NewUserMessage("Whats the weather in pristina."),
19		},
20		Temperature: ai.FloatOption(0.7),
21		Tools: []ai.Tool{
22			ai.FunctionTool{
23				Name:        "weather",
24				Description: "Gets the weather for a location",
25				InputSchema: map[string]any{
26					"type": "object",
27					"properties": map[string]any{
28						"location": map[string]string{
29							"type":        "string",
30							"description": "the city",
31						},
32					},
33					"required": []string{
34						"location",
35					},
36				},
37			},
38		},
39	})
40	if err != nil {
41		fmt.Println(err)
42		return
43	}
44
45	for chunk := range stream {
46		data, _ := json.Marshal(chunk)
47		fmt.Println(string(data))
48	}
49}