o
    gG/                  	   @   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)Z)dZ*dZ+d(ddZ,d dlZd dlZdd Z-d(ddZ.e/dkrdZ*dZ0dZ1dZ2g dg dg dg d g d!g d"d#d$gg d%d&Z3g dg dg dg d g d!g d"d#d$gg d%d&Z4e5e4Z6e.e1e2e6e*d'\Z7Z8e9e8 e9e7 e9e:e7 dS dS ))    N)OllamaEmbeddings)FAISS)ChatPromptTemplate)AsyncWebCrawler)ChatGroq)PromptTemplate)StrOutputParserPydanticOutputParser)RunnablePassthrough)PyPDFLoader
TextLoaderDocx2txtLoaderUnstructuredPowerPointLoader)	PdfReader)SemanticChunker)	BaseModelField)ListOptionalDict)urlparse8gsk_igZbGeSv0MAqutmjrX9HWGdyb3FYc1U6fPEfvHFdLNFytjmyPGUHznomic-embed-textmixtral-8x7b-32768c              
   C   s   t |||d}tddgdd}|j| |d}z||}|jd d }	|j}
W |
|	fS  tyD } ztd	|  W Y d }~d
S d }~ww )Ntemperaturegroq_api_key
model_name	skills_dbdesignationsa  
        You are an expert in skill-to-designation mapping. Your task is to analyze a given set of skills and map them to the most appropriate designations.

        ### Input Skills:
        {skills_db}

        ### Designations:
        {designations}

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

        ### Notes:
        - Ensure that each skill is mapped only to the most relevant designation(s).
        - If a skill fits multiple designations, 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 designations check does designation 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.**
        input_variablestemplate)r   r   token_usagetotal_tokens*An error occurred during chat invocation: Nr   )r   r   formatinvokeresponse_metadatacontent	Exceptionprint)r   r   r   r   r   chatsystem_promptformatted_promptoutputtoken_consumedskill_to_designation_mappinge r3   ?/var/www/eduai.edurigo.com/skill_map/testing/designation_map.pymap_skills_to_designations   s    !
r5   c                 C   s"   t d| t j}|r|dS dS )z/Extracts the JSON object from a response stringz```json\s*(\{.*?\})\s*```   N)researchDOTALLgroup)response_text
json_matchr3   r3   r4   extract_json_from_responseV   s   
r=   c              
   C   s*  |st dt|||d}tj|dd}tg ddd}|j| dd	d
d|dd	d
d|d}	z,||	}
td|
j	  t
|
j	}|sPtd W dS t|}|
jd d }W ||fS  tjy{ } ztd|  W Y d }~dS d }~w ty } ztd|  W Y d }~dS d }~ww )Nz8Groq API Key is missing. Please provide a valid API key.r   F)ensure_ascii)r   r   mapped_skillsa  
        You are an expert in skill-to-designation mapping. Your task is to validate and refine a given mapping of skills to designations to ensure accuracy, completeness, and clarity.
    
        ### Input:
        - **Existing Mapped Skills** (JSON format):  
        {mapped_skills}  

        ### Input Skills:
        {skills_db}

        ### Designations:
        {designations}

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

        ### Validation & Enhancement Guidelines:
        1. **Ensure Relevance:**  
        - Verify that each skill is appropriately mapped to the correct designation(s).  
        - Remove any incorrectly assigned skills.  
        2. **Check for Completeness:**  
        - Identify and add missing skills relevant to each designation.  
        - Ensure no important skills are overlooked.  
        3. **Refine Overlapping Skills:**  
        - If a skill belongs to multiple designations, 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 Designations:**  
        - If a designation 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   {z{{}z}}zRaw API response: z*Error: No valid JSON found in the responser%   r"   r#   zError decoding JSON response: r$   )
ValueErrorr   jsondumpsr   r&   replacer'   r+   r)   r=   loadsr(   JSONDecodeErrorr*   )r   r   r?   r   r   r   r,   formatted_mapped_skillsr-   r.   r/   	json_datar1   r0   r2   r3   r3   r4   $map_skills_to_designations_non_empty]   s@   ,


	rJ   __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EmpathyrR   rQ   rO   rN   rM   rP   
Creativity)	r_   rN   r^   rR   rP   rQ   rO   rM   r]   )r\   r[   Software TestingVersion Control)rZ   r\   r[   rU   rV   CommunicationrP   )r`   ra   r[   rX   rY   r\   rW   )ManagerEngineerCEOz
CO-Founder	DeveloperzSenior DeveloperzHR HeadzProduct Engineer)r   r   r?   r   )r   r   );ostimerandomr7   rC   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   shutilGROQ_API_KEYOLLAMA_MODELr5   r=   rJ   __name__industryskillsetDesignationmappedr?   rD   rI   
skill_treer0   r+   typer3   r3   r3   r4   <module>   s~    
6
Y
K

L 