mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(perceive): drop legacy perceive updater modules and remove PreProcessExecutor
This commit is contained in:
@@ -1,18 +1,9 @@
|
||||
package work.slhaf.partner.core.perceive;
|
||||
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.Capability;
|
||||
import work.slhaf.partner.core.perceive.pojo.User;
|
||||
|
||||
@Capability(value = "perceive")
|
||||
public interface PerceiveCapability {
|
||||
User getUser(String userInfo, String client);
|
||||
|
||||
User getUser(String id);
|
||||
|
||||
User addUser(String userInfo, String platform, String userNickName);
|
||||
|
||||
void updateUser(User user);
|
||||
|
||||
String refreshInteract();
|
||||
|
||||
long showLastInteract();
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.perceive.updater;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.Init;
|
||||
import work.slhaf.partner.api.agent.factory.component.annotation.InjectModule;
|
||||
import work.slhaf.partner.common.thread.InteractionThreadPoolExecutor;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.perceive.pojo.User;
|
||||
import work.slhaf.partner.module.common.module.PostRunningAgentModule;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.RelationExtractor;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.entity.RelationExtractResult;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.static_extractor.StaticMemoryExtractor;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 感知更新,异步
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class PerceiveUpdater extends PostRunningAgentModule {
|
||||
@InjectCapability
|
||||
private PerceiveCapability perceiveCapability;
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
@InjectModule
|
||||
private RelationExtractor relationExtractor;
|
||||
@InjectModule
|
||||
private StaticMemoryExtractor staticMemoryExtractor;
|
||||
private InteractionThreadPoolExecutor executor;
|
||||
|
||||
@Init
|
||||
public void init() {
|
||||
this.executor = InteractionThreadPoolExecutor.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doExecute(PartnerRunningFlowContext context) {
|
||||
executor.execute(() -> {
|
||||
ReentrantLock userLock = new ReentrantLock();
|
||||
User user = new User();
|
||||
user.setUuid(context.getSource());
|
||||
List<Callable<Void>> tasks = new ArrayList<>();
|
||||
tasks.add(() -> {
|
||||
runStaticExtractorAction(context, userLock, user);
|
||||
return null;
|
||||
});
|
||||
tasks.add(() -> {
|
||||
runRelationExtractorAction(context, userLock, user);
|
||||
return null;
|
||||
});
|
||||
executor.invokeAll(tasks);
|
||||
perceiveCapability.updateUser(user);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean relyOnMessage() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void runRelationExtractorAction(PartnerRunningFlowContext context, ReentrantLock userLock, User user) {
|
||||
RelationExtractResult relationExtractResult = relationExtractor.execute(context);
|
||||
userLock.lock();
|
||||
user.setRelation(relationExtractResult.getRelation());
|
||||
user.setImpressions(relationExtractResult.getImpressions());
|
||||
user.setAttitude(relationExtractResult.getAttitude());
|
||||
user.updateRelationChange(relationExtractResult.getRelationChangeHistory());
|
||||
userLock.unlock();
|
||||
}
|
||||
|
||||
private void runStaticExtractorAction(PartnerRunningFlowContext context, ReentrantLock userLock, User user) {
|
||||
HashMap<String, String> newStaticMemory = staticMemoryExtractor.execute(context);
|
||||
userLock.lock();
|
||||
user.setStaticMemory(newStaticMemory);
|
||||
userLock.unlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
return 7;
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.perceive.updater.relation_extractor;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
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.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.perceive.pojo.User;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.entity.RelationExtractInput;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.relation_extractor.entity.RelationExtractResult;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class RelationExtractor extends AbstractAgentModule.Sub<PartnerRunningFlowContext, RelationExtractResult> implements ActivateModel {
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
@InjectCapability
|
||||
private PerceiveCapability perceiveCapability;
|
||||
private List<Message> tempMessages;
|
||||
|
||||
@Override
|
||||
public RelationExtractResult execute(PartnerRunningFlowContext context) {
|
||||
tempMessages = new ArrayList<>(cognationCapability.getChatMessages());
|
||||
String userId = context.getSource();
|
||||
RelationExtractInput input = getRelationInput(userId);
|
||||
RelationExtractResult relationExtractResult = getRelationResult(input);
|
||||
User user = getTempUser(context, relationExtractResult);
|
||||
perceiveCapability.updateUser(user);
|
||||
return relationExtractResult;
|
||||
}
|
||||
|
||||
private User getTempUser(PartnerRunningFlowContext context, RelationExtractResult relationExtractResult) {
|
||||
User user = new User();
|
||||
user.setUuid(context.getSource());
|
||||
user.setRelation(relationExtractResult.getRelation());
|
||||
user.setImpressions(relationExtractResult.getImpressions());
|
||||
user.setAttitude(relationExtractResult.getAttitude());
|
||||
return user;
|
||||
}
|
||||
|
||||
private RelationExtractResult getRelationResult(RelationExtractInput input) {
|
||||
return formattedChat(
|
||||
List.of(new Message(Message.Character.USER, JSONObject.toJSONString(input))),
|
||||
RelationExtractResult.class
|
||||
);
|
||||
}
|
||||
|
||||
private RelationExtractInput getRelationInput(String userId) {
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
User user = perceiveCapability.getUser(userId);
|
||||
map.put("[用户昵称] <用户的昵称信息>", user.getNickName());
|
||||
map.put("[关系] <你与用户的关系>", user.getRelation());
|
||||
map.put("[态度] <你对于用户的态度>", user.getAttitude().toString());
|
||||
map.put("[印象] <你对于用户的印象>", user.getImpressions().toString());
|
||||
map.put("[静态记忆] <你对该用户的事实性记忆>", user.getStaticMemory().toString());
|
||||
RelationExtractInput input = new RelationExtractInput();
|
||||
input.setPrimaryUserPerceive(map);
|
||||
input.setChatMessages(tempMessages);
|
||||
return input;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modelKey() {
|
||||
return "relation_extractor";
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.perceive.updater.relation_extractor.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class RelationExtractInput {
|
||||
private HashMap<String, String> primaryUserPerceive;
|
||||
private List<Message> chatMessages;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.perceive.updater.relation_extractor.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class RelationExtractResult {
|
||||
private String relation;
|
||||
private List<String> impressions;
|
||||
private List<String> attitude;
|
||||
private String relationChangeHistory;
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.perceive.updater.static_extractor;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
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.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.module.modules.perceive.updater.static_extractor.entity.StaticMemoryExtractInput;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class StaticMemoryExtractor extends AbstractAgentModule.Sub<PartnerRunningFlowContext, HashMap<String, String>> implements ActivateModel {
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
@InjectCapability
|
||||
private PerceiveCapability perceiveCapability;
|
||||
|
||||
@Override
|
||||
public HashMap<String, String> execute(PartnerRunningFlowContext context) {
|
||||
StaticMemoryExtractInput input = StaticMemoryExtractInput.builder()
|
||||
.userId(context.getSource())
|
||||
.messages(cognationCapability.getChatMessages())
|
||||
.existedStaticMap(perceiveCapability.getUser(context.getSource()).getStaticMemory())
|
||||
.build();
|
||||
String response = chat(List.of(new Message(Message.Character.USER, JSONUtil.toJsonPrettyStr(input))));
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
HashMap<String, String> result = new HashMap<>();
|
||||
jsonObject.forEach((k, v) -> result.put(k, (String) v));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String modelKey() {
|
||||
return "static_extractor";
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.perceive.updater.static_extractor.entity;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class StaticMemoryExtractInput {
|
||||
private String userId;
|
||||
private List<Message> messages;
|
||||
private Map<String, String> existedStaticMap;
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package work.slhaf.partner.module.modules.process;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.agent.factory.capability.annotation.InjectCapability;
|
||||
import work.slhaf.partner.api.agent.factory.component.abstracts.AbstractAgentModule;
|
||||
import work.slhaf.partner.core.cognation.CognationCapability;
|
||||
import work.slhaf.partner.core.memory.MemoryCapability;
|
||||
import work.slhaf.partner.core.perceive.PerceiveCapability;
|
||||
import work.slhaf.partner.core.perceive.pojo.User;
|
||||
import work.slhaf.partner.runtime.interaction.data.context.PartnerRunningFlowContext;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class PreprocessExecutor extends AbstractAgentModule.Running<PartnerRunningFlowContext> {
|
||||
private static final String INFO_PLATFORM = "platform";
|
||||
private static final String INFO_NICKNAME = "nickname";
|
||||
|
||||
@InjectCapability
|
||||
private CognationCapability cognationCapability;
|
||||
@InjectCapability
|
||||
private MemoryCapability memoryCapability;
|
||||
@InjectCapability
|
||||
private PerceiveCapability perceiveCapability;
|
||||
|
||||
@Override
|
||||
public void execute(PartnerRunningFlowContext context) {
|
||||
getInteractionContext(context);
|
||||
}
|
||||
|
||||
|
||||
private void getInteractionContext(PartnerRunningFlowContext context) {
|
||||
log.debug("[PreprocessExecutor] 预处理原始输入: {}", context);
|
||||
String platform = context.getAdditionalUserInfo().getOrDefault(INFO_PLATFORM, "");
|
||||
String nickName = context.getAdditionalUserInfo().getOrDefault(INFO_NICKNAME, "");
|
||||
String sourceUserId = parseSourceUserId(context.getSource());
|
||||
User user = perceiveCapability.getUser(sourceUserId, platform);
|
||||
if (user == null) {
|
||||
user = perceiveCapability.addUser(sourceUserId, platform, nickName);
|
||||
}
|
||||
String userId = user.getUuid();
|
||||
String userStr = "[" + nickName + "(" + userId + ")]";
|
||||
log.debug("[PreprocessExecutor] 已识别用户: {} {}", userStr, context.getSource());
|
||||
log.debug("[PreprocessExecutor] 预处理结果: {}", context);
|
||||
}
|
||||
|
||||
private String parseSourceUserId(String source) {
|
||||
int split = source.indexOf(':');
|
||||
if (split < 0 || split + 1 >= source.length()) {
|
||||
return source;
|
||||
}
|
||||
return source.substring(split + 1).trim();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user