-
Notifications
You must be signed in to change notification settings - Fork 13.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValueError: Prompt missing required variables: {'agent_scratchpad', 'tool_names', 'tools'} #21866
Comments
To solve this error you have 2 options:
from langchain_community.agent_toolkits.sql.base import create_sql_agent
llm = OpenAI()
prompt = hub.pull("hwchase17/react")
db = SQLDatabase.from_uri(f"mysql+mysqlconnector://{user}:{password}@{host}/{database}")
agent = create_sql_agent(llm,db=db,prompt=prompt)
your_prompt = PromptTemplate.from_template("""Query:{Query}""")
agent.invoke(your_prompt.format(query="question")) By default, the create_sql_agent function utilizes ZERO_SHOT_REACT_DESCRIPTION, which relies on this code. # This code is from langchain_community.agent_toolkits.sql.base
if agent_type == AgentType.ZERO_SHOT_REACT_DESCRIPTION:
if prompt is None:
from langchain.agents.mrkl import prompt as react_prompt
format_instructions = (
format_instructions or react_prompt.FORMAT_INSTRUCTIONS
)
template = "\n\n".join(
[
react_prompt.PREFIX,
"{tools}",
format_instructions,
react_prompt.SUFFIX,
]
)
prompt = PromptTemplate.from_template(template)
agent = RunnableAgent(
runnable=create_react_agent(llm, tools, prompt),
input_keys_arg=["input"],
return_keys_arg=["output"],
**kwargs,
)
|
#Does the above code work on the Google Gemini Pro model or the Google PaLM LLM? Load environment variablesload_dotenv() def get_few_shot_db_chain(question):
Streamlit appst.title("SQL Query Assistant") user_question = st.text_input("Enter your question about the database:") if st.button("Ask"): and I need to pass my own prompt template in vectorized formate |
I'm still facing issues. How can I resolve them? Is the above code correct for the Google Gemini Pro model or the Google PaLM LLM? and I need to pass my own prompt template in vectorized formate |
The problem is not with what type of LLM you are using # This code is from langchain_community.agent_toolkits.sql.base
if agent_type == AgentType.ZERO_SHOT_REACT_DESCRIPTION:
if the prompt is None:
from langchain.agents.make import prompt as react_prompt
format_instructions = (
format_instructions or react_prompt.FORMAT_INSTRUCTIONS
)
template = "\n\n".join(
[
react_prompt.PREFIX,
"{tools}",
format_instructions,
react_prompt.SUFFIX,
]
)
prompt = PromptTemplate.from_template(template)
agent = RunnableAgent(
runnable=create_react_agent(LLM, tools, prompt),
input_keys_arg=["input"],
return_keys_arg=["output"],
**kwargs,
) The above is the internal code of langchain SQL agent, and if you check the default agent type is if agent_type == AgentType.ZERO_SHOT_REACT_DESCRIPTION:
if prompt is None:
from langchain.agents.mrkl import prompt as react_prompt
format_instructions = (
format_instructions or react_prompt.FORMAT_INSTRUCTIONS
)
template = "\n\n".join(
[
react_prompt.PREFIX,
"{tools}",
format_instructions,
react_prompt.SUFFIX,
]
)
prompt = PromptTemplate.from_template(template) To solve this problem you have two options:
agent = create_sql_agent(llm,db=db,prompt=prompt)
few_shot_prompt = FewShotPromptTemplate(
example_selector=example_selector,
example_prompt=example_prompt,
prefix=mysql_prompt,
suffix=SUFFIX,
input_variables=["input"], # These variables are used in the prefix and suffix
)
agent.invoke(few_shot_prompt.format(input="get me table"))
Also I need more clarity on the reason for example_selector and the need for passing MySQL prompt, I can check and give you the working code |
Checked other resources
Example Code
from langchain.prompts import SemanticSimilarityExampleSelector
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Chroma
from example_template import few_shots
from langchain.prompts import FewShotPromptTemplate
from langchain.chains.sql_database.prompt import PROMPT_SUFFIX,_mysql_prompt
from langchain.prompts.prompt import PromptTemplate
from langchain_google_genai import GoogleGenerativeAI
from langchain_community.utilities.sql_database import SQLDatabase
from langchain_community.agent_toolkits import create_sql_agent
import os
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
to_vector = ["".join(example.values())for example in few_shots]
vectorStore = Chroma.from_texts(to_vector,embeddings,metadatas=few_shots)
example_prompt =PromptTemplate(input_variables=["Question","SQLQuery","SQLResult","Answer"],template="\nQuestion: {Question}\nSQLQuery: {SQLQuery}\nSQLResult: {SQLResult}\nAnswer: {Answer}")
example_selector = SemanticSimilarityExampleSelector(vectorstore=vectorStore,k=2)
fewShot_Prompt_Template= FewShotPromptTemplate(
example_selector=example_selector,
example_prompt=example_prompt,
prefix=_mysql_prompt,
suffix=PROMPT_SUFFIX,
input_variables=["input","table_info","top_k",],
)
os.environ["MYSQL_HOST"] = "localhost"
os.environ["MYSQL_USER"] = "root"
os.environ["MYSQL_PASSWORD"] = ""
os.environ["MYSQL_DATABASE"] = "fhcjgjvkhkjk"
host = os.environ.get('MYSQL_HOST')
user = os.environ.get('MYSQL_USER')
password = os.environ.get('MYSQL_PASSWORD')
database = os.environ.get('MYSQL_DATABASE')
GEMINI_API_KEY = ''
llm = GoogleGenerativeAI(model="gemini-pro", google_api_key=GEMINI_API_KEY)
db = SQLDatabase.from_uri(f"mysql+mysqlconnector://{user}:{password}@{host}/{database}")
agent_executor = create_sql_agent(llm, db=db, verbose=True,prompt=fewShot_Prompt_Template)
Error Message and Stack Trace (if applicable)
ValueError Traceback (most recent call last)
Cell In[70], line 27
21 llm = GoogleGenerativeAI(model="gemini-pro", google_api_key=GEMINI_API_KEY)
24 db = SQLDatabase.from_uri(f"mysql+mysqlconnector://{user}:{password}@{host}/{database}")
---> 27 agent_executor = create_sql_agent(llm, db=db, verbose=True,prompt=fewShot_Prompt_Template)
File c:\Users\SATHISH\AppData\Local\Programs\Python\Python311\Lib\site-packages\langchain_community\agent_toolkits\sql\base.py:180, in create_sql_agent(llm, toolkit, agent_type, callback_manager, prefix, suffix, format_instructions, input_variables, top_k, max_iterations, max_execution_time, early_stopping_method, verbose, agent_executor_kwargs, extra_tools, db, prompt, **kwargs)
170 template = "\n\n".join(
171 [
172 react_prompt.PREFIX,
(...)
176 ]
177 )
178 prompt = PromptTemplate.from_template(template)
179 agent = RunnableAgent(
--> 180 runnable=create_react_agent(llm, tools, prompt),
181 input_keys_arg=["input"],
182 return_keys_arg=["output"],
183 **kwargs,
184 )
186 elif agent_type == AgentType.OPENAI_FUNCTIONS:
187 if prompt is None:
File c:\Users\SATHISH\AppData\Local\Programs\Python\Python311\Lib\site-packages\langchain\agents\react\agent.py:114, in create_react_agent(llm, tools, prompt, output_parser, tools_renderer, stop_sequence)
...
118 tool_names=", ".join([t.name for t in tools]),
119 )
120 if stop_sequence:
ValueError: Prompt missing required variables: {'agent_scratchpad', 'tool_names', 'tools'}
Description
I encountered a
**ValueError**
when trying to create an SQL agent using LangChain. The error message indicated that the prompt was missing required variables:agent_scratchpad
,tool_names
, andtools
. Despite consulting various resources including Medium blogs, YouTube videos, GitHub references, and the LangChain documentation, I have not been able to find a solution.System Info
"pip freeze | grep langchain"
platform windows
python version 12
The text was updated successfully, but these errors were encountered: