Skip to content

Commit

Permalink
deps: updates openai to latest and adjusts example (#514)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
codefromthecrypt authored Jan 23, 2025
1 parent fef0621 commit 7778b95
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/openai/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
7 changes: 3 additions & 4 deletions examples/openai/src/main/java/openai/example/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private static LogRecordBuilder newEvent(String name) {
private static Value<?> buildToolCallEventObject(ChatCompletionMessageToolCall call) {
Map<String, Value<?>> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -2048,7 +2048,6 @@ void toolsWithFollowupAndCaptureContent() {
ChatCompletionMessageParam assistantMessage =
ChatCompletionMessageParam.ofChatCompletionAssistantMessageParam(
ChatCompletionAssistantMessageParam.builder()
.role(ChatCompletionAssistantMessageParam.Role.ASSISTANT)
.content(ChatCompletionAssistantMessageParam.Content.ofTextContent(""))
.toolCalls(toolCalls)
.build());
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -2349,31 +2346,27 @@ static ChatCompletionTool buildGetDeliveryDateToolDefinition() {
private static ChatCompletionMessageParam createAssistantMessage(String content) {
return ChatCompletionMessageParam.ofChatCompletionAssistantMessageParam(
ChatCompletionAssistantMessageParam.builder()
.role(ChatCompletionAssistantMessageParam.Role.ASSISTANT)
.content(ChatCompletionAssistantMessageParam.Content.ofTextContent(content))
.build());
}

private static ChatCompletionMessageParam createUserMessage(String content) {
return ChatCompletionMessageParam.ofChatCompletionUserMessageParam(
ChatCompletionUserMessageParam.builder()
.role(ChatCompletionUserMessageParam.Role.USER)
.content(ChatCompletionUserMessageParam.Content.ofTextContent(content))
.build());
}

private static ChatCompletionMessageParam createSystemMessage(String content) {
return ChatCompletionMessageParam.ofChatCompletionSystemMessageParam(
ChatCompletionSystemMessageParam.builder()
.role(ChatCompletionSystemMessageParam.Role.SYSTEM)
.content(ChatCompletionSystemMessageParam.Content.ofTextContent(content))
.build());
}

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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,31 +578,27 @@ 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());
}

private static ChatCompletionMessageParam createUserMessage(String content) {
return ChatCompletionMessageParam.ofChatCompletionUserMessageParam(
ChatCompletionUserMessageParam.builder()
.role(ChatCompletionUserMessageParam.Role.USER)
.content(ChatCompletionUserMessageParam.Content.ofTextContent(content))
.build());
}

private static ChatCompletionMessageParam createSystemMessage(String content) {
return ChatCompletionMessageParam.ofChatCompletionSystemMessageParam(
ChatCompletionSystemMessageParam.builder()
.role(ChatCompletionSystemMessageParam.Role.SYSTEM)
.content(ChatCompletionSystemMessageParam.Content.ofTextContent(content))
.build());
}

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());
Expand Down

0 comments on commit 7778b95

Please sign in to comment.