o
    eRhZa                     @   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 dl m,Z, d dl-mZ d dl m!Z!m"Z" dZ.dZ/dd Z0G dd de!Z1G dd de,e&e2e1f  Z3dd Z4d%ddZ5e6d krdZ.d!Z7d"Z8d#Z9e4e7e8e9d d$Z:e;e: 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)urlparse)
ChatOllama)	RootModel)r	   8gsk_CEh3itIpUAkEkEKsUDqVWGdyb3FYoTjqmXNTBHOSxJFK3obGTzXZznomic-embed-textc           	      C   sZ   |dkrt dtd}ntddd}d}td|fd	g}||B }|| |d
}|j}|S )a3  
    Generates a structured skill tree in JSON format based on the provided prompt text.

    Args:
        prompt_text (str): Input text to describe the industry or domain for skill mapping.
        groq_api_key (str): API key for authenticating with the Groq service.
        model_name (str, optional): Model to use for generating the output. Defaults to "mixtral-8x7b-32768".
        temperature (int, optional): Temperature setting for the model. Defaults to 0 for deterministic output.

    Returns:
        str: The skill tree JSON output as a string.
    r   llama3-70b-8192
model_namegroq_api_keyhttp://127.0.0.1:11434	llama3:8bbase_urlmodelaG  You are an advanced skill mapping assistant. Your task is to analyze inputs such as job roles, project descriptions, and team needs, and produce a structured JSON output in two levels: 
    1. Identify relevant skills (technical, soft, domain-specific).
    2. Perform skill gap analysis by comparing identified skills with existing team skills and providing recommendations.

    ### Formatting Guidelines:
    - Always respond in the listed JSON format.
    - Include **soft skills** as a parent skill category.
    - It should be not more than 2 level arcitecture( parent-child)

    ### Required JSON Format:
    {{
      "Parent Skill": {{
        "description": "Description of the parent skill category.",
        "Skills": {{
          "Child Skill": "Description of the specific skill."
        }}
      }},
      "Parent Skill": {{
        "description": "Description of the parent skill category.",
        "Skills": {{
          "Child Skill": "Description of the specific skill."
        }}
      }},
      "Parent Skill": {{
        "description": "Description of the parent skill category.",
        "Skills": {{
          "Child Skill": "Description of the specific skill."
        }}
      }},
      "Parent Skill": {{
        "description": "Description of the parent skill category.",
        "Skills": {{
          "Child Skill": "Description of the specific skill."
        }}
      }},
      "Parent Skill": {{
        "description": "Description of the parent skill category.",
        "Skills": {{
          "Child Skill": "Description of the specific skill."
        }}
      }}
    }}

    ### Example Skill Tree:
    {{
      "Management": {{
        "description": "Core skills to ensure excellent guest experiences and service.",
        "Skills": {{
          "Front Desk Management": "Handling guest check-ins, check-outs, and reservations.",
          "Customer Service": "Ensuring guests have a pleasant and personalized experience.",
          "Complaint Handling": "Resolving guest issues efficiently and professionally."
        }}
      }},
      "Operations Management": {{
        "description": "Skills to oversee daily hotel operations and logistics.",
        "Skills": {{
          "Housekeeping Management": "Supervising cleaning and room preparation services.",
          "Inventory Management": "Managing stock levels of supplies and essentials.",
          "Quality Assurance": "Ensuring all services meet set quality standards."
        }}
      }},
      "Soft Skills": {{
        "description": "Essential interpersonal and communication skills.",
        "Skills": {{
          "Communication": "Effectively conveying information and ideas.",
          "Leadership": "Guiding and inspiring team members.",
          "Teamwork": "Collaborating with colleagues to achieve goals.",
          "Problem Solving": "Identifying and addressing challenges.",
          "Adaptability": "Quickly adjusting to changes and new situations."
        }}
      }}
    }}

    ### Notes:
    - For every skill tree, ensure the **Soft Skills** category is included 
    - Provide clear and concise descriptions for each parent and child skill.
    - Be consistent with formatting and indentation for readability.
    - Give around 40 child skills and minimum 7 parent skills by default
    - Follow the example structure to ensure accurate and structured output.
    system)humanz/Industry: {industry}
Description: {description})industrydescription)r   GROQ_API_KEYr   r   from_messagesinvokecontent)	r%   r&   GPUchatsystem_promptpromptchainoutput
skill_tree r2   N/var/www/eduai.edurigo.com/doc_train/edurigo_ai/skill_map/testing/skill_map.pygenerate_skill_treee   s   	Rr4   c                   @   s&   e Zd ZU eed< eeef ed< dS )SkillCategoryr&   skillsN)__name__
__module____qualname__str__annotations__r   r2   r2   r2   r3   r5      s   
 r5   c                   @   s   e Zd ZdS )	SkillTreeN)r7   r8   r9   r2   r2   r2   r3   r<      s    r<   c           
   
   C   s   |dkrt dtd}ntddd}t dtdd}tg d	d
d}|j| ||d}z||}|jd d }|j}W ||fS  tyV }	 zt	d|	  W Y d }	~	dS d }	~	ww )Nr   r   r   r   r   r    llama-3.3-70b-versatiletemperaturer   r   )	skills_dbr%   r.   a4  
You are an advanced skill mapping assistant. Your task is to analyze inputs such as job roles, project descriptions, and team needs, and produce a structured JSON output in two levels: 
1. Identify relevant skills (technical, soft, domain-specific) that are not present in the provided skills database.
2. Perform skill gap analysis by comparing identified skills with the database and providing recommendations.

