o
    ih/                     @   s*  d Z ddlZddlZddlmZmZ ddlmZ ejej	d e
eZdededed	ee d
edee dee defddZdd Zedkrz+eddddddgdgddddgdddgdgd Zed! eejed"d# W dS  ey Z zed$ee d% e  W Y dZ[dS dZ[ww dS )&zc
Skill analysis for TNA system using LLM
Follows department.py pattern - simple, direct, effective
    N)DictList)
ChatOllama)level
user_query
admin_namedepartment_namefiltered_skillstotal_users_in_departmentall_skills_dataoffline_coursesreturnc                 C   sz  zt ddd}d}|dkrF|rFg }	|r&t|dkr&d|d v r&|d d }	|	rFd| d	t|	 d
| d}|ddd |	dd D 7 }d}
|rbdt| d}
|
ddd |dd D 7 }
d}|rdt| d}|dd D ]#}|dg }|rd|nd}|d|d  d|d  d| d7 }qtd|  d| d|
 d}||j }tdt|  z7d |v rd!|v r|	d }|
d!d" }||| }t|}|d#g }|d$g }|d%g }d&d |D }d'd |D }d(d |D }t|t| t| }g }|d)|  d*| d+ |rE|dkrE|d,| d- |D ]}|d.|d/  d0|d1   q-|d |rg|d2 |D ]}|d.|d/  d0|d1   qO|d |r|d3 |D ]}|d.|d/  d0|d1   qq|d |d4| d5 g }|D ]}|i |d6d7i q|D ]}|i |d6d8i q|D ]}|i |d6d9i qd||g g d:}td;| d<t| d=t| d>t| d?	 |W W S W n tjtfy } ztd@|  W Y d}~nd}~ww d)|  dAg g g d:W S  ty< } ztdB|  dCg g t|dDW  Y d}~S d}~ww )Ezc
    Analyze user query and recommend skills and courses.
    Simple. Direct. No overthinking.
    z
gemma3:12bg?)modeltemperature Nullr   skillsz
Department (z
) Skills (z skills from z	 users):

c                 S   s   g | ]}d | qS )-  .0skillr   r   M/var/www/eduai.edurigo.com/doc_train/edurigo_ai/TNA/testing/skill_analysis.py
<listcomp>#   s    z.analyze_skills_and_courses.<locals>.<listcomp>N
   z
All Organization Skills (z	 total):
c                 S   s   g | ]	}d |d  qS )r   
skill_namer   r   r   r   r   r   )   s       z
Available Offline Courses (z, zNo skills listedr   course_namez (ID: 	course_idz) - Skills: zLYou are analyzing skills for a Training Needs Analysis request.

USER GOAL: ud  

TASK: Recommend 10-15 skills highly relevant to the user's goal.

ANALYSIS STEPS:
1. Evaluate department skills for relevance to user goal (≥70% relevant)
2. Evaluate organization skills for relevance to user goal (≥70% relevant)  
3. If total < 10 skills, suggest external skills relevant to the goal (≥70% relevant)
4. Stop when you reach 10-15 total skills

OUTPUT FORMAT (JSON):
{
  "department_skills": [
    {
      "skill_name": "skill name",
      "relevance_score": 85,
      "reasoning": "why this skill helps achieve the goal"
    }
  ],
  "organization_skills": [
    {
      "skill_name": "skill name", 
      "relevance_score": 75,
      "reasoning": "why this skill helps achieve the goal"
    }
  ],
  "external_skills": [
    {
      "skill_name": "skill name",
      "relevance_score": 78,
      "reasoning": "why this skill helps achieve the goal"
    }
  ]
}

IMPORTANT:
- Only include skills with ≥70% relevance to the user goal
- Aim for 10-15 total skills across all categories
- Focus on skills that directly help achieve the stated goal

Provide ONLY the JSON response, no additional text.z$LLM skill analysis response length: {}   department_skillsorganization_skillsexternal_skillsc                 S       g | ]}| d ddkr|qS relevance_scorer   F   getr   sr   r   r   r   u        c                 S   r&   r'   r*   r,   r   r   r   r   v   r.   c                 S   r&   r'   r*   r,   r   r   r   r   w   r.   zBased on your goal 'z', here are z recommended skills:
zDEPARTMENT SKILLS (z):u   • r   z - 	reasoningzORGANIZATION SKILLS:zEXTERNAL SKILLS:zTotal: z skills recommendedsource
departmentorganizationexternalstrategic_presentationrecommended_skillscourses_offlineonline_recommended_coursesz(Successfully parsed skill analysis with z skills (dept: z, org: z, ext: )zJSON parse failed: z', we recommend focusing on relevant skill development to achieve your objectives. Please try again for detailed skill recommendations.zSkill analysis failed: z;Unable to complete analysis at this time. Please try again.)r5   r7   r8   error)r   lenjoinr+   invokecontentstriploggerinfoindexrindexjsonloadsappendJSONDecodeError
ValueErrorwarning	Exceptionr:   str)r   r   r   r	   r
   r   r   llmdept_skills_textdept_skill_namesorg_skills_textcourses_textcourseskills_list
skills_strpromptresponse
json_startjson_endjson_strparsed_resultdept_skills
org_skills
ext_skillstotal_skillspresentation_partsr   
all_skillsresulter   r   r   analyze_skills_and_courses   s     &/

"

"

"
0
rb   c                  C   s  dg ddg} ddddddd	d
ddddg}ddddddgdg}ddddddd
ddddgdddddgdddd d!d"d#d$d%g d&d'd(d)gd*}t d+ t d, t d- t d.| d d/   t d0t|  t d1t|  t d2 t tj|d3d4 |S )5z5Test the data processing logic without LLM dependencyr   )Python
JavaScriptCommunicationuser_idr   i_  zcommunication skillskill_idr   i`  ztechnical skillsy   rd   z   rc   43)Python Programming for Complete BeginnerszLearn Python from scratchpythonprogramming)r   r   short_descriptiondescriptionr   zBased on your goal to improve technical ability, we recommend focusing on Python and JavaScript skills from your department, plus additional technical skills from the organization. The Python course directly aligns with your needs.r1   g      "@z,Direct match for technical improvement goals)r   r0   r(   r/   g      !@z$Essential for web development skillsz*Perfect match for Python skill developmentg     V@)r   namereason
percentagezAdvanced JavaScript ConceptsUdemyzJavaScript Expertz6 weeksIntermediatez+Deep dive into advanced JavaScript conceptszhttps://udemy.com/advanced-jsPaidz4.7/5)rd   ES6zAsync ProgrammingU   z(Complements department JavaScript skills)titleplatform
instructordurationskill_levelrq   urlpriceratingskills_coveredr(   rs   r4   z=== SKILL ANALYSIS TEST ===z0User Query: Want to improve my technical abilityzDepartment: DeveloperzDepartment Skills: r   zTotal Org Skills: zAvailable Courses: z
=== MOCK RESPONSE ===   indent)printr;   rD   dumps)test_filtered_skillstest_all_skillstest_coursesmock_responser   r   r   test_skill_analysis_logic   st   	
*r   __main__z$Want to improve my technical abilityAscent	Developerrc   rd   rf      rk   rh   rl   rm   rn   )r   r   r   )r   r   r   r	   r
   r   r   z=== ACTUAL LLM RESPONSE ===r   r   zLLM not available (z), running logic test:)__doc__rD   loggingtypingr   r   langchain_ollamar   basicConfigINFO	getLogger__name__r@   rK   intrb   r   r`   r   r   rJ   ra   r   r   r   r   <module>   sT    

 /R
