-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (64 loc) · 1.87 KB
/
main.go
File metadata and controls
75 lines (64 loc) · 1.87 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
// This example demonstrates basic Google Gemini tracing with Braintrust.
package main
import (
"context"
"fmt"
"log"
"os"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/trace"
"google.golang.org/genai"
"github.com/braintrustdata/braintrust-sdk-go"
tracegenai "github.com/braintrustdata/braintrust-sdk-go/trace/contrib/genai"
)
func geminiAPIKey() string {
if apiKey := os.Getenv("GOOGLE_API_KEY"); apiKey != "" {
return apiKey
}
return os.Getenv("GEMINI_API_KEY")
}
func main() {
tp := trace.NewTracerProvider()
defer tp.Shutdown(context.Background()) //nolint:errcheck
otel.SetTracerProvider(tp)
bt, err := braintrust.New(tp,
braintrust.WithProject("go-sdk-examples"),
braintrust.WithBlockingLogin(true),
)
if err != nil {
log.Fatal(err)
}
// Create Gemini client with Braintrust tracing
client, err := genai.NewClient(context.Background(), &genai.ClientConfig{
HTTPClient: tracegenai.Client(), // Add tracing via custom HTTP client
APIKey: geminiAPIKey(),
Backend: genai.BackendGeminiAPI,
})
if err != nil {
log.Fatal(err)
}
// Get a tracer instance
tracer := otel.Tracer("genai-example")
// Create a parent span to wrap the Gemini call
ctx, span := tracer.Start(context.Background(), "examples/genai/main.go")
defer span.End()
// Make a simple generateContent request with Gemini thinking enabled.
thinkingBudget := int32(256)
resp, err := client.Models.GenerateContent(
ctx,
"gemini-2.5-flash",
genai.Text("Look at this sequence: 2, 6, 12, 20, 30. What is the next number? Answer briefly."),
&genai.GenerateContentConfig{
ThinkingConfig: &genai.ThinkingConfig{
IncludeThoughts: true,
ThinkingBudget: &thinkingBudget,
ThinkingLevel: genai.ThinkingLevelMedium,
},
},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %s\n", resp.Text())
fmt.Printf("View trace: %s\n", bt.Permalink(span))
}