### Input Skills Database:
{skills_db}

### User Input:
{industry}  : Industry

{prompt} : Description for that industry
### Required JSON Format:
{{
  "Parent Skill 1": {{
    "description": "Description of the first parent skill category.",
    "Skills": {{
      "Child Skill 1": "Description of the first specific skill.",
      "Child Skill 2": "Description of the second specific skill."
    }}
  }},
  "Parent Skill 2": {{
    "description": "Description of the second parent skill category.",
    "Skills": {{
      "Child Skill 3": "Description of the third specific skill.",
      "Child Skill 4": "Description of the fourth specific skill."
    }}
  }},
  "Parent Skill 3": {{
    "description": "Description of the third parent skill category.",
    "Skills": {{
      "Child Skill 5": "Description of the fifth specific skill.",
      "Child Skill 6": "Description of the sixth specific skill."
    }}
  }},
  "Parent Skill 4": {{
    "description": "Description of the fourth parent skill category.",
    "Skills": {{
      "Child Skill 7": "Description of the seventh specific skill.",
      "Child Skill 8": "Description of the eighth specific skill."
    }}
  }},
  "Soft Skills": {{
    "description": "Essential interpersonal and communication skills.",
    "Skills": {{
      "Communication": "Effectively conveying information and ideas.",
      "Leadership": "Guiding and inspiring team members.",
      "Teamwork": "Collaborating with colleagues to achieve goals.",
      "Problem Solving": "Identifying and addressing challenges.",
      "Adaptability": "Quickly adjusting to changes and new situations."
    }}
  }}
}}

### Example Skill Tree:
{{
  "Management": {{
    "description": "Core skills to ensure excellent guest experiences and service.",
    "skills": {{
      "Front Desk Management": "Handling guest check-ins, check-outs, and reservations.",
      "Customer Service": "Ensuring guests have a pleasant and personalized experience.",
      "Complaint Handling": "Resolving guest issues efficiently and professionally."
    }}
  }},
  "Operations Management": {{
    "description": "Skills to oversee daily hotel operations and logistics.",
    "skills": {{
      "Housekeeping Management": "Supervising cleaning and room preparation services.",
      "Inventory Management": "Managing stock levels of supplies and essentials.",
      "Quality Assurance": "Ensuring all services meet set quality standards."
    }}
  }},
  "Soft Skills": {{
    "description": "Essential interpersonal and communication skills.",
    "skills": {{
      "Conflict Resolution": "Mediating and resolving disputes effectively.",
      "Time Management": "Organizing and prioritizing tasks efficiently.",
      "Empathy": "Understanding and addressing team members' perspectives.",
      "Negotiation": "Reaching mutually beneficial agreements.",
      "Creativity": "Innovative thinking and problem-solving."
    }}
  }}
}}

### Notes:
- For every skill tree, ensure the **Soft Skills** category is included.
- Provide clear and concise descriptions for each parent and child skill.
- Be consistent with formatting and indentation for readability.
- Generate skills not present in the provided database.
- Ensure the skills are entirely distinct from those in {skills_db} by cross-referencing and excluding existing skills.
- Follow the example structure to ensure accurate and structured output.
- Give around 40 child skills and minimum 7 parent skills, all other than {skills_db} 

### 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.   r@   token_usagetotal_tokens*An error occurred during chat invocation: Nr   )
r   r'   r   r   formatr)   response_metadatar*   	Exceptionprint)
r%   r.   r@   r+   r,   r-   r0   token_consumedr1   er2   r2   r3   &generate_skill_tree_new_skills_with_db   s.    

rN   r=   c              
   C   s   t dtdd}tdgdd}|j| d}z||}|jd d	 }|j}	W |	|fS  tyB }
 ztd
|
  W Y d }
~
dS d }
~
ww )Nr   r=   r>   r%   a  
      You are an expert in business and industry analysis. Based on the industry name provided, generate a concise and informative description explaining what the industry is, the type of products or services it typically offers, and its impact on the economy or society. 
        Ensure the description is written in a professional tone and is no more than 3-4 sentences.

        Industry Name: {industry}

        Description:
        rA   )r%   rD   rE   rF   rG   )	r   r'   r   rH   r)   rI   r*   rJ   rK   )r%   r   r   r?   r,   r-   r.   r0   rL   r1   rM   r2   r2   r3   description_skill  s    

rO   __main__zE-commerce RetailzAn online retail platform selling consumer electronics and accessories. The business involves inventory management, customer service, digital marketing, supply chain coordination, and data analytics to optimize sales and customer experience.a  
      "Budgeting",
      "Cost Estimation",
    
      "Civil Engineering",
      "Structural Analysis",
   
      "AutoCAD",
      "Revit",
    
      "Communication",
      "Leadership",
      "Teamwork",
      "Urban Design",
      "Water Supply and Distribution"
    )r%   r.   r@   r+   )r=   r   )<ostimerandomrejson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   shutilr   langchain.output_parsersr'   OLLAMA_MODELr4   r5   r:   r<   rN   rO   r7   r%   r.   skillsetr1   rK   r2   r2   r2   r3   <module>   sT    F{ 
20