feat(ai-web): 增加模板渲染的处理
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
<curator.version>5.1.0</curator.version>
|
||||
<hutool.version>5.8.27</hutool.version>
|
||||
<mapstruct.version>1.6.3</mapstruct.version>
|
||||
<liteflow.version>2.13.2</liteflow.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -153,12 +154,27 @@
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-spring-boot-starter</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>${liteflow.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-el-builder</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>${liteflow.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-graaljs</artifactId>
|
||||
<version>${liteflow.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-python</artifactId>
|
||||
<version>${liteflow.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-lua</artifactId>
|
||||
<version>${liteflow.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.noear</groupId>
|
||||
|
||||
@@ -50,6 +50,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.blinkfox</groupId>
|
||||
<artifactId>fenix-spring-boot-starter</artifactId>
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.template.TemplateEngine;
|
||||
import cn.hutool.extra.template.TemplateUtil;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
||||
import java.util.Map;
|
||||
import org.eclipse.collections.api.factory.Maps;
|
||||
@@ -11,6 +14,8 @@ import org.eclipse.collections.api.map.ImmutableMap;
|
||||
* @version 20250711
|
||||
*/
|
||||
public class FlowHelper {
|
||||
private static final TemplateEngine TEMPLATE_ENGINE = TemplateUtil.createEngine();
|
||||
|
||||
public static ImmutableMap<String, Object> generateInputVariablesMap(String nodeId, FlowContext context) {
|
||||
var variableMap = Maps.mutable.<String, Object>empty();
|
||||
var currentNodeData = context.get(nodeId);
|
||||
@@ -18,27 +23,39 @@ public class FlowHelper {
|
||||
var inputsMap = (Map<String, Map<String, String>>) currentNodeData.get("inputs");
|
||||
for (String variableName : inputsMap.keySet()) {
|
||||
var expression = inputsMap.get(variableName).get("variable");
|
||||
if (StrUtil.contains(expression, ".")) {
|
||||
var splits = StrUtil.splitTrim(expression, ".", 2);
|
||||
var targetNodeId = splits.get(0);
|
||||
var targetVariableName = splits.get(1);
|
||||
if (!context.getData().containsKey(targetNodeId)) {
|
||||
throw new RuntimeException(StrUtil.format("Target node id not found: {}", targetNodeId));
|
||||
}
|
||||
var targetNodeData = context.getData().get(targetNodeId);
|
||||
if (!targetNodeData.containsKey(targetVariableName)) {
|
||||
throw new RuntimeException(StrUtil.format("Target node variable not found: {}.{}", targetNodeId, targetVariableName));
|
||||
}
|
||||
var targetVariable = targetNodeData.get(targetVariableName);
|
||||
var targetVariable = generateVariable(expression, context);
|
||||
if (ObjectUtil.isNotNull(targetVariable)) {
|
||||
variableMap.put(variableName, targetVariable);
|
||||
} else if (context.getInput().containsKey(expression)) {
|
||||
if (!context.getInput().containsKey(variableName)) {
|
||||
throw new RuntimeException(StrUtil.format("Target variable not found in input {}", variableName));
|
||||
}
|
||||
variableMap.put(variableName, context.getInput().get(variableName));
|
||||
}
|
||||
}
|
||||
}
|
||||
return variableMap.toImmutable();
|
||||
}
|
||||
|
||||
public static Object generateVariable(String expression, FlowContext context) {
|
||||
if (StrUtil.contains(expression, ".")) {
|
||||
var splits = StrUtil.splitTrim(expression, ".", 2);
|
||||
var targetNodeId = splits.get(0);
|
||||
var targetVariableName = splits.get(1);
|
||||
if (!context.getData().containsKey(targetNodeId)) {
|
||||
throw new RuntimeException(StrUtil.format("Target node id not found: {}", targetNodeId));
|
||||
}
|
||||
var targetNodeData = context.getData().get(targetNodeId);
|
||||
if (!targetNodeData.containsKey(targetVariableName)) {
|
||||
throw new RuntimeException(StrUtil.format("Target node variable not found: {}.{}", targetNodeId, targetVariableName));
|
||||
}
|
||||
return targetNodeData.get(targetVariableName);
|
||||
} else if (context.getInput().containsKey(expression)) {
|
||||
if (!context.getInput().containsKey(expression)) {
|
||||
throw new RuntimeException(StrUtil.format("Target variable not found in input {}", expression));
|
||||
}
|
||||
return context.getInput().get(expression);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String renderTemplateText(String templateText, Map<?, ?> data) {
|
||||
var template = TEMPLATE_ENGINE.getTemplate(templateText);
|
||||
return template.render(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine.node;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.lanyuanxiaoyao.service.ai.web.configuration.SpringBeanGetter;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.FlowHelper;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
@@ -14,10 +17,9 @@ public class LlmNode extends FlowNodeRunner {
|
||||
public void run() {
|
||||
var variableMap = FlowHelper.generateInputVariablesMap(getNodeId(), getContext());
|
||||
log.info("Variable map: {}", variableMap);
|
||||
setData("text", "llm");
|
||||
|
||||
/* var prompt = (String) getData("systemPrompt");
|
||||
if (StrUtil.isNotBlank(prompt)) {
|
||||
var sourcePrompt = (String) getData("systemPrompt");
|
||||
if (StrUtil.isNotBlank(sourcePrompt)) {
|
||||
var prompt = FlowHelper.renderTemplateText(sourcePrompt, variableMap.toMap());
|
||||
var builder = SpringBeanGetter.getBean("chat", ChatClient.Builder.class);
|
||||
var client = builder.build();
|
||||
var content = client.prompt()
|
||||
@@ -25,6 +27,6 @@ public class LlmNode extends FlowNodeRunner {
|
||||
.call()
|
||||
.content();
|
||||
setData("text", content);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
public class OutputNode extends FlowNodeRunner {
|
||||
@Override
|
||||
public void run() {
|
||||
var variableMap = FlowHelper.generateInputVariablesMap(getNodeId(), getContext());
|
||||
log.info("Variable map: {}", variableMap);
|
||||
String expression = getData("output");
|
||||
var targetVariable = FlowHelper.generateVariable(expression, getContext());
|
||||
log.info("Target: {}", targetVariable);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user