o
    .lh)y                     @   sp  d dl Z d dlZd dlmZm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mZ d dl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 Z d dl	m
Z
 d d
lmZmZmZ d dl Z 		dTdededeeeef  deeeef  dedede
dedefddZdeeeef  deeeef  dedefddZdeeeef  deeeef  defddZdeeeef  deeeef  dededef
ddZded ed!efd"d#Zd$d% Z e!d&krd'Z"d(Z#d)Z$d*d+d,d-d.d/d0d1d2d.d3d4d1d5d.d5d6d7d8d.d9d:d1d3d.d2d;d,d<d.gZ%d*d+d=d/d0d=d3d4d=d5d6d=d9d:d=d2d;d=d>d?d=d<d@d=dAdBd=dCdDd=dEdFd=d-dGd=gZ&dHZ'ee"e#e%e&e$e'dIZ(g dJZ)g dKZ*g dLZ+e ,e)Z-e ,e*Z.e ,e+Z/ee-e.e/Z0e e"e(e0Z1e2e1 e2dM e2dN e2dM e2e( ed'd(e%e&dOdPd=dQdRd=d8dSd=g e$dHdIZ3e e"e3e0Z4e2e4 dS dS )U    N)ListOptional)ChatGroq)
ChatOllama)PromptTemplate)LLMChain)r   )PydanticOutputParser)	BaseModelField	validatorValidationError)r   DictAny
gemma3:12bgoaldepartment_namemapping_skills
all_skillstotal_users_in_departmentusernamellm
model_namereturnc                 C   s   |du r
t |dd}t|||}d| d|  d| d| d| d	| d
|  d}	z|	}
||
}t|dr;|j W S t| W S  tyZ } zdt| W  Y d}~S d}~ww )aq  
    Analyze department skills and provide strategic recommendations for admins using Ollama LLM.
    
    Args:
        goal (str): The business challenge or objective for skill development
        department_name (str): Name of the department being analyzed
        mapping_skills (List[Dict]): Skills currently present in the department
            Expected format: [{"skill_id": int, "skill_name": str, "skill_level": str, "skill_count": int}]
            skill_level represents: "Beginner", "Intermediate", "Advanced", "Expert"
            skill_count represents: number of users with this skill at this level
        all_skills (List[Dict]): All available skills in the organization
            Expected format: [{"skill_id": int, "skill_name": str}]
        total_users_in_department (int): Total number of users in the department
        username (str): Admin's name for personalized greeting
        llm (ChatOllama): Pre-configured ChatOllama instance (optional)
        model_name (str): Ollama model name to use (default: gemma3:12b)
    
    Returns:
        str: Strategic skills analysis and recommendations for admin
    Nr   modeltemperaturez
You are an expert business consultant providing strategic skills analysis. You will have a conversational discussion with an admin about their team's skills and provide actionable insights.

**Context:**
- Team Size: z# people
- Business Challenge/Goal: z

**Team Skills Data:**
ap  

**IMPORTANT - Skills Analysis Priority Framework:**
Follow this exact order when analyzing skills:

1. **FIRST: Goal-Driven Analysis** - Prioritize skills that directly align with the business goal/query
2. **SECOND: Focus on Mapped Skills** - Analyze existing department skills and their levels (1=Beginner, 2=Intermediate, 3=Advanced)
3. **THIRD: Leverage Available Skillset** - Consider organizational skills not yet present in the department
4. **FOURTH: External Skills (only if critical gaps remain)** - Suggest additional skills only when mapped and available skills are insufficient

**Your Task:**
Start with "Hi a  !" and then provide a conversational analysis as if you're speaking directly to them. Be warm, professional, and insightful.

**Cover these areas in a conversational way:**

1. **Overall Assessment**: 
   - Give them a quick overview of where the team stands for achieving the goal
   - **Prioritize mapped skills first** - highlight which tracked skills are strongest
   - **Then assess available skillset** - what current capabilities can be leveraged
   - Provide an overall capability rating based on existing skills before suggesting new ones

