Skip to content

Commit

Permalink
Add property to conditionally set the LangGraph recursion limit
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhulen committed Dec 14, 2024
1 parent 31f3b6a commit 7ae3b23
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ public class LangGraphReactAgentWorkflow : AgentWorkflowBase
/// <inheritdoc/>
[JsonIgnore]
public override string Type => AgentWorkflowTypes.LangGraphReactAgent;

/// <summary>
/// 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.
/// </summary>
[JsonPropertyName("graph_recursion_limit")]
public int? GraphRecursionLimit { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 7ae3b23

Please sign in to comment.