forked from NikhilSehgal123/Azure-OpenAI-SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_app_streamlit.py
More file actions
47 lines (36 loc) · 1.38 KB
/
main_app_streamlit.py
File metadata and controls
47 lines (36 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# main_app.py
import streamlit as st
import sqlite3
import pandas as pd
import sql_db
from prompts.prompts import SYSTEM_MESSAGE
from azure_openai import get_completion_from_messages
import json
def query_database(query, conn):
""" Run SQL query and return results in a dataframe """
return pd.read_sql_query(query, conn)
# Create or connect to SQLite database
conn = sql_db.create_connection()
# Schema Representation for finances table
schemas = sql_db.get_schema_representation()
st.title("SQL Query Generator with GPT-4")
st.write("Enter your message to generate SQL and view results.")
# Input field for the user to type a message
user_message = st.text_input("Enter your message:")
if user_message:
# Format the system message with the schema
formatted_system_message = SYSTEM_MESSAGE.format(schema=schemas['finances'])
# Use GPT-4 to generate the SQL query
response = get_completion_from_messages(formatted_system_message, user_message)
json_response = json.loads(response)
query = json_response['query']
# Display the generated SQL query
st.write("Generated SQL Query:")
st.code(query, language="sql")
try:
# Run the SQL query and display the results
sql_results = query_database(query, conn)
st.write("Query Results:")
st.dataframe(sql_results)
except Exception as e:
st.write(f"An error occurred: {e}")