From 7778b95df7ad99d5db2ae8050f2405e8159a784e Mon Sep 17 00:00:00 2001 From: Adrian Cole <64215+codefromthecrypt@users.noreply.github.com> Date: Thu, 23 Jan 2025 00:08:04 -0800 Subject: [PATCH] deps: updates openai to latest and adjusts example (#514) Signed-off-by: Adrian Cole --- examples/openai/build.gradle.kts | 2 +- .../openai/src/main/java/openai/example/Chat.java | 7 +++---- gradle/libs.versions.toml | 2 +- .../openai/wrappers/ChatCompletionEventsHelper.java | 2 +- .../wrappers/InstrumentedChatCompletionService.java | 2 +- .../test/java/co/elastic/otel/openai/ChatTest.java | 11 ++--------- .../otel/openai/LiveAPIChatIntegrationTest.java | 4 ---- 7 files changed, 9 insertions(+), 21 deletions(-) diff --git a/examples/openai/build.gradle.kts b/examples/openai/build.gradle.kts index 7082300d..e2154022 100644 --- a/examples/openai/build.gradle.kts +++ b/examples/openai/build.gradle.kts @@ -14,7 +14,7 @@ repositories { dependencies { implementation(platform("org.slf4j:slf4j-bom:2.0.16")) - implementation("com.openai:openai-java:0.11.8") + implementation("com.openai:openai-java:0.13.0") implementation("org.slf4j:slf4j-simple") } diff --git a/examples/openai/src/main/java/openai/example/Chat.java b/examples/openai/src/main/java/openai/example/Chat.java index 95cbe093..02837c0e 100644 --- a/examples/openai/src/main/java/openai/example/Chat.java +++ b/examples/openai/src/main/java/openai/example/Chat.java @@ -14,10 +14,9 @@ public static void main(String[] args) { String message = "Answer in up to 3 words: Which ocean contains Bouvet Island?"; ChatCompletionCreateParams params = ChatCompletionCreateParams.builder() - .addMessage(ChatCompletionMessageParam.ofChatCompletionUserMessageParam(ChatCompletionUserMessageParam.builder() - .role(ChatCompletionUserMessageParam.Role.USER) - .content(ChatCompletionUserMessageParam.Content.ofTextContent(message)) - .build())) + .addMessage(ChatCompletionUserMessageParam.builder() + .content(message) + .build()) .model(chatModel) .build(); diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 299dee4a..c1ffe4e6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -23,7 +23,7 @@ opentelemetryContribAlpha = "1.42.0-alpha" opentelemetrySemconvAlpha = "1.29.0-alpha" # instrumented libraries -openaiClient = "0.11.9" +openaiClient = "0.13.0" [libraries] diff --git a/instrumentation/openai-client-instrumentation/src/main/java/co/elastic/otel/openai/wrappers/ChatCompletionEventsHelper.java b/instrumentation/openai-client-instrumentation/src/main/java/co/elastic/otel/openai/wrappers/ChatCompletionEventsHelper.java index 38d65dba..015b736b 100644 --- a/instrumentation/openai-client-instrumentation/src/main/java/co/elastic/otel/openai/wrappers/ChatCompletionEventsHelper.java +++ b/instrumentation/openai-client-instrumentation/src/main/java/co/elastic/otel/openai/wrappers/ChatCompletionEventsHelper.java @@ -227,7 +227,7 @@ private static LogRecordBuilder newEvent(String name) { private static Value buildToolCallEventObject(ChatCompletionMessageToolCall call) { Map> result = new HashMap<>(); result.put("id", Value.of(call.id())); - result.put("type", Value.of(call.type().toString())); + result.put("type", Value.of(call._type().toString())); result.put("function", buildFunctionEventObject(call.function())); return Value.of(result); } diff --git a/instrumentation/openai-client-instrumentation/src/main/java/co/elastic/otel/openai/wrappers/InstrumentedChatCompletionService.java b/instrumentation/openai-client-instrumentation/src/main/java/co/elastic/otel/openai/wrappers/InstrumentedChatCompletionService.java index bb7b8dbf..412045d6 100644 --- a/instrumentation/openai-client-instrumentation/src/main/java/co/elastic/otel/openai/wrappers/InstrumentedChatCompletionService.java +++ b/instrumentation/openai-client-instrumentation/src/main/java/co/elastic/otel/openai/wrappers/InstrumentedChatCompletionService.java @@ -140,7 +140,7 @@ public void onStart( if (val.isResponseFormatText()) { attributes.put( GEN_AI_OPENAI_REQUEST_RESPONSE_FORMAT, - val.asResponseFormatText().type().toString()); + val.asResponseFormatText()._type().toString()); } }); } diff --git a/instrumentation/openai-client-instrumentation/src/test/java/co/elastic/otel/openai/ChatTest.java b/instrumentation/openai-client-instrumentation/src/test/java/co/elastic/otel/openai/ChatTest.java index 591e12f1..d035b987 100644 --- a/instrumentation/openai-client-instrumentation/src/test/java/co/elastic/otel/openai/ChatTest.java +++ b/instrumentation/openai-client-instrumentation/src/test/java/co/elastic/otel/openai/ChatTest.java @@ -255,7 +255,7 @@ void allTheClientOptions() { .topP(1.0) .stopOfStrings(Collections.singletonList("foo")) .seed(100L) - .responseFormat(ResponseFormatText.builder().type(ResponseFormatText.Type.TEXT).build()) + .responseFormat(ResponseFormatText.builder().build()) .build(); long startTimeNanos = System.nanoTime(); @@ -1374,7 +1374,7 @@ void streamAllTheClientOptions() throws Exception { .topP(1.0) .stopOfStrings(Collections.singletonList("foo")) .seed(100L) - .responseFormat(ResponseFormatText.builder().type(ResponseFormatText.Type.TEXT).build()) + .responseFormat(ResponseFormatText.builder().build()) .build(); long startTimeNanos = System.nanoTime(); @@ -2048,7 +2048,6 @@ void toolsWithFollowupAndCaptureContent() { ChatCompletionMessageParam assistantMessage = ChatCompletionMessageParam.ofChatCompletionAssistantMessageParam( ChatCompletionAssistantMessageParam.builder() - .role(ChatCompletionAssistantMessageParam.Role.ASSISTANT) .content(ChatCompletionAssistantMessageParam.Content.ofTextContent("")) .toolCalls(toolCalls) .build()); @@ -2301,7 +2300,6 @@ private static ChatCompletionTool buildGetWeatherToolDefinition() { properties.put("location", JsonObject.of(location)); return ChatCompletionTool.builder() - .type(ChatCompletionTool.Type.FUNCTION) .function( FunctionDefinition.builder() .name("get_weather") @@ -2326,7 +2324,6 @@ static ChatCompletionTool buildGetDeliveryDateToolDefinition() { properties.put("order_id", JsonObject.of(orderId)); return ChatCompletionTool.builder() - .type(ChatCompletionTool.Type.FUNCTION) .function( FunctionDefinition.builder() .name("get_delivery_date") @@ -2349,7 +2346,6 @@ static ChatCompletionTool buildGetDeliveryDateToolDefinition() { private static ChatCompletionMessageParam createAssistantMessage(String content) { return ChatCompletionMessageParam.ofChatCompletionAssistantMessageParam( ChatCompletionAssistantMessageParam.builder() - .role(ChatCompletionAssistantMessageParam.Role.ASSISTANT) .content(ChatCompletionAssistantMessageParam.Content.ofTextContent(content)) .build()); } @@ -2357,7 +2353,6 @@ private static ChatCompletionMessageParam createAssistantMessage(String content) private static ChatCompletionMessageParam createUserMessage(String content) { return ChatCompletionMessageParam.ofChatCompletionUserMessageParam( ChatCompletionUserMessageParam.builder() - .role(ChatCompletionUserMessageParam.Role.USER) .content(ChatCompletionUserMessageParam.Content.ofTextContent(content)) .build()); } @@ -2365,7 +2360,6 @@ private static ChatCompletionMessageParam createUserMessage(String content) { private static ChatCompletionMessageParam createSystemMessage(String content) { return ChatCompletionMessageParam.ofChatCompletionSystemMessageParam( ChatCompletionSystemMessageParam.builder() - .role(ChatCompletionSystemMessageParam.Role.SYSTEM) .content(ChatCompletionSystemMessageParam.Content.ofTextContent(content)) .build()); } @@ -2373,7 +2367,6 @@ private static ChatCompletionMessageParam createSystemMessage(String content) { private static ChatCompletionMessageParam createToolMessage(String response, String id) { return ChatCompletionMessageParam.ofChatCompletionToolMessageParam( ChatCompletionToolMessageParam.builder() - .role(ChatCompletionToolMessageParam.Role.TOOL) .toolCallId(id) .content(ChatCompletionToolMessageParam.Content.ofTextContent(response)) .build()); diff --git a/instrumentation/openai-client-instrumentation/src/test/java/co/elastic/otel/openai/LiveAPIChatIntegrationTest.java b/instrumentation/openai-client-instrumentation/src/test/java/co/elastic/otel/openai/LiveAPIChatIntegrationTest.java index fb45cd34..0367e7ce 100644 --- a/instrumentation/openai-client-instrumentation/src/test/java/co/elastic/otel/openai/LiveAPIChatIntegrationTest.java +++ b/instrumentation/openai-client-instrumentation/src/test/java/co/elastic/otel/openai/LiveAPIChatIntegrationTest.java @@ -578,7 +578,6 @@ void streamWithCaptureMessageContent() throws Exception { private static ChatCompletionMessageParam createAssistantMessage(String content) { return ChatCompletionMessageParam.ofChatCompletionAssistantMessageParam( ChatCompletionAssistantMessageParam.builder() - .role(ChatCompletionAssistantMessageParam.Role.ASSISTANT) .content(ChatCompletionAssistantMessageParam.Content.ofTextContent(content)) .build()); } @@ -586,7 +585,6 @@ private static ChatCompletionMessageParam createAssistantMessage(String content) private static ChatCompletionMessageParam createUserMessage(String content) { return ChatCompletionMessageParam.ofChatCompletionUserMessageParam( ChatCompletionUserMessageParam.builder() - .role(ChatCompletionUserMessageParam.Role.USER) .content(ChatCompletionUserMessageParam.Content.ofTextContent(content)) .build()); } @@ -594,7 +592,6 @@ private static ChatCompletionMessageParam createUserMessage(String content) { private static ChatCompletionMessageParam createSystemMessage(String content) { return ChatCompletionMessageParam.ofChatCompletionSystemMessageParam( ChatCompletionSystemMessageParam.builder() - .role(ChatCompletionSystemMessageParam.Role.SYSTEM) .content(ChatCompletionSystemMessageParam.Content.ofTextContent(content)) .build()); } @@ -602,7 +599,6 @@ private static ChatCompletionMessageParam createSystemMessage(String content) { private static ChatCompletionMessageParam createToolMessage(String response, String id) { return ChatCompletionMessageParam.ofChatCompletionToolMessageParam( ChatCompletionToolMessageParam.builder() - .role(ChatCompletionToolMessageParam.Role.TOOL) .toolCallId(id) .content(ChatCompletionToolMessageParam.Content.ofTextContent(response)) .build());