When using the agent to call the tool, a RuntimeError occurs: generator ignored GeneratorExit #22346
Replies: 2 comments 1 reply
-
To resolve the In the provided code, the Here is an example of how you can modify the def __iter__(self: "AgentExecutorIterator") -> Iterator[AddableDict]:
logger.debug("Initialising AgentExecutorIterator")
self.reset()
callback_manager = CallbackManager.configure(
self.callbacks,
self.agent_executor.callbacks,
self.agent_executor.verbose,
self.tags,
self.agent_executor.tags,
self.metadata,
self.agent_executor.metadata,
)
run_manager = callback_manager.on_chain_start(
dumpd(self.agent_executor),
self.inputs,
self.run_id,
name=self.run_name,
)
try:
while self.agent_executor._should_continue(
self.iterations, self.time_elapsed
):
next_step_seq: NextStepOutput = []
for chunk in self.agent_executor._iter_next_step(
self.name_to_tool_map,
self.color_mapping,
self.inputs,
self.intermediate_steps,
run_manager,
):
next_step_seq.append(chunk)
if self.yield_actions:
if isinstance(chunk, AgentAction):
yield AddableDict(actions=[chunk], messages=chunk.messages)
elif isinstance(chunk, AgentStep):
yield AddableDict(steps=[chunk], messages=chunk.messages)
next_step = self.agent_executor._consume_next_step(next_step_seq)
self.update_iterations()
output = self._process_next_step_output(next_step, run_manager)
is_final = "intermediate_step" not in output
if not self.yield_actions or is_final:
yield output
if is_final:
return
except GeneratorExit:
logger.debug("GeneratorExit caught, cleaning up")
run_manager.on_chain_error(GeneratorExit("Generator closed"))
raise
except BaseException as e:
run_manager.on_chain_error(e)
raise
yield self._stop(run_manager)
async def __aiter__(self) -> AsyncIterator[AddableDict]:
logger.debug("Initialising AgentExecutorIterator (async)")
self.reset()
callback_manager = AsyncCallbackManager.configure(
self.callbacks,
self.agent_executor.callbacks,
self.agent_executor.verbose,
self.tags,
self.agent_executor.tags,
self.metadata,
self.agent_executor.metadata,
)
run_manager = await callback_manager.on_chain_start(
dumpd(self.agent_executor),
self.inputs,
self.run_id,
name=self.run_name,
)
try:
async with asyncio_timeout(self.agent_executor.max_execution_time):
while self.agent_executor._should_continue(
self.iterations, self.time_elapsed
):
next_step_seq: NextStepOutput = []
async for chunk in self.agent_executor._aiter_next_step(
self.name_to_tool_map,
self.color_mapping,
self.inputs,
self.intermediate_steps,
run_manager,
):
next_step_seq.append(chunk)
if self.yield_actions:
if isinstance(chunk, AgentAction):
yield AddableDict(actions=[chunk], messages=chunk.messages)
elif isinstance(chunk, AgentStep):
yield AddableDict(steps=[chunk], messages=chunk.messages)
next_step = self.agent_executor._consume_next_step(next_step_seq)
self.update_iterations()
output = await self._aprocess_next_step_output(next_step, run_manager)
is_final = "intermediate_step" not in output
if not self.yield_actions or is_final:
yield output
if is_final:
return
except GeneratorExit:
logger.debug("GeneratorExit caught, cleaning up")
await run_manager.on_chain_error(GeneratorExit("Generator closed"))
raise
except (TimeoutError, asyncio.TimeoutError):
yield await self._astop(run_manager)
return
except BaseException as e:
await run_manager.on_chain_error(e)
raise
yield await self._astop(run_manager) By adding the |
Beta Was this translation helpful? Give feedback.
-
@wangyaoyong-wyy 似乎这个报错很常见,类似的代码使用ChatGPT 就不会报错,不知道具体模型哪里的回复有问题了 |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
When I input questions for Q&A, The agent did not execute the output correctly after calling the tool
System Info
platform:windows11
python:3.11
langchian:0.2.1
Beta Was this translation helpful? Give feedback.
All reactions