langchain
langchain copied to clipboard
Custom agent tutorial doesnt handle replacing SerpAPI with google search tool
Start with the following tutorial: https://python.langchain.com/en/latest/modules/agents/agents/custom_llm_agent.html
But instead of using SerpAPI, use the google search tool:
from langchain.agents import load_tools
tools = load_tools(["google-search"])
The step that creates the CustomPromptTemplate will encounter a validation error:
ValidationError Traceback (most recent call last)
Cell In[36], line 24
21 kwargs["tool_names"] = ", ".join([tool.name for tool in self.tools])
22 return self.template.format(**kwargs)
---> 24 prompt = CustomPromptTemplate(
25 template=template,
26 tools=tools,
27 # This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically
28 # This includes the `intermediate_steps` variable because that is needed
29 input_variables=["input", "intermediate_steps"]
30 )
File [/site-packages/pydantic/main.py:341]/.venv-jupyter/lib/python3.10/site-packages/pydantic/main.py:341), in pydantic.main.BaseModel.__init__()
ValidationError: 1 validation error for CustomPromptTemplate
tools -> 0
Tool.__init__() missing 1 required positional argument: 'func' (type=type_error)
The problem appears to be that the result of calling load_tools(["google-search"])
is a BaseTool and not a Tool and doesn't have a func
. This can be fixed by modifying the CustomPromptTemplate to use BaseTool instead of Tool.
from langchain.tools import BaseTool
class CustomPromptTemplate(StringPromptTemplate):
# The template to use
template: str
# The list of tools available
tools: List[BaseTool]
def format(self, **kwargs) -> str:
<SNIP>
However I am not sure if this is the correct fix, or if the problem is that load_tools should create a Tool
instead of a BaseTool
. i.e. is this a doc issue or a product issue?
I'm having the same dilemma/issue as well :)
To substitute SerpAPI with GoogleSearch we also need to ensure the correct python classes are loaded.
So, instead of using the load_tools()
function like we all initially tried (and as paulbatum helpfully demonstrated), let's try building a list of tools in a different way.
The following code works for me.
from langchain import OpenAI, GoogleSearchAPIWrapper, LLMChain
import os
os.environ["GOOGLE_CSE_ID"] = "6832f56b74e622d9f"
os.environ["GOOGLE_API_KEY"] = "RfcvhiFuS4W4nbRpAWn7KdHZfdMA53mzbdS-lDC"
search = GoogleSearchAPIWrapper()
tools = [
Tool(
name="Search",
func=search.run,
description="useful for when you need to answer questions about current events",
)
]
Here's what's different:
- Add
GoogleSearchAPIWrapper
to the python import statement for the langchain module. - Add your Google Search API credentials to the script. In the above example environmental variables are used to store the credentials because the class looks for them there. Alternatively, you could directly pass your credentials as arguments to
GoogleSearchAPIWrapper()
. - Create an instance of the
GoogleSearchAPIWrapper
class and store it in the variablesearch
. - Build your list of tools, which is literally a list of
Tool
objects. Our instance ofSearchAPIWrapper
is passed to thefunc
parameter ofTool
, but only in part. By that I mean just therun
method of thesearch
object is passed as an argument toTool
.
How about for human tool? I don't see a wrapper for that
@tlazaro11 Sorry, I don't understand your question. Could you please provide an excerpt of the code and error message or part of documentation you're having difficulty with?
@tlazaro11 I'm not clear on what you're trying to do but you can also try the fix I outlined of changing your CustomPromptTemplate to use BaseTool
Hi, @paulbatum! I'm Dosu, and I'm here to help the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.
From what I understand, the issue is related to the Custom agent tutorial not handling the replacement of SerpAPI with the Google search tool correctly. There have been some suggestions and discussions in the comments about potential fixes, including modifying the CustomPromptTemplate to use BaseTool instead of Tool. It seems like there was a resolution to this issue by making that modification.
Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.
Thank you for your understanding and contribution to the LangChain project! Let us know if you have any further questions or concerns.
Hi. I am getting the same error.
my tool is as following:
def listBeneficiaries():
"""Tool to list all the beneficiaries."""
response = requests.get('https://test-flentas.free.beeceptor.com/beneficiaries')
return json.dumps(response.json())
class ListBeneficiaries(BaseTool):
name = "list_beneficiaries"
description = """
use this tool when you want list all the beneficiaries of the customer. output of this tool is json string
"""
# args_schema: Type[BaseModel] = StockPercentChangeInput
def _run(self,):
response = listBeneficiaries()
return response
def _arun(self, ticker: str):
raise NotImplementedError("This tool does not support async")
tools = [ListBeneficiaries()]
then I am writing custom agent prompt and all.
After that I am creating the PromptTemplate:
prompt = CustomPromptTemplate(
template=template,
tools_getter=tools,
# This omits the `agent_scratchpad`, `tools`, and `tool_names` variables because those are generated dynamically
# This includes the `intermediate_steps` variable because that is needed
input_variables=["input", "intermediate_steps"],
)
Error I am getting after running this code:
ValidationError: 1 validation error for CustomPromptTemplate tools_getter -> 0 __init__() missing 1 required positional argument: 'func' (type=type_error)
Hi,
I got exactly the same problem as @sudamerushabh .
Appreciate any help!
refer https://towardsdatascience.com/building-a-math-application-with-langchain-agents-23919d09a4d3