KeyError: 'Input to ChatPromptTemplate is missing variables {\'"status"\'}. #22355
Replies: 2 comments 2 replies
-
Hey there, @quangnguyen1135! I'm here to help you out with any bugs, questions, or contributions you have in mind. Let's get to work! To fix the Here is an example of how you can include the from langchain_core.prompts import ChatPromptTemplate
# Assuming you have a ChatPromptTemplate instance
chat_prompt_template = ChatPromptTemplate.from_messages([
("system", "You are checking the status of orders."),
("human", "What is the status of the orders?"),
("ai", "The status of the orders is {status}.")
])
# Provide the required 'status' variable in the input
input_variables = {
"status": "undelivered" # or whatever value is appropriate
}
# Use the template with the input variables
result = chat_prompt_template.format(**input_variables)
print(result) Make sure that wherever you are calling the |
Beta Was this translation helpful? Give feedback.
-
Hi @quangnguyen1135, You should use double brackets if it's not identified as a prompt variable. # Define SQL chain
def get_sql_chain(db):
template = """
You are a data analyst at a company. You are interacting with a user who is asking you questions about the company's database.
<SCHEMA>{schema}</SCHEMA>
Conversation History: {chat_history}
For example:
Question: trạng thái của đơn hàng
SQL Query:
""
SELECT
ti.tracking_number,
c.name AS carrier_name,
JSON_UNQUOTE(JSON_EXTRACT(ti.shipments, '$[*].time')) AS shipment_times,
JSON_UNQUOTE(JSON_EXTRACT(ti.shipments, '$[*].status')) AS shipment_statuses,
JSON_UNQUOTE(JSON_EXTRACT(ti.shipments, '$[*].description')) AS shipment_descriptions,
JSON_UNQUOTE(JSON_EXTRACT(ti.shipments, '$[*].location')) AS shipment_locations,
ti.created_at,
ti.updated_at
FROM
tracking_info ti
JOIN
carriers c ON ti.carrier_id = c.id
WHERE
ti.tracking_number NOT IN (
SELECT tracking_number
FROM tracking_info
WHERE JSON_UNQUOTE(JSON_EXTRACT(shipments, '$[*].status')) = 'Delivered'
)
ORDER BY
ti.updated_at DESC;
""
Kiểm tra các đơn hàng còn chưa Delivered:
'''
SELECT count(*)
FROM tracking_info
WHERE NOT JSON_CONTAINS(shipments, '{{"status": "Delivered"}}');
'''
Your turn:
Question: {question}
SQL Query:
"""
prompt = ChatPromptTemplate.from_template(template)
llm = ChatGoogleGenerativeAI(model="gemini-1.0-pro", temperature=0.3)
def get_schema(_):
return db.get_table_info()
return (
RunnablePassthrough.assign(schema=get_schema)
| prompt
| llm
| StrOutputParser()
)
# Define response chain
def get_response(user_query: str, chat_history: list):
db = init_database()
sql_chain = get_sql_chain(db)
template = """
You are a data analyst at a company. You are interacting with a user who is asking you questions about the company's database.
Based on the table schema below, question, sql query, and sql response, write a natural language response in the specified format if the question is related to the database.
<SCHEMA>{schema}</SCHEMA>
Conversation History: {chat_history}
SQL Query: <SQL>{query}</SQL>
User question: {question}
SQL Response: {response}
Your turn:
"""
prompt = ChatPromptTemplate.from_template(template)
llm = ChatGoogleGenerativeAI(model="gemini-1.0-pro", temperature=0.3)
chain = (
RunnablePassthrough.assign(query=sql_chain).assign(
schema=lambda _: db.get_table_info(),
response=lambda vars: db.run(vars["query"]) if "query" in vars else None,
)
| prompt
| llm
| StrOutputParser()
)
''' |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I just want the bot to check how many undelivered orders there are in the database
but the bot understands it as a variable, I just put that sentence in the prompt so the bot can use it to query
data in mysql like:
'''
INSERT INTO
tracking_info
(id
,tracking_number
,carrier_id
,shipments
,created_at
,updated_at
)VALUES(280578,'420070169261290339726900366546',1615,'[{"time": "2024-02-03 11:10:00", "status": "OutForDelivery", "carrier": {"code": "21051", "name": "USPS", "carrier_id": 1615, "provider_id": 1}, "location": "CRANFORD, NJ 07016", "description": "Out for Delivery, Expected Delivery by 9:00pm -> Your item is out for delivery on February 3, 2024 at 6:10 am in CRANFORD, NJ 07016."}, {"time": "2024-02-02 17:43:00", "status": "Arrived at Post Office", "carrier": {"code": "21051", "name": "USPS", "carrier_id": 1615, "provider_id": 1}, "location": "CRANFORD, NJ 07016", "description": "Arrived at Post Office"}]','2024-02-03 03:28:08','2024-02-03 17:20:03')
'''
System Info
Beta Was this translation helpful? Give feedback.
All reactions