Browse Source
AI rule node: use `ObjectNode` instead of more general `JsonNode` for JSON Schema config field
pull/13371/head
Dmytro Skarzhynets
12 months ago
No known key found for this signature in database
GPG Key ID: 2B51652F224037DF
3 changed files with
9 additions and
7 deletions
-
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/ai/Langchain4jJsonSchemaAdapter.java
-
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/ai/TbAiNode.java
-
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/ai/TbAiNodeConfiguration.java
|
|
|
@ -16,6 +16,7 @@ |
|
|
|
package org.thingsboard.rule.engine.ai; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
import dev.langchain4j.model.chat.request.json.JsonArraySchema; |
|
|
|
import dev.langchain4j.model.chat.request.json.JsonBooleanSchema; |
|
|
|
import dev.langchain4j.model.chat.request.json.JsonEnumSchema; |
|
|
|
@ -42,10 +43,10 @@ final class Langchain4jJsonSchemaAdapter { |
|
|
|
/** |
|
|
|
* Creates a Langchain4j {@link JsonSchema} from the given root JSON Schema node. |
|
|
|
* |
|
|
|
* @param rootSchemaNode a valid JSON Schema as a Jackson {@link JsonNode} |
|
|
|
* @param rootSchemaNode a valid JSON Schema as a Jackson {@link ObjectNode} |
|
|
|
* @return the corresponding Langchain4j {@link JsonSchema} |
|
|
|
*/ |
|
|
|
public static JsonSchema fromJsonNode(JsonNode rootSchemaNode) { |
|
|
|
public static JsonSchema fromJsonNode(ObjectNode rootSchemaNode) { |
|
|
|
return JsonSchema.builder() |
|
|
|
.name(rootSchemaNode.get("title").textValue()) |
|
|
|
.rootElement(parse(rootSchemaNode)) |
|
|
|
|
|
|
|
@ -16,6 +16,7 @@ |
|
|
|
package org.thingsboard.rule.engine.ai; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
import com.google.common.util.concurrent.FutureCallback; |
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
import dev.langchain4j.data.message.SystemMessage; |
|
|
|
@ -97,11 +98,11 @@ public final class TbAiNode extends TbAbstractExternalNode implements TbNode { |
|
|
|
aiSettingsId = config.getAiSettingsId(); |
|
|
|
} |
|
|
|
|
|
|
|
private static JsonSchema getJsonSchema(ResponseFormatType responseFormatType, JsonNode jsonSchema) { |
|
|
|
private static JsonSchema getJsonSchema(ResponseFormatType responseFormatType, ObjectNode jsonSchema) { |
|
|
|
if (responseFormatType == ResponseFormatType.TEXT) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
return responseFormatType == ResponseFormatType.JSON && jsonSchema != null && !jsonSchema.isNull() ? Langchain4jJsonSchemaAdapter.fromJsonNode(jsonSchema) : null; |
|
|
|
return responseFormatType == ResponseFormatType.JSON && jsonSchema != null ? Langchain4jJsonSchemaAdapter.fromJsonNode(jsonSchema) : null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
@ -16,7 +16,7 @@ |
|
|
|
package org.thingsboard.rule.engine.ai; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore; |
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
import dev.langchain4j.model.chat.request.ResponseFormatType; |
|
|
|
import jakarta.validation.constraints.AssertTrue; |
|
|
|
import jakarta.validation.constraints.Max; |
|
|
|
@ -46,7 +46,7 @@ public class TbAiNodeConfiguration implements NodeConfiguration<TbAiNodeConfigur |
|
|
|
@NotNull |
|
|
|
private ResponseFormatType responseFormatType; |
|
|
|
|
|
|
|
private JsonNode jsonSchema; |
|
|
|
private ObjectNode jsonSchema; |
|
|
|
|
|
|
|
@Min(value = 1, message = "must be at least 1 second") |
|
|
|
@Max(value = 600, message = "cannot exceed 600 seconds (10 minutes)") |
|
|
|
@ -55,7 +55,7 @@ public class TbAiNodeConfiguration implements NodeConfiguration<TbAiNodeConfigur |
|
|
|
@JsonIgnore |
|
|
|
@AssertTrue(message = "provided JSON Schema must conform to the Draft 2020-12 meta-schema") |
|
|
|
public boolean isJsonSchemaValid() { |
|
|
|
return jsonSchema == null || jsonSchema.isNull() || JsonSchemaUtils.isValidJsonSchema(jsonSchema); |
|
|
|
return jsonSchema == null || JsonSchemaUtils.isValidJsonSchema(jsonSchema); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|