feat(agents): allow trimming of intermediate steps to last N (#6476)

Added an option to trim intermediate steps to last N steps. This is
especially useful for long-running agents. Users can explicitly specify
N or provide a function that does custom trimming/manipulation on
intermediate steps. I've mimicked the API of the `handle_parsing_errors`
parameter.
pull/6757/head
Nir Gazit 1 year ago committed by GitHub
parent 92ef77da35
commit a8bbfb2da3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -659,6 +659,9 @@ s
as an argument, and the result of that function will be passed to the agent as an argument, and the result of that function will be passed to the agent
as an observation. as an observation.
""" """
trim_intermediate_steps: Union[
int, Callable[[List[Tuple[AgentAction, str]]], List[Tuple[AgentAction, str]]]
] = -1
@classmethod @classmethod
def from_agent_and_tools( def from_agent_and_tools(
@ -788,6 +791,8 @@ s
Override this to take control of how the agent makes and acts on choices. Override this to take control of how the agent makes and acts on choices.
""" """
try: try:
intermediate_steps = self._prepare_intermediate_steps(intermediate_steps)
# Call the LLM to see what to do. # Call the LLM to see what to do.
output = self.agent.plan( output = self.agent.plan(
intermediate_steps, intermediate_steps,
@ -879,6 +884,8 @@ s
Override this to take control of how the agent makes and acts on choices. Override this to take control of how the agent makes and acts on choices.
""" """
try: try:
intermediate_steps = self._prepare_intermediate_steps(intermediate_steps)
# Call the LLM to see what to do. # Call the LLM to see what to do.
output = await self.agent.aplan( output = await self.agent.aplan(
intermediate_steps, intermediate_steps,
@ -1088,3 +1095,16 @@ s
"", "",
) )
return None return None
def _prepare_intermediate_steps(
self, intermediate_steps: List[Tuple[AgentAction, str]]
) -> List[Tuple[AgentAction, str]]:
if (
isinstance(self.trim_intermediate_steps, int)
and self.trim_intermediate_steps > 0
):
return intermediate_steps[-self.trim_intermediate_steps :]
elif callable(self.trim_intermediate_steps):
return self.trim_intermediate_steps(intermediate_steps)
else:
return intermediate_steps

Loading…
Cancel
Save