2. **Detailed Skills Analysis (Follow Priority Order)**:
   
   **Phase 1 - Mapped Skills Deep Dive:**
   - **List the specific mapped skills most relevant to the business goal**
   - Analyze current proficiency levels for each relevant mapped skill
   - **Identify which mapped skills are strong enough to leverage immediately**
   - **Pinpoint which mapped skills need urgent improvement** (be specific about target proficiency levels)
   - Calculate if these mapped skills alone can achieve 70%+ of the goal
   
   **Phase 2 - Available Skills Strategic Selection:**
   - **From the available skillset, identify 5-8 specific skills that directly support the business goal**
   - **Rank these available skills by impact potential** (high/medium/low impact on goal achievement)
   - **Specify which team members should develop which available skills** (if known)
   - **Estimate timeline for developing each prioritized available skill** (weeks/months to functional level)
   - Explain how each selected skill will specifically help achieve the goal
   
   **Phase 3 - External Skills (Only if Critical Gaps Remain):**
   - **Only after analyzing mapped + available skills, identify any critical skill gaps**
   - **List 3-5 specific external skills needed** (no generic suggestions)
   - **Justify why each external skill is essential** and cannot be substituted by available skills
   - **Prioritize external skills by urgency** (must-have vs nice-to-have)
   - Suggest specific acquisition methods (training, hiring, consulting)

3. **Strategic Skills Recommendations (Be Specific)**:
   - **Immediate Focus (30 days)**: List exact mapped skills to improve with target proficiency increases
   - **Next Quarter**: Specify which available skills to develop and to what level  
   - **Future Planning**: Name specific external skills and acquisition timelines
   - **Skills Training Matrix**: Suggest specific training methods for each prioritized skill
   - **Skills-to-Goal Mapping**: Explain how each recommended skill directly contributes to achieving the business goal

4. **Action Plan**:
   - **Week 1-2**: Inventory and assess current mapped skills proficiency
   - **Month 1**: Focus on improving existing skill gaps
   - **Months 2-3**: Develop secondary available skills
   - **Months 4-6**: Consider external skills only if progress is insufficient
   - **Learning Resources**: I've provided curated courses below that match your priority skills

5. **Risk Discussion**:
   - What happens if you rely only on current skills?
   - Timeline risks for the business goal achievement
   - When external skills become absolutely necessary vs. nice-to-have

6. **Success Tracking**:
   - Measure improvement in existing mapped skills first
   - Track utilization of available but underused skills
   - Monitor goal progress before investing in external skills
   - Set clear thresholds for when external skills become necessary

7. **Recommended Learning Resources**:
   - Reference the curated courses provided below that align with your priority skills
   - Explain how these courses will help develop the specific skills identified in the analysis

**Style Guidelines:**
- Start with "Hi a  !" - be conversational and direct
- **BE SPECIFIC WITH SKILL NAMES** - no generic terms like "technical skills" or "soft skills"
- **USE ACTUAL SKILL DATA** - reference specific skills from the skills_analysis_data
- **QUANTIFY EVERYTHING** - use percentages, proficiency levels, and specific targets
- Use "you" and "your team" throughout
- **Focus on concrete skill recommendations** rather than general advice
- **Explain the "why" behind each skill suggestion** - how it helps achieve the goal
- Keep it engaging but data-driven
- **DO NOT ask questions at the end** - provide complete analysis and recommendations
- **Reference the courses provided below** instead of discussing budget considerations

**Key Principle:** 
Help zq get specific, actionable skill insights. Always name specific skills from the data, explain their relevance to "z", and provide concrete development paths. If the available skills aren't sufficient, be clear about which external skills are needed and why. Focus on skill-by-skill analysis rather than general capability discussions.
contentz"Error generating skills analysis: )r   &_prepare_comprehensive_skills_analysisinvokehasattrr   stripstr	Exception)r   r   r   r   r   r   r   r   skills_analysis_dataanalysis_promptfull_promptresponsee r(   M/var/www/eduai.edurigo.com/doc_train/edurigo_ai/TNA/testing/skill_analysis.pyanalyze_department_skills   sB    R^^a

r*   total_usersc                    s  dd | D  d dg}| d i }| D ]}|d }| d }|d|d	d
}|d tfdddD r?d}	nHtfdddD rMd}	n:tfdddD r[d}	n,tfdddD rid}	ntfdddD rwd}	ntfdddD rd}	nd}	|	|vrg ||	< ||	  |||d  |d!krd"n|d#krd$nd%}
| d& d'| d( d)|d*d+| d,|
 d- q fd.d/|D }|r| d0 | d1t|  |d2d3 D ]}| d&|d   qt|d3kr| d4t|d3  d5 |r{| d6 | D ]i\}	}td7d |D }td8d |D t| }| d9|	 d:|d*d; t|d<d= d>d?}|D ]5}|d@ d!krNdAn
