diff --git a/examples/src/main/java/dev/braintrust/examples/AnthropicInstrumentationExample.java b/examples/src/main/java/dev/braintrust/examples/AnthropicInstrumentationExample.java index 5bc3c9b..de0e071 100644 --- a/examples/src/main/java/dev/braintrust/examples/AnthropicInstrumentationExample.java +++ b/examples/src/main/java/dev/braintrust/examples/AnthropicInstrumentationExample.java @@ -28,21 +28,9 @@ public static void main(String[] args) throws Exception { var rootSpan = tracer.spanBuilder("anthropic-java-instrumentation-example").startSpan(); try (var ignored = rootSpan.makeCurrent()) { - Thread.sleep(70); // just to make span look interesting - - var request = - MessageCreateParams.builder() - .model(Model.CLAUDE_3_5_HAIKU_20241022) - .system("You are the world's greatest philosopher") - .addUserMessage("What's the meaning of life? Be very brief.") - .maxTokens(50) - .temperature(0.0) - .build(); - - var response = anthropicClient.messages().create(request); - System.out.println("~~~ GOT RESPONSE: " + response); - - Thread.sleep(30); // not required, just to show span duration + messagesApiExample(anthropicClient); + // streaming instrumentation coming soon + // messagesStreamingExample(anthropicClient); } finally { rootSpan.end(); } @@ -54,6 +42,37 @@ public static void main(String[] args) throws Exception { rootSpan.getSpanContext().getTraceId(), rootSpan.getSpanContext().getSpanId()); - System.out.println("\n\n Example complete! View your data in Braintrust: " + url); + System.out.println( + "\n\n Example complete! View your data in Braintrust: %s\n".formatted(url)); + } + + private static void messagesApiExample(AnthropicClient anthropicClient) { + var request = + MessageCreateParams.builder() + .model(Model.CLAUDE_3_5_HAIKU_20241022) + .system("Use as few words as possible in your answers") + .addUserMessage("Who was the first president of the United States?") + .maxTokens(50) + .temperature(0.0) + .build(); + var response = anthropicClient.messages().create(request); + System.out.println("\n~~~ MESSAGES RESPONSE: %s\n".formatted(response)); + } + + private static void messagesStreamingExample(AnthropicClient anthropicClient) { + var request = + MessageCreateParams.builder() + .model(Model.CLAUDE_3_5_HAIKU_20241022) + .system("Use as few words as possible in your answers") + .addUserMessage("Who was the first president of the United States?") + .maxTokens(50) + .temperature(0.0) + .build(); + + System.out.println("\n~~~ STREAMING RESPONSE:"); + try (var stream = anthropicClient.messages().createStreaming(request)) { + stream.stream().forEach(System.out::print); + } + System.out.println("\n"); } } diff --git a/examples/src/main/java/dev/braintrust/examples/OpenAIInstrumentationExample.java b/examples/src/main/java/dev/braintrust/examples/OpenAIInstrumentationExample.java index 3d52c32..eca96e8 100644 --- a/examples/src/main/java/dev/braintrust/examples/OpenAIInstrumentationExample.java +++ b/examples/src/main/java/dev/braintrust/examples/OpenAIInstrumentationExample.java @@ -22,18 +22,9 @@ public static void main(String[] args) throws Exception { BraintrustOpenAI.wrapOpenAI(openTelemetry, OpenAIOkHttpClient.fromEnv()); var rootSpan = tracer.spanBuilder("openai-java-instrumentation-example").startSpan(); try (var ignored = rootSpan.makeCurrent()) { - Thread.sleep(70); // Not required. This is just to make the span look interesting - var request = - ChatCompletionCreateParams.builder() - .model(ChatModel.GPT_4O_MINI) - .addSystemMessage("You are a helpful assistant") - .addUserMessage("What is the capital of France?") - .temperature(0.0) - .build(); - var response = openAIClient.chat().completions().create(request); - System.out.println("~~~ GOT RESPONSE: " + response); - openAIClient.completions(); - Thread.sleep(30); // Not required. This is just to make the span look interesting + chatCompletionsExample(openAIClient); + // streaming instrumentation coming soon + // chatCompletionsStreamingExample(openAIClient); } finally { rootSpan.end(); } @@ -43,6 +34,35 @@ public static void main(String[] args) throws Exception { .formatted( rootSpan.getSpanContext().getTraceId(), rootSpan.getSpanContext().getSpanId()); - System.out.println("\n\n Example complete! View your data in Braintrust: " + url); + System.out.println( + "\n\n Example complete! View your data in Braintrust: %s\n".formatted(url)); + } + + private static void chatCompletionsExample(OpenAIClient openAIClient) { + var request = + ChatCompletionCreateParams.builder() + .model(ChatModel.GPT_4O_MINI) + .addSystemMessage("You are a helpful assistant") + .addUserMessage("What is the capital of France?") + .temperature(0.0) + .build(); + var response = openAIClient.chat().completions().create(request); + System.out.println("\n~~~ CHAT COMPLETIONS RESPONSE: %s\n".formatted(response)); + } + + private static void chatCompletionsStreamingExample(OpenAIClient openAIClient) { + var request = + ChatCompletionCreateParams.builder() + .model(ChatModel.GPT_4O_MINI) + .addSystemMessage("You are a helpful assistant") + .addUserMessage("What is the capital of France?") + .temperature(0.0) + .build(); + + System.out.println("\n~~~ STREAMING RESPONSE:"); + try (var stream = openAIClient.chat().completions().createStreaming(request)) { + stream.stream().forEach(System.out::print); + } + System.out.println("\n"); } } diff --git a/src/main/java/dev/braintrust/instrumentation/anthropic/otel/GenAiAttributes.java b/src/main/java/dev/braintrust/instrumentation/anthropic/otel/GenAiAttributes.java index 305bb38..d328b68 100644 --- a/src/main/java/dev/braintrust/instrumentation/anthropic/otel/GenAiAttributes.java +++ b/src/main/java/dev/braintrust/instrumentation/anthropic/otel/GenAiAttributes.java @@ -1,13 +1,7 @@ package dev.braintrust.instrumentation.anthropic.otel; -import static io.opentelemetry.api.common.AttributeKey.stringKey; - -import io.opentelemetry.api.common.AttributeKey; - // copied from GenAiIncubatingAttributes final class GenAiAttributes { - static final AttributeKey GEN_AI_PROVIDER_NAME = stringKey("gen_ai.provider.name"); - static final class GenAiOperationNameIncubatingValues { static final String CHAT = "chat"; diff --git a/src/test/java/dev/braintrust/instrumentation/anthropic/BraintrustAnthropicTest.java b/src/test/java/dev/braintrust/instrumentation/anthropic/BraintrustAnthropicTest.java index a145b74..176644a 100644 --- a/src/test/java/dev/braintrust/instrumentation/anthropic/BraintrustAnthropicTest.java +++ b/src/test/java/dev/braintrust/instrumentation/anthropic/BraintrustAnthropicTest.java @@ -119,6 +119,8 @@ void testWrapAnthropic() { var span = spanData.get(0); // Verify standard GenAI attributes + assertEquals( + "anthropic", span.getAttributes().get(AttributeKey.stringKey("gen_ai.system"))); assertEquals( "claude-3-5-haiku-20241022", span.getAttributes().get(AttributeKey.stringKey("gen_ai.request.model")));