Yaswanth-Bolla commited on
Commit
d55dc5e
·
1 Parent(s): 07be4c8

Wrath of the json

Browse files
Files changed (1) hide show
  1. business_continuity.py +16 -27
business_continuity.py CHANGED
@@ -117,22 +117,23 @@ def generate_recovery_strategies(request: BusinessContinuityRequest):
117
  raise HTTPException(status_code=400, detail="Department cannot be empty")
118
 
119
  try:
120
- # Create system prompt - simplified and direct
121
- system_prompt = """You are a business continuity expert. Your response must be a valid JSON object only.
122
- Follow this exact format:
 
123
 
124
  {
125
- "people_unavailability_strategy": "Strategy for when key staff are unavailable (2 sentences).",
126
  "people_reasoning": "Why this strategy works for this process (1 sentence).",
127
- "technology_data_unavailability_strategy": "Strategy for system failures (2 sentences).",
128
  "technology_reasoning": "Why this technology strategy works (1 sentence).",
129
- "site_unavailability_strategy": "Strategy for site unavailability (2 sentences).",
130
  "site_reasoning": "Why this site strategy works (1 sentence).",
131
- "third_party_vendors_unavailability_strategy": "Strategy for supplier disruptions (2 sentences).",
132
  "vendor_reasoning": "Why this vendor strategy works (1 sentence)."
133
  }
134
 
135
- Do not include any other text or formatting in your response."""
136
 
137
  # Create user message - simplified
138
  user_message = f"""Generate business continuity strategies for:
@@ -148,28 +149,16 @@ Description: {request.business_process.process_description}"""
148
  logger.error(f"Request {request_id}: API call failed - {str(e)}")
149
  return create_error_response(request_id, "API error")
150
 
151
- # Parse JSON response with safer approach
152
  try:
153
- # First try direct parsing
154
  strategies_data = json.loads(api_response)
155
  logger.info(f"Request {request_id}: Successfully parsed JSON")
156
- except json.JSONDecodeError:
157
- logger.warning(f"Request {request_id}: Direct JSON parsing failed, trying to clean response")
158
-
159
- # Clean the response - look for JSON inside any markdown or text
160
- try:
161
- # Try to find JSON pattern
162
- json_match = re.search(r'({[\s\S]*?})(?:\s*$|\n)', api_response)
163
- if json_match:
164
- json_text = json_match.group(1)
165
- strategies_data = json.loads(json_text)
166
- logger.info(f"Request {request_id}: Successfully parsed JSON after cleaning")
167
- else:
168
- raise ValueError("Could not find JSON in response")
169
- except Exception as e:
170
- logger.error(f"Request {request_id}: JSON parsing failed after cleaning - {str(e)}")
171
- logger.error(f"Request {request_id}: Raw response preview: {api_response[:300]}")
172
- return create_error_response(request_id, "Could not parse API response")
173
 
174
  # Safely get fields with fallbacks
175
  return RecoveryStrategiesResponse(
 
117
  raise HTTPException(status_code=400, detail="Department cannot be empty")
118
 
119
  try:
120
+ # Create system prompt with strong emphasis on valid JSON output
121
+ system_prompt = """You are a business continuity expert. CRITICAL: You must ONLY output a valid, parsable JSON object and nothing else. No explanations, no formatting, just pure JSON.
122
+
123
+ Your output MUST be in this EXACT format with NO DEVIATIONS:
124
 
125
  {
126
+ "people_unavailability_strategy": "Strategy for when key staff are unavailable (2-3 sentences).",
127
  "people_reasoning": "Why this strategy works for this process (1 sentence).",
128
+ "technology_data_unavailability_strategy": "Strategy for system failures (2-3 sentences).",
129
  "technology_reasoning": "Why this technology strategy works (1 sentence).",
130
+ "site_unavailability_strategy": "Strategy for site unavailability (2-3 sentences).",
131
  "site_reasoning": "Why this site strategy works (1 sentence).",
132
+ "third_party_vendors_unavailability_strategy": "Strategy for supplier disruptions (2-3 sentences).",
133
  "vendor_reasoning": "Why this vendor strategy works (1 sentence)."
134
  }
135
 
136
+ DO NOT add markdown code blocks, explanations, or any text before or after the JSON. ONLY output the raw JSON object."""
137
 
138
  # Create user message - simplified
139
  user_message = f"""Generate business continuity strategies for:
 
149
  logger.error(f"Request {request_id}: API call failed - {str(e)}")
150
  return create_error_response(request_id, "API error")
151
 
152
+ # Parse JSON response directly
153
  try:
154
+ # Simple direct parsing - trust the LLM to format correctly
155
  strategies_data = json.loads(api_response)
156
  logger.info(f"Request {request_id}: Successfully parsed JSON")
157
+ except json.JSONDecodeError as e:
158
+ # If parsing fails, just return the error
159
+ logger.error(f"Request {request_id}: JSON parsing failed - {str(e)}")
160
+ logger.error(f"Request {request_id}: Raw response preview: {api_response[:300]}")
161
+ return create_error_response(request_id, "Invalid JSON response from API")
 
 
 
 
 
 
 
 
 
 
 
 
162
 
163
  # Safely get fields with fallbacks
164
  return RecoveryStrategiesResponse(