|d@ d#krWdBndC}| dD| dE|dF  d'|dG  d)|d@ d*dH|dI  dJ qCqt| }i dK}| D ]&}|d|d	d
}|d }|vrdK|< |  |7  < ||7 }qdK}|dKkrdLdMdNdOdP}tfdQd| D }||dO  d }| dR | dS|d*dT | dU| dV | dWt| dX | dY dZD ]$}|dK}|dKkr|| d }| d[| d'| d\|d*d] qfd^d/| D }|rX| d_ |d2d` D ]"}|d  d }| da|d  db|d  d( d)|d*d]	 q5dc|S )dzJPrepare comprehensive department skills data for admin-level LLM analysis.c                 S      h | ]}|d  qS skill_idr(   .0skillr(   r(   r)   	<setcomp>       z9_prepare_comprehensive_skills_analysis.<locals>.<setcomp>zDEPARTMENT OVERVIEW: z total people
z#CURRENT DEPARTMENT SKILLS ANALYSIS:skill_countd   skill_level
skill_typeUnknown
skill_namec                 3       | ]	}|   v V  qd S Nlower)r0   techr9   r(   r)   	<genexpr>       z9_prepare_comprehensive_skills_analysis.<locals>.<genexpr>)python
javascriptjavareactnodezc++zc#z.netzProgramming & Developmentc                 3   r:   r;   r<   )r0   devopsr?   r(   r)   r@      rA   )docker
kubernetesawsazuregcprG   zci/cdjenkinszDevOps & Cloudc                 3   r:   r;   r<   )r0   datar?   r(   r)   r@      rA   )sqldatabase	analyticsrN   tableauzpower biexcelzData & Analyticsc                 3   r:   r;   r<   )r0   mgmtr?   r(   r)   r@      rA   )project
management
leadershipagilescrumteamzManagement & Leadershipc                 3   r:   r;   r<   )r0   designr?   r(   r)   r@      rA   )uiuxr[   figma	photoshopcreativezDesign & Creativec                 3   r:   r;   r<   )r0   businessr?   r(   r)   r@      rA   )ra   strategyanalysis
consultingsaleszBusiness & StrategyzOther Technical Skills)namecount
percentagelevelF   Strong(   ModerateLimited     • : /z	 people (.1fz%) at z	 level - z	 Coveragec                       g | ]
}|d   vr|qS r-   r(   r/   current_skill_idsr(   r)   
<listcomp>       z:_prepare_comprehensive_skills_analysis.<locals>.<listcomp>zB
SKILL GAPS - AVAILABLE IN ORGANIZATION BUT MISSING IN DEPARTMENT:zTotal Missing Skills: N        • ... and  more skillsz!
SKILLS DISTRIBUTION BY CATEGORY:c                 s       | ]}|d  V  qdS )rg   Nr(   r/   r(   r(   r)   r@          c                 s   r{   )rh   Nr(   r/   r(   r(   r)   r@      r|   z
  z (Avg Coverage: z%):c                 S   s   | d S )Nrh   r(   )xr(   r(   r)   <lambda>  s    z8_prepare_comprehensive_skills_analysis.<locals>.<lambda>T)keyreverserh   u   🟢u   🟡u   🔴z     rf   rg   z%) - ri   z levelr               )BeginnerIntermediateAdvancedExpertc                 3   s$    | ]\}}  |d | V  qdS )r   N)get)r0   ri   weight)level_distributionr(   r)   r@     s   " z-
STRATEGIC SUMMARY FOR ADMIN DECISION-MAKING:u'     • Department Skill Maturity Score: z!% (Higher = More Advanced Skills)u     • Current Skills Coverage: z different skillsu!     • Potential Skill Expansion: z additional skills availableu     • Skill Level Distribution:)r   r   r   r   z    - z skill instances (z%)c                    s$   g | ]}|d    d dk r|qS )r4   r5      r(   r/   )r+   r(   r)   rv   (  s   $ u6   
⚠️  CRITICAL COVERAGE GAPS (< 30% of department):   u       • z: Only 
)appendr   anylenitemssumsortedjoin)r   r   r+   analysis_partsskills_by_categoryr1   r4   skill_percentager6   categorycoverage_statusmissing_skillsskillstotal_people_in_categoryavg_coveragesorted_skillscoverage_indicatortotal_current_skillstotal_skill_instancesri   rg   maturity_scorelevel_weightsweighted_totalrh   low_coverage_skillsr(   )ru   r   r9   r+   r)   r      s   


