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