Sw1ft0 commited on
Commit
b109b68
·
1 Parent(s): fa73c30

Expand safe_builtins and add them in the prompt

Browse files
Files changed (1) hide show
  1. app.py +31 -24
app.py CHANGED
@@ -149,6 +149,36 @@ def increment_usage_count():
149
  f.write(str(count))
150
 
151
  def answer_question(history, message):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  if get_usage_count() >= MAX_REQUESTS_PER_DAY:
153
  return "OpenAI API usage limit reached for today."
154
  increment_usage_count()
@@ -199,6 +229,7 @@ Rules:
199
  - If result is numeric, round to 2 decimal places.
200
  - If result is a list, return the full list (not just the first element).
201
  - If a required column or metric is missing, return a clear error string in `result` instead of crashing.
 
202
  """
203
 
204
 
@@ -236,30 +267,6 @@ Rules:
236
  # Prepare sandbox
237
  local_vars = {"df1": df1, "df2": df2, "pd": pd}
238
 
239
- # Define a restricted set of built-ins
240
- safe_builtins = {
241
- "abs": abs,
242
- "all": all,
243
- "any": any,
244
- "bool": bool,
245
- "dict": dict,
246
- "float": float,
247
- "int": int,
248
- "len": len,
249
- "list": list,
250
- "max": max,
251
- "min": min,
252
- "range": range,
253
- "str": str,
254
- "sum": sum,
255
- "round": round,
256
- "KeyError": KeyError,
257
- "ValueError": ValueError,
258
- "sorted": sorted,
259
- "enumerate": enumerate,
260
- "zip": zip,
261
- }
262
-
263
  # Execute code
264
  safe_globals = {"__builtins__": safe_builtins, "df1": df1, "df2": df2, "pd": pd}
265
  local_vars = {}
 
149
  f.write(str(count))
150
 
151
  def answer_question(history, message):
152
+ # Define a restricted set of built-ins
153
+ safe_builtins = {
154
+ "abs": abs,
155
+ "all": all,
156
+ "any": any,
157
+ "bool": bool,
158
+ "dict": dict,
159
+ "float": float,
160
+ "int": int,
161
+ "len": len,
162
+ "list": list,
163
+ "max": max,
164
+ "min": min,
165
+ "range": range,
166
+ "str": str,
167
+ "sum": sum,
168
+ "round": round,
169
+ "KeyError": KeyError,
170
+ "ValueError": ValueError,
171
+ "sorted": sorted,
172
+ "enumerate": enumerate,
173
+ "zip": zip,
174
+ "set": set,
175
+ "type": type,
176
+ "isinstance": isinstance,
177
+ "pd": pd,
178
+ "np": np,
179
+ "print": print,
180
+ "Exception": Exception
181
+ }
182
  if get_usage_count() >= MAX_REQUESTS_PER_DAY:
183
  return "OpenAI API usage limit reached for today."
184
  increment_usage_count()
 
229
  - If result is numeric, round to 2 decimal places.
230
  - If result is a list, return the full list (not just the first element).
231
  - If a required column or metric is missing, return a clear error string in `result` instead of crashing.
232
+ - List of allowed functions: {list(safe_builtins.keys())}
233
  """
234
 
235
 
 
267
  # Prepare sandbox
268
  local_vars = {"df1": df1, "df2": df2, "pd": pd}
269
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  # Execute code
271
  safe_globals = {"__builtins__": safe_builtins, "df1": df1, "df2": df2, "pd": pd}
272
  local_vars = {}