mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-23 07:09:31 +00:00
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.
This commit is contained in:
parent
92ef77da35
commit
a8bbfb2da3
@ -659,6 +659,9 @@ s
|
||||
as an argument, and the result of that function will be passed to the agent
|
||||
as an observation.
|
||||
"""
|
||||
trim_intermediate_steps: Union[
|
||||
int, Callable[[List[Tuple[AgentAction, str]]], List[Tuple[AgentAction, str]]]
|
||||
] = -1
|
||||
|
||||
@classmethod
|
||||
def from_agent_and_tools(
|
||||
@ -788,6 +791,8 @@ s
|
||||
Override this to take control of how the agent makes and acts on choices.
|
||||
"""
|
||||
try:
|
||||
intermediate_steps = self._prepare_intermediate_steps(intermediate_steps)
|
||||
|
||||
# Call the LLM to see what to do.
|
||||
output = self.agent.plan(
|
||||
intermediate_steps,
|
||||
@ -879,6 +884,8 @@ s
|
||||
Override this to take control of how the agent makes and acts on choices.
|
||||
"""
|
||||
try:
|
||||
intermediate_steps = self._prepare_intermediate_steps(intermediate_steps)
|
||||
|
||||
# Call the LLM to see what to do.
|
||||
output = await self.agent.aplan(
|
||||
intermediate_steps,
|
||||
@ -1088,3 +1095,16 @@ s
|
||||
"",
|
||||
)
|
||||
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…
Reference in New Issue
Block a user