-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
53 lines (38 loc) · 1.17 KB
/
main.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
51
52
53
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain
# Setting up the api key
import environ
env = environ.Env()
environ.Env.read_env()
API_KEY = env('apikey')
# Setup database
db = SQLDatabase.from_uri(
f"postgresql+psycopg2://postgres:{env('dbpass')}@localhost:5433/tasks",
)
# 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()