(@




 
2
r   c                    sp   dd |D   fdd| D }|sdS dd |dd D }d	 |}t|dkr6|d
t|d  d7 }|S )zTPrepare a summary of skills available in organization but not in current department.c                 S   r,   r-   r(   r/   r(   r(   r)   r2   3  r3   z4_prepare_available_skills_summary.<locals>.<setcomp>c                    rs   r-   r(   r/   rt   r(   r)   rv   4  rw   z5_prepare_available_skills_summary.<locals>.<listcomp>zAAll organizational skills are already present in this department.c                 S   s   g | ]}|d  qS r?   r(   r/   r(   r(   r)   rv   9  r3   N   , z	 ... and rz   )r   r   )r   r   available_skillsskill_namesresultr(   rt   r)   !_prepare_available_skills_summary1  s   
r   	thresholdc                 C   s  t || }g }g }g }dd | D }| D ]5}	|	d | d }
|	d |k r8||	d |	d |
|	ddd q||	d |	d |
|	ddd q|D ]}	|	d	 |vr\||	d  qMg }|r|d
 |D ]'}	|d|	d  d|	d  d| d|	d dd||	d   d|d  d qh|r|d |dd|dd   t|dkr|dt|d  d |r|d |D ]}	|d|	d  d|	d  d| d|	d dd	 qd|S ) zBAnalyze and prioritize skills based on saturation levels and gaps.c                 S   r,   r-   r(   r/   r(   r(   r)   r2   K  r3   z,_analyze_skill_priorities.<locals>.<setcomp>r4   r5   r9   r7   r8   )rf   rg   rh   typer.   z4HIGH PRIORITY - Under-represented skills to develop:ro   rf   z: Currently rg   rq   z (rh   rr   z
%) - Need z more to reach %z/
MEDIUM PRIORITY - Missing skills to introduce:r   N
   ry   rz   zH
LOWER PRIORITY - Well-saturated skills (focus on depth/specialization):rp   z%) - Well coveredr   )intr   r   r   r   )r   r   r+   r   threshold_counthigh_priority_skillslow_priority_skillsr   ru   r1   r   priority_analysisr(   r(   r)   _analyze_skill_prioritiesA  sN   




L

4
r   course_namescourse_descriptions
course_idsc                 C   s@   t | }t |}t |}dd t|||D }d|i}|S )Nc                 S   s   g | ]\}}}|||d qS ))rf   descriptioncourseIdr(   )r0   rf   desccidr(   r(   r)   rv     s    
z.string_course_data_to_json.<locals>.<listcomp>courses)jsonloadszip)r   r   r   course_names_listcourse_descriptions_listcourse_ids_listcourses_listrN   r(   r(   r)   string_course_data_to_jsonz  s   



r   c                 C   s8  | r|st dtddgdd}tg ddd}tdd	d
}|  }t|tr*|}nt|trCtdd |D r>d|}n	t|}nt|}||B }||B }	||d}
|||d}|		|}|	|
}|j
}|j
}|dd }|dd }t|}t|}||dg |di dg d}|S )Nz0Goal/problem and missing skills must be providedr   missing_skills_stra  
        You are a professional learning and development advisor.

        Given the user's business goal and their skills analysis, output the recommendation report in **STRICT JSON FORMAT**.

        ### JSON Output Format:

        {
        "Goal":{
        "goalTitle": "string",
        "course_recommendations_online": [
            {
            "platform": "Platform name",
            "course_name": "Course title",
            "level": "Beginner / Intermediate / Advanced",
            "why_recommended": "Reason why the course is useful",
            "relevance_score": integer (0-100),
            "link": "Course URL or 'Available on [Platform]'"
            }
        ]
        
        }
        }

        **Rules:**
        - Only return valid JSON, no markdown, no headings.
        - Use double quotes "" around all keys and string values.
        - Ensure the JSON is syntactically correct (ready for `json.loads()`).
        - If a field is missing, use an empty string "" or empty array [].
        - Only recommend courses with relevance_score more than 75
        - Provide 8-10 courses maximum

        ---

        ### Inputs:

        Goal / Problem:
        {goal}

        Skills analysis:
        {missing_skills_str}
        )input_variablestemplate)r   r   course_data_jsona-  
        You are a professional learning and development advisor.

        You are given:
        - A business goal
        - Analysis of skills
        - A database of available courses (with name, id, and description)

        ### Business Goal:
        {goal}

        ### Skill analysis:
        {missing_skills_str}

        ### Available Courses:
        {course_data_json}

        **PRIORITY FRAMEWORK FOR COURSE SELECTION:**
        1. **PRIMARY**: Courses that directly support the business goal
        2. **SECONDARY**: Courses that address skill gaps identified in the analysis
        3. **TERTIARY**: Courses that enhance existing skill levels (beginner to intermediate/advanced)
        
        Your task is to:
        1. Evaluate each course for alignment with the goal and skills analysis using the priority framework above.
        2. Select the most relevant courses (minimum 5) giving highest priority to goal alignment.
        3. For each course, return ONLY the following fields:
           - "course_name": string
           - "course_id": string
           - "reason_to_recommend": string (explain how it supports the goal)
           - "relevance_score": integer (0-100, with goal alignment weighted heavily)

        Only return JSON Output.

        class SimpleOfflineCourseRecommendation(BaseModel):

        ### JSON Output Format:

        {
          "goaltitle": "string",
          "course_recommendations_offline": [
            {
              "course_name": "Course title",
              "course_id": "Course ID from course_data_json",
              "why_recommended": "Reason why the course is useful",
              "relevance_score": integer (0-100),
              "link": "Course URL or 'Available on [Platform]'"
            }
          ]
        }

        **Rules:**
        - Only return valid JSON, no markdown, no headings.
        - Use double quotes "" around all keys and string values.
        - Ensure the JSON is syntactically correct (ready for `json.loads()`).
        - Only include courses with relevance_score more than 75
        r   r   r   c                 s   s    | ]}t |tV  qd S r;   )
