FakeListLLM в create_react_agent не позволяет использовать инструментыPython

Программы на Python
Anonymous
FakeListLLM в create_react_agent не позволяет использовать инструменты

Сообщение Anonymous »

Я пытаюсь реализовать поддельный LLM для использования в create_react_agent. FakeLLM необходим для целей тестирования. Мне сложно использовать инструменты с только что описанной настройкой.
Это мой код:
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

Код: Выделить всё

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"))
Когда я запускаю
поэзию, запускаю pytesttests/test_protein_info_retieval.py::test_create_protein_info_retrieval_agent
Я получаю эту ошибку:

Код: Выделить всё

 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'\
Очевидно, в моей реализации LLM отсутствует возможность генерировать хорошее приглашение для преобразования в вызов tol.
Может ли кто-нибудь мне помочь?

Код: Выделить всё

your text
Я также пытался переопределить метод _call, но безуспешно.
Я просто хочу иметь воспроизводимый способ использования того или иного инструмента, чтобы Я могу протестировать свое приложение.

Подробнее здесь: https://stackoverflow.com/questions/787 ... -use-tools

Вернуться в «Python»