Significantly strengthen system prompts for robustness
Browse filesMajor 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>
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 |
-
|
| 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 |
-
|
| 303 |
-
|
| 304 |
-
-
|
| 305 |
-
-
|
| 306 |
-
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
-
|
| 317 |
-
-
|
| 318 |
-
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|