|
| 1 | +package dev.braintrust.examples; |
| 2 | + |
| 3 | +import com.google.genai.Client; |
| 4 | +import dev.braintrust.Braintrust; |
| 5 | +import dev.braintrust.config.BraintrustConfig; |
| 6 | +import dev.braintrust.instrumentation.genai.BraintrustGenAI; |
| 7 | +import io.opentelemetry.api.OpenTelemetry; |
| 8 | +import io.opentelemetry.api.trace.Span; |
| 9 | +import io.opentelemetry.api.trace.Tracer; |
| 10 | +import io.opentelemetry.context.Scope; |
| 11 | +import org.springframework.ai.chat.model.ChatModel; |
| 12 | +import org.springframework.ai.chat.prompt.Prompt; |
| 13 | +import org.springframework.ai.google.genai.GoogleGenAiChatModel; |
| 14 | +import org.springframework.ai.google.genai.GoogleGenAiChatOptions; |
| 15 | +import org.springframework.boot.CommandLineRunner; |
| 16 | +import org.springframework.boot.SpringApplication; |
| 17 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 18 | +import org.springframework.boot.autoconfigure.http.client.HttpClientAutoConfiguration; |
| 19 | +import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration; |
| 20 | +import org.springframework.context.annotation.Bean; |
| 21 | + |
| 22 | +/** Spring Boot application demonstrating Braintrust + Spring AI integration */ |
| 23 | +@SpringBootApplication( |
| 24 | + // NOTE: these excludes are specific to the Braintrust examples project to play nice with |
| 25 | + // other examples' classpaths. Excludes are not required for production spring apps |
| 26 | + exclude = {HttpClientAutoConfiguration.class, RestClientAutoConfiguration.class}) |
| 27 | +public class SpringAIExample { |
| 28 | + |
| 29 | + public static void main(String[] args) { |
| 30 | + SpringApplication.run(SpringAIExample.class, args); |
| 31 | + } |
| 32 | + |
| 33 | + @Bean |
| 34 | + public CommandLineRunner run(ChatModel chatModel, Tracer tracer, Braintrust braintrust) { |
| 35 | + return args -> { |
| 36 | + Span rootSpan = tracer.spanBuilder("spring-ai-example").startSpan(); |
| 37 | + try (Scope scope = rootSpan.makeCurrent()) { |
| 38 | + System.out.println("\n=== Running Spring Boot Example ===\n"); |
| 39 | + |
| 40 | + // Make a simple chat call |
| 41 | + var prompt = new Prompt("what's the name of the most popular java DI framework?"); |
| 42 | + var response = chatModel.call(prompt); |
| 43 | + |
| 44 | + System.out.println( |
| 45 | + "~~~ SPRING AI CHAT RESPONSE: %s\n" |
| 46 | + .formatted(response.getResult().getOutput().getText())); |
| 47 | + } finally { |
| 48 | + rootSpan.end(); |
| 49 | + } |
| 50 | + |
| 51 | + var url = |
| 52 | + braintrust.projectUri() |
| 53 | + + "/logs?r=%s&s=%s" |
| 54 | + .formatted( |
| 55 | + rootSpan.getSpanContext().getTraceId(), |
| 56 | + rootSpan.getSpanContext().getSpanId()); |
| 57 | + |
| 58 | + System.out.println( |
| 59 | + "\n Example complete! View your data in Braintrust: %s\n".formatted(url)); |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + @Bean |
| 64 | + public Braintrust braintrust() { |
| 65 | + return Braintrust.get(BraintrustConfig.fromEnvironment()); |
| 66 | + } |
| 67 | + |
| 68 | + @Bean |
| 69 | + public OpenTelemetry openTelemetry(Braintrust braintrust) { |
| 70 | + return braintrust.openTelemetryCreate(); |
| 71 | + } |
| 72 | + |
| 73 | + @Bean |
| 74 | + public Tracer tracer(OpenTelemetry openTelemetry) { |
| 75 | + return openTelemetry.getTracer("spring-ai-instrumentation"); |
| 76 | + } |
| 77 | + |
| 78 | + @Bean |
| 79 | + public String aiProvider() { |
| 80 | + // return "openai"; |
| 81 | + // return "anthropic"; |
| 82 | + return "google"; |
| 83 | + } |
| 84 | + |
| 85 | + @Bean |
| 86 | + public ChatModel chatModel(String aiProvider, OpenTelemetry openTelemetry) { |
| 87 | + return switch (aiProvider) { |
| 88 | + case "openai", "anthropic" -> { |
| 89 | + throw new RuntimeException("TODO: " + aiProvider); |
| 90 | + } |
| 91 | + case "google" -> { |
| 92 | + if (null == System.getenv("GOOGLE_API_KEY") |
| 93 | + && null == System.getenv("GEMINI_API_KEY")) { |
| 94 | + System.err.println( |
| 95 | + "\n" |
| 96 | + + "WARNING: Neither GOOGLE_API_KEY nor GEMINI_API_KEY found. This" |
| 97 | + + " example will likely fail.\n" |
| 98 | + + "Set either: export GOOGLE_API_KEY='your-key' (recommended) or" |
| 99 | + + " export GEMINI_API_KEY='your-key'\n"); |
| 100 | + } |
| 101 | + Client genAIClient = BraintrustGenAI.wrap(openTelemetry, new Client.Builder()); |
| 102 | + yield GoogleGenAiChatModel.builder() |
| 103 | + .genAiClient(genAIClient) |
| 104 | + .defaultOptions( |
| 105 | + GoogleGenAiChatOptions.builder() |
| 106 | + .model("gemini-2.0-flash-lite") |
| 107 | + .temperature(0.0) |
| 108 | + .maxOutputTokens(50) |
| 109 | + .build()) |
| 110 | + .build(); |
| 111 | + } |
| 112 | + default -> throw new RuntimeException("unsupported provider: " + aiProvider); |
| 113 | + }; |
| 114 | + } |
| 115 | +} |
0 commit comments