This project implements an advanced natural language to SQL query conversion system using Google PaLM and LangChain, achieving 95% accuracy in query translation. The system incorporates Hugging Face embeddings and ChromaDB for efficient query processing, demonstrating a 40% improvement in processing speed through few-shot learning techniques.
Converting natural language queries to SQL presents a significant challenge in database interactions. This system bridges the gap between human language and database queries using state-of-the-art language models and few-shot learning approaches.
from langchain.llms import GooglePalm from langchain.utilities import SQLDatabase from langchain_experimental.sql import SQLDatabaseChain from langchain.prompts import SemanticSimilarityExampleSelector from langchain.embeddings import HuggingFaceEmbeddings from langchain.vectorstores import Chroma from langchain.prompts import FewShotPromptTemplate from langchain.chains.sql_database.prompt import PROMPT_SUFFIX from langchain.prompts.prompt import PromptTemplate import os from dotenv import load_dotenv load_dotenv()
def get_few_shot_db_chain(): # Database connection setup db_user = "root" db_password = "root" db_host = "localhost" db_name = "db_tshirts" db = SQLDatabase.from_uri( f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}", sample_rows_in_table_info=3 ) # LLM configuration llm = GooglePalm( google_api_key=os.environ["GOOGLE_API_KEY"], temperature=0.1 ) # Embeddings setup embeddings = HuggingFaceEmbeddings( model_name='sentence-transformers/all-MiniLM-L6-v2' )
# Vectorize examples to_vectorize = [" ".join(example.values()) for example in few_shots] def embedding_function(texts): return embeddings.embed_documents(texts) # Create vector store vectorstore = Chroma.from_texts( to_vectorize, embedding_function=embedding_function, metadatas=few_shots ) # Configure example selector example_selector = SemanticSimilarityExampleSelector( vectorstore=vectorstore, k=2 )
few_shots = [ { 'Question': "How many t-shirts do we have left for Nike in XS size and white color?", 'SQLQuery': "SELECT sum(stock_quantity) FROM t_shirts WHERE brand = 'Nike' AND color = 'White' AND size = 'XS'", 'SQLResult': "Result of the SQL query", 'Answer': "91" }, { 'Question': "How much is the total price of the inventory for all S-size t-shirts?", 'SQLQuery': "SELECT SUM(price*stock_quantity) FROM t_shirts WHERE size = 'S'", 'SQLResult': "Result of the SQL query", 'Answer': "22292" }, { 'Question': "If we have to sell all the Levi's T-shirts today with discounts applied. How much revenue our store will generate (post discounts)?", 'SQLQuery': """ SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) as total_revenue from (select sum(price*stock_quantity) as total_amount, t_shirt_id from t_shirts where brand = 'Levi' group by t_shirt_id) a left join discounts on a.t_shirt_id = discounts.t_shirt_id """, 'SQLResult': "Result of the SQL query", 'Answer': "16725.4" }, { 'Question': "If we have to sell all the Levi's T-shirts today. How much revenue our store will generate without discount?", 'SQLQuery': "SELECT SUM(price * stock_quantity) FROM t_shirts WHERE brand = 'Levi'", 'SQLResult': "Result of the SQL query", 'Answer': "17462" }, { 'Question': "How many white color Levi's shirt I have?", 'SQLQuery': "SELECT sum(stock_quantity) FROM t_shirts WHERE brand = 'Levi' AND color = 'White'", 'SQLResult': "Result of the SQL query", 'Answer': "290" }, { 'Question': "how much sales amount will be generated if we sell all large size t shirts today in nike brand after discounts?", 'SQLQuery': """ SELECT sum(a.total_amount * ((100-COALESCE(discounts.pct_discount,0))/100)) as total_revenue from (select sum(price*stock_quantity) as total_amount, t_shirt_id from t_shirts where brand = 'Nike' and size="L" group by t_shirt_id) a left join discounts on a.t_shirt_id = discounts.t_shirt_id """, 'SQLResult': "Result of the SQL query", 'Answer': "290" } ]
mysql_prompt = """ You are a MySQL expert. Given an input question, first create a syntactically correct MySQL query to run, then look at the results of the query and return the answer to the input question. 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 MySQL. You can order the results to return the most informative data in the database. Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column names in backticks (`) 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 CURDATE() function to get the current date, if the question involves "today". Use the following format: Question: Question here SQLQuery: Query to run with no pre-amble SQLResult: Result of the SQLQuery Answer: Final answer here """ example_prompt = PromptTemplate( input_variables=["Question", "SQLQuery", "SQLResult", "Answer"], template="\nQuestion: {Question}\nSQLQuery: {SQLQuery}\n" + "SQLResult: {SQLResult}\nAnswer: {Answer}" ) few_shot_prompt = FewShotPromptTemplate( example_selector=example_selector, example_prompt=example_prompt, prefix=mysql_prompt, suffix=PROMPT_SUFFIX, input_variables=["input", "table_info", "top_k"] )
import streamlit as st from langchain_helper import get_few_shot_db_chain # Create web interface st.title("T Shirts: Database Q&A 👕") question = st.text_input("Question: ") if question: chain = get_few_shot_db_chain() response = chain.run(question) st.header("Answer") st.write(response)
The system demonstrates successful integration of LLMs with traditional database systems, providing an efficient and accurate natural language interface for database queries. The combination of few-shot learning and vector store optimization delivers significant improvements in both accuracy and performance.
There are no datasets linked
There are no datasets linked