atiaomar1978-hub commented on code in PR #26106:
URL: https://github.com/apache/camel/pull/26106#discussion_r3938272480


##########
components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIEmbeddingsProducer.java:
##########
@@ -127,6 +151,10 @@ private void processInternal(Exchange exchange) throws 
Exception {
         calculateSimilarityIfRequested(exchange, embeddings);
     }
 
+    private static Integer toTokenCount(long tokens) {

Review Comment:
   Fixed in d1a3fc28 — removed `toTokenCount` and pass `usage.promptTokens()` 
directly to `GenAiUsage.of(Long, ...)`, matching 
`OpenAIProducer.createChatCompletion`.



##########
components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIResponsesProducer.java:
##########
@@ -136,28 +142,89 @@ private void processInternal(Exchange exchange) throws 
Exception {
 
         Class<?> responseClass = resolveOutputClass(in, outputClass);
         if (responseClass != null) {
-            processStructured(exchange, config, paramsBuilder, responseClass);
+            processStructured(exchange, config, paramsBuilder, responseClass, 
model);
             return;
         }
         if (ObjectHelper.isNotEmpty(jsonSchema)) {
             OpenAIResponsesSupport.applyJsonSchemaTextFormat(paramsBuilder, 
jsonSchema);
         }
 
         ResponseCreateParams params = paramsBuilder.build();
-        Response response = 
getEndpoint().getClient().responses().create(params);
+        Response response = createResponse(exchange, model, params);
         finishExchange(exchange, config, response, 
OpenAIResponsesSupport.extractAssistantText(response));
     }
 
     private void processStructured(
             Exchange exchange, OpenAIConfiguration config, 
ResponseCreateParams.Builder paramsBuilder,
-            Class<?> responseClass)
+            Class<?> responseClass, String model)
             throws Exception {
         StructuredResponseCreateParams<?> structuredParams = 
paramsBuilder.text(responseClass).build();
-        StructuredResponse<?> structured = 
getEndpoint().getClient().responses().create(structuredParams);
-        Response raw = structured.rawResponse();
+        Response raw = createStructuredResponse(exchange, model, 
structuredParams);
         finishExchange(exchange, config, raw, 
OpenAIResponsesSupport.extractAssistantText(raw));
     }
 
+    private Response createResponse(Exchange exchange, String model, 
ResponseCreateParams params) throws Exception {
+        GenAiObservationContext observationContext = 
GenAiObservationContext.builder()
+                .operationName(GenAiOperationName.CHAT)
+                .system("openai")
+                .requestModel(model)
+                .componentScheme("openai")
+                .build();
+        GenAiObservation observation = GenAiObservability.start(exchange, 
observationContext);
+        try {
+            Response response = 
getEndpoint().getClient().responses().create(params);
+            recordResponseSuccess(observation, response);
+            return response;
+        } catch (Exception e) {
+            GenAiErrorSupport.apply(exchange, e);
+            observation.recordError(e);
+            throw e;
+        } finally {
+            observation.close();
+        }
+    }
+
+    private Response createStructuredResponse(
+            Exchange exchange, String model, StructuredResponseCreateParams<?> 
structuredParams)
+            throws Exception {
+        GenAiObservationContext observationContext = 
GenAiObservationContext.builder()
+                .operationName(GenAiOperationName.CHAT)
+                .system("openai")
+                .requestModel(model)
+                .componentScheme("openai")
+                .build();
+        GenAiObservation observation = GenAiObservability.start(exchange, 
observationContext);
+        try {
+            StructuredResponse<?> structured = 
getEndpoint().getClient().responses().create(structuredParams);
+            Response raw = structured.rawResponse();
+            recordResponseSuccess(observation, raw);
+            return raw;
+        } catch (Exception e) {
+            GenAiErrorSupport.apply(exchange, e);
+            observation.recordError(e);
+            throw e;
+        } finally {
+            observation.close();
+        }
+    }
+
+    private static void recordResponseSuccess(GenAiObservation observation, 
Response response) {
+        String finishReason = 
OpenAIResponsesSupport.extractFinishStatus(response)
+                .map(OpenAIResponsesProducer::mapFinishReason)
+                .orElse(null);
+        response.usage().ifPresentOrElse(
+                usage -> observation.recordSuccess(GenAiUsage.of(
+                        toTokenCount(usage.inputTokens()),
+                        toTokenCount(usage.outputTokens()),
+                        finishReason,
+                        response.model().toString())),
+                () -> observation.recordSuccess(GenAiUsage.of(null, null, 
finishReason, response.model().toString())));
+    }

