From f3d29ba2eb90f115860515ce345a1124ed28e019 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Fri, 21 Jun 2024 16:43:32 +0800 Subject: [PATCH] refactor: refactor Agent.java --- .../main/java/com/agentsflex/agent/Agent.java | 25 +++++-- .../com/agentsflex/agent/DefaultAgent.java | 2 +- .../java/com/agentsflex/agent/LLMAgent.java | 2 +- .../java/com/agentsflex/agent/Output.java | 4 ++ .../java/com/agentsflex/agent/OutputKey.java | 65 +++++++++++++++++++ .../java/com/agentsflex/agent/Parameter.java | 9 +++ .../com/agentsflex/chain/node/AgentNode.java | 10 +-- 7 files changed, 104 insertions(+), 13 deletions(-) create mode 100644 agents-flex-core/src/main/java/com/agentsflex/agent/OutputKey.java diff --git a/agents-flex-core/src/main/java/com/agentsflex/agent/Agent.java b/agents-flex-core/src/main/java/com/agentsflex/agent/Agent.java index b97556d..b972269 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/agent/Agent.java +++ b/agents-flex-core/src/main/java/com/agentsflex/agent/Agent.java @@ -28,7 +28,8 @@ public abstract class Agent { protected String name; protected String description; private ContextMemory memory; - private List outputKeys; + private List inputParameters; + private List outputKeys; public Agent() { this.id = UUID.randomUUID().toString(); @@ -76,16 +77,21 @@ public abstract class Agent { } public List getInputParameters() { - List parameters = defineInputParameter(); - return parameters == null ? Collections.emptyList() : parameters; + if (this.inputParameters == null) { + this.inputParameters = defineInputParameter(); + if (this.inputParameters == null) { + this.inputParameters = Collections.emptyList(); + } + } + return this.inputParameters; } - public List getOutputKeys() { + public List getOutputKeys() { return outputKeys; } - public void setOutputKeys(List outputKeys) { + public void setOutputKeys(List outputKeys) { this.outputKeys = outputKeys; } @@ -94,7 +100,10 @@ public abstract class Agent { this.outputKeys = new ArrayList<>(); } - this.outputKeys.addAll(Arrays.asList(keys)); + for (String key : keys) { + this.outputKeys.add(new OutputKey(key)); + } + return this; } @@ -102,7 +111,9 @@ public abstract class Agent { return execute(variables, null); } - protected abstract List defineInputParameter(); + protected List defineInputParameter() { + return Collections.emptyList(); + } public abstract Output execute(Map variables, Chain chain); diff --git a/agents-flex-core/src/main/java/com/agentsflex/agent/DefaultAgent.java b/agents-flex-core/src/main/java/com/agentsflex/agent/DefaultAgent.java index d365b6f..c0d8395 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/agent/DefaultAgent.java +++ b/agents-flex-core/src/main/java/com/agentsflex/agent/DefaultAgent.java @@ -53,7 +53,7 @@ public abstract class DefaultAgent extends Agent { String key = variables.keySet().iterator().next(); value = variables.get(key); } - List outputKeys = getOutputKeys(); + List outputKeys = getOutputKeys(); if (outputKeys != null && outputKeys.size() == 1) { return Output.of(outputKeys.get(0), execute(value, 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 17e40b1..31296d0 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 @@ -119,7 +119,7 @@ public class LLMAgent extends Agent { protected Output onMessage(AiMessage aiMessage) { - List outputKeys = getOutputKeys(); + List outputKeys = getOutputKeys(); if (outputKeys != null && outputKeys.size() == 1) { return Output.of(outputKeys.get(0), aiMessage.getContent()); } diff --git a/agents-flex-core/src/main/java/com/agentsflex/agent/Output.java b/agents-flex-core/src/main/java/com/agentsflex/agent/Output.java index 5d67bdc..831f20a 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/agent/Output.java +++ b/agents-flex-core/src/main/java/com/agentsflex/agent/Output.java @@ -47,6 +47,10 @@ public class Output extends HashMap { return this; } + public static Output of(OutputKey key, Object value) { + return of(key.getKey(), value); + } + public static Output of(String key, Object value) { Output output = new Output(); output.put(key, value); diff --git a/agents-flex-core/src/main/java/com/agentsflex/agent/OutputKey.java b/agents-flex-core/src/main/java/com/agentsflex/agent/OutputKey.java new file mode 100644 index 0000000..47ac2d6 --- /dev/null +++ b/agents-flex-core/src/main/java/com/agentsflex/agent/OutputKey.java @@ -0,0 +1,65 @@ +/* + * 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.agent; + +public class OutputKey { + private String key; + private String type; + private String description; + + public OutputKey() { + } + + public OutputKey(String key) { + this.key = key; + } + + public OutputKey(String key, String type) { + this.key = key; + this.type = type; + } + + + public OutputKey(String key, String type, String description) { + this.key = key; + this.type = type; + this.description = description; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/agents-flex-core/src/main/java/com/agentsflex/agent/Parameter.java b/agents-flex-core/src/main/java/com/agentsflex/agent/Parameter.java index 3a7024b..3d6c06e 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/agent/Parameter.java +++ b/agents-flex-core/src/main/java/com/agentsflex/agent/Parameter.java @@ -17,6 +17,7 @@ package com.agentsflex.agent; public class Parameter { private String name; + private String description; private String type; private boolean required; private boolean isDefault; @@ -59,6 +60,14 @@ public class Parameter { this.name = name; } + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + public String getType() { return type; } diff --git a/agents-flex-core/src/main/java/com/agentsflex/chain/node/AgentNode.java b/agents-flex-core/src/main/java/com/agentsflex/chain/node/AgentNode.java index 35e0f1d..faee5c0 100644 --- a/agents-flex-core/src/main/java/com/agentsflex/chain/node/AgentNode.java +++ b/agents-flex-core/src/main/java/com/agentsflex/chain/node/AgentNode.java @@ -17,6 +17,7 @@ package com.agentsflex.chain.node; import com.agentsflex.agent.Agent; import com.agentsflex.agent.Output; +import com.agentsflex.agent.OutputKey; import com.agentsflex.agent.Parameter; import com.agentsflex.chain.Chain; @@ -95,16 +96,17 @@ public class AgentNode extends AbstractBaseNode { } Output output = agent.execute(variables, chain); - List outputKeys = agent.getOutputKeys(); + List outputKeys = agent.getOutputKeys(); if (outputKeys == null || outputKeys.isEmpty() || outputMapping == null || outputMapping.isEmpty()) { return output; } Map newResult = new HashMap<>(outputKeys.size()); - for (String outputKey : outputKeys) { - String newKey = outputMapping.getOrDefault(outputKey, outputKey); - newResult.put(newKey, output.get(outputKey)); + for (OutputKey outputKey : outputKeys) { + String oldKey = outputKey.getKey(); + String newKey = outputMapping.getOrDefault(oldKey, oldKey); + newResult.put(newKey, output.get(oldKey)); } return newResult;