How to pass custom prompt to create_sql_query_chain #26966
Replies: 1 comment 1 reply
-
Hey @Shambhuraj11! I'm here to help you out with any bugs, questions, or contributions you have. Let's squash those bugs together! 😉 To pass a custom prompt to from langchain_core.prompts import PromptTemplate
from langchain.chains import create_sql_query_chain
from langchain_community.utilities import SQLDatabase
from langchain_openai import ChatOpenAI
# Define the custom prompt template
template = '''Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.
Use the following format:
Question: "Question here"
SQLQuery: "SQL Query to run"
SQLResult: "Result of the SQLQuery"
Answer: "Final answer here"
Only use the following tables:
{table_info}.
Question: {input}'''
# Create the PromptTemplate object
custom_prompt = PromptTemplate(
input_variables=["input", "table_info", "top_k", "dialect"],
template=template
)
# Initialize the SQLDatabase and language model
db = SQLDatabase.from_uri("sqlite:///Chinook.db")
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
# Create the SQL query chain with the custom prompt
chain = create_sql_query_chain(llm, db, prompt=custom_prompt)
# Example usage
response = chain.invoke({"question": "How many employees are there"})
print(response) This custom prompt includes the required input variables: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I am getting below error for my case. When i use default prompt it automatically fetch table info from my DB but when i am customizing it, i am getting error.
ValueError: Prompt must have input variables: 'input', 'top_k', 'table_info'
can anyone help me?
System Info
langchain 0.3.1
langchain-chroma 0.1.4
langchain-community 0.3.1
langchain-core 0.3.6
Beta Was this translation helpful? Give feedback.
All reactions