o
     )riG                     @   sn   d dl mZ d dlZd dlmZ eejdZdedefddZ	d	ed
edefddZ
d
ededefddZdS )    )OpenAIN)settings)api_keytext_contentreturnc              
   C   s   d| dd  d}z!t jjjddddd	|dgd
did}|jd jj}t|W S  t	yF } zt
d|  i W  Y d}~S d}~ww )zM
    Sends the resume text content to OpenAI to extract structured data.
    u_  
    You are a professional resume parser. Extract the following information from the resume text provided below.
    Return the output in a strictly valid JSON format with the following keys:
    - is_resume (boolean, true if the text appears to be a resume/CV, false otherwise)
    - name (string)
    - email (string)
    - phone (string)
    - location (string)
    - summary (string)
    - experience (string, format as a perfect list of bullet points using '-' or '•')
    - education (string, format as a perfect list of bullet points using '-' or '•')
    - skills (string, format as a perfect list of bullet points using '-' or '•')
    - certifications (string, format as a perfect list of bullet points using '-' or '•')
    - languages (string, format as a perfect list of bullet points using '-' or '•')
    - projects (string, format as a perfect list of bullet points using '-' or '•'. Thoroughly scan for standalone 'Projects' sections, 'Academic Projects', 'Personal Projects', or key projects mentioned within 'Professional Experience'. Extract all relevant project details.)
    - others (string, format as a perfect list of bullet points. Extract ALL other information present in the resume that does not fit into the categories above, such as Awards, Hobbies, Volunteering, References, Publications, etc.)
    - linkedin_url (string)
    - github_url (string)
    
    CRITICAL: For experience, education, skills, certifications, languages, projects, and others, ensure the output is well-structured with clear, perfect bullet points. If a field is not found, set it to null.

    Resume Text:
    Ni:  z 
    gpt-4o-minisystemz<You are a helpful assistant that extracts data from resumes.rolecontentusertypejson_objectmodelmessagesresponse_formatr   z"Error parsing resume with OpenAI: )clientchatcompletionscreatechoicesmessager   jsonloads	Exceptionprint)r   promptresponser   e r    L/var/www/html/Resume-Parser/resume-parser-inhouse/services/openai_service.pyparse_resume_content   s$   
	r"   resume_textjd_textc              
   C   s   d|dd  d| dd  d}zt jjjdd|d	gd
did}t|jd jjW S  t	yK } zt
d|  ddg dW  Y d}~S d}~ww )zG
    Compares a resume against a JD and returns a score and pitch.
    a.  
    You are an expert HR recruiter. Compare the following Resume against the Job Description (JD).
    
    Return a strictly valid JSON with:
    - score (integer, 0-100)
    - pitch (string, exactly 2 sentences explaining why they are a good fit)
    - missing_skills (list of strings)
    
    JD: Ni  z
    
    Resume: i'  z
    r   r   r	   r   r   r   r   zError matching JD: zError during matching.)scorepitchmissing_skills)r   r   r   r   r   r   r   r   r   r   r   )r#   r$   r   r   r   r    r    r!   match_resume_to_jd4   s"   



r(   resumes_datac              
   C   s   d|  dt | d}z%tjjjddddd|dgd	d
id}|jd jj}t 	|
dg W S  tyL } ztd|  g W  Y d}~S d}~ww )z
    Ranks multiple resumes against a JD in a single LLM call for efficiency.
    Returns list of dicts with: id, score, pitch, missing_skills.
    zN
    You are an expert technical recruiter. 
    JOB DESCRIPTION / ROLE:
    "z"
    
    CANDIDATES:
    a}  
    
    INSTRUCTIONS:
    1. Analyze each candidate against the Job Description.
    2. Assign a relevance score (0-100).
    3. Write a 'pitch' (2 sentences explaining the fit).
    4. List 'missing_skills' (key skills from JD that are absent).
    5. Return a JSON object with a key "rankings" containing a list of objects.
       Each object MUST verify the 'id' from the candidate input.
       
    Output Schema:
    {
        "rankings": [
            {
                "id": <id>,
                "score": <int>,
                "pitch": <string>,
                "missing_skills": [<string>]
            }
        ]
    }
    r   r   z6You are a precise ranking assistant. Output JSON only.r	   r   r   r   r   r   rankingszError in bulk ranking: N)r   dumpsr   r   r   r   r   r   r   r   getr   r   )r$   r)   r   r   r   r   r    r    r!   bulk_rank_resumesO   s(   r-   )openair   r   databaser   OPENAI_API_KEYr   strdictr"   r(   listr-   r    r    r    r!   <module>   s    -