user986508
user986508

Reputation: 199

Langchain Agent error - ValueError: Prompt missing required variables: {'agent_scratchpad', 'tool_names', 'tools'}

I am having an issue with simple langchain agent creation v0.3, documentation is also not so great.

I don't know why my simple agent isn't working. Here is my code, and I don't know how to resolve the issue.

So what I am trying to do is, create a simple agent in langchain v0.3 that calls a function. I think there is an issue with the imports maybe, I am not so sure.

# Import necessary modules from langchain_community
from langchain_openai import ChatOpenAI
from langchain.agents.react.agent import create_react_agent
from langchain_community.tools import Tool
from langchain.prompts import PromptTemplate

# Define a mock function to find similar companies
def find_similar_companies(company_name: str) -> str:
    # Mock data for demonstration purposes
    mock_data = {
        "XYZ Corp": "ABC Inc, DEF Ltd",
        "Acme Corp": "GHI LLC, JKL PLC"
    }
    return f"Similar companies to {company_name}: {mock_data.get(company_name, 'No similar companies found.')}"

# Define the tools using the Tool class
tools = [
    Tool(
        name="Find Similar Companies",
        func=find_similar_companies,
        description="Use this tool to find similar companies based on a company name."
    )
]

# Initialize the ChatOpenAI model
llm = ChatOpenAI(
    model="gpt-3.5-turbo",
    openai_api_key="API_KEY"
)

# Define the PromptTemplate
prompt_template = PromptTemplate(
    template="""
    You are an intelligent agent. Use the tools available to answer the query.
    Available tools: {tools_description}
    Query: {input}
    """,
    input_variables=["input", "tools_description"]
)

# Create a React agent with the create_react_agent function
agent = create_react_agent(
    llm=llm,
    tools=tools,
    prompt=prompt_template
)

# Test the agent
query = "Find similar companies to XYZ Corp"
response = agent.invoke({"input": query})

# Print the response
print("Agent Response:", response)

and the error is:

(tensorflow_env) usman@Ihsanulhaqs-MBP aasaas-product % python3 app.py
/Users/usman/Documents/projects/machine_learning/tensorflow_env/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
  warnings.warn(
Traceback (most recent call last):
  File "/Users/usman/Documents/projects/machine_learning/aasaas-product/app.py", line 57, in <module>
    agent = create_react_agent(
  File "/Users/usman/Documents/projects/machine_learning/tensorflow_env/lib/python3.9/site-packages/langchain/agents/react/agent.py", line 117, in create_react_agent
    raise ValueError(f"Prompt missing required variables: {missing_vars}")
ValueError: Prompt missing required variables: {'tool_names', 'agent_scratchpad', 'tools'}
(tensorflow_env) usman@Ihsanulhaqs-MBP aasaas-product % 

Upvotes: 1

Views: 179

Answers (0)

Related Questions