Nipun Claude commited on
Commit
6fa1a97
·
1 Parent(s): f1701d3

Significantly strengthen system prompts for robustness

Browse files

Major improvements to system prompt:
- Add mandatory safety & robustness rules with clear validation steps
- Strengthen plot generation requirements with critical saving sequence
- Add comprehensive data validation checks (empty df, missing columns, sufficient data)
- Add operation safety rules (try-except, bounds checking, type conversion)
- Make plot requirements more explicit with step-by-step instructions
- Add debugging helpers for plot issues

This should fix plots not displaying and make code generation much more robust.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. src.py +48 -21
src.py CHANGED
@@ -287,7 +287,7 @@ df["Timestamp"] = pd.to_datetime(df["Timestamp"])
287
 
288
  system_prompt = """Generate Python code to answer the user's question about air quality data.
289
 
290
- IMPORTANT: Only generate Python code - no explanations, no thinking, just clean code.
291
 
292
  AVAILABLE LIBRARIES:
293
  You can use these pre-installed libraries:
@@ -299,26 +299,53 @@ You can use these pre-installed libraries:
299
  Use appropriate libraries for trend analysis, regression, statistical tests, etc.
300
  For simple trends, prefer numpy.polyfit() over complex statistical libraries when possible.
301
 
302
- WHEN TO USE DIFFERENT OUTPUT TYPES:
303
- - Simple questions asking "Which city", "What month" (1-2 values) → TEXT ANSWERS (store text in 'answer')
304
- - Questions asking "Plot", "Show chart", "Visualize" PLOTS (store filename in 'answer')
305
- - Questions with tabular data (lists of cities, rates, rankings, comparisons) → DATAFRAMES (store dataframe in 'answer')
306
- - Examples of DATAFRAME outputs:
307
- * Lists of cities with values (pollution levels, improvement rates)
308
- * Rankings or comparisons across multiple entities
309
- * Any result that would be >5 rows of data
310
- * Calculate/List/Compare operations with multiple results
311
-
312
- SAFETY & ROBUSTNESS RULES:
313
- - Always check if data exists before processing: if df.empty: answer = "No data available"
314
- - Handle missing values: use .dropna() or .fillna() appropriately
315
- - Use try-except blocks for risky operations like indexing
316
- - Validate city/location names exist in data before filtering
317
- - Check for empty results after filtering: if filtered_df.empty: answer = "No data found for specified criteria"
318
- - Use .round(2) for numerical results to avoid long decimals
319
- - Handle division by zero: check denominators before division
320
- - Validate date ranges exist in data
321
- - Use proper string formatting for answers with units (μg/m³)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
 
323
  CRITICAL CODING PRACTICES:
324
 
 
287
 
288
  system_prompt = """Generate Python code to answer the user's question about air quality data.
289
 
290
+ CRITICAL: Only generate Python code - no explanations, no thinking, just clean executable code.
291
 
292
  AVAILABLE LIBRARIES:
293
  You can use these pre-installed libraries:
 
299
  Use appropriate libraries for trend analysis, regression, statistical tests, etc.
300
  For simple trends, prefer numpy.polyfit() over complex statistical libraries when possible.
301
 
302
+ OUTPUT TYPE REQUIREMENTS:
303
+ 1. PLOT GENERATION (for "plot", "chart", "visualize", "show trend", "graph"):
304
+ - MUST create matplotlib figure with proper labels, title, legend
305
+ - MUST save plot: filename = f"plot_{uuid.uuid4().hex[:8]}.png"
306
+ - MUST call plt.savefig(filename, dpi=300, bbox_inches='tight')
307
+ - MUST call plt.close() to prevent memory leaks
308
+ - MUST store filename in 'answer' variable: answer = filename
309
+ - Handle empty data gracefully before plotting
310
+
311
+ 2. TEXT ANSWERS (for simple "Which", "What", single values):
312
+ - Store direct string answer in 'answer' variable
313
+ - Example: answer = "December had the highest pollution"
314
+
315
+ 3. DATAFRAMES (for lists, rankings, comparisons, multiple results):
316
+ - Create clean DataFrame with descriptive column names
317
+ - Sort appropriately for readability
318
+ - Store DataFrame in 'answer' variable: answer = result_df
319
+
320
+ MANDATORY SAFETY & ROBUSTNESS RULES:
321
+
322
+ DATA VALIDATION (ALWAYS CHECK):
323
+ - Check if DataFrame exists and not empty: if df.empty: answer = "No data available"; return
324
+ - Validate required columns exist: if 'PM2.5' not in df.columns: answer = "Required data not available"; return
325
+ - Check for sufficient data: if len(df) < 10: answer = "Insufficient data for analysis"; return
326
+ - Remove invalid/missing values: df = df.dropna(subset=['PM2.5', 'city', 'Timestamp'])
327
+ - Validate date ranges: ensure timestamps are within expected range
328
+
329
+ OPERATION SAFETY (PREVENT CRASHES):
330
+ - Wrap risky operations in try-except blocks
331
+ - Check denominators before division: if denominator == 0: continue
332
+ - Validate indexing bounds: if idx >= len(array): continue
333
+ - Check for empty results after filtering: if result_df.empty: answer = "No data found"; return
334
+ - Convert data types explicitly: pd.to_numeric(), .astype(int), .astype(str)
335
+ - Handle timezone issues with datetime operations
336
+
337
+ PLOT GENERATION (MANDATORY FOR PLOTS):
338
+ - Check data exists before plotting: if plot_data.empty: answer = "No data to plot"; return
339
+ - Always create new figure: plt.figure(figsize=(12, 8))
340
+ - Add comprehensive labels: plt.title(), plt.xlabel(), plt.ylabel()
341
+ - Handle long city names: plt.xticks(rotation=45, ha='right')
342
+ - Use tight layout: plt.tight_layout()
343
+ - CRITICAL PLOT SAVING SEQUENCE:
344
+ 1. filename = f"plot_{uuid.uuid4().hex[:8]}.png"
345
+ 2. plt.savefig(filename, dpi=300, bbox_inches='tight')
346
+ 3. plt.close()
347
+ 4. answer = filename
348
+ - Debug plot issues: print(f"Plot saved: {filename}") for testing
349
 
350
  CRITICAL CODING PRACTICES:
351