3

I'm using langchain and OpenAI to implement a natural language to SQL query tool. It works okay for schemas with a small number of simple tables. However, when I try to use it for schemas that have many tables or fewer tables with many columns, the prompt which includes all table structures exceeds the token limit for the OpenAI completion service.

I tried limiting the prompt to include just the table and column names without column data type or constraint information, but the prompt still exceeds the token limit.

Dean H.
  • 31
  • 2
  • Could you give an example? One idea could be to use an agent that asks for specific information on a by-need basis, for instance if you spread out your information into several files and let the agent use the "terminal" tool to read the relevant ones depending on the query. I might help more with a concrete example. – user3078439 Mar 23 '23 at 23:34

1 Answers1

0

Try using SQLDatabaseSequentialChain. It limits number of tables per query. Code bellow are directly from Langchain docs.

  1. Based on the query, determine which tables to use.
  2. Based on those tables, call the normal SQL database chain.
from langchain.chains import SQLDatabaseSequentialChain
db = SQLDatabase.from_uri("sqlite:///../../../../notebooks/Chinook.db")

chain = SQLDatabaseSequentialChain.from_llm(llm, db, verbose=True)
chain.run("How many employees are also customers?")
> Entering new SQLDatabaseSequentialChain chain...
Table names to use:
['Customer', 'Employee']

> Entering new SQLDatabaseChain chain...
How many employees are also customers? 
SQLQuery: SELECT COUNT(*) FROM Employee INNER JOIN Customer ON Employee.EmployeeId = Customer.SupportRepId;
SQLResult: [(59,)]
Answer: 59 employees are also customers.
> Finished chain.

> Finished chain.