diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/ActionInterventor.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/ActionInterventor.java index 504444cb..9ae31494 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/ActionInterventor.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/ActionInterventor.java @@ -5,16 +5,27 @@ import work.slhaf.partner.api.agent.factory.module.annotation.AgentModule; import work.slhaf.partner.api.agent.factory.module.annotation.InjectModule; import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.ActivateModel; import work.slhaf.partner.core.action.ActionCapability; +import work.slhaf.partner.core.action.ActionCore.PhaserRecord; import work.slhaf.partner.core.cognation.CognationCapability; import work.slhaf.partner.core.memory.MemoryCapability; import work.slhaf.partner.module.common.module.PreRunningModule; import work.slhaf.partner.module.modules.action.identifier.evaluator.InterventionEvaluator; +import work.slhaf.partner.module.modules.action.identifier.evaluator.entity.EvaluatorInput; +import work.slhaf.partner.module.modules.action.identifier.evaluator.entity.EvaluatorResult; +import work.slhaf.partner.module.modules.action.identifier.evaluator.entity.EvaluatorResult.EvaluatedInterventionData; +import work.slhaf.partner.module.modules.action.identifier.handler.InterventionHandler; +import work.slhaf.partner.module.modules.action.identifier.handler.entity.HandlerInput; +import work.slhaf.partner.module.modules.action.identifier.handler.entity.HandlerInput.HandlerInputData; import work.slhaf.partner.module.modules.action.identifier.recognizer.InterventionRecognizer; import work.slhaf.partner.module.modules.action.identifier.recognizer.entity.RecognizerInput; import work.slhaf.partner.module.modules.action.identifier.recognizer.entity.RecognizerResult; import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext; import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + /** * 负责识别潜在的行动干预信息,作用于正在进行或已存在的行动池中内容 @@ -26,6 +37,8 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel private InterventionRecognizer interventionRecognizer; @InjectModule private InterventionEvaluator interventionEvaluator; + @InjectModule + private InterventionHandler InterventionHandler; @InjectCapability private ActionCapability actionCapability; @@ -34,24 +47,80 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel @InjectCapability private MemoryCapability memoryCapability; + /** + * 键: 本次调用uuid; + * 值:本次调用对应的prompt; + */ + private Map> interventionPrompt = new HashMap<>(); @Override protected void doExecute(PartnerRunningFlowContext context) { - //综合当前正在进行的行动链信息、用户交互历史、激活的记忆切片,尝试识别出是否存在行动干预意图 - //首先通过recognizer进行快速意图识别,识别成功则步入评估阶段,评估成功则直接作用于目标行动链 - //进行快速意图识别时必须结合近期对话与进行中行动链情况 - RecognizerResult recognizerResult = interventionRecognizer.execute(buildRecognizerInput(context.getUserId(), context.getInput())); + // 综合当前正在进行的行动链信息、用户交互历史、激活的记忆切片,尝试识别出是否存在行动干预意图 + // 首先通过recognizer进行快速意图识别,识别成功则步入评估阶段,评估成功则直接作用于目标行动链 + // 进行快速意图识别时必须结合近期对话与进行中行动链情况 + String uuid = context.getUuid(); + String userId = context.getUserId(); + RecognizerResult recognizerResult = interventionRecognizer + .execute(buildRecognizerInput(userId, context.getInput())); if (!recognizerResult.isOk()) { + // 设置相应prompt + setupNoInterventionPrompt(uuid); return; } - //存在则进一步评估、评估通过则并直接添加行动程序至对应行动链 + // 存在则进一步评估、评估通过则并直接添加行动程序至对应行动链 + Map recognizedInterventions = recognizerResult.getInterventions(); + EvaluatorResult evaluatorResult = interventionEvaluator + .execute(buildEvaluatorInput(recognizedInterventions.keySet(), userId)); + List interventions = evaluatorResult.getDataList(); + if (evaluatorResult.isOk()) { + // 同步写入prompt,异步处理干预行为 + setupInterventionPrompt(uuid, interventions); + InterventionHandler.execute(buildHandlerInput(interventions)); + } else { + // 同步写入prompt + setupInterventionIgnoredPrompt(uuid, interventions); + } + } + + private HandlerInput buildHandlerInput(List interventions) { + HandlerInput input = new HandlerInput(); + List inputDataList = input.getData(); + for(EvaluatedInterventionData interventionData: interventions){ + HandlerInputData inputData = new HandlerInputData(); + inputData.setTendency(interventionData.getTendency()); + inputData.setDescription(interventionData.getDescription()); + inputData.setType(interventionData.getType()); + inputData.setActions(interventionData.getActions()); + inputDataList.add(inputData); + } + return input; + } + + private void setupInterventionIgnoredPrompt(String uuid, List dataList) { + + } + + private void setupInterventionPrompt(String uuid, List dataList) { + + } + + private void setupNoInterventionPrompt(String uuid) { + + } + + private EvaluatorInput buildEvaluatorInput(Set interventionTendencies, String userId) { + EvaluatorInput input = new EvaluatorInput(); + input.setInterventionTendencies(interventionTendencies); + input.setRecentMessages(cognationCapability.getChatMessages()); + input.setActivatedSlices(memoryCapability.getActivatedSlices(userId)); + return input; } private RecognizerInput buildRecognizerInput(String userId, String input) { RecognizerInput recognizerInput = new RecognizerInput(); recognizerInput.setInput(input); recognizerInput.setUserDialogMapStr(memoryCapability.getUserDialogMapStr(userId)); - // TODO 参考的对话列表大小或需调整 + // 参考的对话列表大小或需调整 recognizerInput.setRecentMessages(cognationCapability.getChatMessages()); recognizerInput.setExecutingActions(actionCapability.listPhaserRecords()); return recognizerInput; @@ -64,13 +133,12 @@ public class ActionInterventor extends PreRunningModule implements ActivateModel @Override public boolean withBasicPrompt() { - return true; + return false; } @Override protected HashMap getPromptDataMap(PartnerRunningFlowContext context) { - - return null; + return interventionPrompt.get(context.getUuid()); } @Override diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorInput.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorInput.java index 9a80793d..aa5e1c0d 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorInput.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorInput.java @@ -5,10 +5,11 @@ import work.slhaf.partner.api.chat.pojo.Message; import work.slhaf.partner.core.memory.pojo.EvaluatedSlice; import java.util.List; +import java.util.Set; @Data public class EvaluatorInput { - private String interventionTendency; + private Set interventionTendencies; private List activatedSlices; private List recentMessages; } diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorResult.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorResult.java index 37723a9b..0b2e6468 100644 --- a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorResult.java +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/evaluator/entity/EvaluatorResult.java @@ -1,7 +1,37 @@ package work.slhaf.partner.module.modules.action.identifier.evaluator.entity; -import lombok.Data; +import java.util.LinkedHashMap; +import java.util.List; +import lombok.Data; +import work.slhaf.partner.module.modules.action.identifier.handler.entity.InterventionType; + +/** + * 干预倾向评估结果,包含评估通过的倾向文本、对行动链的行为、指定操作的行动单元key、未通过的原因 + */ @Data public class EvaluatorResult { + /** + * 是否存在通过的干预倾向 + */ + private boolean ok; + private List dataList; + + @Data + public static class EvaluatedInterventionData { + /** + * 是否通过 + */ + private boolean ok; + private String tendency; + /** + * 描述信息(包括通过、失败原因) + */ + private String description; + private InterventionType type; + /** + * 键为执行顺序,值为行动单元对应的key + */ + private LinkedHashMap actions; + } } diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/handler/InterventionHandler.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/handler/InterventionHandler.java new file mode 100644 index 00000000..76331573 --- /dev/null +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/handler/InterventionHandler.java @@ -0,0 +1,16 @@ +package work.slhaf.partner.module.modules.action.identifier.handler; + +import work.slhaf.partner.api.agent.factory.module.annotation.AgentSubModule; +import work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts.AgentRunningSubModule; +import work.slhaf.partner.module.modules.action.identifier.handler.entity.HandlerInput; + +@AgentSubModule +public class InterventionHandler extends AgentRunningSubModule { + + @Override + public Void execute(HandlerInput data) { + + return null; + } + +} diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/handler/entity/HandlerInput.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/handler/entity/HandlerInput.java new file mode 100644 index 00000000..d78b8a60 --- /dev/null +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/handler/entity/HandlerInput.java @@ -0,0 +1,21 @@ +package work.slhaf.partner.module.modules.action.identifier.handler.entity; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +import lombok.Data; + +@Data +public class HandlerInput { + + private List data = new ArrayList<>(); + + @Data + public static class HandlerInputData{ + private String tendency; + private String description; + private InterventionType type; + private LinkedHashMap actions; + } +} diff --git a/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/handler/entity/InterventionType.java b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/handler/entity/InterventionType.java new file mode 100644 index 00000000..821249b5 --- /dev/null +++ b/Partner-Main/src/main/java/work/slhaf/partner/module/modules/action/identifier/handler/entity/InterventionType.java @@ -0,0 +1,5 @@ +package work.slhaf.partner.module.modules.action.identifier.handler.entity; + +public enum InterventionType { + APPEND, INSERT, REBUILD, DELETE +}