Tamannathakur commited on
Commit
e47a949
·
verified ·
1 Parent(s): 54f58f2

Update prompts.py

Browse files
Files changed (1) hide show
  1. prompts.py +79 -2
prompts.py CHANGED
@@ -302,15 +302,30 @@ def validate_ai_response(question, ai_response_dict):
302
  """
303
  SAFETY CHECK: Remove plot object if question doesn't ask for visualization
304
  Call this function AFTER getting AI response and BEFORE using it
 
305
  """
306
  if not should_allow_visualization(question):
307
  # Force remove plot if it shouldn't exist
308
  if "plot" in ai_response_dict and ai_response_dict["plot"] is not None:
309
- print(f"⚠️ WARNING: Removing unwanted visualization from AI response")
310
  ai_response_dict["plot"] = None
311
 
312
  return ai_response_dict
313
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
  def get_insights_prompt(context_parts, narrative):
315
  insights_context = "\n".join(context_parts)
316
  return f"""Based on the analysis results, provide 4-6 informative bullet points that explain key insights, patterns, and findings from the data.
@@ -325,4 +340,66 @@ Guidelines:
325
  - Each bullet point should be 1-2 complete sentences
326
  - Focus on meaningful patterns and trends
327
  - Provide actionable insights
328
- - Be clear and specific about what the data shows"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  """
303
  SAFETY CHECK: Remove plot object if question doesn't ask for visualization
304
  Call this function AFTER getting AI response and BEFORE using it
305
+ This is a CRITICAL function - it ensures NO visualizations appear unless explicitly requested
306
  """
307
  if not should_allow_visualization(question):
308
  # Force remove plot if it shouldn't exist
309
  if "plot" in ai_response_dict and ai_response_dict["plot"] is not None:
310
+ print(f"⚠️ WARNING: Removing unwanted visualization from AI response for question: '{question}'")
311
  ai_response_dict["plot"] = None
312
 
313
  return ai_response_dict
314
 
315
+ def final_plot_validation(question, plot_object):
316
+ """
317
+ LAST LINE OF DEFENSE: Call this right before returning/displaying plot
318
+ Returns None if visualization should not be shown
319
+ """
320
+ if plot_object is None:
321
+ return None
322
+
323
+ if not should_allow_visualization(question):
324
+ print(f"🚫 BLOCKED: Preventing visualization for non-visualization query: '{question}'")
325
+ return None
326
+
327
+ return plot_object
328
+
329
  def get_insights_prompt(context_parts, narrative):
330
  insights_context = "\n".join(context_parts)
331
  return f"""Based on the analysis results, provide 4-6 informative bullet points that explain key insights, patterns, and findings from the data.
 
340
  - Each bullet point should be 1-2 complete sentences
341
  - Focus on meaningful patterns and trends
342
  - Provide actionable insights
343
+ - Be clear and specific about what the data shows"""
344
+
345
+ """
346
+ ⚠️⚠️⚠️ CRITICAL: HOW TO USE THESE FUNCTIONS IN YOUR MAIN APP ⚠️⚠️⚠️
347
+
348
+ In your main application (the file that calls the AI), you MUST use these validation functions:
349
+
350
+ STEP 1: After getting AI response, validate it immediately:
351
+ ```python
352
+ # After getting response from AI
353
+ ai_response = json.loads(response_text)
354
+
355
+ # CRITICAL: Validate the response to remove unwanted plots
356
+ ai_response = validate_ai_response(user_question, ai_response)
357
+ ```
358
+
359
+ STEP 2: Before displaying/returning plot, do final validation:
360
+ ```python
361
+ # Get plot from validated response
362
+ plot_object = ai_response.get("plot")
363
+
364
+ # FINAL CHECK: Validate plot before using it
365
+ plot_object = final_plot_validation(user_question, plot_object)
366
+
367
+ # Now use plot_object (it will be None if visualization wasn't requested)
368
+ if plot_object is not None:
369
+ # Create and display the visualization
370
+ pass
371
+ else:
372
+ # No visualization to display
373
+ pass
374
+ ```
375
+
376
+ EXAMPLE INTEGRATION:
377
+ ```python
378
+ def process_question(question, df):
379
+ # Get AI response
380
+ response = call_ai_model(question, df)
381
+ ai_response = json.loads(response)
382
+
383
+ # STEP 1: Validate response
384
+ ai_response = validate_ai_response(question, ai_response)
385
+
386
+ # Process operations
387
+ results = process_operations(ai_response["operations"], df)
388
+
389
+ # STEP 2: Validate plot before using
390
+ plot_spec = final_plot_validation(question, ai_response.get("plot"))
391
+
392
+ # Only create visualization if plot_spec is not None
393
+ if plot_spec:
394
+ chart = create_chart(plot_spec, results)
395
+ return results, chart, ai_response["narrative"]
396
+ else:
397
+ return results, None, ai_response["narrative"]
398
+ ```
399
+
400
+ 🔴 KEY POINTS:
401
+ 1. Call `validate_ai_response()` immediately after parsing AI response
402
+ 2. Call `final_plot_validation()` before creating/displaying any chart
403
+ 3. These functions will automatically block visualizations when keywords are missing
404
+ 4. No changes needed to prompts - validation handles everything
405
+ """