davsclaus commented on code in PR #27494:
URL: https://github.com/apache/camel/pull/27494#discussion_r4210388472
##########
components/camel-ai/camel-typesafe-ai/src/main/java/org/apache/camel/component/typesafeai/TypeSafeAiSemanticAdapter.java:
##########
@@ -94,36 +160,86 @@ public Map<String, SemanticResult>
evaluateBatch(Map<String, SemanticQuestion> q
return results;
}
- private Map<String, Object> definition(SemanticQuestion question) {
+ private Map<String, Object> definition(SemanticEvaluation question) {
validate(question);
Map<String, Object> definition = new HashMap<>();
- definition.put("instructions", question.getInstructions());
+ definition.put("instructions",
question.getParameters().get("instructions"));
definition.put("type", type(question));
- if (question.getType() == SemanticQuestion.Type.SCORE) {
- definition.put("criteria", question.getLevels());
- } else if (!question.getCriteria().isEmpty()) {
- definition.put("criteria", question.getCriteria());
+ if ("score".equals(question.getOperation())) {
+ definition.put("criteria", levels(question));
+ } else if (!criteria(question).isEmpty()) {
+ definition.put("criteria", criteria(question));
}
return definition;
}
- private String type(SemanticQuestion question) {
- return switch (question.getType()) {
- case BOOLEAN -> "noul";
- case CHOICE -> "choice";
- case SCORE -> "score";
+ private String type(SemanticEvaluation question) {
+ return switch (question.getOperation()) {
+ case "boolean" -> "noul";
+ case "choice" -> "choice";
+ case "score" -> "score";
+ default -> throw new IllegalArgumentException("Unsupported
TypeSafe AI operation");
};
}
- private SemanticResult result(SemanticQuestion question, JsonObject
answer, JsonObject response) {
+ private SemanticResult result(SemanticEvaluation question, JsonObject
answer, JsonObject response) {
Map<String, Double> probabilities = new HashMap<>();
if (answer.get("probabilities") instanceof Map<?, ?> values) {
values.forEach((key, value) -> probabilities.put((String) key,
((Number) value).doubleValue()));
}
- return new SemanticResult(
- question.getType() == SemanticQuestion.Type.BOOLEAN ? null :
answer.get(type(question)),
- question.getType() == SemanticQuestion.Type.BOOLEAN ?
answer.getDouble("noul") : null,
+ Double probability = "boolean".equals(question.getOperation()) ?
answer.getDouble("noul") : null;
+ Object value = switch (question.getOperation()) {
+ case "boolean" -> booleanDecision(question, probability);
Review Comment:
Still open from my earlier review: when the response has no `noul`,
`probability` is null and unboxing it into `booleanDecision(..., double)`
throws an NPE with no message. Something like this would give a clear error:
```java
Double probability = null;
if ("boolean".equals(question.getOperation())) {
probability = answer.getDouble("noul");
if (probability == null) {
throw new IllegalArgumentException("TypeSafe AI boolean result is
missing its probability");
}
}
```
##########
core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimplePredicateExpression.java:
##########
@@ -0,0 +1,32 @@
+/*
+ * 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.language.simple;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+
+/**
+ * Opts a Simple function into using its predicate contract for negation,
including any validation in
+ * {@link #matches(org.apache.camel.Exchange)}. Existing expressions retain
value-based truthiness, even when they also
+ * implement {@link Predicate}.
+ */
+public interface SimplePredicateExpression extends Expression, Predicate {
Review Comment:
Nice, this opt-in keeps existing negation semantics intact. Nit: since this
is a new public interface in core, could the javadoc get a `@since 4.23` tag?
--
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]