mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
进行 Partner 本体对于框架的适配,以及框架层的部分调整
框架: - 调整 ActivateModel 中模型初始化设置的 initHook 权重为-1(最优先) - 为 AgentGateway 中 receive 操作提供模板方法,子类需实现发送逻辑并提供适配器 - 取消了 AgentInteractionAdapter 的单例配置 - 调整 RunningFlow 的异常处理,并在RunningFlowContext中提供错误码进行判断 - 调整模块基类 - 本体: - 新增配置加载异常,继承自Agent启动异常 - 修改 GlobalExceptionData 获取逻辑 - 移除 MessageSender 等交互接口,适配框架的交互逻辑 - 异常处理已适配 - 配置加载逻辑已适配 - Gateway 已适配 - CoreModel 已适配
This commit is contained in:
@@ -5,7 +5,7 @@ import work.slhaf.partner.api.agent.factory.config.pojo.ModelConfig;
|
||||
import work.slhaf.partner.api.agent.factory.context.AgentRegisterContext;
|
||||
import work.slhaf.partner.api.agent.factory.context.ConfigFactoryContext;
|
||||
import work.slhaf.partner.api.agent.runtime.config.AgentConfigManager;
|
||||
import work.slhaf.partner.api.agent.runtime.config.DefaultAgentConfigManager;
|
||||
import work.slhaf.partner.api.agent.runtime.config.FileAgentConfigManager;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -24,7 +24,7 @@ public class ConfigLoaderFactory extends AgentBaseFactory {
|
||||
modelPromptMap = factoryContext.getModelPromptMap();
|
||||
|
||||
if (AgentConfigManager.INSTANCE == null){
|
||||
AgentConfigManager.setINSTANCE(new DefaultAgentConfigManager());
|
||||
AgentConfigManager.setINSTANCE(new FileAgentConfigManager());
|
||||
}
|
||||
|
||||
agentConfigManager = AgentConfigManager.INSTANCE;
|
||||
|
||||
@@ -22,12 +22,12 @@ import java.util.List;
|
||||
* 将从当前运行目录的config文件夹下创建并读取配置
|
||||
*/
|
||||
@Slf4j
|
||||
public class DefaultAgentConfigManager extends AgentConfigManager {
|
||||
public class FileAgentConfigManager extends AgentConfigManager {
|
||||
|
||||
private static final String CONFIG_DIR = "./config/";
|
||||
private static final String MODEL_CONFIG_DIR = "./config/model/";
|
||||
private static final String PROMPT_CONFIG_DIR = "./config/prompt/";
|
||||
private static final String MODULE_ENABLED_STATUS_CONFIG_FILE = CONFIG_DIR + "module_enabled_status.json";
|
||||
protected static final String CONFIG_DIR = "./config/";
|
||||
protected static final String MODEL_CONFIG_DIR = "./config/model/";
|
||||
protected static final String PROMPT_CONFIG_DIR = "./config/prompt/";
|
||||
protected static final String MODULE_ENABLED_STATUS_CONFIG_FILE = CONFIG_DIR + "module_enabled_status.json";
|
||||
|
||||
|
||||
@Override
|
||||
@@ -4,7 +4,7 @@ public class GlobalExceptionHandler {
|
||||
|
||||
public static GlobalExceptionHandler INSTANCE = new GlobalExceptionHandler();
|
||||
|
||||
private AgentExceptionCallback exceptionCallback = new DefaultAgentExceptionCallback();
|
||||
private AgentExceptionCallback exceptionCallback = new LogAgentExceptionCallback();
|
||||
|
||||
public void handle(Throwable e) {
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package work.slhaf.partner.api.agent.runtime.exception;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class DefaultAgentExceptionCallback implements AgentExceptionCallback {
|
||||
public class LogAgentExceptionCallback implements AgentExceptionCallback {
|
||||
|
||||
@Override
|
||||
public void onRuntimeException(AgentRuntimeException e) {
|
||||
@@ -4,9 +4,23 @@ import work.slhaf.partner.api.agent.runtime.interaction.data.AgentInputData;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.data.AgentOutputData;
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.flow.entity.RunningFlowContext;
|
||||
|
||||
public interface AgentGateway {
|
||||
public interface AgentGateway <I extends AgentInputData, O extends AgentOutputData, C extends RunningFlowContext>{
|
||||
|
||||
void launch();
|
||||
|
||||
<I extends AgentInputData, O extends AgentOutputData, C extends RunningFlowContext> AgentInteractionAdapter<I, O, C> adapter();
|
||||
default void receive(I inputData){
|
||||
C finalInputData = adapter().parseInputData(inputData);
|
||||
C outputContext = adapter().call(finalInputData);
|
||||
O outputData = adapter().parseOutputData(outputContext);
|
||||
send(outputData);
|
||||
}
|
||||
|
||||
void send(O outputData);
|
||||
|
||||
/**
|
||||
* 通过adapter提供的receive、send方法进行与客户端的交互行为
|
||||
*
|
||||
* @return adapter实例
|
||||
*/
|
||||
AgentInteractionAdapter<I, O, C> adapter();
|
||||
}
|
||||
|
||||
@@ -11,31 +11,15 @@ import java.util.List;
|
||||
|
||||
public abstract class AgentInteractionAdapter<I extends AgentInputData, O extends AgentOutputData, C extends RunningFlowContext> {
|
||||
|
||||
private static AgentInteractionAdapter<?,?,?> INSTANCE;
|
||||
|
||||
protected AgentRunningFlow<C> agentRunningFlow = new AgentRunningFlow<>();
|
||||
protected List<MetaModule> moduleList = AgentConfigManager.INSTANCE.getModuleList();
|
||||
|
||||
public void receive(I inputData) {
|
||||
C finalInputData = parseInputData(inputData);
|
||||
C outputContext = agentRunningFlow.launch(moduleList, finalInputData);
|
||||
O outputData = parseOutputData(outputContext);
|
||||
send(outputData);
|
||||
public C call(C finalInputData){
|
||||
return agentRunningFlow.launch(moduleList, finalInputData);
|
||||
}
|
||||
|
||||
protected abstract O parseOutputData(C outputContext);
|
||||
|
||||
protected abstract C parseInputData(I inputData);
|
||||
|
||||
public abstract void send(O outputData);
|
||||
|
||||
public static <I extends AgentInputData, O extends AgentOutputData, C extends RunningFlowContext> AgentInteractionAdapter<I, O, C> getInstance() {
|
||||
@SuppressWarnings("unchecked")
|
||||
AgentInteractionAdapter<I, O, C> instance = (AgentInteractionAdapter<I, O, C>) INSTANCE;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static <I extends AgentInputData, O extends AgentOutputData, C extends RunningFlowContext> void setInstance(AgentInteractionAdapter<I, O, C> instance) {
|
||||
INSTANCE = instance;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import lombok.EqualsAndHashCode;
|
||||
@Data
|
||||
public abstract class AgentOutputData extends InteractionData{
|
||||
|
||||
private int code;
|
||||
protected int code;
|
||||
|
||||
public static class StatusCode {
|
||||
public static final int SUCCESS = 1;
|
||||
|
||||
@@ -17,8 +17,11 @@ public class AgentRunningFlow<C extends RunningFlowContext> {
|
||||
for (MetaModule metaModule : moduleList) {
|
||||
metaModule.getInstance().execute(interactionContext);
|
||||
}
|
||||
interactionContext.setOk(1);
|
||||
}catch (Exception e){
|
||||
GlobalExceptionHandler.INSTANCE.handle(e);
|
||||
interactionContext.setOk(0);
|
||||
interactionContext.setErrMsg(e.getMessage());
|
||||
}
|
||||
return interactionContext;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public interface ActivateModel {
|
||||
|
||||
AgentConfigManager AGENT_CONFIG_MANAGER = AgentConfigManager.INSTANCE;
|
||||
|
||||
@Init
|
||||
@Init(order = -1)
|
||||
default void modelSettings() {
|
||||
Model model = new Model();
|
||||
ModelConfig modelConfig = AgentConfigManager.INSTANCE.loadModelConfig(modelKey());
|
||||
|
||||
@@ -2,9 +2,11 @@ package work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts;
|
||||
|
||||
import work.slhaf.partner.api.agent.runtime.interaction.flow.entity.RunningFlowContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 流程执行模块基类
|
||||
*/
|
||||
public abstract class AgentRunningModule extends Module {
|
||||
public abstract void execute(RunningFlowContext context);
|
||||
public abstract class AgentRunningModule<C extends RunningFlowContext> extends Module {
|
||||
public abstract void execute(C context) throws IOException, ClassNotFoundException;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package work.slhaf.partner.api.agent.runtime.interaction.flow.abstracts;
|
||||
|
||||
/**
|
||||
* 流程子模块基类
|
||||
*
|
||||
* @param <I> 输入类型
|
||||
* @param <O> 输出类型
|
||||
*/
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package work.slhaf.partner.api.agent.runtime.interaction.flow.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import work.slhaf.partner.api.common.entity.PersistableObject;
|
||||
|
||||
/**
|
||||
* 流程上下文
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public abstract class RunningFlowContext {
|
||||
|
||||
public abstract class RunningFlowContext extends PersistableObject {
|
||||
protected int ok;
|
||||
protected String errMsg;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user