o
    RhY                  	   @   s*  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlZd dlmZ d dl
mZ d dlmZmZ d d	lmZ d d
lmZmZmZmZ d dlmZ d dlmZ d dl m!Z!m"Z" d dl#m$Z$m%Z%m&Z& d dl'm(Z( d dl)m*Z* d dl+Z+dZ,dZ-dd Z.d0ddZ/d dlZd dlZdd Z0dd Z1dd Z2e3dkrdZ,dZ4d Z5d!Z6g d"g d#g d$g d%g d&g d'd(d)gg d*d+Z7g d"g d#g d$g d%g d&g d'g d*d,Z8e9e8Z:e;d- e;e: e;d. e1e5e6e:d/\Z<Z=e;e= e;e< e;e>e< dS dS )1    N)OllamaEmbeddings)FAISS)ChatPromptTemplate)AsyncWebCrawler)ChatGroq)PromptTemplate)StrOutputParserPydanticOutputParser)RunnablePassthrough)PyPDFLoader
TextLoaderDocx2txtLoaderUnstructuredPowerPointLoader)	PdfReader)SemanticChunker)	BaseModelField)ListOptionalDict)urlparse)
ChatOllama8gsk_igZbGeSv0MAqutmjrX9HWGdyb3FYc1U6fPEfvHFdLNFytjmyPGUHznomic-embed-textc              
   C   s  t dtdd}g }|D ] }|dd}|dd}|r&|| d|  q|| qd|}td	d
gdd}|j| dddd|ddddd}	z$||	}
|
j	}t
d| t|}|d u riW dS |
jd d }W ||fS  ty } zt
d|  W Y d }~dS d }~ww )Nr   llama-3.3-70b-versatiletemperaturegroq_api_key
model_namename description: 
	skills_dbjob_profilesa
  
        You are an expert in skill-to-job-profile mapping. Your task is to validate,refine  and map skills to job profiles to ensure accuracy, completeness, and clarity.

        ### Input:
         

        ### Available Skills:
        {skills_db}

        ### Job Profiles (with descriptions where available):
        {job_profiles}

        ### Output Format:
        {{  
        "Job Profile 1": ["Skill 1", "Skill 2", "Skill 3"],  
        "Job Profile 2": ["Skill 4", "Skill 5"],  
        "Job Profile 3": ["Skill 6", "Skill 7", "Skill 8"]  
        }}  

        ### Enhanced Mapping Guidelines:
        1. **Leverage Job Descriptions:**  
        - Use the provided job descriptions to better understand the role requirements.  
        - Map skills that align with the specific responsibilities and expertise mentioned in descriptions.  
        
        2. **Ensure Relevance:**  
        - Verify that each skill is appropriately mapped to the correct job profile(s) based on both the role name and description.  
        - Remove any incorrectly assigned skills.  
        
        3. **Check for Completeness:**  
        - Identify and add missing skills relevant to each job profile based on the description context.  
        - For profiles without descriptions, use industry-standard expectations for that role.  
        
        4. **Refine Overlapping Skills:**  
        - If a skill belongs to multiple job profiles, ensure it is correctly included in each where relevant.  
        - Consider the description context to determine skill relevance.  
        
        5. **Improve Consistency & Clarity:**  
        - Standardize skill naming to avoid duplicates with different names.  
        - Use precise and industry-recognized terminology.  
        
        6. **Handle Profiles with/without Descriptions:**  
        - For profiles with descriptions: prioritize skills that match the described responsibilities.  
        - For profiles without descriptions: use standard industry expectations for that role title.

        7. **Include Soft Skills Where Relevant:**
        - Evaluate the job description for behavioral, interpersonal, or communication requirements.
        - Map relevant soft skills such as Communication, Problem Solving, Teamwork, Leadership, or Adaptability when implied or directly stated
        - Do not exclude soft skills if they play a crucial role in client-facing or collaborative roles.
        
        7. **Response Format:**  
        - **ONLY return the JSON object without any explanations, comments, or additional text.**  
        - **DO NOT wrap the JSON in code blocks (```json ... ```).**  
        - Use the exact job profile names as provided in the input.
        input_variablestemplate{{{}}})r#   r$   Raw API response:Nr   token_usagetotal_tokens*An error occurred during chat invocation: )r   GROQ_API_KEYgetappendjoinr   formatreplaceinvokecontentprintextract_json_from_responseresponse_metadata	Exception)r#   job_profiles_datachatformatted_job_profilesprofiler   r    job_profiles_textsystem_promptformatted_promptoutputoutskill_to_JobProfile_mappingtoken_consumede rI   P/var/www/eduai.edurigo.com/doc_train/edurigo_ai/skill_map/prod/jobProfile_map.pymap_skills_to_jobProfile   s>   
