Problems binding tools with LangChain and LangGraph

#49
by tldevelop - opened

Is there a way to bind tools to this model? I'm creating an agent with this model using LangChain, LangGraph, HugginFaceEndpoint and ChatHuggingFace

for testing I only binded TavilySearchResults from langchain_community.tools.tavily_search, created the system prompt explaining the context and even putting the tools available in there but at the moment of invoking it, it calls a tool that doesn't exists in the tool list passed

this is a key error output from one of my tests:


KeyError Traceback (most recent call last)
Cell In[13], line 1
----> 1 result = assistant.graph.invoke({'messages':[HumanMessage(content=user_says)]},thread)

File ~/anaconda3/envs/test-hf/lib/python3.12/site-packages/langgraph/pregel/init.py:1624, in Pregel.invoke(self, input, config, stream_mode, output_keys, input_keys, interrupt_before, interrupt_after, debug, **kwargs)
1622 else:
1623 chunks = []
-> 1624 for chunk in self.stream(
1625 input,
1626 config,
1627 stream_mode=stream_mode,
1628 output_keys=output_keys,
1629 input_keys=input_keys,
1630 interrupt_before=interrupt_before,
1631 interrupt_after=interrupt_after,
1632 debug=debug,
1633 **kwargs,
1634 ):
1635 if stream_mode == "values":
1636 latest = chunk

File ~/anaconda3/envs/test-hf/lib/python3.12/site-packages/langgraph/pregel/init.py:1110, in Pregel.stream(self, input, config, stream_mode, output_keys, input_keys, interrupt_before, interrupt_after, debug)
1107 del fut, task
1109 # panic on failure or timeout
...
---> 31 result = self.tools[tool['name']].invoke(tool['args'])
32 results.append(ToolMessage(tool_call_id=tool['id'], name=tool['name'], content=str(result)))
33 print("Back to the model!")

KeyError: 'weather'

This is how I'm managing the agent:

class Agent:
def init(self, model, tools, checkpointer, system=""):
self.system = system
graph = StateGraph(AgentState)
graph.add_node("llm", self.call_llm)
graph.add_node("action", self.take_action)
graph.add_conditional_edges("llm", self.exists_action, {True: "action", False: END})
graph.add_edge("action", "llm")
graph.set_entry_point("llm")
self.graph = graph.compile(checkpointer=checkpointer)
self.tools = {tool.name: tool for tool in tools}
self.model = model.bind_tools(tools)

def call_llm(self, state: AgentState):
    messages = state['messages']
    if self.system:
        messages = [SystemMessage(content=self.system)] + messages
    message = self.model.invoke(messages)
    return {'messages': [message]}

def exists_action(self, state: AgentState):
    result = state['messages'][-1]
    return len(result.tool_calls) > 0

def take_action(self, state: AgentState):
    tool_calls = state['messages'][-1].tool_calls
    results = []
    for tool in tool_calls:
        print(f"Calling: {tool}")
        result = self.tools[tool['name']].invoke(tool['args'])
        results.append(ToolMessage(tool_call_id=tool['id'], name=tool['name'], content=str(result)))
    print("Back to the model!")
    return {'messages': results}

Initializing the agent:

assistant = Agent(chat_model, [searchTool], system=system_prompt2, checkpointer=memory)

Even with custom tools using StructuredTool.from_function, hope someone can help me with this

Sign up or log in to comment