francesco
francesco

Reputation: 41

FakeListLLM in create_react_agent doesn't allow to use tools

I am trying to implement a fake LLM to use in create_react_agent. The FakeLLM is needed for testing purposes. I am struggling to use tools with the setup I've just described.

This is my code:

protein_info_retrieval.py

from langgraph.prebuilt import create_react_agent
from langgraph.checkpoint.sqlite import SqliteSaver
from langchain_google_vertexai import ChatVertexAI
from langchain_core.tools import tool
from langchain.tools import Tool

@tool
def uniprot_search(query: str) -\> dict:
    """Searches the query on UNIPROT and returns the list of retrieved records"""
    return {"database":"uniprot", "id": "P12345","name":"TestProtein","sequence":"MKTWITNGG","function":"Unknown"}

@tool
def ncbi_search(query: str) -\> dict:
    """Searches the query on NCBI and returns the list of retrieved records"""
    return {"database":"ncbi", "id": "P12345","name":"TestProtein","sequence":"MKTWITNGG","function":"Unknown"}

def create_protein_info_retrieval_agent(llm=None) -\> callable:
    message = "You are a helpful assistant. Respond only in Spanish."

    _llm = llm if llm is not None else ChatVertexAI(model="gemini-pro")
    
    _toolkit = [uniprot_search, ncbi_search]
    
    agent_executor  = create_react_agent(model=_llm, 
                                        tools=_toolkit, 
                                        state_modifier=message, 
                                        debug=True)
    return agent_executor

mocked_objects.py

from typing import Optional, List, Any
from langchain_community.llms import FakeListLLM
from langchain.tools import tool
from langchain_core.callbacks import CallbackManagerForLLMRun
from langchain_core.messages import AIMessage

class MockFakeListLLM(FakeListLLM):

    def bind_tools(self, tools):
        # This is a mock implementation
        return self    

def fake_protein_info_llm():
\# Step 1: Define the FakeListLLM with responses
response_uniprot =  "Thought: I need to use the Search tool to find information about LangChain.\\nAction: Search\\nAction Input: When was LangChain created?"
responses_ncbi =   "tool_calls="

    responses = [ response_uniprot, responses_ncbi]
    fake_llm = MockFakeListLLM(responses=responses)
    return fake_llm`

tests/test_protein_info_retrieval.py

import pytest
from app.agents.protein_info_retrieval import create_protein_info_retrieval_agent
from tests.mocked_objects import \*

def print_stream(stream):
    for s in stream:
        message = s\["messages"\]\[-1\]
        if isinstance(message, tuple):
            print(message)
        else:
            message.pretty_print()

def test_create_protein_info_retrieval_agent():
    inputs = {"messages": \[("user", "When was LangChain created?")\]}
    llm = fake_protein_info_llm()
    agent = create_protein_info_retrieval_agent(llm=llm)  
    print_stream(agent.stream(inputs, stream_mode="values"))

When I run

poetry run pytest tests/test_protein_info_retrieval.py::test_create_protein_info_retrieval_agent

I get this error:

 def should_continue(state: AgentState):
messages = state\["messages"\]
last_message = messages\[-1\]
\# If there is no function call, then we finish

>       if not last_message.tool_calls:

E       AttributeError: 'HumanMessage' object has no attribute 'tool_calls'

../.cache/pypoetry/virtualenvs/app-aIDH_6-R-py3.11/lib/python3.11/site-packages/langgraph/prebuilt/chat_agent_executor.py:565: AttributeError
======================================================================== short test summary info =========================================================================
FAILED tests/test_protein_info_retrieval.py::test_create_protein_info_retrieval_agent - AttributeError: 'HumanMessage' object has no attribute 'tool_calls'\

Apparently, my LLM implementation lacks the ability to generate good prompt to be converted to tol calling.

Can somebody help me?

I tried to override also the \_call method, but without any success.

I just want to have a reproducible way to use one tool or another so that I can test my app.

Upvotes: 0

Views: 277

Answers (0)

Related Questions