[agent_executor] convenience func: lookup tool by name (#2001)

A quick convenience function to lookup a tool by name

Co-authored-by: blob42 <spike@w530>
This commit is contained in:
blob42 2023-03-28 06:10:34 +00:00 committed by GitHub
parent 4be2f9d75a
commit b7f392fdd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -363,6 +363,10 @@ class AgentExecutor(Chain, BaseModel):
else: else:
return self.agent.return_values return self.agent.return_values
def lookup_tool(self, name: str) -> BaseTool:
"""Lookup tool by name."""
return {tool.name: tool for tool in self.tools}[name]
def _should_continue(self, iterations: int) -> bool: def _should_continue(self, iterations: int) -> bool:
if self.max_iterations is None: if self.max_iterations is None:
return True return True

View File

@ -289,3 +289,25 @@ def test_agent_with_new_prefix_suffix() -> None:
prompt_str = agent.agent.llm_chain.prompt.template prompt_str = agent.agent.llm_chain.prompt.template
assert prompt_str.startswith(prefix), "Prompt does not start with prefix" assert prompt_str.startswith(prefix), "Prompt does not start with prefix"
assert prompt_str.endswith(suffix), "Prompt does not end with suffix" assert prompt_str.endswith(suffix), "Prompt does not end with suffix"
def test_agent_lookup_tool() -> None:
"""Test agent lookup tool."""
fake_llm = FakeListLLM(
responses=["FooBarBaz\nAction: Search\nAction Input: misalignment"]
)
tools = [
Tool(
name="Search",
func=lambda x: x,
description="Useful for searching",
return_direct=True,
),
]
agent = initialize_agent(
tools=tools,
llm=fake_llm,
agent="zero-shot-react-description",
)
assert agent.lookup_tool("Search") == tools[0]