isinstancer!   )r0   itemr(   r(   r)   r@     s    z<recommend_online_offline_courses_goal_new.<locals>.<genexpr>r   )r   r   z```jsonz```course_recommendations_offlineGoalcourse_recommendations_online)r   r   r   )
ValueErrorr   r   r    r   r!   listallr   r   r   r   r   r   )r   r   courses_availablecourse_recommendation_prompt2'course_recommendation_prompt_simplifiedr   goal_strr   online_course_extraction_chainoffline_course_extraction_chaininput_data_onlineinput_data_offlinerecommendations_offlinerecommendations_onlinerecommendations_offline_data1recommendations_online_data1recommendations_offline_data2recommendations_online_data2recommendations_offline_datarecommendations_online_datafinal_outputr(   r(   r)   )recommend_online_offline_courses_goal_new  sX   .=







r   __main__zOur goal is to improve customer satisfaction scores by 20% in the next 6 months. Please identify training needs that can help frontline employees and support teams contribute to this goal.Engineeringr   r   Pythonr      )r.   r9   r7   r4   r   
JavaScriptr      r   zProject Managementr   SQLr   rx   r   DockerReact   )r.   r9      
KubernetesAWS	   DevOpsr   zNode.js   zCI/CDzAgile MethodologyPuru)r   r   r   r   r   r   )zPython for BeginnerszData Science Essentialsz"Machine Learning with Scikit-LearnzDeep Learning with TensorFlowzSales FundamentalszCRM & Customer Engagement)zoLearn the fundamentals of Python programming, including variables, loops, functions, and basic data structures.zqCovers the basics of data analysis, statistics, and visualization using tools like Pandas, NumPy, and Matplotlib.zhBuild and evaluate models using regression, classification, and clustering techniques with Scikit-Learn.zWExplore neural networks, CNNs, and RNNs using TensorFlow to build deep learning models.zMaster the basics of selling, including prospecting, lead qualification, objection handling, and closing techniques to drive revenue growth.zLearn how to effectively use CRM tools to manage leads, track sales pipelines, and build lasting customer relationships through personalized engagement.)123456z<============================================================z(SKILLS ANALYSIS REPORT - ADMIN DASHBOARD   zMachine Learning   zData AnalyticszBusiness Intelligence)Nr   )5r   retypingr   r   pandaspdlangchain_groqr   langchain_ollamar   langchain_core.promptsr   langchain.chainsr   langchain.output_parsersr   langchain_core.pydantic_v1r	   r
   r   r   r   r   r!   r   r*   r   r   floatr   r   r   __name__sample_goalsample_departmentsample_total_userssample_mapping_skillssample_all_skillsr   admin_reportr   r   r   dumpscourse_names_jsoncourse_descriptions_jsoncourse_ids_jsoncourse_datarecommend_coursesprintdata_focused_reportrecommend_courses_2r(   r(   r(   r)   <module>   s   		
  
.|"
9 
2
			


