"SELECT * FROM albums WHERE ArtistId = (SELECT ArtistId FROM artists WHERE Name = 'AC/DC');",#              "List all tracks in the 'Rock' genre.": "SELECT * FROM tracks WHERE GenreId = (SELECT GenreId FROM genres WHERE Name = 'Rock');",#              'Find the total duration of all tracks.': 'SELECT SUM(Milliseconds) FROM tracks;',#              'List all customers from Canada.': "SELECT * FROM customers WHERE Country = 'Canada';",#              'How many tracks are there in the album with ID 5?': 'SELECT COUNT(*) FROM tracks WHERE AlbumId = 5;',#              'Find the total number of invoices.': 'SELECT COUNT(*) FROM invoices;',#              'List all tracks that are longer than 5 minutes.': 'SELECT * FROM tracks WHERE Milliseconds > 300000;',#              'Who are the top 5 customers by total purchase?': 'SELECT CustomerId, SUM(Total) AS TotalPurchase FROM invoices GROUP BY CustomerId ORDER BY TotalPurchase DESC LIMIT 5;',#              'Which albums are from the year 2000?': "SELECT * FROM albums WHERE strftime('%Y', ReleaseDate) = '2000';",#              'How many employees are there': 'SELECT COUNT(*) FROM "employee"'#             }We can then create a retriever using the list of questions, assigning the target SQL query as metadata:from langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.vectorstores import FAISSfrom langchain.schema import Documentembeddings = OpenAIEmbeddings()few_shot_docs = [Document(page_content=question, metadata={'sql_query': few_shots[question]}) for question in few_shots.keys()]vector_db = FAISS.from_documents(few_shot_docs, embeddings)retriever = vector_db.as_retriever()Now we can create our own custom tool and append it as a new tool in the create_sql_agent function:from langchain.agents.agent_toolkits import create_retriever_tooltool_description = """This tool will help you understand similar examples to adapt them to the user question.Input to this tool should be the user question."""retriever_tool =
Open In Colab
Open In Colab ->: "SELECT * FROM albums WHERE ArtistId = (SELECT ArtistId FROM artists WHERE Name = 'AC/DC');",#              "List all tracks in the 'Rock' genre.": "SELECT * FROM tracks WHERE GenreId = (SELECT GenreId FROM genres WHERE Name = 'Rock');",#              'Find the total duration of all tracks.': 'SELECT SUM(Milliseconds) FROM tracks;',#              'List all customers from Canada.': "SELECT * FROM customers WHERE Country = 'Canada';",#              'How many tracks are there in the album with ID 5?': 'SELECT COUNT(*) FROM tracks WHERE AlbumId = 5;',#              'Find the total number of invoices.': 'SELECT COUNT(*) FROM invoices;',#              'List all tracks that are longer than 5 minutes.': 'SELECT * FROM tracks WHERE Milliseconds > 300000;',#              'Who are the top 5 customers by total purchase?': 'SELECT CustomerId, SUM(Total) AS TotalPurchase FROM invoices GROUP BY CustomerId ORDER BY TotalPurchase DESC LIMIT 5;',#              'Which albums are from the year 2000?': "SELECT * FROM albums WHERE strftime('%Y', ReleaseDate) = '2000';",#              'How many employees are there': 'SELECT COUNT(*) FROM "employee"'#             }We can then create a retriever using the list of questions, assigning the target SQL query as metadata:from langchain.embeddings.openai import OpenAIEmbeddingsfrom langchain.vectorstores import FAISSfrom langchain.schema import Documentembeddings = OpenAIEmbeddings()few_shot_docs = [Document(page_content=question, metadata={'sql_query': few_shots[question]}) for question in few_shots.keys()]vector_db = FAISS.from_documents(few_shot_docs, embeddings)retriever = vector_db.as_retriever()Now we can create our own custom tool and append it as a new tool in the create_sql_agent function:from langchain.agents.agent_toolkits import create_retriever_tooltool_description = """This tool will help you understand similar examples to adapt them to the user question.Input to this tool should be the user question."""retriever_tool =