;

rK   mixtral-8x7b-32768c              
   C   s   t ddd}tddgdd}|j| |d}z||}|jd	 }|j}	W |	|fS  tyA }
 ztd
|
  W Y d }
~
dS d }
~
ww )Nhttp://127.0.0.1:11434	llama3:8bbase_urlmodelr#   
JobProfileay  
        You are an expert in skill-to-Job Profile mapping. Your task is to analyze a given set of skills and map them to the most appropriate JobProfile.

        ### Input Skills:
        {skills_db}

        ### Designations:
        {JobProfile}

        ### Output Format:
        {{
        "JobProfile 1": ["Skill 1", "Skill 2", "Skill 3"],
        "JobProfile 2": ["Skill 4", "Skill 5"],
        "JobProfile 3": ["Skill 6", "Skill 7", "Skill 8"]
        }}

        ### Notes:
        - Ensure that each skill is mapped only to the most relevant JobProfile(s).
        - If a skill fits multiple JobProfile, include it in both.
        - Use clear and concise mappings.
        - Respond only with the JSON object in the specified format, without any additional text.
        - If you found no skills for some JobProfile check does JobProfile at last one more time

        ### Instructions:
        - **Respond only with the JSON object in the specified format.**
        - **Do not include any additional text, explanations, or code fences (e.g., ```json).**
        - **Ensure that the JSON is properly formatted and valid.**
        r%   )r#   rR   output_tokensr0   r-   )r   r   r5   r7   usage_metadatar8   r<   r9   )r#   rR   r   r   r>   rB   rC   rD   r/   skill_to_jobProfile_mappingrH   rI   rI   rJ   ,map_skills_to_jobProfile_without_description   s&   !

rV   c              
   C   s   z'|  d}| d}|dkr!|dkr!| ||d  }t|W S td W d S  tyA } ztd|  W Y d }~d S d }~ww )Nr(   r*      z#Could not find JSON object in text.z"Error extracting or parsing JSON: )findrfindjsonloadsr9   r<   )textstartendjson_strrH   rI   rI   rJ   r:      s   

r:   c              
   C   s.  t dtdd}tj|dd}g }|D ] }|dd}|dd}|r-|| d	|  q|| qd
|}	tg ddd}
|
j| 	dd	dd|		dd	dd|d}z$|
|}|j}td| t|}|d u rqW dS |jd d }W ||fS  ty } ztd|  W Y d }~dS d }~ww )Nr   r   r   Fensure_asciir   r   r    r!   r"   )r#   r$   mapped_skillsaw	  
        You are an expert in skill-to-job-profile mapping. Your task is to validate and refine a given mapping of skills to job profiles to ensure accuracy, completeness, and clarity.

        ### Input:
        - **Existing Mapped Skills** (JSON format):  
        {mapped_skills}  

        ### Available Skills:
        {skills_db}

        ### Job Profiles (with descriptions where available):
        {job_profiles}

        ### Output Format:
        {{  
        "Job Profile 1": ["Skill 1", "Skill 2", "Skill 3"],  
        "Job Profile 2": ["Skill 4", "Skill 5"],  
        "Job Profile 3": ["Skill 6", "Skill 7", "Skill 8"]  
        }}  

        ### Enhanced Mapping Guidelines:
        1. **Leverage Job Descriptions:**  
        - Use the provided job descriptions to better understand the role requirements.  
        - Map skills that align with the specific responsibilities and expertise mentioned in descriptions.  
        
        2. **Ensure Relevance:**  
        - Verify that each skill is appropriately mapped to the correct job profile(s) based on both the role name and description.  
        - Remove any incorrectly assigned skills.  
        
        3. **Check for Completeness:**  
        - Identify and add missing skills relevant to each job profile based on the description context.  
        - For profiles without descriptions, use industry-standard expectations for that role.  
        
        4. **Refine Overlapping Skills:**  
        - If a skill belongs to multiple job profiles, ensure it is correctly included in each where relevant.  
        - Consider the description context to determine skill relevance.  
        
        5. **Improve Consistency & Clarity:**  
        - Standardize skill naming to avoid duplicates with different names.  
        - Use precise and industry-recognized terminology.  
        
        6. **Handle Profiles with/without Descriptions:**  
        - For profiles with descriptions: prioritize skills that match the described responsibilities.  
        - For profiles without descriptions: use standard industry expectations for that role title.
        
        7. **Response Format:**  
        - **ONLY return the JSON object without any explanations, comments, or additional text.**  
        - **DO NOT wrap the JSON in code blocks (```json ... ```).**  
        - Use the exact job profile names as provided in the input.
        r%   r(   r)   r*   r+   r,   r-   r.   r/   r0   )r   r1   r[   dumpsr2   r3   r4   r   r5   r6   r7   r8   r9   r:   r;   r<   )r#   r=   rc   r>   formatted_mapped_skillsr?   r@   r   r    rA   rB   rC   rD   rE   rF   rG   rH   rI   rI   rJ   "map_skills_to_jobProfile_non_empty   sB   
7

rf   c              
   C   s   t ddd}tj|dd}tg ddd}|j| d	d
dd|d	d
dd|d}z"||}|j}td| t	|}	|	d u rFW dS |j
d }
W |	|
fS  tyi } ztd|  W Y d }~dS d }~ww )NrM   rN   rO   Fra   r#   rR   rc   a  
        You are an expert in skill-to-JobProfile mapping. Your task is to validate and refine a given mapping of skills to JobProfile to ensure accuracy, completeness, and clarity.
    
        ### Input:
        - **Existing Mapped Skills** (JSON format):  
        {mapped_skills}  

        ### Input Skills:
        {skills_db}

        ### JobProfile:
        {JobProfile}

        ### Output Format:
        {{  
        "JobProfile 1": ["Skill 1", "Skill 2", "Skill 3"],  
        "JobProfile 2": ["Skill 4", "Skill 5"],  
        "JobProfile 3": ["Skill 6", "Skill 7", "Skill 8"]  
        }}  

        ### Validation & Enhancement Guidelines:
        1. **Ensure Relevance:**  
        - Verify that each skill is appropriately mapped to the correct JobProfile(s).  
        - Remove any incorrectly assigned skills.  
        2. **Check for Completeness:**  
        - Identify and add missing skills relevant to each JobProfile.  
        - Ensure no important skills are overlooked.  
        3. **Refine Overlapping Skills:**  
        - If a skill belongs to multiple JobProfile, ensure it is correctly included in each.  
        - Avoid redundant mappings unless justified.  
        4. **Improve Consistency & Clarity:**  
        - Standardize skill naming to avoid duplicates with different names.  
        - Use precise and industry-recognized terminology.  
        5. **Handle Unmapped JobProfile:**  
        - If a JobProfile lacks sufficient mapped skills, reevaluate and attempt to match relevant ones.  
        6. **Respond in Valid JSON Format:**  
        - **ONLY return the JSON object without any explanations, comments, or additional text.**  
        - **DO NOT wrap the JSON in code blocks (```json ... ```).**  
        r%   r(   r)   r*   r+   r,   r-   rS   r0   )r   r[   rd   r   r5   r6   r7   r8   r9   r:   rT   r<   )r#   rR   rc   r>   re   rB   rC   rD   rE   rF   rG   rH   rI   rI   rJ   6map_skills_to_jobProfile_non_empty_without_descriptionG  s8   ,

rh   __main__Infrastructurea}  
      Budgeting,
      Cost Estimation,
    
      Civil Engineering,
      Structural Analysis,
   
      AutoCAD,
      Revit,
    
      Communication,
      Leadership,
      Teamwork,
      Urban Design,
      Water Supply and Distribution
      , SQL, C++ , Management, Housekeeping Management,Front Desk Management,Complaint Handling,Recruitment,Training & Development
    zZ

        HR, Developer, Manager, Planner,CEO, Infrastructure Deverloper
        
        )zTime ManagementzDecision MakingIndustry TrendsSector ExpertiseBusiness AcumenStakeholder CommunicationRisk ManagementzAgile Methodologies
LeadershipTeamworkAdaptability)Data EncryptionIncident ResponseCloud ArchitectureCloud MigrationCloud MonitoringNetwork SecurityProgramming LanguagesPy)	NegotiationEmpathyrp   ro   rm   rl   rk   rn   
Creativity)	r}   rl   r|   rp   rn   ro   rm   rk   r{   )rz   ry   Software TestingVersion Control)rx   rz   ry   rs   rt   Communicationrn   )r~   r   ry   rv   rw   rz   ru   )ManagerEngineerCEO
CO-Founder	DeveloperSenior DeveloperzHR HeadProduct Engineer)r   r   r   r   r   r   r   OPOLKLrg   )rL   r   )?ostimerandomrer[   requestslangchain_community.embeddingsr    langchain_community.vectorstoresr   langchain_core.promptsr   crawl4air   asynciolangchain_groqr   r   langchain_core.output_parsersr   r	   langchain_core.runnablesr
   $langchain_community.document_loadersr   r   r   r   PyPDF2r   $langchain_experimental.text_splitterr   pydanticr   r   typingr   r   r   urllib.parser   langchain_ollamar   shutilr1   OLLAMA_MODELrK   rV   r:   rf   rh   __name__industryskillsetrR   mappedrc   rd   	json_datar9   
skill_treerG   typerI   rI   rI   rJ   <module>   s    
m>i
s
K

L 