-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
102 lines (88 loc) · 2.51 KB
/
Copy pathmain.go
File metadata and controls
102 lines (88 loc) · 2.51 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// This example demonstrates OpenAI tracing with Braintrust using the sashabaranov/go-openai library.
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/sashabaranov/go-openai"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/trace"
"github.com/braintrustdata/braintrust-sdk-go"
traceopenai "github.com/braintrustdata/braintrust-sdk-go/trace/contrib/github.com/sashabaranov/go-openai"
)
func main() {
// Set up OpenTelemetry tracing
tp := trace.NewTracerProvider()
defer tp.Shutdown(context.Background()) //nolint:errcheck
otel.SetTracerProvider(tp)
// Initialize Braintrust
bt, err := braintrust.New(tp,
braintrust.WithProject("go-sdk-examples"),
braintrust.WithBlockingLogin(true),
)
if err != nil {
log.Fatal(err)
}
// Get API key from environment
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
log.Fatal("OPENAI_API_KEY environment variable not set")
}
// Create traced HTTP client
httpClient := traceopenai.Client()
// Create OpenAI client with traced HTTP client
config := openai.DefaultConfig(apiKey)
config.HTTPClient = httpClient
client := openai.NewClientWithConfig(config)
// Get a tracer instance from the global TracerProvider
tracer := otel.Tracer("sashabaranov-openai-example")
// Create a parent span to wrap the OpenAI call
ctx, span := tracer.Start(context.Background(), "examples/sashabaranov-openai/main.go")
defer span.End()
// Example 1: Simple chat completion
resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
Model: openai.GPT4oMini,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "What is the capital of France?",
},
},
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %s\n", resp.Choices[0].Message.Content)
// Example 2: Streaming chat completion
stream, err := client.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{
Model: openai.GPT4oMini,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "Count from 1 to 5",
},
},
StreamOptions: &openai.StreamOptions{
IncludeUsage: true, // Include token usage in streaming responses
},
})
if err != nil {
log.Fatal(err)
}
defer func() {
_ = stream.Close()
}()
fmt.Print("Streaming response: ")
for {
response, err := stream.Recv()
if err != nil {
break
}
if len(response.Choices) > 0 {
fmt.Print(response.Choices[0].Delta.Content)
}
}
fmt.Println()
fmt.Printf("\nView trace: %s\n", bt.Permalink(span))
}