mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-17 23:41:46 +00:00
Feature: AgentExecutor execution time limit (#2399)
`AgentExecutor` already has support for limiting the number of iterations. But the amount of time taken for each iteration can vary quite a bit, so it is difficult to place limits on the execution time. This PR adds a new field `max_execution_time` to the `AgentExecutor` model. When called asynchronously, the agent loop is wrapped in an `asyncio.timeout()` context which triggers the early stopping response if the time limit is reached. When called synchronously, the agent loop checks for both the max_iteration limit and the time limit after each iteration. When used asynchronously `max_execution_time` gives really tight control over the max time for an execution chain. When used synchronously, the chain can unfortunately exceed max_execution_time, but it still gives more control than trying to estimate the number of max_iterations needed to cap the execution time. --------- Co-authored-by: Zachary Jones <zjones@zetaglobal.com>
This commit is contained in:
@@ -70,10 +70,16 @@ def test_agent_bad_action() -> None:
|
||||
|
||||
|
||||
def test_agent_stopped_early() -> None:
|
||||
"""Test react chain when bad action given."""
|
||||
"""Test react chain when max iterations or max execution time is exceeded."""
|
||||
# iteration limit
|
||||
agent = _get_agent(max_iterations=0)
|
||||
output = agent.run("when was langchain made")
|
||||
assert output == "Agent stopped due to max iterations."
|
||||
assert output == "Agent stopped due to iteration limit or time limit."
|
||||
|
||||
# execution time limit
|
||||
agent = _get_agent(max_execution_time=0.0)
|
||||
output = agent.run("when was langchain made")
|
||||
assert output == "Agent stopped due to iteration limit or time limit."
|
||||
|
||||
|
||||
def test_agent_with_callbacks_global() -> None:
|
||||
|
Reference in New Issue
Block a user