diff --git a/agents-flex-core/src/main/java/com/agentsflex/agent/IOAgent.java b/agents-flex-core/src/main/java/com/agentsflex/agent/DefaultAgent.java similarity index 86% rename from agents-flex-core/src/main/java/com/agentsflex/agent/IOAgent.java rename to agents-flex-core/src/main/java/com/agentsflex/agent/DefaultAgent.java index 56c7a07..9195cbb 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/agent/IOAgent.java +++ b/agents-flex-core/src/main/java/com/agentsflex/agent/DefaultAgent.java @@ -21,18 +21,16 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -/** - * input and output agent - */ -public abstract class IOAgent extends Agent { - public IOAgent() { + +public abstract class DefaultAgent extends Agent { + public DefaultAgent() { } - public IOAgent(Object id) { + public DefaultAgent(Object id) { super(id); } - public IOAgent(Object id, String name) { + public DefaultAgent(Object id, String name) { super(id, name); } @@ -58,7 +56,7 @@ public abstract class IOAgent extends Agent { return Output.ofValue(execute(value, chain)); } - public abstract Object execute(Object param, Chain chain); + public abstract Object execute(Object parameter, Chain chain); } diff --git a/agents-flex-core/src/main/java/com/agentsflex/agent/LLMAgent.java b/agents-flex-core/src/main/java/com/agentsflex/agent/LLMAgent.java index 203e9fa..00cb9b5 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/agent/LLMAgent.java +++ b/agents-flex-core/src/main/java/com/agentsflex/agent/LLMAgent.java @@ -97,7 +97,7 @@ public class LLMAgent extends Agent { @Override public Output execute(Map variables, Chain chain) { - SimplePrompt simplePrompt = promptTemplate.format(chain == null ? variables : chain.getMemory().getAll()); + SimplePrompt simplePrompt = promptTemplate.format(variables); AiMessageResponse response = llm.chat(simplePrompt, chatOptions); if (chain != null) { diff --git a/agents-flex-core/src/main/java/com/agentsflex/chain/Chain.java b/agents-flex-core/src/main/java/com/agentsflex/chain/Chain.java index 2aadb70..78d81fd 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/chain/Chain.java +++ b/agents-flex-core/src/main/java/com/agentsflex/chain/Chain.java @@ -16,6 +16,7 @@ package com.agentsflex.chain; import com.agentsflex.agent.Agent; +import com.agentsflex.agent.Output; import com.agentsflex.agent.Parameter; import com.agentsflex.chain.event.OnErrorEvent; import com.agentsflex.chain.event.OnFinishedEvent; @@ -40,7 +41,7 @@ public abstract class Chain implements Serializable { private List children; private ChainStatus status = ChainStatus.READY; - private String errorMessage; + private String message; //理论上是线程安全的,所有有多线程写入的情况,但是只有全部写入完成后才会去通知监听器 private List waitInputParameters = new ArrayList<>(); @@ -230,6 +231,23 @@ public abstract class Chain implements Serializable { return null; } + public void execute(Object variable) { + Map variables = new HashMap<>(1); + variables.put(Output.DEFAULT_VALUE_KEY, variable); + this.execute(variables); + } + + + public T executeForResult(Object variable) { + Map variables = new HashMap<>(1); + variables.put(Output.DEFAULT_VALUE_KEY, variable); + this.execute(variables); + + //noinspection unchecked + return (T) this.getMemory().get(Output.DEFAULT_VALUE_KEY); + } + + public void execute(Map variables) { runInLifeCycle(variables, this::executeInternal); } @@ -306,18 +324,19 @@ public abstract class Chain implements Serializable { } } - public void stopNormal() { + public void stopNormal(String message) { + this.message = message; setStatus(ChainStatus.FINISHED_NORMAL); if (parent != null) { - parent.stopNormal(); + parent.stopNormal(message); } } - public void stopError(String errorMessage) { - this.errorMessage = errorMessage; + public void stopError(String message) { + this.message = message; setStatus(ChainStatus.FINISHED_ABNORMAL); if (parent != null) { - parent.stopError(errorMessage); + parent.stopError(message); } } @@ -328,8 +347,8 @@ public abstract class Chain implements Serializable { } } - public String getErrorMessage() { - return errorMessage; + public String getMessage() { + return message; } @Override diff --git a/agents-flex-core/src/main/java/com/agentsflex/chain/IOChain.java b/agents-flex-core/src/main/java/com/agentsflex/chain/IOChain.java deleted file mode 100644 index ce3c9af..0000000 --- a/agents-flex-core/src/main/java/com/agentsflex/chain/IOChain.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com). - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.agentsflex.chain; - -import com.agentsflex.agent.Output; - -import java.util.HashMap; -import java.util.Map; - -/** - * input and output chain - */ -public class IOChain extends SequentialChain { - - public T execute(Object input) { - Map variables = new HashMap<>(1); - variables.put(Output.DEFAULT_VALUE_KEY, input); - - super.execute(variables); - - //noinspection unchecked - return (T) this.getMemory().get(Output.DEFAULT_VALUE_KEY); - } -} diff --git a/agents-flex-core/src/main/java/com/agentsflex/chain/node/EndNode.java b/agents-flex-core/src/main/java/com/agentsflex/chain/node/EndNode.java index 9fafac8..a71100b 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/chain/node/EndNode.java +++ b/agents-flex-core/src/main/java/com/agentsflex/chain/node/EndNode.java @@ -20,6 +20,24 @@ import com.agentsflex.chain.Chain; import java.util.Map; public class EndNode extends AbstractBaseNode { + private boolean isNormal = true; + private String message; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isNormal() { + return isNormal; + } + + public void setNormal(boolean normal) { + isNormal = normal; + } public EndNode() { this.name = "end"; @@ -27,7 +45,11 @@ public class EndNode extends AbstractBaseNode { @Override public Map execute(Chain chain) { - chain.stopNormal(); + if (isNormal) { + chain.stopNormal(message); + } else { + chain.stopError(message); + } return null; } } diff --git a/agents-flex-core/src/test/java/com/agentsflex/core/test/io/Agent1.java b/agents-flex-core/src/test/java/com/agentsflex/core/test/io/Agent1.java new file mode 100644 index 0000000..0278241 --- /dev/null +++ b/agents-flex-core/src/test/java/com/agentsflex/core/test/io/Agent1.java @@ -0,0 +1,15 @@ +package com.agentsflex.core.test.io; + +import com.agentsflex.agent.DefaultAgent; +import com.agentsflex.chain.Chain; + +public class Agent1 extends DefaultAgent { + public Agent1(Object id) { + super(id); + } + + @Override + public Object execute(Object parameter, Chain chain) { + return "001:" + parameter; + } +} diff --git a/agents-flex-core/src/test/java/com/agentsflex/core/test/io/Agent2.java b/agents-flex-core/src/test/java/com/agentsflex/core/test/io/Agent2.java new file mode 100644 index 0000000..2b1d08b --- /dev/null +++ b/agents-flex-core/src/test/java/com/agentsflex/core/test/io/Agent2.java @@ -0,0 +1,16 @@ +package com.agentsflex.core.test.io; + +import com.agentsflex.agent.DefaultAgent; +import com.agentsflex.chain.Chain; + +public class Agent2 extends DefaultAgent { + + public Agent2(Object id) { + super(id); + } + + @Override + public Object execute(Object parameter, Chain chain) { + return "002:" + parameter; + } +} diff --git a/agents-flex-core/src/test/java/com/agentsflex/core/test/io/AgentChainTest.java b/agents-flex-core/src/test/java/com/agentsflex/core/test/io/AgentChainTest.java new file mode 100644 index 0000000..b1e48b8 --- /dev/null +++ b/agents-flex-core/src/test/java/com/agentsflex/core/test/io/AgentChainTest.java @@ -0,0 +1,29 @@ +package com.agentsflex.core.test.io; + +import com.agentsflex.chain.*; + +public class AgentChainTest { + + public static void main(String[] args) { + + SequentialChain ioChain1 = new SequentialChain(); + ioChain1.addNode(new Agent1("agent1")); + ioChain1.addNode(new Agent2("agent2")); + + SequentialChain ioChain2 = new SequentialChain(); + ioChain2.addNode(new Agent1("agent3")); + ioChain2.addNode(new Agent2("agent4")); + ioChain2.addNode(ioChain1); + + ioChain2.registerEventListener(new ChainEventListener() { + @Override + public void onEvent(ChainEvent event, Chain chain) { + System.out.println(event); + } + }); + + + Object result = ioChain2.executeForResult("your params"); + System.out.println(result); + } +} diff --git a/agents-flex-core/src/test/java/com/agentsflex/core/test/io/IOAgent1.java b/agents-flex-core/src/test/java/com/agentsflex/core/test/io/IOAgent1.java deleted file mode 100644 index 0505106..0000000 --- a/agents-flex-core/src/test/java/com/agentsflex/core/test/io/IOAgent1.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.agentsflex.core.test.io; - -import com.agentsflex.agent.IOAgent; -import com.agentsflex.chain.Chain; - -public class IOAgent1 extends IOAgent { - public IOAgent1(Object id) { - super(id); - } - - @Override - public Object execute(Object param, Chain chain) { - return "001:" + param; - } -} diff --git a/agents-flex-core/src/test/java/com/agentsflex/core/test/io/IOAgent2.java b/agents-flex-core/src/test/java/com/agentsflex/core/test/io/IOAgent2.java deleted file mode 100644 index 64f6d79..0000000 --- a/agents-flex-core/src/test/java/com/agentsflex/core/test/io/IOAgent2.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.agentsflex.core.test.io; - -import com.agentsflex.agent.IOAgent; -import com.agentsflex.chain.Chain; - -public class IOAgent2 extends IOAgent { - - public IOAgent2(Object id) { - super(id); - } - @Override - public Object execute(Object param, Chain chain) { - return "002:" + param; - } -} diff --git a/agents-flex-core/src/test/java/com/agentsflex/core/test/io/IOChainTest.java b/agents-flex-core/src/test/java/com/agentsflex/core/test/io/IOChainTest.java deleted file mode 100644 index a1ebe0f..0000000 --- a/agents-flex-core/src/test/java/com/agentsflex/core/test/io/IOChainTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.agentsflex.core.test.io; - -import com.agentsflex.chain.Chain; -import com.agentsflex.chain.ChainEvent; -import com.agentsflex.chain.ChainEventListener; -import com.agentsflex.chain.IOChain; - -public class IOChainTest { - - public static void main(String[] args) { - - IOChain ioChain1 = new IOChain(); - ioChain1.addNode(new IOAgent1("agent1")); - ioChain1.addNode(new IOAgent2("agent2")); - - IOChain ioChain2 = new IOChain(); - ioChain2.addNode(new IOAgent1("agent3")); - ioChain2.addNode(new IOAgent2("agent4")); - ioChain2.addNode(ioChain1); - - ioChain2.registerEventListener(new ChainEventListener() { - @Override - public void onEvent(ChainEvent event, Chain chain) { - System.out.println(event); - } - }); - - - Object result = ioChain2.execute("your params"); - System.out.println(result); - } -} diff --git a/changes.md b/changes.md index 647a6ba..f530fba 100644 --- a/changes.md +++ b/changes.md @@ -1,7 +1,6 @@ # Agents-Flex ChangeLog ## v1.0.0-beta.2 -- 新增:新增 IOAgent 以及 IOChain,方便用于单一的输入和输出的场景 - 新增:MilvusVectorStore 用于对 Milvus 向量数据库的支持,感谢 @xgc - 新增:执行链的路由节点新增对 QLExpress 和 Groovy 的规则支持 - 优化:重构让 FunctionMessage 继承 AiMessage @@ -11,5 +10,6 @@ - 优化:优化执行链 Chain 以及 Node 节点,方便更加容易的创建和配置 - 优化:重命名 BaseFunctionMessageParser 为 DefaultFunctionMessageParser - 优化:修改 TextParser 为 JSONObjectParser +- 测试:多场景大量测试 Agent 以及 Chain,已初步具备编排能力 - 文档:https://agentsflex.com 官网上线 - 文档:完善基础文档