import json from config.settings import OPENAI_API_KEY, MODEL_NAME, TEMPERATURE, MAX_TOKENS def analyze_resume_with_openai(resume_text): """ Analyze resume using OpenAI API Args: resume_text: Extracted resume text Returns: Dictionary with analysis results """ import openai if not OPENAI_API_KEY: # Return mock data if API key is not configured return get_mock_analysis() openai.api_key = OPENAI_API_KEY prompt = f"""Analyze the following resume and provide a detailed assessment. Return your response as a JSON object with the following structure: {{ "summary": "Brief overview of the candidate", "skills": {{ "technical": ["list of technical skills"], "soft": ["list of soft skills"], "missing": ["important missing skills in this field"] }}, "experience": {{ "highlights": ["key achievements and experiences"], "concerns": ["any gaps or concerns in work history"] }}, "education": {{ "details": "education background description", "relevant": "how relevant is the education" }}, "career_gaps": ["any identified gaps in career progression"], "recommendations": [ "action items to improve resume", "skills to develop", "areas to strengthen" ], "overall_score": {{"rating": 0-100, "explanation": "brief explanation"}}, "job_compatibility": {{ "strengths": ["matched competencies"], "weaknesses": ["areas that need improvement for typical jobs"] }} }} Resume text: {resume_text} Provide ONLY valid JSON response, no additional text.""" try: response = openai.ChatCompletion.create( model=MODEL_NAME, messages=[ {"role": "system", "content": "You are an expert resume analyst and HR consultant."}, {"role": "user", "content": prompt} ], temperature=TEMPERATURE, max_tokens=MAX_TOKENS ) analysis_text = response.choices[0].message.content analysis = json.loads(analysis_text) return analysis except json.JSONDecodeError: # If JSON parsing fails, return mock data return get_mock_analysis() except Exception as e: print(f"Error calling OpenAI API: {e}") return get_mock_analysis() def get_mock_analysis(): """Return mock analysis data for demo purposes""" return { "summary": "Experienced professional with strong technical background and leadership skills. Shows good career progression with relevant certifications.", "skills": { "technical": ["Python", "JavaScript", "SQL", "Git", "Docker", "APIs"], "soft": ["Project Management", "Team Leadership", "Communication", "Problem-solving"], "missing": ["Cloud Architecture (AWS/Azure)", "Machine Learning", "Advanced DevOps"] }, "experience": { "highlights": [ "Led cross-functional team of 5+ developers", "Implemented automated testing increasing code coverage by 40%", "Reduced deployment time by 60% through CI/CD optimization" ], "concerns": ["Gap of 2 months between roles in 2021"] }, "education": { "details": "Bachelor's degree in Computer Science with relevant coursework", "relevant": "Highly relevant to technical roles" }, "career_gaps": [ "Limited experience with emerging technologies", "No mention of continuous learning or certifications in last 2 years" ], "recommendations": [ "Add quantifiable metrics to all achievements", "Include recent certifications or training", "Expand cloud platform experience section", "Consider adding GitHub profile or portfolio link", "Highlight specific results and ROI from projects" ], "overall_score": { "rating": 75, "explanation": "Strong foundational experience with room for growth in emerging technologies" }, "job_compatibility": { "strengths": ["Solid programming fundamentals", "Team collaboration experience"], "weaknesses": ["Limited cloud platform experience", "No mention of recent projects"] } } def format_analysis_for_display(analysis): """ Format analysis dictionary for better display Args: analysis: Dictionary with analysis results Returns: Formatted analysis """ return analysis