-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(studio): support agent node #2175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/node/AgentNode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* | ||
* Copyright 2024-2025 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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.alibaba.cloud.ai.graph.node; | ||
|
||
import com.alibaba.cloud.ai.graph.OverAllState; | ||
import com.alibaba.cloud.ai.graph.action.NodeAction; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.ai.chat.client.ChatClient; | ||
import org.springframework.ai.chat.model.ToolContext; | ||
import org.springframework.ai.chat.prompt.PromptTemplate; | ||
import org.springframework.ai.tool.ToolCallback; | ||
import org.springframework.ai.tool.definition.ToolDefinition; | ||
import org.springframework.ai.tool.metadata.ToolMetadata; | ||
import org.springframework.lang.Nullable; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
// Agent节点 | ||
public class AgentNode implements NodeAction { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AgentNode.class); | ||
|
||
private final ChatClient chatClient; | ||
|
||
private final ToolCallback[] toolCallbacks; | ||
|
||
private final Strategy strategy; | ||
|
||
// Prompt内可以有变量,格式为{varName},将在正式调用Client前替换占位变量 | ||
private final String systemPrompt; | ||
|
||
private final String userPrompt; | ||
|
||
private final Integer maxIterations; | ||
|
||
private final String outputKey; | ||
|
||
public enum Strategy { | ||
|
||
REACT, TOOL_CALLING | ||
|
||
} | ||
|
||
public AgentNode(ChatClient chatClient, ToolCallback[] toolCallbacks, Strategy strategy, String systemPrompt, | ||
String userPrompt, Integer maxIterations, String outputKey) { | ||
this.chatClient = chatClient; | ||
this.strategy = strategy == null ? Strategy.REACT : strategy; | ||
this.systemPrompt = systemPrompt == null ? "" : systemPrompt; | ||
this.userPrompt = userPrompt == null ? "" : userPrompt; | ||
this.maxIterations = maxIterations == null ? 1 : maxIterations; | ||
this.outputKey = outputKey == null ? "agent_output" : outputKey; | ||
if (this.chatClient == null) { | ||
throw new IllegalArgumentException("ChatClient is required"); | ||
} | ||
this.toolCallbacks = Arrays.stream(toolCallbacks).map(toolCallback -> { | ||
// ToolCalling策略调用完工具后直接返回,需要包装一层使得returnDirect为true | ||
if (this.strategy == Strategy.TOOL_CALLING && !toolCallback.getToolMetadata().returnDirect()) { | ||
final ToolMetadata toolMetadata = ToolMetadata.builder().returnDirect(true).build(); | ||
return new ToolCallback() { | ||
@Override | ||
public ToolDefinition getToolDefinition() { | ||
return toolCallback.getToolDefinition(); | ||
} | ||
|
||
@Override | ||
public ToolMetadata getToolMetadata() { | ||
// returnDirect为true的ToolMetadata | ||
return toolMetadata; | ||
} | ||
|
||
@Override | ||
public String call(String toolInput) { | ||
return toolCallback.call(toolInput); | ||
} | ||
|
||
@Override | ||
public String call(String toolInput, @Nullable ToolContext tooContext) { | ||
return toolCallback.call(toolInput, tooContext); | ||
} | ||
|
||
}; | ||
} | ||
else { | ||
return toolCallback; | ||
} | ||
}).toArray(ToolCallback[]::new); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> apply(OverAllState state) throws Exception { | ||
String userPrompt = new PromptTemplate(this.userPrompt).render(state.data()); | ||
String systemPrompt = new PromptTemplate(this.systemPrompt).render(state.data()); | ||
String output = switch (this.strategy) { | ||
case TOOL_CALLING, REACT -> { | ||
int count = this.maxIterations; | ||
// 重试机制 | ||
while (count-- > 0) { | ||
try { | ||
String content = this.chatClient.prompt(systemPrompt) | ||
.toolCallbacks(this.toolCallbacks) | ||
.user(userPrompt) | ||
.call() | ||
.content(); | ||
if (content != null) { | ||
logger.warn("ChatClient Call Return Null..."); | ||
yield content; | ||
VLSMB marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
catch (Exception e) { | ||
logger.warn("ChatClient Call Fail: {}", e.getMessage()); | ||
} | ||
} | ||
yield null; | ||
} | ||
}; | ||
return Map.of(this.outputKey, output == null ? "" : output); | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private ChatClient chatClient; | ||
|
||
private ToolCallback[] toolCallbacks; | ||
|
||
private Strategy strategy; | ||
|
||
private String systemPrompt; | ||
|
||
private String userPrompt; | ||
|
||
private Integer maxIterations; | ||
|
||
private String outputKey; | ||
|
||
public Builder chatClient(ChatClient chatClient) { | ||
this.chatClient = chatClient; | ||
return this; | ||
} | ||
|
||
public Builder toolCallbacks(ToolCallback[] toolCallbacks) { | ||
this.toolCallbacks = toolCallbacks; | ||
return this; | ||
} | ||
|
||
public Builder toolCallBacks(List<ToolCallback> toolCallbacks) { | ||
this.toolCallbacks = toolCallbacks.toArray(ToolCallback[]::new); | ||
return this; | ||
} | ||
|
||
public Builder strategy(Strategy strategy) { | ||
this.strategy = strategy; | ||
return this; | ||
} | ||
|
||
public Builder systemPrompt(String systemPrompt) { | ||
this.systemPrompt = systemPrompt; | ||
return this; | ||
} | ||
|
||
public Builder userPrompt(String userPrompt) { | ||
this.userPrompt = userPrompt; | ||
return this; | ||
} | ||
|
||
public Builder maxIterations(Integer maxIterations) { | ||
this.maxIterations = maxIterations; | ||
return this; | ||
} | ||
|
||
public Builder outputKey(String outputKey) { | ||
this.outputKey = outputKey; | ||
return this; | ||
} | ||
|
||
public AgentNode build() { | ||
return new AgentNode(chatClient, toolCallbacks, strategy, systemPrompt, userPrompt, maxIterations, | ||
outputKey); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...va/com/alibaba/cloud/ai/studio/admin/generator/model/workflow/nodedata/AgentNodeData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2024-2025 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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.alibaba.cloud.ai.studio.admin.generator.model.workflow.nodedata; | ||
|
||
import com.alibaba.cloud.ai.studio.admin.generator.model.Variable; | ||
import com.alibaba.cloud.ai.studio.admin.generator.model.VariableType; | ||
import com.alibaba.cloud.ai.studio.admin.generator.model.workflow.NodeData; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class AgentNodeData extends NodeData { | ||
|
||
public static Variable getDefaultOutputSchema() { | ||
return new Variable("text", VariableType.STRING.value()); | ||
} | ||
|
||
private Map<String, Object> agentParameterMap; | ||
|
||
private String agentStrategyName; | ||
|
||
private String instructionPrompt; | ||
|
||
private String queryPrompt; | ||
|
||
private List<ToolData> toolList; | ||
|
||
private Integer maxIterations; | ||
|
||
public record ToolData(Map<String, Object> parameters, String toolName, String toolDescription) { | ||
} | ||
|
||
public Map<String, Object> getAgentParameterMap() { | ||
return agentParameterMap; | ||
} | ||
|
||
public void setAgentParameterMap(Map<String, Object> agentParameterMap) { | ||
this.agentParameterMap = agentParameterMap; | ||
} | ||
|
||
public String getAgentStrategyName() { | ||
return agentStrategyName; | ||
} | ||
|
||
public void setAgentStrategyName(String agentStrategyName) { | ||
// todo: 支持更多的策略,如MCP,多轮对话 | ||
this.agentStrategyName = switch (agentStrategyName) { | ||
case "function_calling" -> "TOOL_CALLING"; | ||
default -> "REACT"; | ||
}; | ||
} | ||
|
||
public String getInstructionPrompt() { | ||
return instructionPrompt; | ||
} | ||
|
||
public void setInstructionPrompt(String instructionPrompt) { | ||
this.instructionPrompt = instructionPrompt; | ||
} | ||
|
||
public String getQueryPrompt() { | ||
return queryPrompt; | ||
} | ||
|
||
public void setQueryPrompt(String queryPrompt) { | ||
this.queryPrompt = queryPrompt; | ||
} | ||
|
||
public List<ToolData> getToolList() { | ||
return toolList; | ||
} | ||
|
||
public void setToolList(List<ToolData> toolList) { | ||
this.toolList = toolList; | ||
} | ||
|
||
public Integer getMaxIterations() { | ||
return maxIterations; | ||
} | ||
|
||
public void setMaxIterations(Integer maxIterations) { | ||
this.maxIterations = maxIterations; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.