Spaces:
Build error
Build error
Create script.py
Browse files
script.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain
|
| 3 |
+
|
| 4 |
+
api_key = os.getenv('OPENAI_API_KEY')
|
| 5 |
+
|
| 6 |
+
db = SQLDatabase.from_uri("sqlite:///Chinook.db"
|
| 7 |
+
, include_tables=['Customer', 'Invoice']
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
llm = OpenAI(temperature=0.0, openai_api_key=api_key)
|
| 11 |
+
|
| 12 |
+
db_chain = SQLDatabaseChain.from_llm(llm
|
| 13 |
+
, db
|
| 14 |
+
, verbose=True
|
| 15 |
+
, use_query_checker=True
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
queries = ("How many customers are there?"
|
| 19 |
+
, "Which state has the most customers?"
|
| 20 |
+
, "How many invoices are there?"
|
| 21 |
+
, "Which customer has the most invoices?"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
for query in queries:
|
| 25 |
+
print(db_chain.run(query))
|
| 26 |
+
|
| 27 |
+
# https://www.sqlitetutorial.net/sqlite-sample-database/
|