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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
src/ai/services/intelligence_extractor.py
|
| 3 |
+
Comprehensive business intelligence extraction from call transcripts
|
| 4 |
+
"""
|
| 5 |
+
import logging
|
| 6 |
+
from typing import Dict, Any, List
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
class IntelligenceExtractor:
|
| 12 |
+
"""Extract comprehensive business intelligence from interaction transcripts"""
|
| 13 |
+
|
| 14 |
+
def __init__(self, llm_service):
|
| 15 |
+
self.llm_service = llm_service
|
| 16 |
+
self.setup_extraction_schema()
|
| 17 |
+
|
| 18 |
+
def setup_extraction_schema(self):
|
| 19 |
+
"""Setup the comprehensive extraction schema"""
|
| 20 |
+
self.extraction_schema = {
|
| 21 |
+
"business_relationships": {
|
| 22 |
+
"stakeholders": {
|
| 23 |
+
"mentioned_contacts": List[Dict], # Names, titles, influence levels
|
| 24 |
+
"new_contacts": List[Dict], # Previously unknown contacts
|
| 25 |
+
"relationship_changes": List[Dict], # Changes in relationships/roles
|
| 26 |
+
"decision_makers": List[Dict], # Identified decision makers
|
| 27 |
+
"influence_network": Dict # Relationship mapping
|
| 28 |
+
},
|
| 29 |
+
"team_intelligence": {
|
| 30 |
+
"internal_stakeholders": List[Dict], # Our team members mentioned
|
| 31 |
+
"role_changes": List[Dict], # Team role updates
|
| 32 |
+
"expertise_needed": List[str] # Required expertise identified
|
| 33 |
+
}
|
| 34 |
+
},
|
| 35 |
+
"opportunities": {
|
| 36 |
+
"new_opportunities": List[Dict], # New business opportunities
|
| 37 |
+
"existing_updates": List[Dict], # Updates to existing opportunities
|
| 38 |
+
"cross_sell": List[Dict], # Cross-sell opportunities
|
| 39 |
+
"upsell": List[Dict], # Upsell opportunities
|
| 40 |
+
"risk_factors": List[Dict], # Identified risks
|
| 41 |
+
"competition_intel": List[Dict] # Competitive intelligence
|
| 42 |
+
},
|
| 43 |
+
"project_intelligence": {
|
| 44 |
+
"active_projects": List[Dict], # Current project updates
|
| 45 |
+
"project_health": List[Dict], # Project status/health
|
| 46 |
+
"resource_needs": List[Dict], # Resource requirements
|
| 47 |
+
"timeline_updates": List[Dict], # Schedule changes
|
| 48 |
+
"success_metrics": List[Dict] # Performance indicators
|
| 49 |
+
},
|
| 50 |
+
"client_intelligence": {
|
| 51 |
+
"pain_points": List[Dict], # Client challenges
|
| 52 |
+
"strategic_initiatives": List[Dict], # Client's strategic plans
|
| 53 |
+
"budget_cycles": Dict, # Budget timing/constraints
|
| 54 |
+
"technology_stack": List[str], # Tech infrastructure
|
| 55 |
+
"organizational_changes": List[Dict] # Org changes at client
|
| 56 |
+
},
|
| 57 |
+
"market_intelligence": {
|
| 58 |
+
"industry_trends": List[Dict], # Market trends mentioned
|
| 59 |
+
"competitor_mentions": List[Dict], # Competitor information
|
| 60 |
+
"regulatory_updates": List[Dict], # Compliance/regulatory info
|
| 61 |
+
"market_challenges": List[Dict] # Industry challenges
|
| 62 |
+
},
|
| 63 |
+
"follow_up_actions": {
|
| 64 |
+
"meetings": List[Dict], # Scheduled/requested meetings
|
| 65 |
+
"action_items": List[Dict], # Specific tasks
|
| 66 |
+
"deliverables": List[Dict], # Expected deliverables
|
| 67 |
+
"proposals_needed": List[Dict], # Required proposals
|
| 68 |
+
"approvals_required": List[Dict] # Needed approvals
|
| 69 |
+
},
|
| 70 |
+
"sentiment_analysis": {
|
| 71 |
+
"overall_sentiment": float, # Call sentiment score
|
| 72 |
+
"topic_sentiment": Dict, # Sentiment by topic
|
| 73 |
+
"risk_signals": List[Dict], # Potential issues
|
| 74 |
+
"opportunity_signals": List[Dict] # Positive indicators
|
| 75 |
+
},
|
| 76 |
+
"technical_requirements": {
|
| 77 |
+
"integration_needs": List[Dict], # Integration requirements
|
| 78 |
+
"technical_challenges": List[Dict], # Technical issues
|
| 79 |
+
"infrastructure_updates": List[Dict], # Infrastructure needs
|
| 80 |
+
"security_requirements": List[Dict] # Security considerations
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
async def extract_intelligence(self,
|
| 85 |
+
transcript: str,
|
| 86 |
+
context: Dict[str, Any]) -> Dict[str, Any]:
|
| 87 |
+
"""
|
| 88 |
+
Extract comprehensive intelligence from transcript
|
| 89 |
+
|
| 90 |
+
Args:
|
| 91 |
+
transcript: Call transcript text
|
| 92 |
+
context: Additional context (account history, etc.)
|
| 93 |
+
|
| 94 |
+
Returns:
|
| 95 |
+
Dict containing extracted intelligence
|
| 96 |
+
"""
|
| 97 |
+
try:
|
| 98 |
+
# Extract intelligence using LLM
|
| 99 |
+
prompt = self._generate_extraction_prompt(transcript, context)
|
| 100 |
+
analysis = await self.llm_service.analyze_with_context(prompt, self.extraction_schema)
|
| 101 |
+
|
| 102 |
+
# Post-process and link entities
|
| 103 |
+
processed_results = self._process_extracted_intelligence(analysis, context)
|
| 104 |
+
|
| 105 |
+
return processed_results
|
| 106 |
+
|
| 107 |
+
except Exception as e:
|
| 108 |
+
logger.error(f"Intelligence extraction failed: {str(e)}")
|
| 109 |
+
raise
|
| 110 |
+
|
| 111 |
+
def _generate_extraction_prompt(self, transcript: str, context: Dict) -> str:
|
| 112 |
+
"""Generate context-aware extraction prompt"""
|
| 113 |
+
return f"""
|
| 114 |
+
Analyze this sales interaction transcript and extract comprehensive business intelligence.
|
| 115 |
+
Consider all aspects of account management, relationships, opportunities, and market dynamics.
|
| 116 |
+
|
| 117 |
+
Account Context:
|
| 118 |
+
- Client: {context.get('account_name')}
|
| 119 |
+
- Industry: {context.get('industry')}
|
| 120 |
+
- Relationship Status: {context.get('relationship_status')}
|
| 121 |
+
|
| 122 |
+
Focus on:
|
| 123 |
+
1. Relationship mapping and stakeholder influence
|
| 124 |
+
2. Opportunity identification and updates
|
| 125 |
+
3. Project intelligence and health
|
| 126 |
+
4. Market and competitive insights
|
| 127 |
+
5. Follow-up actions and next steps
|
| 128 |
+
6. Technical and integration requirements
|
| 129 |
+
|
| 130 |
+
Transcript:
|
| 131 |
+
{transcript}
|
| 132 |
+
"""
|
| 133 |
+
|
| 134 |
+
def _process_extracted_intelligence(self,
|
| 135 |
+
analysis: Dict[str, Any],
|
| 136 |
+
context: Dict[str, Any]) -> Dict[str, Any]:
|
| 137 |
+
"""Process and link extracted intelligence"""
|
| 138 |
+
try:
|
| 139 |
+
# Enhance with relationship links
|
| 140 |
+
self._link_stakeholders(analysis, context)
|
| 141 |
+
|
| 142 |
+
# Map to existing opportunities
|
| 143 |
+
self._map_opportunities(analysis, context)
|
| 144 |
+
|
| 145 |
+
# Update project intelligence
|
| 146 |
+
self._update_project_status(analysis, context)
|
| 147 |
+
|
| 148 |
+
# Generate action recommendations
|
| 149 |
+
self._generate_recommendations(analysis)
|
| 150 |
+
|
| 151 |
+
return analysis
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
logger.error(f"Intelligence processing failed: {str(e)}")
|
| 155 |
+
raise
|
| 156 |
+
|
| 157 |
+
def _link_stakeholders(self, analysis: Dict, context: Dict) -> None:
|
| 158 |
+
"""Link mentioned stakeholders with CRM data"""
|
| 159 |
+
# Implementation for stakeholder linking
|
| 160 |
+
|
| 161 |
+
def _map_opportunities(self, analysis: Dict, context: Dict) -> None:
|
| 162 |
+
"""Map extracted opportunities to existing pipeline"""
|
| 163 |
+
# Implementation for opportunity mapping
|
| 164 |
+
|
| 165 |
+
def _update_project_status(self, analysis: Dict, context: Dict) -> None:
|
| 166 |
+
"""Update project status based on new intelligence"""
|
| 167 |
+
# Implementation for project updates
|
| 168 |
+
|
| 169 |
+
def _generate_recommendations(self, analysis: Dict) -> None:
|
| 170 |
+
"""Generate action recommendations based on intelligence"""
|
| 171 |
+
# Implementation for recommendation generation
|