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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 rhW dS |
jd }W ||fS  ty } zt	d|  W Y d }~dS d }~ww )Nhttp://127.0.0.1:11434	llama3:8bbase_urlmodel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   output_tokens*An error occurred during chat invocation: )r   getappendjoinr   formatreplaceinvokecontentprintextract_json_from_responseusage_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 rG   S/var/www/eduai.edurigo.com/doc_train/edurigo_ai/skill_map/testing/jobProfile_map.pymap_skills_to_jobProfile   sD   

;

rI   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 )Nr   r   r   r#   
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#   rK   r.   r/   r-   )r   r   r3   r5   r9   r6   r:   r7   )r#   rK   
model_nametemperaturer<   r@   rA   rB   total_tokensskill_to_jobProfile_mappingrF   rG   rG   rH   ,map_skills_to_jobProfile_without_description   s&   !

rP   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loadsr7   r:   )textstartendjson_strrF   rG   rG   rH   r8      s   

r8   c              
   C   s(  t d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 rpW dS |j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/   )r   rU   dumpsr0   r1   r2   r   r3   r4   r5   r6   r7   r8   r9   r:   )r#   r;   r]   r<   formatted_mapped_skillsr=   r>   r   r    r?   r@   rA   rB   rC   rD   rE   rF   rG   rG   rH   "map_skills_to_jobProfile_non_empty   sH   
7

r`   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 )Nr   r   r   Fr[   r#   rK   r]   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-   r.   r/   )r   rU   r^   r   r3   r4   r5   r6   r7   r8   r9   r:   )r#   rK   r]   r<   r_   r@   rA   rB   rC   rD   rE   rF   rG   rG   rH   6map_skills_to_jobProfile_non_empty_without_descriptionG  s8   ,

rb   __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Empathyrj   ri   rg   rf   re   rh   
Creativity)	rw   rf   rv   rj   rh   ri   rg   re   ru   )rt   rs   Software TestingVersion Control)rr   rt   rs   rm   rn   Communicationrh   )rx   ry   rs   rp   rq   rt   ro   )ManagerEngineerCEO
CO-Founder	DeveloperSenior DeveloperzHR HeadProduct Engineer)r{   r|   r}   r~   r   r   r   OPOLKLra   )rJ   r   )?ostimerandomrerU   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   shutilGROQ_API_KEYOLLAMA_MODELrI   rP   r8   r`   rb   __name__industryskillsetrK   mappedr]   r^   	json_datar7   
skill_treerE   typerG   rG   rG   rH   <module>   s    
m>i
s
K

L 