feat(ai-web): 实现入参解析
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
||||
import java.util.Map;
|
||||
import org.eclipse.collections.api.factory.Maps;
|
||||
import org.eclipse.collections.api.map.ImmutableMap;
|
||||
|
||||
/**
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250711
|
||||
*/
|
||||
public class FlowHelper {
|
||||
public static ImmutableMap<String, Object> generateInputVariablesMap(String nodeId, FlowContext context) {
|
||||
var variableMap = Maps.mutable.<String, Object>empty();
|
||||
var currentNodeData = context.get(nodeId);
|
||||
if (currentNodeData.containsKey("inputs")) {
|
||||
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);
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.service.ai.web.engine;
|
||||
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
||||
import lombok.Getter;
|
||||
import org.eclipse.collections.api.map.MutableMap;
|
||||
|
||||
public abstract class FlowNodeRunner {
|
||||
@Getter
|
||||
@@ -19,6 +20,10 @@ public abstract class FlowNodeRunner {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
protected MutableMap<String, Object> getData() {
|
||||
return context.get(nodeId);
|
||||
}
|
||||
|
||||
protected <T> T getData(String key) {
|
||||
var data = context.get(nodeId);
|
||||
return (T) data.get(key);
|
||||
|
||||
@@ -2,10 +2,12 @@ package com.lanyuanxiaoyao.service.ai.web.engine.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.eclipse.collections.api.factory.Maps;
|
||||
import org.eclipse.collections.api.map.ImmutableMap;
|
||||
import org.eclipse.collections.api.map.MutableMap;
|
||||
|
||||
@Data
|
||||
public class FlowContext {
|
||||
private ImmutableMap<String, Object> input = Maps.immutable.empty();
|
||||
private MutableMap<String, MutableMap<String, Object>> data = Maps.mutable.<String, MutableMap<String, Object>>empty().asSynchronized();
|
||||
|
||||
public MutableMap<String, Object> get(String key) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine.node;
|
||||
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.FlowHelper;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -11,6 +12,19 @@ import lombok.extern.slf4j.Slf4j;
|
||||
public class LlmNode extends FlowNodeRunner {
|
||||
@Override
|
||||
public void run() {
|
||||
log.info("LLM Node {}", (String)getData("systemPrompt"));
|
||||
var variableMap = FlowHelper.generateInputVariablesMap(getNodeId(), getContext());
|
||||
log.info("Variable map: {}", variableMap);
|
||||
setData("text", "llm");
|
||||
|
||||
/* var prompt = (String) getData("systemPrompt");
|
||||
if (StrUtil.isNotBlank(prompt)) {
|
||||
var builder = SpringBeanGetter.getBean("chat", ChatClient.Builder.class);
|
||||
var client = builder.build();
|
||||
var content = client.prompt()
|
||||
.user(prompt)
|
||||
.call()
|
||||
.content();
|
||||
setData("text", content);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.lanyuanxiaoyao.service.ai.web.engine.node;
|
||||
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.FlowHelper;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
|
||||
import java.util.Map;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
@@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
public class OutputNode extends FlowNodeRunner {
|
||||
@Override
|
||||
public void run() {
|
||||
log.info("Output Node {}", (Map) getData("inputs"));
|
||||
var variableMap = FlowHelper.generateInputVariablesMap(getNodeId(), getContext());
|
||||
log.info("Variable map: {}", variableMap);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.service.ai.web.service.task;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
|
||||
import com.lanyuanxiaoyao.service.ai.web.engine.FlowExecutor;
|
||||
@@ -47,6 +48,7 @@ public class FlowTaskService extends SimpleServiceSupport<FlowTask> {
|
||||
)
|
||||
);
|
||||
FlowContext context = new FlowContext();
|
||||
context.setInput(mapper.readValue(flowTask.getInput(), new TypeReference<>() {}));
|
||||
context.setData(graphVo.getData());
|
||||
executor.execute(flowGraph, context);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user