88from langchain .llms import OpenAI
99from langchain .callbacks import get_openai_callback
1010from langchain import OpenAI , SQLDatabase , SQLDatabaseChain
11-
11+ import psycopg2
1212import openai
1313import os
1414
2222
2323with 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" )
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