mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(action): remove legacy interventor module and move intervention entities into core action package
This commit is contained in:
@@ -5,8 +5,8 @@ import org.jetbrains.annotations.Nullable;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.Capability;
|
||||
import work.slhaf.partner.core.action.entity.*;
|
||||
import work.slhaf.partner.core.action.entity.cache.CacheAdjustData;
|
||||
import work.slhaf.partner.core.action.entity.intervention.MetaIntervention;
|
||||
import work.slhaf.partner.core.action.runner.RunnerClient;
|
||||
import work.slhaf.partner.module.modules.action.interventor.entity.MetaIntervention;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -12,12 +12,12 @@ import work.slhaf.partner.core.action.entity.*;
|
||||
import work.slhaf.partner.core.action.entity.cache.ActionCacheData;
|
||||
import work.slhaf.partner.core.action.entity.cache.CacheAdjustData;
|
||||
import work.slhaf.partner.core.action.entity.cache.CacheAdjustMetaData;
|
||||
import work.slhaf.partner.core.action.entity.intervention.InterventionType;
|
||||
import work.slhaf.partner.core.action.entity.intervention.MetaIntervention;
|
||||
import work.slhaf.partner.core.action.exception.ActionDataNotFoundException;
|
||||
import work.slhaf.partner.core.action.exception.MetaActionNotFoundException;
|
||||
import work.slhaf.partner.core.action.runner.RunnerClient;
|
||||
import work.slhaf.partner.core.action.runner.SandboxRunnerClient;
|
||||
import work.slhaf.partner.module.modules.action.interventor.entity.InterventionType;
|
||||
import work.slhaf.partner.module.modules.action.interventor.entity.MetaIntervention;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor.entity;
|
||||
package work.slhaf.partner.core.action.entity.intervention;
|
||||
|
||||
public enum InterventionType {
|
||||
/**
|
||||
@@ -1,4 +1,4 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor.entity;
|
||||
package work.slhaf.partner.core.action.entity.intervention;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -7,8 +7,8 @@ import work.slhaf.partner.api.agent.factory.component.annotation.AgentComponent;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.entity.ExecutableAction;
|
||||
import work.slhaf.partner.core.action.entity.MetaActionInfo;
|
||||
import work.slhaf.partner.module.modules.action.interventor.entity.InterventionType;
|
||||
import work.slhaf.partner.module.modules.action.interventor.entity.MetaIntervention;
|
||||
import work.slhaf.partner.core.action.entity.intervention.InterventionType;
|
||||
import work.slhaf.partner.core.action.entity.intervention.MetaIntervention;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package work.slhaf.partner.module.modules.action.executor.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.module.modules.action.interventor.entity.MetaIntervention;
|
||||
import work.slhaf.partner.core.action.entity.intervention.MetaIntervention;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1,222 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.val;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.InjectModule;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
import work.slhaf.partner.core.action.entity.ExecutableAction;
|
||||
import work.slhaf.partner.core.action.entity.PhaserRecord;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.memory.MemoryCapability;
|
||||
import work.slhaf.partner.module.modules.action.interventor.entity.InterventionType;
|
||||
import work.slhaf.partner.module.modules.action.interventor.entity.MetaIntervention;
|
||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.InterventionEvaluator;
|
||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorInput;
|
||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorResult;
|
||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorResult.EvaluatedInterventionData;
|
||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.InterventionRecognizer;
|
||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerInput;
|
||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerResult;
|
||||
import work.slhaf.partner.module.modules.memory.runtime.MemoryRuntime;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* 负责识别潜在的行动干预信息,作用于正在进行或已存在的行动池中内容
|
||||
*/
|
||||
public class ActionInterventor extends AbstractAgentModule.Running<PartnerRunningFlowContext> implements ActivateModel {
|
||||
private final AssemblyHelper assemblyHelper = new AssemblyHelper();
|
||||
private final PromptHelper promptHelper = new PromptHelper();
|
||||
/**
|
||||
* 键: 本次调用uuid;
|
||||
* 值:本次调用对应的prompt;
|
||||
*/
|
||||
private final Map<String, Map<String, String>> interventionPrompt = new HashMap<>();
|
||||
@InjectModule
|
||||
private InterventionRecognizer interventionRecognizer;
|
||||
@InjectModule
|
||||
private InterventionEvaluator interventionEvaluator;
|
||||
@InjectCapability
|
||||
private ActionCapability actionCapability;
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
@InjectCapability
|
||||
private MemoryCapability memoryCapability;
|
||||
@InjectModule
|
||||
private MemoryRuntime memoryRuntime;
|
||||
|
||||
@Override
|
||||
public void execute(PartnerRunningFlowContext context) {
|
||||
// 综合当前正在进行的行动链信息、用户交互历史、激活的记忆切片,尝试识别出是否存在行动干预意图
|
||||
// 首先通过recognizer进行快速意图识别,识别成功则步入评估阶段,评估成功则直接作用于目标行动链
|
||||
// 进行快速意图识别时必须结合近期对话与进行中行动链情况
|
||||
// 干预意图识别
|
||||
String uuid = context.getInfo().getUuid();
|
||||
String userId = context.getSource();
|
||||
RecognizerResult recognizerResult = interventionRecognizer
|
||||
.execute(assemblyHelper.buildRecognizerInput(userId, context.getInput())); // 此处的输入内容携带了所有 PhaserRecord
|
||||
if (!recognizerResult.isOk()) {
|
||||
promptHelper.setupNoInterventionPrompt(uuid);
|
||||
return;
|
||||
}
|
||||
// 干预意图评估
|
||||
EvaluatorResult evaluatorResult = interventionEvaluator
|
||||
.execute(assemblyHelper.buildEvaluatorInput(recognizerResult, userId));
|
||||
List<EvaluatedInterventionData> executingDataList = evaluatorResult.getExecutingDataList();
|
||||
List<EvaluatedInterventionData> preparedDataList = evaluatorResult.getPreparedDataList();
|
||||
// 意图评估结果处理
|
||||
if (evaluatorResult.isOk()) {
|
||||
// 对存在‘异常ActionKey’的评估结果列表进行过滤
|
||||
invalidActionKeysFilter(executingDataList);
|
||||
invalidActionKeysFilter(preparedDataList);
|
||||
// 同步写入prompt,异步处理干预行为,‘异步’在处理流程中体现
|
||||
promptHelper.setupInterventionPrompt(uuid, executingDataList, preparedDataList);
|
||||
handleInterventions(executingDataList, recognizerResult.getExecutingInterventions());
|
||||
handleInterventions(preparedDataList, recognizerResult.getPreparedInterventions());
|
||||
} else {
|
||||
promptHelper.setupInterventionIgnoredPrompt(uuid, executingDataList, preparedDataList);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleInterventions(List<EvaluatedInterventionData> interventionDataList, Map<String, ExecutableAction> interventionDataMap) {
|
||||
val executor = actionCapability.getExecutor(ActionCore.ExecutorType.PLATFORM);
|
||||
executor.execute(() -> {
|
||||
for (EvaluatedInterventionData interventionData : interventionDataList) {
|
||||
// 此处拿到的为 ActionData 或者 PhaserRecord, 来自 Recognizer 的封装
|
||||
val data = interventionDataMap.get(interventionData.getTendency());
|
||||
actionCapability.handleInterventions(interventionData.getMetaInterventionList(), data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void invalidActionKeysFilter(List<EvaluatedInterventionData> interventions) {
|
||||
List<EvaluatedInterventionData> toRemove = new ArrayList<>();
|
||||
for (EvaluatedInterventionData intervention : interventions) {
|
||||
List<MetaIntervention> interventionData = intervention.getMetaInterventionList();
|
||||
List<String> actions = new ArrayList<>();
|
||||
for (MetaIntervention metaData : interventionData) {
|
||||
actions.addAll(metaData.getActions());
|
||||
}
|
||||
// 如果存在异常行动key,则可视为该评估结果存在问题,直接忽略该结果
|
||||
if (!actionCapability.checkExists(actions.toArray(String[]::new))) {
|
||||
toRemove.add(intervention);
|
||||
}
|
||||
// 针对 REBUILD 类型进行特殊校验, REBUILD 类型必须满足所有 MetaIntervention 的类型均为 REBUILD
|
||||
if (!checkRebuildType(interventionData)) {
|
||||
toRemove.add(intervention);
|
||||
}
|
||||
}
|
||||
interventions.removeAll(toRemove);
|
||||
}
|
||||
|
||||
private boolean checkRebuildType(List<MetaIntervention> interventionData) {
|
||||
boolean hasRebuild = false;
|
||||
for (MetaIntervention meta : interventionData) {
|
||||
if (meta.getType() == InterventionType.REBUILD) {
|
||||
hasRebuild = true;
|
||||
} else if (hasRebuild) {
|
||||
// 已经存在REBUILD类型,但又发现了非REBUILD类型,不合法
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modelKey() {
|
||||
return "action_identifier";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
private final class AssemblyHelper {
|
||||
private AssemblyHelper() {
|
||||
}
|
||||
|
||||
private RecognizerInput buildRecognizerInput(String userId, String input) {
|
||||
RecognizerInput recognizerInput = new RecognizerInput();
|
||||
recognizerInput.setInput(input);
|
||||
recognizerInput.setUserDialogMapStr(memoryRuntime.getDialogMapStr());
|
||||
// 参考的对话列表大小或需调整
|
||||
recognizerInput.setRecentMessages(cognationCapability.getChatMessages());
|
||||
recognizerInput.setExecutingActions(actionCapability.listPhaserRecords().stream().map(PhaserRecord::executableAction).toList());
|
||||
recognizerInput.setPreparedActions(actionCapability.listActions(ExecutableAction.Status.PREPARE, userId).stream().toList());
|
||||
return recognizerInput;
|
||||
}
|
||||
|
||||
private EvaluatorInput buildEvaluatorInput(RecognizerResult recognizerResult, String userId) {
|
||||
EvaluatorInput input = new EvaluatorInput();
|
||||
input.setExecutingInterventions(recognizerResult.getExecutingInterventions());
|
||||
input.setPreparedInterventions(recognizerResult.getPreparedInterventions());
|
||||
input.setRecentMessages(cognationCapability.getChatMessages());
|
||||
input.setActivatedSlices(memoryCapability.getActivatedSlices());
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
private final class PromptHelper {
|
||||
private PromptHelper() {
|
||||
}
|
||||
|
||||
private void setupInterventionIgnoredPrompt(String uuid, List<EvaluatedInterventionData> executingDataList, List<EvaluatedInterventionData> preparedDataList) {
|
||||
List<EvaluatedInterventionData> total = Stream.concat(executingDataList.stream(), preparedDataList.stream()).toList();
|
||||
JSONArray reasons = new JSONArray();
|
||||
for (EvaluatedInterventionData data : total) {
|
||||
JSONObject reason = reasons.addObject();
|
||||
reason.put("[干预倾向]", data.getTendency());
|
||||
reason.put("[未采用原因]", data.getDescription());
|
||||
}
|
||||
synchronized (interventionPrompt) {
|
||||
interventionPrompt.put(uuid, Map.of(
|
||||
"[识别状态] <是否识别到干预已存在行动的意图>", "识别到,但都未采用",
|
||||
"[忽略原因] <各个意图被忽略的原因>", reasons.toString(),
|
||||
"[干预行动] <将对已存在行动做出的行为>", "无行为"));
|
||||
}
|
||||
}
|
||||
|
||||
private void setupInterventionPrompt(String uuid, List<EvaluatedInterventionData> executingDataList,
|
||||
List<EvaluatedInterventionData> preparedDataList) {
|
||||
JSONArray contents = new JSONArray();
|
||||
List<EvaluatedInterventionData> temp = Stream.concat(executingDataList.stream(), preparedDataList.stream()).toList();
|
||||
for (EvaluatedInterventionData data : temp) {
|
||||
if (!data.isOk()) {
|
||||
continue;
|
||||
}
|
||||
String tendency = data.getTendency();
|
||||
JSONObject newElement = contents.addObject();
|
||||
newElement.put("[干预倾向]", tendency);
|
||||
JSONArray changes = newElement.putArray("[行动链变动情况]");
|
||||
for (MetaIntervention intervention : data.getMetaInterventionList()) {
|
||||
JSONObject change = changes.addObject();
|
||||
change.put("[干预类型]", intervention.getType());
|
||||
change.put("[干预序号]", intervention.getOrder());
|
||||
change.putArray("[干预内容]").addAll(intervention.getActions());
|
||||
}
|
||||
}
|
||||
synchronized (interventionPrompt) {
|
||||
interventionPrompt.put(uuid, Map.of(
|
||||
"[识别状态] <是否识别到干预已存在行动的意图>", "识别到,将采用",
|
||||
"[干预内容] <将对已存在行动做出的行为>", contents.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
private void setupNoInterventionPrompt(String uuid) {
|
||||
interventionPrompt.put(uuid, Map.of(
|
||||
"[识别状态] <是否识别到干预已存在行动的意图>", "未识别到干预意图",
|
||||
"[干预行动] <将对已存在行动做出的行为>", "无行动"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor.evaluator;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore.ExecutorType;
|
||||
import work.slhaf.partner.core.action.entity.ExecutableAction;
|
||||
import work.slhaf.partner.core.memory.pojo.ActivatedMemorySlice;
|
||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorInput;
|
||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorResult;
|
||||
import work.slhaf.partner.module.modules.action.interventor.evaluator.entity.EvaluatorResult.EvaluatedInterventionData;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
public class InterventionEvaluator extends AbstractAgentModule.Sub<EvaluatorInput, EvaluatorResult>
|
||||
implements ActivateModel {
|
||||
@InjectCapability
|
||||
private ActionCapability actionCapability;
|
||||
|
||||
/**
|
||||
* 基于干预意图、记忆切片、交互上下文、已有行动程序综合评估,尝试评估并选取出合适的行动程序,交付给 ActionInterventor
|
||||
*/
|
||||
@Override
|
||||
public EvaluatorResult execute(EvaluatorInput input) {
|
||||
// 获取必须数据
|
||||
ExecutorService executor = actionCapability.getExecutor(ExecutorType.VIRTUAL);
|
||||
Map<String, ExecutableAction> executingInterventions = input.getExecutingInterventions();
|
||||
Map<String, ExecutableAction> preparedInterventions = input.getPreparedInterventions();
|
||||
CountDownLatch latch = new CountDownLatch(executingInterventions.size() + preparedInterventions.size());
|
||||
// 创建结果容器
|
||||
EvaluatorResult result = new EvaluatorResult();
|
||||
List<EvaluatedInterventionData> executingDataList = result.getExecutingDataList();
|
||||
List<EvaluatedInterventionData> preparedDataList = result.getPreparedDataList();
|
||||
// 并发评估
|
||||
evaluateIntervention(executingDataList, executingInterventions, input, executor, latch);
|
||||
evaluateIntervention(preparedDataList, preparedInterventions, input, executor, latch);
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException e) {
|
||||
log.warn("CountDownLatch阻塞已中断");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void evaluateIntervention(List<EvaluatedInterventionData> evaluatedDataList, Map<String, ExecutableAction> interventionMap, EvaluatorInput input, ExecutorService executor, CountDownLatch latch) {
|
||||
interventionMap.forEach((tendency, actionData) -> executor.execute(() -> {
|
||||
try {
|
||||
String prompt = buildPrompt(input.getRecentMessages(), input.getActivatedSlices(), actionData, tendency);
|
||||
EvaluatedInterventionData evaluatedData = formattedChat(
|
||||
List.of(new Message(Message.Character.USER, prompt)),
|
||||
EvaluatedInterventionData.class);
|
||||
synchronized (evaluatedDataList) {
|
||||
evaluatedDataList.add(evaluatedData);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("干预意图评估出错: {}", tendency, e);
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private String buildPrompt(List<Message> recentMessages, List<ActivatedMemorySlice> activatedSlices,
|
||||
ExecutableAction executableAction, String tendency) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("干预倾向", tendency);
|
||||
json.putArray("近期对话").addAll(recentMessages);
|
||||
json.putArray("参考记忆").addAll(activatedSlices);
|
||||
json.put("将干预的行动", JSONObject.toJSONString(executableAction));
|
||||
return json.toJSONString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modelKey() {
|
||||
return "intervention_evaluator";
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor.evaluator.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.entity.ExecutableAction;
|
||||
import work.slhaf.partner.core.memory.pojo.ActivatedMemorySlice;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class EvaluatorInput {
|
||||
private Map<String, ExecutableAction> executingInterventions;
|
||||
private Map<String, ExecutableAction> preparedInterventions;
|
||||
private List<ActivatedMemorySlice> activatedSlices;
|
||||
private List<Message> recentMessages;
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor.evaluator.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.module.modules.action.interventor.entity.MetaIntervention;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 干预倾向评估结果,包含评估通过的倾向文本、对行动链的行为、指定操作的行动单元key、未通过的原因
|
||||
*/
|
||||
@Data
|
||||
public class EvaluatorResult {
|
||||
/**
|
||||
* 是否存在通过的干预倾向
|
||||
*/
|
||||
private boolean ok;
|
||||
private List<EvaluatedInterventionData> executingDataList;
|
||||
private List<EvaluatedInterventionData> preparedDataList;
|
||||
|
||||
@Data
|
||||
public static class EvaluatedInterventionData {
|
||||
/**
|
||||
* 是否通过
|
||||
*/
|
||||
private boolean ok;
|
||||
private String tendency;
|
||||
/**
|
||||
* 描述信息(包括通过、失败原因)
|
||||
*/
|
||||
private String description;
|
||||
private List<MetaIntervention> metaInterventionList;
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor.recognizer;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.ActivateModel;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.ActionCapability;
|
||||
import work.slhaf.partner.core.action.ActionCore;
|
||||
import work.slhaf.partner.core.action.entity.ExecutableAction;
|
||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.MetaRecognizerResult;
|
||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerInput;
|
||||
import work.slhaf.partner.module.modules.action.interventor.recognizer.entity.RecognizerResult;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
public class InterventionRecognizer extends AbstractAgentModule.Sub<RecognizerInput, RecognizerResult> implements ActivateModel {
|
||||
@InjectCapability
|
||||
private ActionCapability actionCapability;
|
||||
|
||||
@Override
|
||||
public RecognizerResult execute(RecognizerInput input) {
|
||||
// 获取必须数据
|
||||
ExecutorService executor = actionCapability.getExecutor(ActionCore.ExecutorType.VIRTUAL);
|
||||
List<ExecutableAction> executingActions = input.getExecutingActions();
|
||||
List<ExecutableAction> preparedActions = input.getPreparedActions();
|
||||
CountDownLatch countDownLatch = new CountDownLatch(executingActions.size() + preparedActions.size());
|
||||
// 创建结果容器
|
||||
RecognizerResult recognizerResult = new RecognizerResult();
|
||||
Map<String, ExecutableAction> executingInterventions = recognizerResult.getExecutingInterventions();
|
||||
Map<String, ExecutableAction> preparedInterventions = recognizerResult.getPreparedInterventions();
|
||||
// 执行识别操作
|
||||
recognizeIntervention(executingInterventions, executingActions, executor, input, countDownLatch);
|
||||
recognizeIntervention(preparedInterventions, preparedActions, executor, input, countDownLatch);
|
||||
try {
|
||||
countDownLatch.await();
|
||||
} catch (InterruptedException e) {
|
||||
log.warn("CountDownLatch阻塞已中断");
|
||||
}
|
||||
return recognizerResult;
|
||||
}
|
||||
|
||||
private void recognizeIntervention(Map<String, ExecutableAction> interventionsMap, List<ExecutableAction> actions, ExecutorService executor, RecognizerInput input, CountDownLatch latch) {
|
||||
for (ExecutableAction data : actions) {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
String prompt = buildPrompt(data, input);
|
||||
MetaRecognizerResult result = formattedChat(
|
||||
List.of(new Message(Message.Character.USER, prompt)),
|
||||
MetaRecognizerResult.class
|
||||
);
|
||||
if (result.isOk()) {
|
||||
synchronized (interventionsMap) {
|
||||
interventionsMap.put(result.getIntervention(), data);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("LLM干预意图提取出错", e);
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private String buildPrompt(ExecutableAction executableAction, RecognizerInput input) {
|
||||
JSONObject json = new JSONObject();
|
||||
JSONObject actionInfo = json.putObject("行动信息");
|
||||
actionInfo.put("行动倾向", executableAction.getTendency());
|
||||
actionInfo.put("行动原因", executableAction.getReason());
|
||||
actionInfo.put("行动描述", executableAction.getDescription());
|
||||
actionInfo.put("行动状态", executableAction.getStatus());
|
||||
actionInfo.put("行动来源", executableAction.getSource());
|
||||
JSONObject interactionInfo = json.putObject("交互信息");
|
||||
interactionInfo.put("用户输入", input.getInput());
|
||||
interactionInfo.put("当前对话", input.getRecentMessages());
|
||||
interactionInfo.put("近期对话", input.getUserDialogMapStr());
|
||||
return json.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modelKey() {
|
||||
return "intervention_recognizer";
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor.recognizer.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class MetaRecognizerResult {
|
||||
private boolean ok;
|
||||
private String intervention;
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor.recognizer.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.core.action.entity.ExecutableAction;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class RecognizerInput {
|
||||
private String input;
|
||||
private List<Message> recentMessages;
|
||||
/**
|
||||
* 当前用户对应的近两日对话缓存
|
||||
*/
|
||||
private String userDialogMapStr;
|
||||
/**
|
||||
* 正在执行的行动-Phaser记录列表,在Recognizer中结合本次输入并发评估(考虑到不同行动链之间对LLM的影响)
|
||||
*/
|
||||
private List<ExecutableAction> executingActions;
|
||||
private List<ExecutableAction> preparedActions;
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.action.interventor.recognizer.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.core.action.entity.ExecutableAction;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class RecognizerResult {
|
||||
|
||||
private boolean ok;
|
||||
|
||||
/**
|
||||
* <h4>将被干预的‘执行中行动’</h4>
|
||||
* key: 干预倾向
|
||||
* <br/>
|
||||
* value: 干预倾向将作用的行动数据
|
||||
*/
|
||||
private Map<String, ExecutableAction> executingInterventions = new HashMap<>();
|
||||
|
||||
/**
|
||||
* <h4>将被干预的‘等待中行动’</h4>
|
||||
* key: 干预倾向
|
||||
* <br/>
|
||||
* value: 干预倾向将作用的行动数据
|
||||
*/
|
||||
private Map<String, ExecutableAction> preparedInterventions = new HashMap<>();
|
||||
}
|
||||
Reference in New Issue
Block a user