From 7ae3b23ae357c5c2685d7691cee9c540533c3ac2 Mon Sep 17 00:00:00 2001 From: joelhulen Date: Sat, 14 Dec 2024 11:07:36 -0500 Subject: [PATCH] Add property to conditionally set the LangGraph recursion limit --- .../Agent/AgentWorkflows/LangGraphReactAgentWorkflow.cs | 7 +++++++ .../agents/langchain_knowledge_management_agent.py | 6 +++++- .../agent_workflows/langgraph_react_agent_workflow.py | 6 ++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/dotnet/Common/Models/ResourceProviders/Agent/AgentWorkflows/LangGraphReactAgentWorkflow.cs b/src/dotnet/Common/Models/ResourceProviders/Agent/AgentWorkflows/LangGraphReactAgentWorkflow.cs index 1cd5678df7..c81f1c0488 100644 --- a/src/dotnet/Common/Models/ResourceProviders/Agent/AgentWorkflows/LangGraphReactAgentWorkflow.cs +++ b/src/dotnet/Common/Models/ResourceProviders/Agent/AgentWorkflows/LangGraphReactAgentWorkflow.cs @@ -10,5 +10,12 @@ public class LangGraphReactAgentWorkflow : AgentWorkflowBase /// [JsonIgnore] public override string Type => AgentWorkflowTypes.LangGraphReactAgent; + + /// + /// When using LangGraph, the recursion limit sets the number of supersteps that the graph is allowed + /// to execute before it raises an error. The default value is 25. Set this value to null to use the default. + /// + [JsonPropertyName("graph_recursion_limit")] + public int? GraphRecursionLimit { get; set; } } } diff --git a/src/python/PythonSDK/foundationallm/langchain/agents/langchain_knowledge_management_agent.py b/src/python/PythonSDK/foundationallm/langchain/agents/langchain_knowledge_management_agent.py index 40e6a76107..01163e4f7e 100644 --- a/src/python/PythonSDK/foundationallm/langchain/agents/langchain_knowledge_management_agent.py +++ b/src/python/PythonSDK/foundationallm/langchain/agents/langchain_knowledge_management_agent.py @@ -456,7 +456,11 @@ async def invoke_async(self, request: KnowledgeManagementCompletionRequest) -> C graph = create_react_agent(llm, tools=tools, state_modifier=self.prompt.prefix) messages = self._build_conversation_history_message_list(request.message_history, agent.conversation_history_settings.max_history) messages.append(HumanMessage(content=request.user_prompt)) - response = await graph.ainvoke({'messages': messages}, config={"configurable": {"original_user_prompt": request.user_prompt}}) + + response = await graph.ainvoke( + {'messages': messages}, + config={"configurable": {"original_user_prompt": request.user_prompt, **({"recursion_limit": agent.workflow.graph_recursion_limit} if agent.workflow.graph_recursion_limit is not None else {})}} + ) # TODO: process tool messages with analysis results AIMessage with content='' but has addition_kwargs={'tool_calls';[...]} # Get ContentArtifact items from ToolMessages diff --git a/src/python/PythonSDK/foundationallm/models/agents/agent_workflows/langgraph_react_agent_workflow.py b/src/python/PythonSDK/foundationallm/models/agents/agent_workflows/langgraph_react_agent_workflow.py index e86150ab71..9ed4c9fc90 100644 --- a/src/python/PythonSDK/foundationallm/models/agents/agent_workflows/langgraph_react_agent_workflow.py +++ b/src/python/PythonSDK/foundationallm/models/agents/agent_workflows/langgraph_react_agent_workflow.py @@ -1,4 +1,5 @@ -from typing import Any, Self, Literal +from pydantic import Field +from typing import Any, Self, Optional, Literal from foundationallm.langchain.exceptions import LangChainException from foundationallm.utils import object_utils from .agent_workflow_base import AgentWorkflowBase @@ -7,7 +8,8 @@ class LangGraphReactAgentWorkflow(AgentWorkflowBase): """ The configuration for a LangGraph ReAct agent workflow. """ - type: Literal["langgraph-react-agent-workflow"] = "langgraph-react-agent-workflow" + type: Literal["langgraph-react-agent-workflow"] = "langgraph-react-agent-workflow" + graph_recursion_limit: Optional[int] = Field(None, alias="graph_recursion_limit") @staticmethod def from_object(obj: Any) -> Self: