|
3 | 3 | import com.openai.client.OpenAIClient; |
4 | 4 | import com.openai.client.okhttp.OpenAIOkHttpClient; |
5 | 5 | import com.openai.models.ChatModel; |
| 6 | +import com.openai.models.chat.completions.ChatCompletionContentPart; |
| 7 | +import com.openai.models.chat.completions.ChatCompletionContentPartImage; |
| 8 | +import com.openai.models.chat.completions.ChatCompletionContentPartText; |
6 | 9 | import com.openai.models.chat.completions.ChatCompletionCreateParams; |
7 | 10 | import com.openai.models.chat.completions.ChatCompletionStreamOptions; |
| 11 | +import com.openai.models.chat.completions.ChatCompletionUserMessageParam; |
8 | 12 | import dev.braintrust.config.BraintrustConfig; |
9 | 13 | import dev.braintrust.instrumentation.openai.BraintrustOpenAI; |
| 14 | +import dev.braintrust.trace.Base64Attachment; |
10 | 15 | import dev.braintrust.trace.BraintrustTracing; |
| 16 | +import java.util.Arrays; |
11 | 17 |
|
12 | 18 | /** Basic OTel + OpenAI instrumentation example */ |
13 | 19 | public class OpenAIInstrumentationExample { |
@@ -39,11 +45,44 @@ public static void main(String[] args) throws Exception { |
39 | 45 | } |
40 | 46 |
|
41 | 47 | private static void chatCompletionsExample(OpenAIClient openAIClient) { |
| 48 | + // Load and encode the image using Base64Attachment helper |
| 49 | + String imageDataUrl = |
| 50 | + Base64Attachment.ofFile( |
| 51 | + Base64Attachment.ContentType.IMAGE_JPEG, |
| 52 | + "/home/ark/braintrust/java/src/test/java/dev/braintrust/instrumentation/openai/travel-paris-france-poster.jpg") |
| 53 | + .getBase64Data(); |
| 54 | + |
| 55 | + // Create text content part |
| 56 | + ChatCompletionContentPartText textPart = |
| 57 | + ChatCompletionContentPartText.builder() |
| 58 | + .text("What city and landmark are shown in this travel poster? Describe the image.") |
| 59 | + .build(); |
| 60 | + ChatCompletionContentPart textContentPart = ChatCompletionContentPart.ofText(textPart); |
| 61 | + |
| 62 | + // Create image content part with base64-encoded image |
| 63 | + ChatCompletionContentPartImage imagePart = |
| 64 | + ChatCompletionContentPartImage.builder() |
| 65 | + .imageUrl( |
| 66 | + ChatCompletionContentPartImage.ImageUrl.builder() |
| 67 | + .url(imageDataUrl) |
| 68 | + .detail(ChatCompletionContentPartImage.ImageUrl.Detail.HIGH) |
| 69 | + .build()) |
| 70 | + .build(); |
| 71 | + ChatCompletionContentPart imageContentPart = |
| 72 | + ChatCompletionContentPart.ofImageUrl(imagePart); |
| 73 | + |
| 74 | + // Create user message with both text and image |
| 75 | + ChatCompletionUserMessageParam userMessage = |
| 76 | + ChatCompletionUserMessageParam.builder() |
| 77 | + .contentOfArrayOfContentParts( |
| 78 | + Arrays.asList(textContentPart, imageContentPart)) |
| 79 | + .build(); |
| 80 | + |
42 | 81 | var request = |
43 | 82 | ChatCompletionCreateParams.builder() |
44 | | - .model(ChatModel.GPT_4O_MINI) |
45 | | - .addSystemMessage("You are a helpful assistant") |
46 | | - .addUserMessage("What is the capital of France?") |
| 83 | + .model(ChatModel.GPT_4O) |
| 84 | + .addSystemMessage("You are a helpful assistant that can analyze images") |
| 85 | + .addMessage(userMessage) |
47 | 86 | .temperature(0.0) |
48 | 87 | .build(); |
49 | 88 | var response = openAIClient.chat().completions().create(request); |
|
0 commit comments