langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
95.55k stars 15.52k forks source link

'Response is not a valid tool, try one of [Retriever].'), when using OpenAIFunctionsAgentOutputParser() #16355

Closed glejdis closed 7 months ago

glejdis commented 10 months ago

Checked other resources

Example Code

The following code: ''' from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser from langchain.agents.agent import AgentOutputParser from langchain.agents.chat.output_parser import ChatOutputParser

def creat_ai_search_new_agent(embeddings, llm, class_name_rich):

ai_search_endpoint = get_ai_search_endpoint()
ai_search_admin_key = get_ai_search_admin_key()

vector_store = AzureSearch(
        azure_search_endpoint=xxx,
        azure_search_key=xxx,
        index_name=xxx,
        embedding_function=embeddings.embed_query,
        content_key=xxx
    )

"""Retriever that uses `Azure Cognitive Search`."""

azure_search_retriever = AzureSearchVectorStoreRetriever(
        vectorstore=vector_store,
        search_type="hybrid", 
        k=4,
        top=10
    )

retriever_tool = create_retriever_tool(
    azure_search_retriever,
    "Retriever",
    "Useful when you need to retrieve information from documents",
)

class Response(BaseModel):
    """Final response to the question being asked"""

    answer: str = Field(description="The final answer to respond to the user")
    )

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant who retrieves information from documents"),
        ("user", "{input}"),
        MessagesPlaceholder(variable_name="agent_scratchpad"),
    ]
)

llm_with_tools = llm.bind(
    functions=[
        # The retriever tool
        format_tool_to_openai_function(retriever_tool),
        # Response schema
        convert_pydantic_to_openai_function(Response),
    ]
)

try:

    agent = (
        {
            "input": lambda x: x["input"],
            # Format agent scratchpad from intermediate steps
            "agent_scratchpad": lambda x: format_to_openai_function_messages(
                x["intermediate_steps"]
            ),
        }
        | prompt
        | llm_with_tools
        | OpenAIFunctionsAgentOutputParser()
    )

    agent_executor = AgentExecutor(tools=[retriever_tool], agent=agent, verbose=True, return_intermediate_steps=True,        
                                   handle_parsing_errors=True, 
                                   max_iterations=15,
                                   )

except Exception as e:
    print(e)
    print("error instanciating the agent")

return agent_executor

'''

It gives you the error: Response is not a valid tool, try one of [Retriever]. and then after going into a loop reaches the agent limit. The final steps of the agent look like this:

Invoking: Response with {'answer': "XXXXXXX", 'sources': [58, 15, 57, 29]}

Response is not a valid tool, try one of [Retriever].

Description

langchain == 0.1.1 openai==1.7.0

System Info

Using model GPT-4 32K

Related Components

dosubot[bot] commented 10 months ago

🤖

Based on the code you've shared and the error message you're encountering, it seems like the issue is related to how you're using the Response class in your LangChain application.

In your code, you're trying to use Response as a tool in the following line:

convert_pydantic_to_openai_function(Response),

However, Response is not a tool, but a Pydantic model that you've defined to structure the final response to the question being asked. The error message you're seeing is indicating that Response is not recognized as a valid tool, and it suggests using Retriever instead, which is a valid tool in your application.

To resolve this issue, you should remove the line where you're trying to convert Response to an OpenAI function. Instead, you should use Response to structure the final output of your agent after all the tools have been executed and the final answer has been determined.

Here's how you can modify your code:

# Remove this line
# convert_pydantic_to_openai_function(Response),

# ...

# Use Response to structure the final output
final_output = Response(answer=final_answer)

In this modified code, Response is used to structure the final output of your agent, not as a tool. This should resolve the error you're seeing.

Please note that this is based on the information you've provided and the current version of LangChain (v0.1.3). If you're using a different version or if there are other parts of your code that might be causing this issue, please provide more information so I can give a more accurate solution.

For more information on how to use tools in LangChain, you can refer to the LangChain documentation. Specifically, the Tools section provides detailed information on how to define and use tools in your LangChain application.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

ZuzooVn commented 10 months ago

@glejdis have you found the solution?

glejdis commented 10 months ago

@ZuzooVn no solution yet :(

dosubot[bot] commented 10 months ago

🤖

It seems like this issue requires a deeper look, @baskaryan could you please lend your expertise?


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.