Skip to content

Commit be069fe

Browse files
author
vikram-floweraura
committed
db select option
1 parent 7cac30a commit be069fe

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

pages/Ask Your Database.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from langchain.llms import OpenAI
99
from langchain.callbacks import get_openai_callback
1010
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain
11-
11+
import psycopg2
1212
import openai
1313
import os
1414

@@ -22,7 +22,8 @@
2222

2323
with st.form("my_form"):
2424
st.write("Inside the form")
25-
db_type = st.text_input("select db type")
25+
all_db_types = ["Mysql","Postgres","MongoDB"]
26+
db_type = st.selectbox("Select DB Type",all_db_types)
2627
dbusername = st.text_input("select db username")
2728
dbpassword = st.text_input("select db password")
2829
dbname = st.text_input("select db name")
@@ -31,46 +32,41 @@
3132
# Every form must have a submit button.
3233
submitted = st.form_submit_button("Submit")
3334
if submitted:
34-
if db_type=='mysql':
35+
if db_type=='Mysql':
3536
conn = "mysql+pymysql://"+dbusername+":"+dbpassword+"@"+dbhost+"/"+dbname
3637

37-
st.write("Outside the form")
38-
38+
if db_type=='Postgres':
39+
conn = "postgresql+psycopg2://"+dbusername+":"+dbpassword+"@"+dbhost+"/"+dbname
40+
41+
st.write(conn)
42+
43+
if conn!='':
44+
db = SQLDatabase.from_uri(
45+
conn,
46+
)
47+
# setup llm
48+
llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)
3949

4050

51+
# Create db chain
4152

53+
QUERY = """
54+
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.
55+
Use the following format:
4256
43-
st.write(conn)
44-
# db = SQLDatabase.from_uri(
45-
# "mysql+pymysql://bakewish:EmtTSRwDeKOk@admin.bakewish.in/bakewish_030123",
46-
# )
47-
if conn!='':
48-
db = SQLDatabase.from_uri(
49-
conn,
50-
)
51-
52-
st.write("db connection established")
53-
#st.write(db)
54-
55-
# setup llm
56-
llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)
57+
Question: "Question here"
58+
SQLQuery: "SQL Query to run"
59+
SQLResult: "Result of the SQLQuery"
60+
Answer: "Final answer here"
5761
58-
# Create db chain
62+
"""
63+
# Setup the database chain
64+
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)
5965

60-
QUERY = """
61-
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.
62-
Use the following format:
66+
user_question = st.text_input("write a query:")
6367

64-
Question: "Question here"
65-
SQLQuery: "SQL Query to run"
66-
SQLResult: "Result of the SQLQuery"
67-
Answer: "Final answer here"
68+
st.write(db_chain.run(user_question))
6869

69-
"""
70-
71-
# Setup the database chain
72-
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)
73-
74-
user_question = st.text_input("write a query:")
70+
st.write("Outside the form")
7571

76-
st.write(db_chain.run(user_question))
72+
#st.write(db)

0 commit comments

Comments
 (0)