Kiranontimitta commited on
Commit
a8627c5
·
verified ·
1 Parent(s): 3157b85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -12
app.py CHANGED
@@ -4,7 +4,7 @@ import pandas as pd
4
  from sqlalchemy import create_engine
5
  from langchain.chat_models import ChatOpenAI
6
  from langchain.utilities.sql_database import SQLDatabase
7
- from langchain.tools.sql.toolkit import SQLDatabaseToolkit
8
 
9
  # Set OpenAI API Key
10
  openai.api_key = "sk-O7esHSo2XAWm-GXUGXp7_P9l4qXrQMn0CIGzs34ojLT3BlbkFJeXGSSvywppRTAvyT0zZkmZLZsj5cg7XkAkBTh8ZxoA"
@@ -16,7 +16,7 @@ engine = create_engine(DATABASE_URL)
16
  # Set up LangChain components
17
  llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.5) # OpenAI's Chat model for LLM
18
  db = SQLDatabase(engine) # Connect LangChain to the database
19
- toolkit = SQLDatabaseToolkit(llm=llm, db=db) # Create the SQL toolkit
20
 
21
  # Streamlit UI setup
22
  st.title("SQL Data Chatbot with LangChain")
@@ -27,22 +27,18 @@ user_question = st.text_input("Your question:")
27
 
28
  # Process the question if provided
29
  if user_question:
30
- # Generate the SQL query and answer using the toolkit
31
  try:
32
- # Execute the question through the SQL toolkit
33
- answer = toolkit.query(user_question)
34
 
35
  # Display the generated SQL query and answer
36
- st.subheader("Generated SQL Query")
37
- st.code(answer.query, language="sql")
38
-
39
- # Display the generated answer
40
- st.subheader("Answer")
41
- st.write(answer.result)
42
 
43
  # Execute the SQL query to get results
44
  with engine.connect() as conn:
45
- result_df = pd.read_sql_query(answer.query, conn)
46
 
47
  # Show query results if any
48
  if not result_df.empty:
 
4
  from sqlalchemy import create_engine
5
  from langchain.chat_models import ChatOpenAI
6
  from langchain.utilities.sql_database import SQLDatabase
7
+ from langchain.chains import SQLDatabaseChain
8
 
9
  # Set OpenAI API Key
10
  openai.api_key = "sk-O7esHSo2XAWm-GXUGXp7_P9l4qXrQMn0CIGzs34ojLT3BlbkFJeXGSSvywppRTAvyT0zZkmZLZsj5cg7XkAkBTh8ZxoA"
 
16
  # Set up LangChain components
17
  llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.5) # OpenAI's Chat model for LLM
18
  db = SQLDatabase(engine) # Connect LangChain to the database
19
+ sql_chain = SQLDatabaseChain.from_llm(llm, database=db) # Create the SQL chain
20
 
21
  # Streamlit UI setup
22
  st.title("SQL Data Chatbot with LangChain")
 
27
 
28
  # Process the question if provided
29
  if user_question:
30
+ # Generate the SQL query and answer using the SQL chain
31
  try:
32
+ # Execute the question through the SQL chain
33
+ response = sql_chain.run(user_question)
34
 
35
  # Display the generated SQL query and answer
36
+ st.subheader("Generated SQL Query and Answer")
37
+ st.write(response)
 
 
 
 
38
 
39
  # Execute the SQL query to get results
40
  with engine.connect() as conn:
41
+ result_df = pd.read_sql_query(response.query, conn)
42
 
43
  # Show query results if any
44
  if not result_df.empty: