File size: 26,278 Bytes
80a97c8 a3e5843 80a97c8 a3e5843 80a97c8 f89bd21 80a97c8 f89bd21 80a97c8 93f44e2 f89bd21 93f44e2 f89bd21 93f44e2 f89bd21 93f44e2 f89bd21 80a97c8 f89bd21 80a97c8 f89bd21 80a97c8 f89bd21 80a97c8 a3e5843 80a97c8 a3e5843 80a97c8 a3e5843 80a97c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
"""
Skills Identification Agent
Specialized in analyzing user prompts and identifying relevant expert skills based on market analysis
"""
import logging
from typing import Dict, Any, List, Tuple
import json
import re
logger = logging.getLogger(__name__)
class SkillsIdentificationAgent:
def __init__(self, llm_router=None):
self.llm_router = llm_router
self.agent_id = "SKILLS_ID_001"
self.specialization = "Expert skills identification and market analysis"
# Market analysis data from Expert_Skills_Market_Analysis_2024.md
self.market_categories = {
"IT and Software Development": {
"market_share": 25,
"growth_rate": 25.0,
"specialized_skills": [
"Cybersecurity", "Artificial Intelligence & Machine Learning",
"Cloud Computing", "Data Analytics & Big Data",
"Software Engineering", "Blockchain Technology", "Quantum Computing"
]
},
"Finance and Accounting": {
"market_share": 20,
"growth_rate": 6.8,
"specialized_skills": [
"Financial Analysis & Modeling", "Risk Management",
"Regulatory Compliance", "Fintech Solutions",
"ESG Reporting", "Tax Preparation", "Investment Analysis"
]
},
"Healthcare and Medicine": {
"market_share": 15,
"growth_rate": 8.5,
"specialized_skills": [
"Telemedicine Training", "Advanced Nursing Certifications",
"Healthcare Informatics", "Clinical Research",
"Medical Device Technology", "Public Health", "Mental Health Services"
]
},
"Education and Teaching": {
"market_share": 10,
"growth_rate": 3.2,
"specialized_skills": [
"Instructional Design", "Educational Technology Integration",
"Digital Literacy Training", "Special Education",
"Career Coaching", "E-learning Development", "STEM Education"
]
},
"Engineering and Construction": {
"market_share": 10,
"growth_rate": 8.5,
"specialized_skills": [
"Automation Engineering", "Sustainable Design",
"Project Management", "Environmental Engineering",
"Advanced Manufacturing", "Infrastructure Development", "Quality Control"
]
},
"Marketing and Sales": {
"market_share": 10,
"growth_rate": 7.1,
"specialized_skills": [
"Digital Marketing", "Data Analytics",
"Customer Relationship Management", "Content Marketing",
"E-commerce Management", "Market Research", "Sales Strategy"
]
},
"Consulting and Strategy": {
"market_share": 5,
"growth_rate": 6.0,
"specialized_skills": [
"Business Analysis", "Change Management",
"Strategic Planning", "Operations Research",
"Industry-Specific Knowledge", "Problem-Solving", "Leadership Development"
]
},
"Environmental and Sustainability": {
"market_share": 5,
"growth_rate": 15.0,
"specialized_skills": [
"Renewable Energy Technologies", "Environmental Policy",
"Sustainability Reporting", "Ecological Conservation",
"Carbon Management", "Green Technology", "Circular Economy"
]
},
"Arts and Humanities": {
"market_share": 5,
"growth_rate": 2.5,
"specialized_skills": [
"Creative Thinking", "Cultural Analysis",
"Communication", "Digital Media",
"Language Services", "Historical Research", "Philosophical Analysis"
]
}
}
# Skill classification categories for the classification_specialist model
self.skill_categories = [
"technical_programming", "data_analysis", "cybersecurity", "cloud_computing",
"financial_analysis", "risk_management", "regulatory_compliance", "fintech",
"healthcare_technology", "medical_research", "telemedicine", "nursing",
"educational_technology", "curriculum_design", "online_learning", "teaching",
"project_management", "engineering_design", "sustainable_engineering", "manufacturing",
"digital_marketing", "sales_strategy", "customer_management", "market_research",
"business_consulting", "strategic_planning", "change_management", "leadership",
"environmental_science", "sustainability", "renewable_energy", "green_technology",
"creative_design", "content_creation", "communication", "cultural_analysis"
]
async def execute(self, user_input: str, context: Dict[str, Any] = None, **kwargs) -> Dict[str, Any]:
"""
Execute skills identification with two-step process:
1. Market analysis using reasoning_primary model
2. Skill classification using classification_specialist model
"""
try:
logger.info(f"{self.agent_id} processing user input: {user_input[:100]}...")
# Step 1: Market Analysis with reasoning_primary model
market_analysis = await self._analyze_market_relevance(user_input, context)
# Step 2: Skill Classification with classification_specialist model
skill_classification = await self._classify_skills(user_input, context)
# Combine results
combined_data = {
"market_analysis": market_analysis,
"skill_classification": skill_classification,
"user_input": user_input,
"context": context
}
result = {
"agent_id": self.agent_id,
"market_analysis": market_analysis,
"skill_classification": skill_classification,
"identified_skills": self._extract_high_probability_skills(combined_data),
"processing_time": market_analysis.get("processing_time", 0) + skill_classification.get("processing_time", 0),
"confidence_score": self._calculate_overall_confidence(market_analysis, skill_classification)
}
logger.info(f"{self.agent_id} completed with {len(result['identified_skills'])} skills identified")
return result
except Exception as e:
logger.error(f"{self.agent_id} error: {str(e)}")
return self._get_fallback_result(user_input, context)
async def _analyze_market_relevance(self, user_input: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""Use reasoning_primary model to analyze market relevance"""
if self.llm_router:
try:
# Build market analysis prompt with context
market_prompt = self._build_market_analysis_prompt(user_input, context)
logger.info(f"{self.agent_id} calling reasoning_primary for market analysis")
llm_response = await self.llm_router.route_inference(
task_type="general_reasoning",
prompt=market_prompt,
max_tokens=2000,
temperature=0.7
)
if llm_response and isinstance(llm_response, str) and len(llm_response.strip()) > 0:
# Parse LLM response
parsed_analysis = self._parse_market_analysis_response(llm_response)
parsed_analysis["processing_time"] = 0.8
parsed_analysis["method"] = "llm_enhanced"
return parsed_analysis
except Exception as e:
logger.error(f"{self.agent_id} LLM market analysis failed: {e}")
# Fallback to rule-based analysis
return self._rule_based_market_analysis(user_input)
async def _classify_skills(self, user_input: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""Use classification_specialist model to classify skills"""
if self.llm_router:
try:
# Build classification prompt
classification_prompt = self._build_classification_prompt(user_input)
logger.info(f"{self.agent_id} calling classification_specialist for skill classification")
llm_response = await self.llm_router.route_inference(
task_type="intent_classification",
prompt=classification_prompt,
max_tokens=512,
temperature=0.3
)
if llm_response and isinstance(llm_response, str) and len(llm_response.strip()) > 0:
# Parse classification response
parsed_classification = self._parse_classification_response(llm_response)
parsed_classification["processing_time"] = 0.3
parsed_classification["method"] = "llm_enhanced"
return parsed_classification
except Exception as e:
logger.error(f"{self.agent_id} LLM classification failed: {e}")
# Fallback to rule-based classification
return self._rule_based_skill_classification(user_input)
def _build_market_analysis_prompt(self, user_input: str, context: Dict[str, Any] = None) -> str:
"""Build prompt for market analysis using reasoning_primary model with optional context"""
market_data = "\n".join([
f"- {category}: {data['market_share']}% market share, {data['growth_rate']}% growth rate"
for category, data in self.market_categories.items()
])
specialized_skills = "\n".join([
f"- {category}: {', '.join(data['specialized_skills'][:3])}"
for category, data in self.market_categories.items()
])
# Add context information if available (all from cache)
context_info = ""
if context:
session_context = context.get('session_context', {})
session_summary = session_context.get('summary', '') if isinstance(session_context, dict) else ""
user_context = context.get('user_context', '')
interaction_contexts = context.get('interaction_contexts', [])
if session_summary:
context_info = f"\n\nSession Context (session summary): {session_summary[:300]}..."
if user_context:
context_info += f"\n\nUser Context (persona summary): {user_context[:300]}..."
if interaction_contexts:
# Include recent interaction context to understand topic continuity
recent_contexts = interaction_contexts[-2:] # Last 2 interactions
if recent_contexts:
context_info += "\n\nRecent conversation context:"
for idx, ic in enumerate(recent_contexts, 1):
summary = ic.get('summary', '')
if summary:
context_info += f"\n {idx}. {summary}"
return f"""Analyze the following user input and identify the most relevant industry categories and specialized skills based on current market data.
User Input: "{user_input}"
{context_info}
Current Market Distribution:
{market_data}
Specialized Skills by Category (top 3 per category):
{specialized_skills}
Task:
1. Identify which industry categories are most relevant to the user's input (consider conversation context if provided)
2. Select 1-3 specialized skills from each relevant category that best match the user's needs
3. Provide market share percentages and growth rates for identified categories
4. Explain your reasoning for each selection
5. If conversation context is available, consider how previous topics might inform the skill identification
Respond in JSON format:
{{
"relevant_categories": [
{{
"category": "category_name",
"market_share": percentage,
"growth_rate": percentage,
"relevance_score": 0.0-1.0,
"reasoning": "explanation"
}}
],
"selected_skills": [
{{
"skill": "skill_name",
"category": "category_name",
"relevance_score": 0.0-1.0,
"reasoning": "explanation"
}}
],
"overall_analysis": "summary of findings"
}}"""
def _build_classification_prompt(self, user_input: str) -> str:
"""Build prompt for skill classification using classification_specialist model"""
skill_categories_str = ", ".join(self.skill_categories)
return f"""Classify the following user input into relevant skill categories. For each category, provide a probability score (0.0-1.0) indicating how likely the input relates to that skill.
User Input: "{user_input}"
Available Skill Categories: {skill_categories_str}
Task: Provide probability scores for each skill category that passes a 20% threshold.
Respond in JSON format:
{{
"skill_probabilities": {{
"category_name": probability_score,
...
}},
"top_skills": [
{{
"skill": "category_name",
"probability": score,
"confidence": "high/medium/low"
}}
],
"classification_reasoning": "explanation of classification decisions"
}}"""
def _parse_market_analysis_response(self, response: str) -> Dict[str, Any]:
"""Parse LLM response for market analysis"""
try:
# Try to extract JSON from response
json_match = re.search(r'\{.*\}', response, re.DOTALL)
if json_match:
parsed = json.loads(json_match.group())
return parsed
except json.JSONDecodeError:
logger.warning(f"{self.agent_id} Failed to parse market analysis JSON")
# Fallback parsing
return {
"relevant_categories": [{"category": "General", "market_share": 10, "growth_rate": 5.0, "relevance_score": 0.7, "reasoning": "General analysis"}],
"selected_skills": [{"skill": "General Analysis", "category": "General", "relevance_score": 0.7, "reasoning": "Broad applicability"}],
"overall_analysis": "Market analysis completed with fallback parsing",
"method": "fallback_parsing"
}
def _parse_classification_response(self, response: str) -> Dict[str, Any]:
"""Parse LLM response for skill classification"""
try:
# Try to extract JSON from response
json_match = re.search(r'\{.*\}', response, re.DOTALL)
if json_match:
parsed = json.loads(json_match.group())
return parsed
except json.JSONDecodeError:
logger.warning(f"{self.agent_id} Failed to parse classification JSON")
# Fallback parsing
return {
"skill_probabilities": {"general_analysis": 0.7},
"top_skills": [{"skill": "general_analysis", "probability": 0.7, "confidence": "medium"}],
"classification_reasoning": "Classification completed with fallback parsing",
"method": "fallback_parsing"
}
def _rule_based_market_analysis(self, user_input: str) -> Dict[str, Any]:
"""Rule-based fallback for market analysis"""
user_input_lower = user_input.lower()
relevant_categories = []
selected_skills = []
# Pattern matching for different categories
patterns = {
"IT and Software Development": ["code", "programming", "software", "tech", "ai", "machine learning", "data", "cyber", "cloud"],
"Finance and Accounting": ["finance", "money", "investment", "banking", "accounting", "financial", "risk", "compliance"],
"Healthcare and Medicine": ["health", "medical", "doctor", "nurse", "patient", "clinical", "medicine", "healthcare"],
"Education and Teaching": ["teach", "education", "learn", "student", "school", "curriculum", "instruction"],
"Engineering and Construction": ["engineer", "construction", "build", "project", "manufacturing", "design"],
"Marketing and Sales": ["marketing", "sales", "customer", "advertising", "promotion", "brand"],
"Consulting and Strategy": ["consulting", "strategy", "business", "management", "planning"],
"Environmental and Sustainability": ["environment", "sustainable", "green", "renewable", "climate", "carbon"],
"Arts and Humanities": ["art", "creative", "culture", "humanities", "design", "communication"]
}
for category, keywords in patterns.items():
relevance_score = 0.0
for keyword in keywords:
if keyword in user_input_lower:
relevance_score += 0.2
if relevance_score > 0.0:
category_data = self.market_categories[category]
relevant_categories.append({
"category": category,
"market_share": category_data["market_share"],
"growth_rate": category_data["growth_rate"],
"relevance_score": min(1.0, relevance_score),
"reasoning": f"Matched keywords: {[k for k in keywords if k in user_input_lower]}"
})
# Add top skills from this category
for skill in category_data["specialized_skills"][:2]:
selected_skills.append({
"skill": skill,
"category": category,
"relevance_score": relevance_score * 0.8,
"reasoning": f"From {category} category"
})
return {
"relevant_categories": relevant_categories,
"selected_skills": selected_skills,
"overall_analysis": f"Rule-based analysis identified {len(relevant_categories)} relevant categories",
"processing_time": 0.1,
"method": "rule_based"
}
def _rule_based_skill_classification(self, user_input: str) -> Dict[str, Any]:
"""Rule-based fallback for skill classification"""
user_input_lower = user_input.lower()
skill_probabilities = {}
top_skills = []
# Simple keyword matching for skill categories
skill_keywords = {
"technical_programming": ["code", "programming", "software", "development", "python", "java"],
"data_analysis": ["data", "analysis", "statistics", "analytics", "research"],
"cybersecurity": ["security", "cyber", "hack", "protection", "vulnerability"],
"financial_analysis": ["finance", "money", "investment", "financial", "economic"],
"healthcare_technology": ["health", "medical", "healthcare", "clinical", "patient"],
"educational_technology": ["education", "teach", "learn", "student", "curriculum"],
"project_management": ["project", "manage", "planning", "coordination", "leadership"],
"digital_marketing": ["marketing", "advertising", "promotion", "social media", "brand"],
"environmental_science": ["environment", "sustainable", "green", "climate", "carbon"],
"creative_design": ["design", "creative", "art", "visual", "graphic"]
}
for skill, keywords in skill_keywords.items():
probability = 0.0
for keyword in keywords:
if keyword in user_input_lower:
probability += 0.3
if probability > 0.2: # 20% threshold
skill_probabilities[skill] = min(1.0, probability)
top_skills.append({
"skill": skill,
"probability": skill_probabilities[skill],
"confidence": "high" if probability > 0.6 else "medium" if probability > 0.4 else "low"
})
return {
"skill_probabilities": skill_probabilities,
"top_skills": top_skills,
"classification_reasoning": f"Rule-based classification identified {len(top_skills)} relevant skills",
"processing_time": 0.05,
"method": "rule_based"
}
def _extract_high_probability_skills(self, classification: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Extract skills that pass the 20% probability threshold"""
high_prob_skills = []
# From market analysis
market_analysis = classification.get("market_analysis", {})
market_skills = market_analysis.get("selected_skills", [])
for skill in market_skills:
if skill.get("relevance_score", 0) > 0.2:
high_prob_skills.append({
"skill": skill["skill"],
"category": skill["category"],
"probability": skill["relevance_score"],
"source": "market_analysis"
})
# From skill classification
skill_classification = classification.get("skill_classification", {})
classification_skills = skill_classification.get("top_skills", [])
for skill in classification_skills:
if skill.get("probability", 0) > 0.2:
high_prob_skills.append({
"skill": skill["skill"],
"category": "classified",
"probability": skill["probability"],
"source": "skill_classification"
})
# If no skills found from LLM, use rule-based fallback
if not high_prob_skills:
logger.warning(f"{self.agent_id} No skills identified from LLM, using rule-based fallback")
# Extract user input from context if available
user_input = ""
if isinstance(classification, dict) and "user_input" in classification:
user_input = classification["user_input"]
elif isinstance(classification, dict) and "context" in classification:
context = classification["context"]
if isinstance(context, dict) and "user_input" in context:
user_input = context["user_input"]
if user_input:
rule_based_result = self._rule_based_skill_classification(user_input)
rule_skills = rule_based_result.get("top_skills", [])
for skill in rule_skills:
if skill.get("probability", 0) > 0.2:
high_prob_skills.append({
"skill": skill["skill"],
"category": "rule_based",
"probability": skill["probability"],
"source": "rule_based_fallback"
})
# Remove duplicates and sort by probability
unique_skills = {}
for skill in high_prob_skills:
skill_name = skill["skill"]
if skill_name not in unique_skills or skill["probability"] > unique_skills[skill_name]["probability"]:
unique_skills[skill_name] = skill
return sorted(unique_skills.values(), key=lambda x: x["probability"], reverse=True)
def _calculate_overall_confidence(self, market_analysis: Dict[str, Any], skill_classification: Dict[str, Any]) -> float:
"""Calculate overall confidence score"""
market_confidence = len(market_analysis.get("relevant_categories", [])) * 0.1
classification_confidence = len(skill_classification.get("top_skills", [])) * 0.1
return min(1.0, market_confidence + classification_confidence + 0.3)
def _get_fallback_result(self, user_input: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""Provide fallback result when processing fails"""
return {
"agent_id": self.agent_id,
"market_analysis": {
"relevant_categories": [{"category": "General", "market_share": 10, "growth_rate": 5.0, "relevance_score": 0.5, "reasoning": "Fallback analysis"}],
"selected_skills": [{"skill": "General Analysis", "category": "General", "relevance_score": 0.5, "reasoning": "Fallback skill"}],
"overall_analysis": "Fallback analysis due to processing error",
"processing_time": 0.01,
"method": "fallback"
},
"skill_classification": {
"skill_probabilities": {"general_analysis": 0.5},
"top_skills": [{"skill": "general_analysis", "probability": 0.5, "confidence": "low"}],
"classification_reasoning": "Fallback classification due to processing error",
"processing_time": 0.01,
"method": "fallback"
},
"identified_skills": [{"skill": "General Analysis", "category": "General", "probability": 0.5, "source": "fallback"}],
"processing_time": 0.02,
"confidence_score": 0.3,
"error_handled": True
}
# Factory function for easy instantiation
def create_skills_identification_agent(llm_router=None):
return SkillsIdentificationAgent(llm_router)
|