Files
litellm/docs/extras/use_cases/tabular/sql_query.ipynb
T
2023-07-29 07:12:19 -07:00

2.4 KiB

SQL Query

This notebook walks through how to load and run a chain that constructs SQL queries that can be run against your database to answer a question. Note that this ONLY constructs the query and does not run it.

In [4]:
from langchain.chains import create_sql_query_chain

from langchain.chat_models import ChatOpenAI
from langchain.utilities import SQLDatabase
In [7]:
db = SQLDatabase.from_uri("sqlite:///../../../../notebooks/Chinook.db")
In [8]:
chain = create_sql_query_chain(ChatOpenAI(temperature=0), db)
In [12]:
response = chain.invoke({"question":"How many employees are there"})
In [14]:
print(response)
SELECT COUNT(*) FROM Employee
In [15]:
db.run(response)
Out [15]:
'[(8,)]'
In [ ]: