You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: app.py
+75-10Lines changed: 75 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -18,10 +18,12 @@
18
18
ifnotDATABASE_URL:
19
19
print('Please set DATABASE_URL in .env file.')
20
20
sys.exit(1)
21
-
openai.api_key=os.getenv('OPENAI_TOKEN')
22
-
ifnotos.getenv('OPENAI_TOKEN'):
23
-
print('Please set OPENAI_TOKEN in .env file.')
24
-
sys.exit(1)
21
+
22
+
ifos.getenv('OPENAI_TOKEN'):
23
+
openai.api_key=os.getenv('OPENAI_TOKEN')
24
+
25
+
ifnotopenai.api_key:
26
+
print('Please set OPENAI_TOKEN in .env file or set token in UI') # Not a critical error
25
27
26
28
# Generate SQL Schema from PostgreSQL
27
29
schema=Schema()
@@ -31,10 +33,10 @@
31
33
@app.get('/')
32
34
defindex():
33
35
"""Show SQL Schema + prompt to ask GPT-3 to generate SQL queries"""
34
-
# Get JSON data (not escaped)
35
-
normalized_json_data=json.dumps(json_data);
36
+
normalized_json_data=json.dumps(json_data)
36
37
returnrender_template(
37
38
'index.html',
39
+
has_openai_key=bool(openai.api_key),
38
40
sql_schema=sql_schema,
39
41
json_data=normalized_json_data
40
42
)
@@ -44,14 +46,25 @@ def generate():
44
46
"""Generate SQL query from prompt + user input"""
45
47
try:
46
48
content=request.json
47
-
print('Content:', content)
48
49
user_input=content['query']
49
50
query_temperture=content['temp']
50
51
selected=content['selected']
51
52
print('Selected tables:', selected)
52
53
print('User input:', user_input)
53
54
print('Query temperture:', query_temperture)
54
55
56
+
ifnotcontent['api_key'] andnotopenai.api_key:
57
+
return {
58
+
'success': False,
59
+
'error': 'Please set OPENAI_TOKEN in .env file or set token in UI'
60
+
}
61
+
62
+
ifcontent['api_key'] andnotopenai.api_key:
63
+
openai.api_key=content['api_key'] # Inject API key from UI
64
+
print('API key was set from UI')
65
+
else:
66
+
print('API key was set from .env file')
67
+
55
68
# Update prompt
56
69
regen_schema=schema.regen(selected)
57
70
new_prompt=f'Given an input question, respond with syntactically correct PostgreSQL. Be creative but the SQL must be correct, not nessesary to use all tables. {regen_schema}'
@@ -90,7 +103,7 @@ def generate():
90
103
print(err)
91
104
return {
92
105
'success': False,
93
-
'sql_query': err
106
+
'error': str(err)
94
107
}
95
108
96
109
@app.post('/run')
@@ -123,13 +136,65 @@ def execute():
123
136
print(err)
124
137
return {
125
138
'success': False,
126
-
'error': err
139
+
'error': str(err)
140
+
}
141
+
exceptExceptionaserr:
142
+
print(err)
143
+
return {
144
+
'success': False,
145
+
'error': str(err)
146
+
}
147
+
148
+
@app.post('/generate_prompt')
149
+
defgenerate_prompt():
150
+
"""Generate prompt from selected tables"""
151
+
try:
152
+
content=request.json
153
+
selected=content['selected']
154
+
query_temperture=content['temp']
155
+
156
+
ifnotcontent['api_key'] andnotopenai.api_key:
157
+
return {
158
+
'success': False,
159
+
'error': 'Please set OPENAI_TOKEN in .env file or set token in UI'
160
+
}
161
+
162
+
ifcontent['api_key'] andnotopenai.api_key:
163
+
openai.api_key=content['api_key']
164
+
165
+
# Update prompt
166
+
regen_schema=schema.regen(selected)
167
+
final_prompt=f'Your task is create creative prompt, using this scheme of database: {regen_schema}\n\nDo not generate SQL query, generate text based prompt:\n\n'
0 commit comments