-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp.py
50 lines (37 loc) · 1.16 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain
import environ
env = environ.Env()
environ.Env.read_env()
API_KEY = env('OPENAI_API_KEY')
# Setup database
db = SQLDatabase.from_uri(
f"postgresql+psycopg2://postgres:{env('DBPASS')}@localhost:5432/{env('DATABASE')}",
)
# setup llm
llm = OpenAI(temperature=0, openai_api_key=API_KEY)
# Create db chain
QUERY = """
Given an input question, first create a syntactically correct postgresql query to run, then look at the results of the query and return the answer.
Use the following format:
Question: Question here
SQLQuery: SQL Query to run
SQLResult: Result of the SQLQuery
Answer: Final answer here
{question}
"""
# Setup the database chain
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)
def get_prompt():
print("Type 'exit' to quit")
while True:
prompt = input("Enter a prompt: ")
if prompt.lower() == 'exit':
print('Exiting...')
break
else:
try:
question = QUERY.format(question=prompt)
print(db_chain.run(question))
except Exception as e:
print(e)
get_prompt()