Skip to content

The agent engineering platform.

Harrison/fix react stateful (#219)

Harrison Chase056899816669
diff --git a/langchain/agents/agent.py b/langchain/agents/agent.py
index a3aebe2fdb..eda22924c2 100644
--- a/langchain/agents/agent.py
+++ b/langchain/agents/agent.py
@@ -87,6 +87,9 @@ class Agent(Chain, BaseModel, ABC):
"""Create a prompt for this class."""
return cls.prompt
+ def _prepare_for_new_call(self) -> None:
+ pass
+
@classmethod
def from_llm_and_tools(cls, llm: LLM, tools: List[Tool], **kwargs: Any) -> "Agent":
"""Construct an agent from an LLM and tools."""
@@ -119,6 +122,8 @@ class Agent(Chain, BaseModel, ABC):
def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
"""Run text through and get agent response."""
text = inputs[self.input_key]
+ # Do any preparation necessary when receiving a new input.
+ self._prepare_for_new_call()
# Construct a mapping of tool name to tool for easy lookup
name_to_tool_map = {tool.name: tool.func for tool in self.tools}
# Construct the initial string to pass into the LLM. This is made up
diff --git a/langchain/agents/react/base.py b/langchain/agents/react/base.py
index bb5dac044b..a9f1d4cae6 100644
--- a/langchain/agents/react/base.py
+++ b/langchain/agents/react/base.py
@@ -31,6 +31,9 @@ class ReActDocstoreAgent(Agent, BaseModel):
f"Tool names should be Lookup and Search, got {tool_names}"
)
+ def _prepare_for_new_call(self) -> None:
+ self.i = 1
+
def _fix_text(self, text: str) -> str:
return text + f"\nAction {self.i}:"