AI rule node: move system prompt to rule node config
This commit is contained in:
parent
9ef4295d90
commit
c0480ed930
@ -53,27 +53,20 @@ import static org.thingsboard.server.dao.service.ConstraintValidator.validateFie
|
|||||||
)
|
)
|
||||||
public final class TbAiNode extends TbAbstractExternalNode implements TbNode {
|
public final class TbAiNode extends TbAbstractExternalNode implements TbNode {
|
||||||
|
|
||||||
private static final SystemMessage SYSTEM_MESSAGE = SystemMessage.from("""
|
private SystemMessage systemMessage;
|
||||||
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 PromptTemplate userPromptTemplate;
|
private PromptTemplate userPromptTemplate;
|
||||||
private ChatModel chatModel;
|
private ChatModel chatModel;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
|
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: ";
|
String errorPrefix = "'" + ctx.getSelf().getName() + "' node configuration is invalid: ";
|
||||||
try {
|
try {
|
||||||
validateFields(config, errorPrefix);
|
validateFields(config, errorPrefix);
|
||||||
} catch (DataValidationException e) {
|
} catch (DataValidationException e) {
|
||||||
throw new TbNodeException(e, true);
|
throw new TbNodeException(e, true);
|
||||||
}
|
}
|
||||||
|
systemMessage = SystemMessage.from(config.getSystemPrompt());
|
||||||
userPromptTemplate = PromptTemplate.from("""
|
userPromptTemplate = PromptTemplate.from("""
|
||||||
User-provided task or question: %s
|
User-provided task or question: %s
|
||||||
Rule engine message payload: {{msgPayload}}
|
Rule engine message payload: {{msgPayload}}
|
||||||
@ -96,7 +89,7 @@ public final class TbAiNode extends TbAbstractExternalNode implements TbNode {
|
|||||||
UserMessage userMessage = userPromptTemplate.apply(variables).toUserMessage();
|
UserMessage userMessage = userPromptTemplate.apply(variables).toUserMessage();
|
||||||
|
|
||||||
var chatRequest = ChatRequest.builder()
|
var chatRequest = ChatRequest.builder()
|
||||||
.messages(List.of(SYSTEM_MESSAGE, userMessage))
|
.messages(List.of(systemMessage, userMessage))
|
||||||
.responseFormat(ResponseFormat.JSON)
|
.responseFormat(ResponseFormat.JSON)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@ -121,7 +114,7 @@ public final class TbAiNode extends TbAbstractExternalNode implements TbNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroy() {
|
public void destroy() {
|
||||||
config = null;
|
systemMessage = null;
|
||||||
userPromptTemplate = null;
|
userPromptTemplate = null;
|
||||||
chatModel = null;
|
chatModel = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,6 +28,10 @@ public class TbAiNodeConfiguration implements NodeConfiguration<TbAiNodeConfigur
|
|||||||
@NotNull
|
@NotNull
|
||||||
private AiSettingsId aiSettingsId;
|
private AiSettingsId aiSettingsId;
|
||||||
|
|
||||||
|
@NotBlank
|
||||||
|
@Length(min = 1, max = 1000)
|
||||||
|
private String systemPrompt;
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
||||||
@Length(min = 1, max = 1000)
|
@Length(min = 1, max = 1000)
|
||||||
private String userPrompt;
|
private String userPrompt;
|
||||||
@ -35,6 +39,12 @@ public class TbAiNodeConfiguration implements NodeConfiguration<TbAiNodeConfigur
|
|||||||
@Override
|
@Override
|
||||||
public TbAiNodeConfiguration defaultConfiguration() {
|
public TbAiNodeConfiguration defaultConfiguration() {
|
||||||
var configuration = new TbAiNodeConfiguration();
|
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");
|
configuration.setUserPrompt("Tell me a joke");
|
||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user