Kiranontimitta commited on
Commit
dae7884
·
verified ·
1 Parent(s): e61bc7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -3,8 +3,8 @@ import openai
3
  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.chains import SQLDatabaseChain
8
 
9
  # Set OpenAI API Key
10
  openai.api_key = "sk-O7esHSo2XAWm-GXUGXp7_P9l4qXrQMn0CIGzs34ojLT3BlbkFJeXGSSvywppRTAvyT0zZkmZLZsj5cg7XkAkBTh8ZxoA"
@@ -15,8 +15,16 @@ engine = create_engine(DATABASE_URL)
15
 
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,18 +35,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 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:
 
3
  import pandas as pd
4
  from sqlalchemy import create_engine
5
  from langchain.chat_models import ChatOpenAI
6
+ from langchain.prompts import PromptTemplate
7
+ from langchain.chains import LLMChain
8
 
9
  # Set OpenAI API Key
10
  openai.api_key = "sk-O7esHSo2XAWm-GXUGXp7_P9l4qXrQMn0CIGzs34ojLT3BlbkFJeXGSSvywppRTAvyT0zZkmZLZsj5cg7XkAkBTh8ZxoA"
 
15
 
16
  # Set up LangChain components
17
  llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.5) # OpenAI's Chat model for LLM
18
+
19
+ # Custom prompt to generate SQL query
20
+ prompt_template = """
21
+ You are an SQL expert. Write an SQL query to answer the following question:
22
+ Question: {question}
23
+ SQL Query:
24
+ """
25
+
26
+ prompt = PromptTemplate(input_variables=["question"], template=prompt_template)
27
+ llm_chain = LLMChain(llm=llm, prompt=prompt)
28
 
29
  # Streamlit UI setup
30
  st.title("SQL Data Chatbot with LangChain")
 
35
 
36
  # Process the question if provided
37
  if user_question:
38
+ # Generate the SQL query using the custom LLM chain
39
  try:
40
+ # Generate SQL query with the language model
41
+ sql_query = llm_chain.run({"question": user_question}).strip()
42
 
43
+ # Display the generated SQL query
44
+ st.subheader("Generated SQL Query")
45
+ st.code(sql_query, language="sql")
46
 
47
+ # Execute SQL query to get the results
48
  with engine.connect() as conn:
49
+ result_df = pd.read_sql_query(sql_query, conn)
50
 
51
  # Show query results if any
52
  if not result_df.empty: