AI rule node: move system prompt to rule node config

This commit is contained in:
Dmytro Skarzhynets 2025-05-15 18:50:00 +03:00
parent 9ef4295d90
commit c0480ed930
No known key found for this signature in database
GPG Key ID: 2B51652F224037DF
2 changed files with 15 additions and 12 deletions

View File

@ -53,27 +53,20 @@ import static org.thingsboard.server.dao.service.ConstraintValidator.validateFie
)
public final class TbAiNode extends TbAbstractExternalNode implements TbNode {
private static final SystemMessage SYSTEM_MESSAGE = SystemMessage.from("""
Take a deep breath and work on this step by step.
You are an industry-leading IoT domain expert with deep experience in telemetry data analysis.
Your task is to complete the user-provided task or answer a question.
You may use additional context information called "Rule engine message payload", "Rule engine message metadata" and "Rule engine message type".
Your response must be in JSON format.""");
private TbAiNodeConfiguration config;
private SystemMessage systemMessage;
private PromptTemplate userPromptTemplate;
private ChatModel chatModel;
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
config = TbNodeUtils.convert(configuration, TbAiNodeConfiguration.class);
var config = TbNodeUtils.convert(configuration, TbAiNodeConfiguration.class);
String errorPrefix = "'" + ctx.getSelf().getName() + "' node configuration is invalid: ";
try {
validateFields(config, errorPrefix);
} catch (DataValidationException e) {
throw new TbNodeException(e, true);
}
systemMessage = SystemMessage.from(config.getSystemPrompt());
userPromptTemplate = PromptTemplate.from("""
User-provided task or question: %s
Rule engine message payload: {{msgPayload}}
@ -96,7 +89,7 @@ public final class TbAiNode extends TbAbstractExternalNode implements TbNode {
UserMessage userMessage = userPromptTemplate.apply(variables).toUserMessage();
var chatRequest = ChatRequest.builder()
.messages(List.of(SYSTEM_MESSAGE, userMessage))
.messages(List.of(systemMessage, userMessage))
.responseFormat(ResponseFormat.JSON)
.build();
@ -121,7 +114,7 @@ public final class TbAiNode extends TbAbstractExternalNode implements TbNode {
@Override
public void destroy() {
config = null;
systemMessage = null;
userPromptTemplate = null;
chatModel = null;
}

View File

@ -28,6 +28,10 @@ public class TbAiNodeConfiguration implements NodeConfiguration<TbAiNodeConfigur
@NotNull
private AiSettingsId aiSettingsId;
@NotBlank
@Length(min = 1, max = 1000)
private String systemPrompt;
@NotBlank
@Length(min = 1, max = 1000)
private String userPrompt;
@ -35,6 +39,12 @@ public class TbAiNodeConfiguration implements NodeConfiguration<TbAiNodeConfigur
@Override
public TbAiNodeConfiguration defaultConfiguration() {
var configuration = new TbAiNodeConfiguration();
configuration.setSystemPrompt("""
Take a deep breath and work on this step by step.
You are an industry-leading IoT domain expert with deep experience in telemetry data analysis.
Your task is to complete the user-provided task or answer a question.
You may use additional context information called "Rule engine message payload", "Rule engine message metadata" and "Rule engine message type".
Your response must be in JSON format.""");
configuration.setUserPrompt("Tell me a joke");
return configuration;
}