gnodet-bot commented on code in PR #30326: URL: https://github.com/apache/camel/pull/30326#discussion_r4634315180
########## components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIResponsesGenAiMessages.java: ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openai; + +import java.util.List; +import java.util.Locale; +import java.util.stream.Collectors; + +import com.openai.models.responses.EasyInputMessage; +import com.openai.models.responses.Response; +import com.openai.models.responses.ResponseCreateParams; +import com.openai.models.responses.ResponseFunctionToolCall; +import com.openai.models.responses.ResponseInputContent; +import com.openai.models.responses.ResponseInputItem; +import com.openai.models.responses.ResponseOutputItem; +import com.openai.models.responses.ResponseOutputMessage; +import com.openai.models.responses.Tool; +import org.apache.camel.component.ai.observability.GenAiMessages; +import org.apache.camel.component.ai.observability.GenAiToolDefinitions; + +/** + * Converts Responses API requests and responses into the messages of the GenAI semantic conventions, for observability + * level {@code full}. + */ +final class OpenAIResponsesGenAiMessages { + + private OpenAIResponsesGenAiMessages() { + } + + static GenAiMessages input(ResponseCreateParams params) { + GenAiMessages messages = new GenAiMessages(); + params.input().ifPresent(input -> { + if (input.isText()) { + messages.text(GenAiMessages.USER, input.asText()); + } else if (input.isResponse()) { + for (ResponseInputItem item : input.asResponse()) { + addInputItem(messages, item); + } + } + }); + return messages; + } + + static GenAiMessages output(Response response, String finishReason) { + GenAiMessages messages = new GenAiMessages(); + for (ResponseOutputItem item : response.output()) { + if (item.isMessage()) { + String text = item.asMessage().content().stream() + .filter(ResponseOutputMessage.Content::isOutputText) + .map(c -> c.asOutputText().text()) + .collect(Collectors.joining("\n")); + messages.text(GenAiMessages.ASSISTANT, text.isEmpty() ? null : text); + } else if (item.isFunctionCall()) { + ResponseFunctionToolCall call = item.asFunctionCall(); + messages.toolCall(call.callId(), call.name(), call.arguments()); + } + } + return messages.finishReason(finishReason); + } + + static GenAiToolDefinitions toolDefinitions(ResponseCreateParams params) { + GenAiToolDefinitions definitions = new GenAiToolDefinitions(); + for (Tool tool : params.tools().orElse(List.of())) { + tool.function().ifPresent(function -> definitions.function( + function.name(), + function.description().orElse(null), + null)); + } Review Comment: π‘ `toolDefinitions` passes `null` for the parameters argument. The chat-completion adapter (`OpenAIGenAiMessages.toolDefinitions`) converts `definition.parameters()` to a `Map` β this gives tracing backends the JSON schema of each tool. Passing `null` loses that information. If `Tool.Function` exposes a `parameters()` method in the SDK, it would be worth populating it analogously to the chat path. ########## components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIResponsesProducer.java: ########## @@ -392,22 +435,51 @@ private void processToolLoop( } } - private Response createResponse(Exchange exchange, String model, ResponseCreateParams params) throws Exception { - return observedCall(exchange, model, () -> getEndpoint().getClient().responses().create(params)); + private Response createResponse(Exchange exchange, String model, ResponseCreateParams params, String conversationId) + throws Exception { + return observedCall(exchange, model, params, conversationId, + () -> getEndpoint().getClient().responses().create(params)); } - private Response observedCall(Exchange exchange, String model, ThrowingSupplier<Response, Exception> call) + private Response observedCall( + Exchange exchange, String model, ResponseCreateParams params, String conversationId, + ThrowingSupplier<Response, Exception> call) throws Exception { GenAiObservationContext observationContext = GenAiObservationContext.builder() .operationName(GenAiOperationName.CHAT) .system("openai") .requestModel(model) .componentScheme("openai") + .conversationId(conversationId) + .requestParameter("temperature", params.temperature().orElse(null)) + .requestParameter("top_p", params.topP().orElse(null)) + .requestParameter("max_tokens", params.maxOutputTokens().orElse(null)) + .tracedByFramework(OpenAIClientTracing.tracedByOpenTelemetry(exchange.getContext(), getEndpoint().getClient())) .build(); Review Comment: β οΈ **`tracedByFramework` with `responses()` β invisible traces when OTel agent is attached.** The OTel Java agent (2.33.x) instruments `chat/completions` and `embeddings` but does **not** instrument `responses()`. When the agent is attached, `OpenAIClientTracing.tracedByOpenTelemetry(...)` returns `true` (the client proxy wraps all services generically), so `GenAiObservability` creates a NOOP span for Responses calls β Camel records nothing, and OTel records nothing either. The call becomes invisible in traces. Until the OTel Java agent ships Responses API instrumentation, omit `tracedByFramework` here so Camel always records its own span: ```suggestion // OTel Java agent 2.33.x does not instrument responses() yet β omit tracedByFramework // so Camel always records its own span rather than producing a NOOP. ``` ########## components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIResponsesGenAiMessages.java: ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openai; + +import java.util.List; +import java.util.Locale; +import java.util.stream.Collectors; + +import com.openai.models.responses.EasyInputMessage; +import com.openai.models.responses.Response; +import com.openai.models.responses.ResponseCreateParams; +import com.openai.models.responses.ResponseFunctionToolCall; +import com.openai.models.responses.ResponseInputContent; +import com.openai.models.responses.ResponseInputItem; +import com.openai.models.responses.ResponseOutputItem; +import com.openai.models.responses.ResponseOutputMessage; +import com.openai.models.responses.Tool; +import org.apache.camel.component.ai.observability.GenAiMessages; +import org.apache.camel.component.ai.observability.GenAiToolDefinitions; + +/** + * Converts Responses API requests and responses into the messages of the GenAI semantic conventions, for observability + * level {@code full}. + */ +final class OpenAIResponsesGenAiMessages { + + private OpenAIResponsesGenAiMessages() { + } + + static GenAiMessages input(ResponseCreateParams params) { + GenAiMessages messages = new GenAiMessages(); + params.input().ifPresent(input -> { + if (input.isText()) { + messages.text(GenAiMessages.USER, input.asText()); + } else if (input.isResponse()) { + for (ResponseInputItem item : input.asResponse()) { + addInputItem(messages, item); + } + } + }); + return messages; + } + + static GenAiMessages output(Response response, String finishReason) { + GenAiMessages messages = new GenAiMessages(); + for (ResponseOutputItem item : response.output()) { + if (item.isMessage()) { + String text = item.asMessage().content().stream() + .filter(ResponseOutputMessage.Content::isOutputText) + .map(c -> c.asOutputText().text()) + .collect(Collectors.joining("\n")); + messages.text(GenAiMessages.ASSISTANT, text.isEmpty() ? null : text); + } else if (item.isFunctionCall()) { + ResponseFunctionToolCall call = item.asFunctionCall(); + messages.toolCall(call.callId(), call.name(), call.arguments()); + } + } + return messages.finishReason(finishReason); + } + + static GenAiToolDefinitions toolDefinitions(ResponseCreateParams params) { + GenAiToolDefinitions definitions = new GenAiToolDefinitions(); + for (Tool tool : params.tools().orElse(List.of())) { + tool.function().ifPresent(function -> definitions.function( + function.name(), + function.description().orElse(null), + null)); + } + return definitions; + } + + private static void addInputItem(GenAiMessages messages, ResponseInputItem item) { + if (item.isEasyInputMessage()) { + EasyInputMessage msg = item.asEasyInputMessage(); + String role = roleOf(msg.role().asString()); + EasyInputMessage.Content content = msg.content(); + if (content.isTextInput()) { + messages.text(role, content.asTextInput()); + } else if (content.isResponseInputMessageContentList()) { + String text = content.asResponseInputMessageContentList().stream() + .filter(ResponseInputContent::isInputText) + .map(c -> c.asInputText().text()) + .collect(Collectors.joining("\n")); + messages.text(role, text.isEmpty() ? null : text); + } + } else if (item.isFunctionCallOutput()) { + ResponseInputItem.FunctionCallOutput output = item.asFunctionCallOutput(); Review Comment: π‘ `addInputItem` only handles `EasyInputMessage`, `FunctionCallOutput`, and `FunctionCall`. Input items built by the producer itself β `ResponseInputItem.ofMessage(...)` for image/PDF prompts (`OpenAIResponsesInputBuilder`) and `ResponseInputItem.ofResponseOutputMessage(...)` for assistant preambles (`OpenAIResponsesSupport.toInputItems`) β fall through silently and their text is not recorded. This keeps the two paths inconsistent with the chat-completion adapter, which records `[image]` placeholders and refusals. ########## components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIResponsesGenAiMessages.java: ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openai; + +import java.util.List; +import java.util.Locale; +import java.util.stream.Collectors; + +import com.openai.models.responses.EasyInputMessage; +import com.openai.models.responses.Response; +import com.openai.models.responses.ResponseCreateParams; +import com.openai.models.responses.ResponseFunctionToolCall; +import com.openai.models.responses.ResponseInputContent; +import com.openai.models.responses.ResponseInputItem; +import com.openai.models.responses.ResponseOutputItem; +import com.openai.models.responses.ResponseOutputMessage; +import com.openai.models.responses.Tool; +import org.apache.camel.component.ai.observability.GenAiMessages; +import org.apache.camel.component.ai.observability.GenAiToolDefinitions; + +/** + * Converts Responses API requests and responses into the messages of the GenAI semantic conventions, for observability + * level {@code full}. + */ +final class OpenAIResponsesGenAiMessages { + + private OpenAIResponsesGenAiMessages() { + } + + static GenAiMessages input(ResponseCreateParams params) { + GenAiMessages messages = new GenAiMessages(); + params.input().ifPresent(input -> { + if (input.isText()) { + messages.text(GenAiMessages.USER, input.asText()); + } else if (input.isResponse()) { + for (ResponseInputItem item : input.asResponse()) { + addInputItem(messages, item); + } + } + }); + return messages; + } + + static GenAiMessages output(Response response, String finishReason) { + GenAiMessages messages = new GenAiMessages(); + for (ResponseOutputItem item : response.output()) { + if (item.isMessage()) { + String text = item.asMessage().content().stream() + .filter(ResponseOutputMessage.Content::isOutputText) + .map(c -> c.asOutputText().text()) + .collect(Collectors.joining("\n")); + messages.text(GenAiMessages.ASSISTANT, text.isEmpty() ? null : text); + } else if (item.isFunctionCall()) { + ResponseFunctionToolCall call = item.asFunctionCall(); + messages.toolCall(call.callId(), call.name(), call.arguments()); + } + } + return messages.finishReason(finishReason); + } + + static GenAiToolDefinitions toolDefinitions(ResponseCreateParams params) { + GenAiToolDefinitions definitions = new GenAiToolDefinitions(); + for (Tool tool : params.tools().orElse(List.of())) { + tool.function().ifPresent(function -> definitions.function( + function.name(), + function.description().orElse(null), + null)); + } + return definitions; + } + + private static void addInputItem(GenAiMessages messages, ResponseInputItem item) { + if (item.isEasyInputMessage()) { + EasyInputMessage msg = item.asEasyInputMessage(); + String role = roleOf(msg.role().asString()); + EasyInputMessage.Content content = msg.content(); + if (content.isTextInput()) { + messages.text(role, content.asTextInput()); + } else if (content.isResponseInputMessageContentList()) { + String text = content.asResponseInputMessageContentList().stream() + .filter(ResponseInputContent::isInputText) + .map(c -> c.asInputText().text()) + .collect(Collectors.joining("\n")); + messages.text(role, text.isEmpty() ? null : text); + } + } else if (item.isFunctionCallOutput()) { + ResponseInputItem.FunctionCallOutput output = item.asFunctionCallOutput(); + String callId = output.callId().orElse(null); + String content = output.output().string().orElse(null); + messages.toolResult(callId, content); + } else if (item.isFunctionCall()) { + ResponseFunctionToolCall call = item.asFunctionCall(); Review Comment: β οΈ **`toolCall()` appends to the last message β wrong role for multi-turn input.** When `addInputItem` processes a `isFunctionCall()` item in a multi-turn conversation, `messages.toolCall(...)` appends the tool call data to whichever message was most recently added. In the second+ tool-loop iteration the input sequence is `[β¦user message, function_call_output (tool role), function_call, β¦]`, so the `function_call` part lands on the tool-result message rather than on an assistant-role message. The GenAI semantic conventions require `tool_call` entries in an **assistant** message. Consider inserting an explicit `messages.text(ASSISTANT, null)` before the first `toolCall()` call when the previous item is not already an assistant message. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
