langchain icon indicating copy to clipboard operation
langchain copied to clipboard

"Unhashable Type: Tool" when using custom tool

Open aeturneus opened this issue 1 year ago • 4 comments

The following custom tool definition triggers an "TypeError: unhashable type: 'Tool'"

@tool def gender_guesser(query: str) -> str: """Useful for when you need to guess a person's gender based on their first name. Pass only the first name as the query, returns the gender.""" d = gender.Detector() return d.get_gender(str)

llm = ChatOpenAI(temperature=5.0) math_llm = OpenAI(temperature=0.0) tools = load_tools( ["human", "llm-math", gender_guesser], llm=math_llm, )

agent_chain = initialize_agent( tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, )

aeturneus avatar Apr 08 '23 03:04 aeturneus

I'm having the same thing trying load this:

bash_tool = Tool( name="Bash", func=BashProcess().run, description="""A Bash shell. Use this to execute bash commands. Input should be a valid bash command. If you expect output it should be printed out.""", ) print("loading tools...")

tools = load_tools([bash_tool], llm=llm)

kimlage avatar Apr 08 '23 14:04 kimlage

The issue appears to be that load_tools() isn't compatible with custom tools.

It's unclear from the documentation how to use custom tool definitions at the same time as existing tools.

aeturneus avatar Apr 08 '23 16:04 aeturneus

This seemed to work:

tools = load_tools(
    ["human", "llm-math"], 
    llm=math_llm,
)
tools = tools + [gender_guesser]

aeturneus avatar Apr 08 '23 16:04 aeturneus

Initializing an empty tools list first, then adding a custom tool worked for me:

tools = load_tools([], llm=math_llm) tools = tools + [gender_guesser]

samuelbarbosaa avatar Apr 25 '23 18:04 samuelbarbosaa

Hi, @aeturneus. I'm Dosu, and I'm helping 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 you reported is related to using a custom tool definition in the code, which triggers a "TypeError: unhashable type: 'Tool'". It seems that the problem lies in the compatibility of the load_tools() function with custom tools. Both you and kimlage have suggested different workarounds, such as initializing an empty tools list first and then adding the custom tool, or using a different syntax to load the tools.

Before we close this issue, we would like to confirm 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 contribution to the LangChain repository, and please don't hesitate to reach out if you have any further questions or concerns.

Best regards, Dosu

dosubot[bot] avatar Sep 18 '23 16:09 dosubot[bot]

Initializing an empty tools list first, then adding a custom tool worked for me:

tools = load_tools([], llm=math_llm) tools = tools + [gender_guesser]

It helped for me

iagobrandaowt avatar Oct 17 '23 16:10 iagobrandaowt

I used tools = tools + [custom_tool] and had this error 'list' object has no attribute 'is_single_input' on the initialize_agent level Any potential solutions ?

dimaelzeinstudeo avatar Dec 20 '23 14:12 dimaelzeinstudeo

I was using version 0.0.195

iagobrandaowt avatar Dec 20 '23 14:12 iagobrandaowt

@iagobrandaowt thanks for the detail. That did not work Is it maybe because my custom_tool doesn't use any LLM ? tools = load_tools(["tool1","tool2"], llm=llm) tools = tools + [custom_tool]

dimaelzeinstudeo avatar Dec 20 '23 14:12 dimaelzeinstudeo