Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,55 +14,25 @@ import random
|
|
| 14 |
# you need to set your OpenAI API key as environment variable
|
| 15 |
openai.api_key = st.secrets["API_KEY"]
|
| 16 |
|
| 17 |
-
|
| 18 |
-
_id: ObjectId("573a1390f29313caabcd4135"),
|
| 19 |
-
genres: [ 'Short' ],
|
| 20 |
-
runtime: 1,
|
| 21 |
-
cast: [ 'Charles Kayser', 'John Ott' ],
|
| 22 |
-
num_mflix_comments: 0,
|
| 23 |
-
title: 'Blacksmith Scene',
|
| 24 |
-
countries: [ 'USA' ],
|
| 25 |
-
released: ISODate("1893-05-09T00:00:00.000Z"),
|
| 26 |
-
directors: [ 'William K.L. Dickson' ],
|
| 27 |
-
rated: 'UNRATED',
|
| 28 |
-
awards: { wins: 1, nominations: 0, text: '1 win.' },
|
| 29 |
-
lastupdated: '2015-08-26 00:03:50.133000000',
|
| 30 |
-
year: 1893,
|
| 31 |
-
imdb: { rating: 6.2, votes: 1189, id: 5 },
|
| 32 |
-
type: 'movie',
|
| 33 |
-
tomatoes: {
|
| 34 |
-
viewer: { rating: 3, numReviews: 184, meter: 32 },
|
| 35 |
-
lastUpdated: ISODate("2015-06-28T18:34:09.000Z")
|
| 36 |
-
}
|
| 37 |
-
}"""
|
| 38 |
-
|
| 39 |
-
MOVIES_EXAMPLE_QUESTIONS = [
|
| 40 |
(
|
| 41 |
-
"
|
| 42 |
-
"greater than 6.0 are there in this dataset?"
|
| 43 |
),
|
| 44 |
(
|
| 45 |
-
"
|
| 46 |
-
"than IMDB rating? Keep in mind that IMDB goes from 1-10 and tomatoes "
|
| 47 |
-
"only from 1-5, so you need to normalise the ratings to do a fair comparison."
|
| 48 |
),
|
| 49 |
-
"
|
| 50 |
(
|
| 51 |
-
"
|
| 52 |
-
"'1920-1929'. Sort ascending by decade."
|
| 53 |
-
),
|
| 54 |
-
(
|
| 55 |
-
"Find movies that are suitable to watch with my kids, both by genre and their "
|
| 56 |
-
"parental guidance rating. Just recommend good movies."
|
| 57 |
),
|
| 58 |
]
|
| 59 |
|
| 60 |
BASE_CHAT_MESSAGES = [
|
| 61 |
{
|
| 62 |
"role": "system",
|
| 63 |
-
"content": "You are an expert
|
| 64 |
-
"You will accept
|
| 65 |
-
"
|
| 66 |
"return a single code block with the aggregation pipeline without the aggregate command.",
|
| 67 |
}
|
| 68 |
]
|
|
@@ -78,7 +48,7 @@ def ask_model(doc, question):
|
|
| 78 |
messages = BASE_CHAT_MESSAGES + [
|
| 79 |
{
|
| 80 |
"role": "user",
|
| 81 |
-
"content": f"Example
|
| 82 |
}
|
| 83 |
]
|
| 84 |
|
|
@@ -112,10 +82,10 @@ if not "default_question" in st.session_state:
|
|
| 112 |
|
| 113 |
|
| 114 |
st.markdown(
|
| 115 |
-
"""#
|
| 116 |
|
| 117 |
-
This demo app uses OpenAI's GPT-
|
| 118 |
-
aggregation pipeline from
|
| 119 |
|
| 120 |
🚧 The app is experimental and may return incorrect results. Do not enter any sensitive information! 🚧
|
| 121 |
"""
|
|
@@ -126,18 +96,12 @@ aggregation pipeline from an English question and example document.
|
|
| 126 |
col_left, col_right = st.columns(2, gap="large")
|
| 127 |
|
| 128 |
with col_left:
|
| 129 |
-
st.markdown("### Example
|
| 130 |
# wrap textareas in form
|
| 131 |
with st.form("text_inputs"):
|
| 132 |
-
doc = st.text_area(
|
| 133 |
-
"Enter example document from collection, e.g. db.collection.findOne()",
|
| 134 |
-
value=MOVIES_EXAMPLE_DOC,
|
| 135 |
-
height=300,
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
# question textarea
|
| 139 |
-
|
| 140 |
-
label="
|
| 141 |
value=st.session_state.default_question,
|
| 142 |
)
|
| 143 |
|
|
@@ -146,7 +110,7 @@ with col_left:
|
|
| 146 |
if submitted:
|
| 147 |
st.session_state._id = None
|
| 148 |
st.session_state.feedback = False
|
| 149 |
-
st.session_state.response = ask_model(
|
| 150 |
|
| 151 |
|
| 152 |
with col_right:
|
|
|
|
| 14 |
# you need to set your OpenAI API key as environment variable
|
| 15 |
openai.api_key = st.secrets["API_KEY"]
|
| 16 |
|
| 17 |
+
SQL_EXAMPLE_QUERIES = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
(
|
| 19 |
+
"SELECT CustomerName, City FROM Customers;"
|
|
|
|
| 20 |
),
|
| 21 |
(
|
| 22 |
+
"Select distinct Salary from Employee e1 where 2=Select count(distinct Salary) from Employee e2 where e1.salary<=e2.salary;"
|
|
|
|
|
|
|
| 23 |
),
|
| 24 |
+
"Select Employee_name,Salary/12 as ‘Monthly Salary’ from employee;",
|
| 25 |
(
|
| 26 |
+
"Select * from Employee where Rowid= select min(Rowid) from Employee;"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
),
|
| 28 |
]
|
| 29 |
|
| 30 |
BASE_CHAT_MESSAGES = [
|
| 31 |
{
|
| 32 |
"role": "system",
|
| 33 |
+
"content": "You are an expert SQL to MongoDB aggregation pipeline translation system."
|
| 34 |
+
"You will accept SQL query and return a MongoDB aggregation pipeline "
|
| 35 |
+
"that can convert the SQL query. Do not explain the query or add any additional comments, only "
|
| 36 |
"return a single code block with the aggregation pipeline without the aggregate command.",
|
| 37 |
}
|
| 38 |
]
|
|
|
|
| 48 |
messages = BASE_CHAT_MESSAGES + [
|
| 49 |
{
|
| 50 |
"role": "user",
|
| 51 |
+
"content": f"Example SQL Query: {question.strip()}\n\n",
|
| 52 |
}
|
| 53 |
]
|
| 54 |
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
st.markdown(
|
| 85 |
+
"""# SQL to MQL Demo
|
| 86 |
|
| 87 |
+
This demo app uses OpenAI's GPT-3.5 (gpt-3.5) model to generate a MongoDB
|
| 88 |
+
aggregation pipeline from a SQL query.
|
| 89 |
|
| 90 |
🚧 The app is experimental and may return incorrect results. Do not enter any sensitive information! 🚧
|
| 91 |
"""
|
|
|
|
| 96 |
col_left, col_right = st.columns(2, gap="large")
|
| 97 |
|
| 98 |
with col_left:
|
| 99 |
+
st.markdown("### Example SQL query")
|
| 100 |
# wrap textareas in form
|
| 101 |
with st.form("text_inputs"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# question textarea
|
| 103 |
+
query = st.text_area(
|
| 104 |
+
label="SQL query",
|
| 105 |
value=st.session_state.default_question,
|
| 106 |
)
|
| 107 |
|
|
|
|
| 110 |
if submitted:
|
| 111 |
st.session_state._id = None
|
| 112 |
st.session_state.feedback = False
|
| 113 |
+
st.session_state.response = ask_model(query)
|
| 114 |
|
| 115 |
|
| 116 |
with col_right:
|