Spaces:
Runtime error
Runtime error
Commit ·
1f51659
1
Parent(s): 7a26ec6
Fixed the fallback issue
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849507 unmodified lines5859606162633 unmodified lines676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216# business_continuity.pyfrom fastapi import APIRouter, HTTPExceptionfrom pydantic import BaseModelfrom typing import List, Optional, Dict, Anyimport osimport openaiimport jsonimport uuid# Environment VariablesGROQ_API_KEY = os.environ.get("GROQ_API_KEY")if GROQ_API_KEY:GROQ_API_KEY = GROQ_API_KEY.strip()# Model Setupdef generate_response(system_prompt: str, user_message: str):if not GROQ_API_KEY:raise Exception("GROQ_API_KEY environment variable is not set")client = openai.OpenAI(api_key=GROQ_API_KEY, base_url="https://api.groq.com/openai/v1")try:response = client.chat.completions.create(model="llama3-8b-8192",messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": user_message}],temperature=0.4)return response.choices[0].message.contentexcept Exception as e:raise Exception(f"GROQ API connection failed: {str(e)}")# Request Modelsclass BusinessProcess(BaseModel):department: strsub_department: Optional[str] = ""process_name: strprocess_description: strclass AnalysisData(BaseModel):impact_analysis: Optional[Dict[str, Any]] = {}minimum_operating_requirements: Optional[Dict[str, Any]] = {}class BusinessContinuityRequest(BaseModel):business_process: BusinessProcessanalysis_data: AnalysisData# Response Modelsclass RecoveryStrategiesResponse(BaseModel):7 unmodified linesthird_party_vendors_unavailability_strategy: strvendor_reasoning: strmessage: str# Business Continuity Routerbusiness_continuity_router = APIRouter()3 unmodified lines"""Generate comprehensive business continuity recovery strategies for a business process"""system_prompt = """You are a business continuity expert with extensive experience in developing recovery strategies for various business processes. Your task is to generate comprehensive, actionable recovery strategies and their reasoning for specific business processes.CRITICAL: You must respond with ONLY a valid JSON object. Do not include any markdown formatting, code blocks, or additional text.For each recovery strategy, you need to:1. Provide detailed, actionable steps that can be immediately implemented2. Consider the specific nature of the business process and its dependencies3. Include realistic timelines and resource requirements4. Reference industry best practices and standards (ISO 22301, NIST, COOP guidelines)5. Provide clear reasoning that explains why each strategy is appropriate for the specific processConsider these factors:- Business process criticality and dependencies- Resource requirements (human, technology, facilities)- Regulatory and compliance considerations- Cost-effectiveness and feasibility- Recovery time objectives (RTO) and recovery point objectives (RPO)- Industry-specific risks and challenges- Stakeholder communication requirements- Testing and maintenance needsProvide specific justifications that reference:- Industry standards and best practices- Historical incident data and lessons learned- Process-specific vulnerabilities and requirements- Resource availability and constraints- Regulatory requirements and compliance needs- Business impact considerations- Technology dependencies and alternativesRESPOND WITH ONLY THIS EXACT JSON FORMAT (no markdown, no code blocks, no additional text):{"people_unavailability_strategy": "Detailed strategy for handling personnel unavailability with specific actionable steps","people_reasoning": "Clear explanation of why this strategy is appropriate for this specific business process","technology_data_unavailability_strategy": "Detailed strategy for handling technology and data unavailability with specific steps","technology_reasoning": "Clear explanation of why this technology strategy fits this process","site_unavailability_strategy": "Detailed strategy for handling site unavailability with actionable steps","site_reasoning": "Clear explanation of why this site strategy is suitable for this process","third_party_vendors_unavailability_strategy": "Detailed strategy for handling vendor/supplier disruptions with specific steps","vendor_reasoning": "Clear explanation of why this vendor strategy is appropriate for this process"}"""# Convert request to JSON for the promptinput_json = {"business_process": {"department": request.business_process.department,"sub_department": request.business_process.sub_department,"process_name": request.business_process.process_name,"process_description": request.business_process.process_description},"analysis_data": {"impact_analysis": request.analysis_data.impact_analysis,"minimum_operating_requirements": request.analysis_data.minimum_operating_requirements}}user_message = f"""You are a business continuity expert. Generate comprehensive recovery strategies AND their reasoning for the business process described in the following JSON data:{json.dumps(input_json, indent=2)}Please analyze the provided business process information and provide specific, actionable recovery strategies for each of the following scenarios. Each strategy should be detailed, practical, and specific to this business process.For EACH strategy, also provide a clear reasoning explanation of WHY this strategy was recommended for this specific process.Strategy Categories Required:1. People Unavailability Strategy - What to do when key personnel are unavailable2. Technology/Data Unavailability Strategy - How to handle IT system failures or data loss3. Site Unavailability Strategy - Plans for when the primary work location is inaccessible4. Third Party Vendors Unavailability Strategy - Contingencies for vendor/supplier disruptionsOutput Requirements:- Format your response as a valid JSON object with these exact keys:- people_unavailability_strategy- people_reasoning- technology_data_unavailability_strategy- technology_reasoning- site_unavailability_strategy- site_reasoning- third_party_vendors_unavailability_strategy- vendor_reasoning- Each strategy should be a detailed string (2-3 sentences minimum) with actionable steps- Each reasoning should explain WHY this strategy was chosen for this specific process (1-2 sentences)- Return ONLY the JSON object, no additional text or formatting"""try:result = generate_response(system_prompt, user_message)# Clean the response - remove markdown code blocks if presentcleaned_result = result.strip()if cleaned_result.startswith('```json'):cleaned_result = cleaned_result[7:] # Remove ```jsonelif cleaned_result.startswith('```'):cleaned_result = cleaned_result[3:] # Remove ```if cleaned_result.endswith('```'):cleaned_result = cleaned_result[:-3] # Remove trailing ```cleaned_result = cleaned_result.strip()# Extract JSON from the responsejson_start = cleaned_result.find('{')json_end = cleaned_result.rfind('}') + 1if json_start != -1 and json_end > json_start:json_str = cleaned_result[json_start:json_end]strategies_data = json.loads(json_str)return RecoveryStrategiesResponse(success=True,people_unavailability_strategy=strategies_data.get("people_unavailability_strategy", ""),people_reasoning=strategies_data.get("people_reasoning", ""),technology_data_unavailability_strategy=strategies_data.get("technology_data_unavailability_strategy", ""),technology_reasoning=strategies_data.get("technology_reasoning", ""),site_unavailability_strategy=strategies_data.get("site_unavailability_strategy", ""),site_reasoning=strategies_data.get("site_reasoning", ""),third_party_vendors_unavailability_strategy=strategies_data.get("third_party_vendors_unavailability_strategy", ""),vendor_reasoning=strategies_data.get("vendor_reasoning", ""),message="Successfully generated comprehensive recovery strategies")else:raise ValueError("No valid JSON found in response")except (json.JSONDecodeError, ValueError) as e:# Fallback response with generic but relevant strategiesprocess_name = request.business_process.process_namedepartment = request.business_process.departmentfallback_response = RecoveryStrategiesResponse(success=True,people_unavailability_strategy=f"Develop cross-training programs for key roles in {process_name}, maintain updated contact lists for temporary staff, and establish clear escalation procedures for critical decision-making. Document all critical processes to ensure knowledge transfer and implement a buddy system for essential personnel.",people_reasoning=f"Cross-training and documentation are essential for {process_name} in {department} to reduce single points of failure and ensure continuity when key personnel are unavailable.",technology_data_unavailability_strategy=f"Implement regular automated data backups for {process_name} systems, establish redundant systems and failover procedures, and maintain updated disaster recovery documentation. Test backup systems monthly and ensure all critical data is replicated to secure off-site locations.",technology_reasoning=f"Data protection and system redundancy are critical for {process_name} operations to minimize downtime and ensure rapid recovery from technology failures.",site_unavailability_strategy=f"Identify and pre-approve alternative work locations for {process_name} operations, enable remote work capabilities with necessary tools and secure access, and establish communication protocols for distributed teams. Maintain emergency supplies and equipment at alternate locations.",site_reasoning=f"Alternative work arrangements ensure {process_name} operations can continue even when primary {department} facilities are inaccessible due to emergencies or disasters.",third_party_vendors_unavailability_strategy=f"Maintain relationships with alternate vendors and suppliers critical to {process_name}, keep emergency contact information updated, and develop contingency contracts for essential services. Regularly assess vendor stability and maintain inventory buffers for critical supplies.",vendor_reasoning=f"Vendor diversification and contingency planning reduce dependency risks for {process_name} and ensure continuity of critical external services and supplies.",message="Generated fallback recovery strategies due to processing error")return fallback_responseexcept Exception as e:raise HTTPException(status_code=500, detail=f"Error generating recovery strategies: {str(e)}")1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081097 unmodified lines1171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402413 unmodified lines245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373# business_continuity.pyfrom fastapi import APIRouter, HTTPExceptionfrom pydantic import BaseModel, Fieldfrom typing import List, Optional, Dict, Anyimport osimport openaiimport jsonimport uuidimport loggingimport tracebackimport refrom dotenv import load_dotenv# Configure logginglogging.basicConfig(level=logging.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')logger = logging.getLogger(__name__)# Load environment variables from .env fileload_dotenv()# Environment VariablesGROQ_API_KEY = os.environ.get("GROQ_API_KEY")if GROQ_API_KEY:GROQ_API_KEY = GROQ_API_KEY.strip()logger.info(f"GROQ_API_KEY loaded: {'Yes' if GROQ_API_KEY else 'No'}")if GROQ_API_KEY:logger.info(f"GROQ_API_KEY preview: {GROQ_API_KEY[:10]}...")# Model Setup with improved error handlingdef generate_response(system_prompt: str, user_message: str, max_retries: int = 3):"""Generate response from GROQ API with retry logic and comprehensive error handling"""if not GROQ_API_KEY:logger.error("GROQ_API_KEY environment variable is not set")raise Exception("GROQ_API_KEY environment variable is not set")logger.info("Initializing GROQ API client")for attempt in range(max_retries):try:logger.info(f"Attempt {attempt + 1}/{max_retries} - Making API call to GROQ")client = openai.OpenAI(api_key=GROQ_API_KEY,base_url="https://api.groq.com/openai/v1",timeout=60.0 # Increased timeout)response = client.chat.completions.create(model="llama3-8b-8192",messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": user_message}],temperature=0.2, # Even lower for more consistent responsesmax_tokens=3000, # Increased further to avoid truncationtop_p=0.9,frequency_penalty=0.0,presence_penalty=0.0)content = response.choices[0].message.contentlogger.info(f"API call successful - Response length: {len(content) if content else 0}")if not content or content.strip() == "":logger.warning(f"Empty response on attempt {attempt + 1}")if attempt == max_retries - 1:raise Exception("Received empty response from GROQ API after all retries")continuereturn contentexcept openai.APITimeoutError as e:logger.warning(f"Timeout error on attempt {attempt + 1}: {str(e)}")if attempt == max_retries - 1:raise Exception(f"GROQ API timeout after {max_retries} attempts: {str(e)}")except openai.APIError as e:logger.error(f"GROQ API error on attempt {attempt + 1}: {str(e)}")if attempt == max_retries - 1:raise Exception(f"GROQ API error after {max_retries} attempts: {str(e)}")except Exception as e:logger.error(f"Unexpected error on attempt {attempt + 1}: {str(e)}")logger.error(f"Traceback: {traceback.format_exc()}")if attempt == max_retries - 1:raise Exception(f"GROQ API connection failed after {max_retries} attempts: {str(e)}")# Request Models with validationclass BusinessProcess(BaseModel):department: str = Field(..., min_length=1, max_length=100)sub_department: Optional[str] = Field(default="", max_length=100)process_name: str = Field(..., min_length=1, max_length=200)process_description: str = Field(..., min_length=1, max_length=1000)class AnalysisData(BaseModel):impact_analysis: Optional[Dict[str, Any]] = Field(default_factory=dict)minimum_operating_requirements: Optional[Dict[str, Any]] = Field(default_factory=dict)class BusinessContinuityRequest(BaseModel):business_process: BusinessProcessanalysis_data: AnalysisData = Field(default_factory=lambda: AnalysisData())# Response Modelsclass RecoveryStrategiesResponse(BaseModel):7 unmodified linesthird_party_vendors_unavailability_strategy: strvendor_reasoning: strmessage: strrequest_id: str = Field(default_factory=lambda: str(uuid.uuid4()))def clean_json_response(response_text: str) -> str:"""Clean and extract JSON from API response"""logger.info("Cleaning JSON response")if not response_text:logger.error("Empty response text provided")raise ValueError("Empty response text")# Remove common markdown formattingcleaned = response_text.strip()# Remove code blocks more aggressivelypatterns = [r'```json\s*', # Matches opening fence with optional spaces after ```jsonr'```', # Matches closing or generic triple backticks]for pattern in patterns:cleaned = re.sub(pattern, '', cleaned, flags=re.MULTILINE | re.DOTALL)cleaned = cleaned.strip()# Try to find complete JSON by counting bracesjson_start = cleaned.find('{')if json_start == -1:logger.error(f"No opening brace found. Response preview: {cleaned[:300]}...")raise ValueError("No JSON opening brace found in response")# Count braces to find the matching closing bracebrace_count = 0json_end = -1for i in range(json_start, len(cleaned)):if cleaned[i] == '{':brace_count += 1elif cleaned[i] == '}':brace_count -= 1if brace_count == 0:json_end = ibreakif json_end == -1:logger.error(f"No matching closing brace found. Response preview: {cleaned[:500]}...")# Try using the last closing brace as fallbackjson_end = cleaned.rfind('}')if json_end == -1 or json_end <= json_start:raise ValueError("No valid JSON structure found in response")json_str = cleaned[json_start:json_end + 1]logger.info(f"Extracted JSON length: {len(json_str)}")logger.info(f"JSON preview: {json_str[:150]}...")logger.info(f"JSON ending: ...{json_str[-50:]}")# Validate that it's at least syntactically correct JSONtry:json.loads(json_str)logger.info("JSON syntax validation passed")except json.JSONDecodeError as e:logger.error(f"JSON syntax validation failed: {str(e)}")logger.error(f"Problematic JSON: {json_str}")raise ValueError(f"Invalid JSON syntax: {str(e)}")return json_strdef validate_strategies_data(data: dict) -> dict:"""Validate and clean strategies data"""logger.info("Validating strategies data")required_fields = ["people_unavailability_strategy","people_reasoning","technology_data_unavailability_strategy","technology_reasoning","site_unavailability_strategy","site_reasoning","third_party_vendors_unavailability_strategy","vendor_reasoning"]# Ensure all required fields exist and are stringsfor field in required_fields:if field not in data:logger.warning(f"Missing field: {field}")data[field] = "Strategy not generated - please retry request"elif not isinstance(data[field], str):logger.warning(f"Invalid type for field {field}: {type(data[field])}")data[field] = str(data[field]) if data[field] is not None else "Strategy not generated"elif len(data[field].strip()) == 0:logger.warning(f"Empty field: {field}")data[field] = "Strategy not generated - please retry request"logger.info("Data validation completed")return datadef generate_fallback_response(request: BusinessContinuityRequest, error_msg: str = "") -> RecoveryStrategiesResponse:"""Generate fallback response when API fails"""logger.info("Generating fallback response")process_name = request.business_process.process_namedepartment = request.business_process.departmentreturn RecoveryStrategiesResponse(success=True,people_unavailability_strategy=f"Implement cross-training programs for critical roles in {process_name}, maintain updated emergency contact lists, and establish clear succession planning. Create detailed process documentation and implement job rotation to reduce single points of failure.",people_reasoning=f"Cross-training and succession planning are essential for {process_name} in {department} to ensure continuity when key personnel are unavailable.",technology_data_unavailability_strategy=f"Establish automated backup systems for {process_name} data, implement redundant infrastructure with failover capabilities, and maintain disaster recovery procedures. Conduct regular backup testing and ensure secure off-site data replication.",technology_reasoning=f"Robust backup and redundancy systems are critical for {process_name} to minimize downtime and ensure rapid recovery from technology failures.",site_unavailability_strategy=f"Identify and prepare alternative work locations for {process_name} operations, enable comprehensive remote work capabilities, and establish emergency communication protocols. Maintain emergency supplies and equipment at backup locations.",site_reasoning=f"Alternative work arrangements ensure {process_name} can continue operating when primary {department} facilities are inaccessible.",third_party_vendors_unavailability_strategy=f"Develop relationships with backup vendors for {process_name} critical services, maintain emergency supplier contacts, and establish contingency contracts. Regularly assess vendor reliability and maintain strategic inventory buffers.",vendor_reasoning=f"Vendor diversification and contingency planning reduce supply chain risks for {process_name} and ensure continuity of essential external services.",message=f"Fallback strategies generated due to API processing error: {error_msg}" if error_msg else "Fallback strategies generated")# Business Continuity Routerbusiness_continuity_router = APIRouter()3 unmodified lines"""Generate comprehensive business continuity recovery strategies for a business process"""request_id = str(uuid.uuid4())logger.info(f"Request {request_id}: Starting recovery strategies generation")logger.info(f"Request {request_id}: Process - {request.business_process.process_name}")logger.info(f"Request {request_id}: Department - {request.business_process.department}")try:# Validate inputif not request.business_process.process_name.strip():logger.error(f"Request {request_id}: Empty process name")raise HTTPException(status_code=400, detail="Process name cannot be empty")if not request.business_process.department.strip():logger.error(f"Request {request_id}: Empty department")raise HTTPException(status_code=400, detail="Department cannot be empty")system_prompt = """You are an expert business continuity consultant. Your task is to generate specific, actionable recovery strategies for business processes.CRITICAL INSTRUCTIONS:1. Respond with ONLY a valid JSON object2. Do not include any markdown formatting, code blocks, or additional text3. Each strategy must be 2-3 detailed sentences with specific actionable steps4. Each reasoning must be 1-2 sentences explaining why the strategy fits the specific processRequired JSON format (no deviations allowed):{"people_unavailability_strategy": "Detailed strategy for handling personnel unavailability with specific steps","people_reasoning": "Clear explanation of why this strategy suits this specific process","technology_data_unavailability_strategy": "Comprehensive strategy for technology and data failures with actionable steps","technology_reasoning": "Explanation of why this technology strategy is appropriate for this process","site_unavailability_strategy": "Detailed strategy for site/facility unavailability with specific actions","site_reasoning": "Why this site strategy is suitable for this specific process","third_party_vendors_unavailability_strategy": "Comprehensive strategy for vendor/supplier disruptions","vendor_reasoning": "Why this vendor strategy is appropriate for this process"}"""user_message = f"""Generate business continuity recovery strategies for this specific process:PROCESS DETAILS:- Process Name: {request.business_process.process_name}- Department: {request.business_process.department}- Sub-Department: {request.business_process.sub_department or 'Not specified'}- Description: {request.business_process.process_description}ANALYSIS DATA:- Impact Analysis: {json.dumps(request.analysis_data.impact_analysis) if request.analysis_data.impact_analysis else 'Not provided'}- Minimum Operating Requirements: {json.dumps(request.analysis_data.minimum_operating_requirements) if request.analysis_data.minimum_operating_requirements else 'Not provided'}Generate comprehensive recovery strategies for these four scenarios:1. Key personnel unavailability2. Technology/data system failures3. Primary site/facility unavailability4. Critical third-party vendor/supplier disruptionsFocus on actionable, specific strategies tailored to this exact process and department context."""logger.info(f"Request {request_id}: Making API call to GROQ")# Get response from APIapi_response = generate_response(system_prompt, user_message)if not api_response:logger.error(f"Request {request_id}: Empty API response")return generate_fallback_response(request, "Empty API response")logger.info(f"Request {request_id}: API response received, length: {len(api_response)}")# Clean and parse JSONtry:logger.info(f"Request {request_id}: Raw API response length: {len(api_response)}")logger.info(f"Request {request_id}: Raw response first 200 chars: {api_response[:200]}")logger.info(f"Request {request_id}: Raw response last 200 chars: {api_response[-200:]}")json_str = clean_json_response(api_response)strategies_data = json.loads(json_str)logger.info(f"Request {request_id}: JSON parsing successful")except (json.JSONDecodeError, ValueError) as e:logger.error(f"Request {request_id}: JSON parsing failed - {str(e)}")logger.error(f"Request {request_id}: Raw response preview: {api_response[:300]}...")return generate_fallback_response(request, f"JSON parsing failed: {str(e)}")# Validate the parsed datatry:strategies_data = validate_strategies_data(strategies_data)response = RecoveryStrategiesResponse(success=True,people_unavailability_strategy=strategies_data["people_unavailability_strategy"],people_reasoning=strategies_data["people_reasoning"],technology_data_unavailability_strategy=strategies_data["technology_data_unavailability_strategy"],technology_reasoning=strategies_data["technology_reasoning"],site_unavailability_strategy=strategies_data["site_unavailability_strategy"],site_reasoning=strategies_data["site_reasoning"],third_party_vendors_unavailability_strategy=strategies_data["third_party_vendors_unavailability_strategy"],vendor_reasoning=strategies_data["vendor_reasoning"],message="Successfully generated comprehensive recovery strategies",request_id=request_id)logger.info(f"Request {request_id}: Successfully generated strategies")return responseexcept Exception as e:logger.error(f"Request {request_id}: Data validation failed - {str(e)}")return generate_fallback_response(request, f"Data validation failed: {str(e)}")except HTTPException:# Re-raise HTTP exceptionsraiseexcept Exception as e:logger.error(f"Request {request_id}: Unexpected error - {str(e)}")logger.error(f"Request {request_id}: Traceback: {traceback.format_exc()}")# Return fallback response instead of raising HTTP errorreturn generate_fallback_response(request, f"Unexpected error: {str(e)}")# Health check endpoint@business_continuity_router.get("/api/business-continuity/health")def health_check():"""Health check endpoint"""return {"status": "healthy","groq_api_configured": bool(GROQ_API_KEY),"timestamp": uuid.uuid4().hex}
business_continuity.py+312-155