Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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<String> GEN_AI_PROVIDER_NAME = stringKey("gen_ai.provider.name");

static final class GenAiOperationNameIncubatingValues {
static final String CHAT = "chat";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand Down