Files
langchain/docs/examples/chains/sqlite.ipynb
T
2022-11-22 06:16:26 -08:00

2.6 KiB

SQLite example

This example showcases hooking up an LLM to answer questions over a database.

This uses the example Chinook database. To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file in a notebooks folder at the root of this repository.

In [1]:
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain
In [2]:
db = SQLDatabase.from_uri("sqlite:///../../../notebooks/Chinook.db")
llm = OpenAI(temperature=0)
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)
In [3]:
db_chain.run("How many employees are there?")
Out [3]:

> Entering new chain...
How many employees are there?
SQLQuery: SELECT COUNT(*) FROM Employee
SQLResult: [(8,)]
Answer: 8
> Finished chain.
' 8'
In [ ]: