ImprovementsThe query builder can be improved in several ways, such as (but not limited to):Customizing database description to your specific use caseHardcoding a few examples of questions and their corresponding SQL query in the promptUsing a vector database to include dynamic examples that are relevant to the specific user questionAll these examples involve customizing the chain's prompt. For example, we can include a few examples in our prompt like so:from langchain.prompts import PromptTemplateTEMPLATE = """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}.Some examples of SQL queries that correspond to questions are:{few_shot_examples}Question: {input}"""CUSTOM_PROMPT = PromptTemplate(    input_variables=["input", "few_shot_examples", "table_info", "dialect"], template=TEMPLATE)We can also access this prompt in the LangChain prompt hub.This will work with your LangSmith API key.from langchain import hubCUSTOM_PROMPT = hub.pull("rlm/text-to-sql")Case 2: Text-to-SQL query and execution‚ÄãWe can use SQLDatabaseChain from langchain_experimental to create and run SQL queries.from langchain.llms import OpenAIfrom langchain_experimental.sql import SQLDatabaseChainllm = OpenAI(temperature=0, verbose=True)db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)db_chain.run("How many employees are there?")            > Entering new SQLDatabaseChain chain...    How many employees are there?    SQLQuery:SELECT COUNT(*) FROM "Employee";    SQLResult: [(8,)]    Answer:There are 8 employees.    > Finished chain.    'There are 8 employees.'As we can see, we get the same result as the previous case.Here, the chain also handles the query execution and provides a final answer based on the user question
Open In Colab
Open In Colab ->: ImprovementsThe query builder can be improved in several ways, such as (but not limited to):Customizing database description to your specific use caseHardcoding a few examples of questions and their corresponding SQL query in the promptUsing a vector database to include dynamic examples that are relevant to the specific user questionAll these examples involve customizing the chain's prompt. For example, we can include a few examples in our prompt like so:from langchain.prompts import PromptTemplateTEMPLATE = """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}.Some examples of SQL queries that correspond to questions are:{few_shot_examples}Question: {input}"""CUSTOM_PROMPT = PromptTemplate(    input_variables=["input", "few_shot_examples", "table_info", "dialect"], template=TEMPLATE)We can also access this prompt in the LangChain prompt hub.This will work with your LangSmith API key.from langchain import hubCUSTOM_PROMPT = hub.pull("rlm/text-to-sql")Case 2: Text-to-SQL query and execution‚ÄãWe can use SQLDatabaseChain from langchain_experimental to create and run SQL queries.from langchain.llms import OpenAIfrom langchain_experimental.sql import SQLDatabaseChainllm = OpenAI(temperature=0, verbose=True)db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)db_chain.run("How many employees are there?")            > Entering new SQLDatabaseChain chain...    How many employees are there?    SQLQuery:SELECT COUNT(*) FROM "Employee";    SQLResult: [(8,)]    Answer:There are 8 employees.    > Finished chain.    'There are 8 employees.'As we can see, we get the same result as the previous case.Here, the chain also handles the query execution and provides a final answer based on the user question