forked from zendev-sh/goai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (61 loc) · 1.68 KB
/
Copy pathmain.go
File metadata and controls
70 lines (61 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//go:build ignore
// Example: OpenAI Code Interpreter tool.
//
// The model writes and executes Python code in a sandboxed container.
// Server-side execution -- no local setup needed.
//
// Via OpenAI direct:
//
// export OPENAI_API_KEY=...
// go run examples/code-interpreter/main.go openai
//
// Via Azure OpenAI:
//
// export AZURE_OPENAI_API_KEY=... AZURE_RESOURCE_NAME=...
// go run examples/code-interpreter/main.go azure
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/zendev-sh/goai"
"github.com/zendev-sh/goai/provider"
"github.com/zendev-sh/goai/provider/azure"
"github.com/zendev-sh/goai/provider/openai"
)
func main() {
auth := "azure"
if len(os.Args) > 1 {
auth = os.Args[1]
}
var model provider.LanguageModel
switch auth {
case "openai":
model = openai.Chat("gpt-4.1-mini",
openai.WithAPIKey(os.Getenv("OPENAI_API_KEY")))
case "azure":
model = azure.Chat("gpt-4.1-mini",
azure.WithAPIKey(os.Getenv("AZURE_OPENAI_API_KEY")))
default:
log.Fatalf("Unknown: %s (use openai or azure)", auth)
}
def := openai.Tools.CodeInterpreter()
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
result, err := goai.GenerateText(ctx, model,
goai.WithPrompt("Calculate the first 20 Fibonacci numbers and show them as a formatted table."),
goai.WithMaxOutputTokens(2000),
goai.WithTools(goai.Tool{
Name: def.Name,
ProviderDefinedType: def.ProviderDefinedType,
ProviderDefinedOptions: def.ProviderDefinedOptions,
}),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text)
fmt.Printf("\nUsage: %d in, %d out\n", result.TotalUsage.InputTokens, result.TotalUsage.OutputTokens)
}