Review Comment:
   Fixed in d1a3fc28 — removed `toTokenCount`; `usage.inputTokens()` and 
`usage.outputTokens()` are passed directly as `long` values.



##########
components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIResponsesProducer.java:
##########
@@ -136,28 +142,89 @@ private void processInternal(Exchange exchange) throws 
Exception {
 
         Class<?> responseClass = resolveOutputClass(in, outputClass);
         if (responseClass != null) {
-            processStructured(exchange, config, paramsBuilder, responseClass);
+            processStructured(exchange, config, paramsBuilder, responseClass, 
model);
             return;
         }
         if (ObjectHelper.isNotEmpty(jsonSchema)) {
             OpenAIResponsesSupport.applyJsonSchemaTextFormat(paramsBuilder, 
jsonSchema);
         }
 
         ResponseCreateParams params = paramsBuilder.build();
-        Response response = 
getEndpoint().getClient().responses().create(params);
+        Response response = createResponse(exchange, model, params);
         finishExchange(exchange, config, response, 
OpenAIResponsesSupport.extractAssistantText(response));
     }
 
     private void processStructured(
             Exchange exchange, OpenAIConfiguration config, 
ResponseCreateParams.Builder paramsBuilder,
-            Class<?> responseClass)
+            Class<?> responseClass, String model)
             throws Exception {
         StructuredResponseCreateParams<?> structuredParams = 
paramsBuilder.text(responseClass).build();
-        StructuredResponse<?> structured = 
getEndpoint().getClient().responses().create(structuredParams);
-        Response raw = structured.rawResponse();
+        Response raw = createStructuredResponse(exchange, model, 
structuredParams);
         finishExchange(exchange, config, raw, 
OpenAIResponsesSupport.extractAssistantText(raw));
     }
 
+    private Response createResponse(Exchange exchange, String model, 
ResponseCreateParams params) throws Exception {
+        GenAiObservationContext observationContext = 
GenAiObservationContext.builder()
+                .operationName(GenAiOperationName.CHAT)
+                .system("openai")
+                .requestModel(model)
+                .componentScheme("openai")
+                .build();

Review Comment:
   Fixed in d1a3fc28 — extracted shared `observedCall(exchange, model, 
ThrowingSupplier<Response, Exception>)` helper used by both code paths.



##########
components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIModerationProducer.java:
##########
@@ -87,19 +93,32 @@ private void processInternal(Exchange exchange) throws 
Exception {
             paramsBuilder.inputOfStrings(inputs);
         }
 
-        ModerationCreateResponse response = getEndpoint().getClient()
-                .moderations().create(paramsBuilder.build());
+        GenAiObservationContext observationContext = 
GenAiObservationContext.builder()
+                .operationName(GenAiOperationName.MODERATION)
+                .system("openai")
+                .requestModel(model)
+                .componentScheme("openai")
+                .build();
+        GenAiObservation observation = GenAiObservability.start(exchange, 
observationContext);
+        ModerationCreateResponse response;
+        try {
+            response = 
getEndpoint().getClient().moderations().create(paramsBuilder.build());
+            observation.recordSuccess(GenAiUsage.of(null, null, null, 
response.model()));
+        } catch (Exception e) {
+            GenAiErrorSupport.apply(exchange, e);
+            observation.recordError(e);
+            throw e;
+        } finally {
+            observation.close();

Review Comment:
   Fixed in d1a3fc28 — restored both security comments (mismatch must fail the 
exchange; store full response only after validation).



-- 
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]

Reply via email to