create_sql_query_chain alternative with a custom database access layer #23943
Unanswered
HemanthVikash
asked this question in
Q&A
Replies: 1 comment
-
To properly import the from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessage
PROMPT_SUFFIX = """Only use the following tables:
{table_info}
Question: {input}"""
_VECTOR_SQL_DEFAULT_TEMPLATE = """You are a {dialect} expert. 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 to the input question.
{dialect} queries has a vector distance function called `DISTANCE(column, array)` to compute relevance to the user's question and sort the feature array column by the relevance.
When the query is asking for {top_k} closest row, you have to use this distance function to calculate distance to entity's array on vector column and order by the distance to retrieve relevant rows.
*NOTICE*: `DISTANCE(column, array)` only accept an array column as its first argument and a `NeuralArray(entity)` as its second argument. You also need a user defined function called `NeuralArray(entity)` to retrieve the entity's array.
Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the LIMIT clause as per {dialect}. You should only order according to the distance function.
Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in double quotes (") to denote them as delimited identifiers.
Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.
Pay attention to use today() function to get the current date, if the question involves "today". `ORDER BY` clause should always be after `WHERE` clause. DO NOT add semicolon to the end of SQL. Pay attention to the comment in table schema.
Use the following format:
Question: "Question here"
SQLQuery: "SQL Query to run"
SQLResult: "Result of the SQLQuery"
Answer: "Final answer here"
"""
system_message = SystemMessage(content=_VECTOR_SQL_DEFAULT_TEMPLATE + PROMPT_SUFFIX)
human_prompt = HumanMessagePromptTemplate(
prompt=PromptTemplate(
template="Based on the following example, extract entities and relations from the provided text.\n\n"
"Use the following entity types, don't use other entity that is not defined below:\n"
"# ENTITY TYPES:\n{node_labels}\n\n"
"Use the following relation types, don't use other relation that is not defined below:\n"
"# RELATION TYPES:\n{rel_types}\n\n"
"Below are a number of examples of text and their extracted entities and relationships.\n"
"{examples}\n\n"
"For the following text, extract entities and relations as in the provided example.\n"
"{format_instructions}\nText: {input}",
input_variables=["input"],
partial_variables={
"format_instructions": "Format instructions here",
"node_labels": "Node labels here",
"rel_types": "Relation types here",
"examples": "Examples here",
},
)
)
VECTOR_SQL_PROMPT = ChatPromptTemplate.from_messages([system_message, human_prompt]) This template includes placeholders for |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checked other resources
Commit to Help
Example Code
Description
I have this piece of code that is my sql generation chain. The system prompt expects 'input', 'history', 'dialect', and 'table_info'.
I am trying to completely get rid of the
db=self.snowflake_engine
line and replace it with a secure local api call that doesn't have complete access to the database.With that in mind, how do I properly import the
dialect
andtable_info
variables into the system prompt. Ideally, a ChatPromptTemplate message will take in theinput
. Right now this is my final_prompt:Just like
few_shot_prompt
, I would like to add a message that can includedialect
andtable_info
.Is this the right approach or am I thinking about it entirely wrong? If there is no way to pass this through the system prompt, I could always just send the information right when I invoke. But I would rather do it the right way if there is one for this kind of problem.
System Info
System Information
Package Information
Packages not installed (Not Necessarily a Problem)
The following packages were not found:
Beta Was this translation helpful? Give feedback.
All reactions