Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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.
|
7 |
-
from langchain.chains import
|
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 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
31 |
try:
|
32 |
-
#
|
33 |
-
|
34 |
|
35 |
-
# Display the generated SQL query
|
36 |
-
st.subheader("Generated SQL Query
|
37 |
-
st.
|
38 |
|
39 |
-
# Execute
|
40 |
with engine.connect() as conn:
|
41 |
-
result_df = pd.read_sql_query(
|
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:
|