feat(ai-web): 完成流程任务的前后端拉通
This commit is contained in:
@@ -9,6 +9,7 @@ import com.lanyuanxiaoyao.service.ai.web.entity.FlowTask;
|
|||||||
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTaskTemplate;
|
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTaskTemplate;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.service.task.FlowTaskService;
|
import com.lanyuanxiaoyao.service.ai.web.service.task.FlowTaskService;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.service.task.FlowTaskTemplateService;
|
import com.lanyuanxiaoyao.service.ai.web.service.task.FlowTaskTemplateService;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
@@ -47,6 +48,12 @@ public class TaskController extends SimpleControllerSupport<FlowTask, TaskContro
|
|||||||
return AmisResponse.responseSuccess(mapper.readValue(task.getTemplateInputSchema(), Map.class));
|
return AmisResponse.responseSuccess(mapper.readValue(task.getTemplateInputSchema(), Map.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("execute/{id}")
|
||||||
|
public AmisResponse<?> execute(@PathVariable("id") Long id) throws JsonProcessingException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
|
flowTaskService.execute(id);
|
||||||
|
return AmisResponse.responseSuccess();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected SaveItemMapper<FlowTask, SaveItem> saveItemMapper() {
|
protected SaveItemMapper<FlowTask, SaveItem> saveItemMapper() {
|
||||||
return item -> {
|
return item -> {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.lanyuanxiaoyao.service.ai.web.engine;
|
package com.lanyuanxiaoyao.service.ai.web.engine;
|
||||||
|
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
|
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.engine.store.FlowStore;
|
import com.lanyuanxiaoyao.service.ai.web.engine.store.FlowStore;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
@@ -21,7 +22,11 @@ public class FlowExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void execute(FlowGraph graph) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
public void execute(FlowGraph graph) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
var runner = new FlowGraphRunner(graph, flowStore, runnerMap);
|
execute(graph, new FlowContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void execute(FlowGraph graph, FlowContext context) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
|
var runner = new FlowGraphRunner(graph, context, flowStore, runnerMap);
|
||||||
runner.run();
|
runner.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import org.eclipse.collections.api.multimap.set.ImmutableSetMultimap;
|
|||||||
*/
|
*/
|
||||||
public final class FlowGraphRunner {
|
public final class FlowGraphRunner {
|
||||||
private final FlowGraph flowGraph;
|
private final FlowGraph flowGraph;
|
||||||
|
private final FlowContext flowContext;
|
||||||
private final FlowStore flowStore;
|
private final FlowStore flowStore;
|
||||||
private final ImmutableMap<String, Class<? extends FlowNodeRunner>> nodeRunnerClass;
|
private final ImmutableMap<String, Class<? extends FlowNodeRunner>> nodeRunnerClass;
|
||||||
private final Queue<FlowNode> executionQueue = new LinkedList<>();
|
private final Queue<FlowNode> executionQueue = new LinkedList<>();
|
||||||
@@ -27,8 +28,9 @@ public final class FlowGraphRunner {
|
|||||||
private final ImmutableSetMultimap<String, FlowEdge> nodeOutputMap;
|
private final ImmutableSetMultimap<String, FlowEdge> nodeOutputMap;
|
||||||
private final ImmutableMap<String, FlowNode> nodeMap;
|
private final ImmutableMap<String, FlowNode> nodeMap;
|
||||||
|
|
||||||
public FlowGraphRunner(FlowGraph flowGraph, FlowStore flowStore, ImmutableMap<String, Class<? extends FlowNodeRunner>> nodeRunnerClass) {
|
public FlowGraphRunner(FlowGraph flowGraph, FlowContext flowContext, FlowStore flowStore, ImmutableMap<String, Class<? extends FlowNodeRunner>> nodeRunnerClass) {
|
||||||
this.flowGraph = flowGraph;
|
this.flowGraph = flowGraph;
|
||||||
|
this.flowContext = flowContext;
|
||||||
this.flowStore = flowStore;
|
this.flowStore = flowStore;
|
||||||
this.nodeRunnerClass = nodeRunnerClass;
|
this.nodeRunnerClass = nodeRunnerClass;
|
||||||
|
|
||||||
@@ -40,13 +42,12 @@ public final class FlowGraphRunner {
|
|||||||
public void run() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
public void run() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
|
||||||
flowStore.init(flowGraph);
|
flowStore.init(flowGraph);
|
||||||
|
|
||||||
var context = new FlowContext();
|
|
||||||
for (FlowNode node : flowGraph.nodes()) {
|
for (FlowNode node : flowGraph.nodes()) {
|
||||||
executionQueue.offer(node);
|
executionQueue.offer(node);
|
||||||
}
|
}
|
||||||
while (!executionQueue.isEmpty()) {
|
while (!executionQueue.isEmpty()) {
|
||||||
var node = executionQueue.poll();
|
var node = executionQueue.poll();
|
||||||
process(node, context);
|
process(node, flowContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.ai.web.engine.node;
|
||||||
|
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @version 20250711
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class LlmNode extends FlowNodeRunner {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
log.info("LLM Node {}", (String)getData("systemPrompt"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.ai.web.engine.node;
|
||||||
|
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.FlowNodeRunner;
|
||||||
|
import java.util.Map;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @version 20250711
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class OutputNode extends FlowNodeRunner {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
log.info("Output Node {}", (Map) getData("inputs"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,60 @@
|
|||||||
package com.lanyuanxiaoyao.service.ai.web.service.task;
|
package com.lanyuanxiaoyao.service.ai.web.service.task;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
|
import com.lanyuanxiaoyao.service.ai.web.base.service.SimpleServiceSupport;
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.FlowExecutor;
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowContext;
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowEdge;
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowGraph;
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.entity.FlowNode;
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.node.LlmNode;
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.node.OutputNode;
|
||||||
|
import com.lanyuanxiaoyao.service.ai.web.engine.store.InMemoryFlowStore;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTask;
|
import com.lanyuanxiaoyao.service.ai.web.entity.FlowTask;
|
||||||
import com.lanyuanxiaoyao.service.ai.web.repository.FlowTaskRepository;
|
import com.lanyuanxiaoyao.service.ai.web.repository.FlowTaskRepository;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.eclipse.collections.api.factory.Maps;
|
||||||
|
import org.eclipse.collections.api.map.MutableMap;
|
||||||
|
import org.eclipse.collections.api.set.ImmutableSet;
|
||||||
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class FlowTaskService extends SimpleServiceSupport<FlowTask> {
|
public class FlowTaskService extends SimpleServiceSupport<FlowTask> {
|
||||||
public FlowTaskService(FlowTaskRepository flowTaskRepository) {
|
private final ObjectMapper mapper;
|
||||||
|
|
||||||
|
public FlowTaskService(FlowTaskRepository flowTaskRepository, Jackson2ObjectMapperBuilder builder) {
|
||||||
super(flowTaskRepository);
|
super(flowTaskRepository);
|
||||||
|
this.mapper = builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void execute(Long id) throws JsonProcessingException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
|
||||||
|
var flowTask = detailOrThrow(id);
|
||||||
|
var graphVo = mapper.readValue(flowTask.getTemplateFlowGraph(), FlowGraphVo.class);
|
||||||
|
var flowGraph = new FlowGraph(IdUtil.fastUUID(), graphVo.getNodes(), graphVo.getEdges());
|
||||||
|
|
||||||
|
var store = new InMemoryFlowStore();
|
||||||
|
var executor = new FlowExecutor(
|
||||||
|
store,
|
||||||
|
Maps.immutable.of(
|
||||||
|
"output-node", OutputNode.class,
|
||||||
|
"llm-node", LlmNode.class
|
||||||
|
)
|
||||||
|
);
|
||||||
|
FlowContext context = new FlowContext();
|
||||||
|
context.setData(graphVo.getData());
|
||||||
|
executor.execute(flowGraph, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static final class FlowGraphVo {
|
||||||
|
private ImmutableSet<FlowNode> nodes;
|
||||||
|
private ImmutableSet<FlowEdge> edges;
|
||||||
|
private MutableMap<String, MutableMap<String, Object>> data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user