| |
| """ |
| Community Intelligence Abstraction Layer |
| Enables cross-company learning while preserving privacy |
| """ |
| import os |
| import json |
| import hashlib |
| import secrets |
| from typing import Dict, List, Any, Optional, Tuple |
| from datetime import datetime, timedelta |
| from sqlalchemy import create_engine, Column, String, Integer, DateTime, Text, JSON, Boolean, Float, Index, func |
| from sqlalchemy.ext.declarative import declarative_base |
| from sqlalchemy.orm import sessionmaker, Session |
| from sqlalchemy.dialects.postgresql import UUID |
| import uuid |
| from collections import defaultdict, Counter |
| import statistics |
| ===================================================================== |
| ANONYMIZED COMMUNITY DATABASE MODELS |
| ===================================================================== |
| CommunityBase = declarative_base() |
| class AnonymizedCompanyProfile(CommunityBase): |
| tablename = "community_company_profiles" |
| id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) |
|
|
| |
| profile_hash = Column(String(64), unique=True, nullable=False) |
| cohort_id = Column(String(50), index=True) |
|
|
| |
| stage_category = Column(String(50), index=True) |
| industry_category = Column(String(50), index=True) |
| team_size_range = Column(String(20), index=True) |
| funding_category = Column(String(30), index=True) |
| geography_region = Column(String(50), index=True) |
|
|
| |
| business_model_type = Column(String(50)) |
| revenue_range = Column(String(30)) |
|
|
| |
| success_score = Column(Float) |
| growth_trajectory = Column(String(30)) |
|
|
| |
| decision_speed = Column(String(30)) |
| risk_tolerance = Column(String(30)) |
| learning_style = Column(String(30)) |
|
|
| |
| data_points_contributing = Column(Integer, default=1) |
| last_updated = Column(DateTime, default=datetime.utcnow) |
| created_at = Column(DateTime, default=datetime.utcnow) |
| class CommunityToolPattern(CommunityBase): |
| tablename = "community_tool_patterns" |
| id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) |
|
|
| |
| tool_name = Column(String(255), index=True) |
| tool_category = Column(String(100), index=True) |
| alternative_tools = Column(JSON) |
|
|
| |
| cohort_context = Column(String(100), index=True) |
| stage_category = Column(String(50), index=True) |
| industry_category = Column(String(50), index=True) |
| team_size_range = Column(String(20), index=True) |
|
|
| |
| adoption_rate = Column(Float) |
| sample_size = Column(Integer) |
| confidence_level = Column(String(20)) |
|
|
| |
| avg_satisfaction_score = Column(Float) |
| satisfaction_distribution = Column(JSON) |
| retention_rate = Column(Float) |
|
|
| |
| typical_use_cases = Column(JSON) |
| integration_patterns = Column(JSON) |
| switching_patterns = Column(JSON) |
|
|
| |
| selection_reasons = Column(JSON) |
| rejection_reasons = Column(JSON) |
| price_sensitivity = Column(String(30)) |
|
|
| |
| adoption_timeline_days = Column(Integer) |
| typical_team_size_when_adopted = Column(String(20)) |
| typical_revenue_when_adopted = Column(String(30)) |
|
|
| |
| companies_using_success_rate = Column(Float) |
|
|
| |
| last_updated = Column(DateTime, default=datetime.utcnow) |
| created_at = Column(DateTime, default=datetime.utcnow) |
| class CommunityDecisionPattern(CommunityBase): |
| tablename = "community_decision_patterns" |
| id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) |
|
|
| |
| decision_category = Column(String(100), index=True) |
| decision_type = Column(String(100), index=True) |
| decision_context = Column(String(200)) |
|
|
| |
| cohort_context = Column(String(100), index=True) |
| stage_category = Column(String(50), index=True) |
| industry_category = Column(String(50), index=True) |
| team_size_range = Column(String(20), index=True) |
|
|
| |
| decision_options = Column(JSON) |
| chosen_option = Column(String(200)) |
| alternative_options = Column(JSON) |
|
|
| |
| success_rate = Column(Float) |
| avg_time_to_outcome_days = Column(Integer) |
| outcome_satisfaction = Column(Float) |
| would_repeat_rate = Column(Float) |
|
|
| |
| success_factors = Column(JSON) |
| failure_factors = Column(JSON) |
| critical_timing_factors = Column(JSON) |
|
|
| |
| typical_budget_range = Column(String(50)) |
| typical_time_investment = Column(String(50)) |
| team_members_typically_involved = Column(JSON) |
|
|
| |
| common_next_decisions = Column(JSON) |
| dependencies = Column(JSON) |
|
|
| |
| sample_size = Column(Integer) |
| confidence_level = Column(String(20)) |
|
|
| |
| last_updated = Column(DateTime, default=datetime.utcnow) |
| created_at = Column(DateTime, default=datetime.utcnow) |
| class CommunityJourneyPattern(CommunityBase): |
| tablename = "community_journey_patterns" |
| id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) |
|
|
| |
| journey_type = Column(String(100), index=True) |
| cohort_context = Column(String(100), index=True) |
|
|
| |
| typical_step_sequence = Column(JSON) |
| critical_milestones = Column(JSON) |
| optional_steps = Column(JSON) |
| common_variations = Column(JSON) |
|
|
| |
| total_duration_days_p50 = Column(Integer) |
| total_duration_days_p90 = Column(Integer) |
| step_durations = Column(JSON) |
|
|
| |
| success_rate_by_path = Column(JSON) |
| accelerating_factors = Column(JSON) |
| blocking_factors = Column(JSON) |
|
|
| |
| typical_team_size_evolution = Column(JSON) |
| typical_funding_pattern = Column(JSON) |
| key_tools_by_stage = Column(JSON) |
|
|
| |
| final_outcomes_distribution = Column(JSON) |
| key_metrics_evolution = Column(JSON) |
|
|
| |
| sample_size = Column(Integer) |
| confidence_level = Column(String(20)) |
|
|
| |
| last_updated = Column(DateTime, default=datetime.utcnow) |
| created_at = Column(DateTime, default=datetime.utcnow) |
| class CommunityMarketIntelligence(CommunityBase): |
| tablename = "community_market_intelligence" |
| id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) |
|
|
| |
| industry_category = Column(String(50), index=True) |
| geography_region = Column(String(50), index=True) |
| time_period = Column(String(50)) |
|
|
| |
| trend_type = Column(String(100), index=True) |
| trend_data = Column(JSON) |
|
|
| |
| emerging_patterns = Column(JSON) |
| declining_patterns = Column(JSON) |
| seasonal_factors = Column(JSON) |
|
|
| |
| industry_benchmarks = Column(JSON) |
| stage_benchmarks = Column(JSON) |
|
|
| |
| sample_size = Column(Integer) |
| confidence_level = Column(String(20)) |
|
|
| |
| last_updated = Column(DateTime, default=datetime.utcnow) |
| created_at = Column(DateTime, default=datetime.utcnow) |
| ===================================================================== |
| ANONYMIZATION ENGINE |
| ===================================================================== |
| class AnonymizationEngine: |
| def init(self): |
| self.anonymization_rules = self._load_anonymization_rules() |
| def _load_anonymization_rules(self) -> Dict[str, Any]: |
| """Load rules for anonymizing different data types""" |
| return { |
| "stage_mapping": { |
| "idea": "idea", |
| "pre-seed": "early", |
| "seed": "early", |
| "series-a": "growth", |
| "series-b": "growth", |
| "series-c": "scale", |
| "series-d": "scale" |
| }, |
| "industry_mapping": { |
| "saas": "tech", |
| "fintech": "tech", |
| "healthtech": "tech", |
| "ai": "tech", |
| "ml": "tech", |
| "edtech": "tech", |
| "ecommerce": "commerce", |
| "marketplace": "commerce", |
| "retail": "commerce", |
| "consulting": "services", |
| "agency": "services" |
| }, |
| "team_size_ranges": { |
| (1, 3): "1-3", |
| (4, 10): "4-10", |
| (11, 25): "11-25", |
| (26, 50): "26-50", |
| (51, 100): "51-100", |
| (101, 1000): "100+" |
| }, |
| "revenue_ranges": { |
| (0, 1000): "0-1k", |
| (1001, 5000): "1k-5k", |
| (5001, 10000): "5k-10k", |
| (10001, 25000): "10k-25k", |
| (25001, 50000): "25k-50k", |
| (50001, 100000): "50k-100k", |
| (100001, float('inf')): "100k+" |
| }, |
| "geography_mapping": { |
| "united_states": "north_america", |
| "canada": "north_america", |
| "mexico": "north_america", |
| "united_kingdom": "europe", |
| "germany": "europe", |
| "france": "europe", |
| "netherlands": "europe", |
| "spain": "europe", |
| "italy": "europe", |
| "china": "asia", |
| "japan": "asia", |
| "south_korea": "asia", |
| "singapore": "asia", |
| "india": "asia" |
| } |
| } |
|
|
| def anonymize_company_profile(self, company_data: Dict[str, Any]) -> Dict[str, Any]: |
| """Convert company data to anonymous profile""" |
| |
| |
| stage = company_data.get("stage", "unknown") |
| industry = company_data.get("industry", "unknown") |
| team_size = company_data.get("team_size", 1) |
| |
| anonymized = { |
| "stage_category": self.anonymization_rules["stage_mapping"].get(stage, "unknown"), |
| "industry_category": self.anonymization_rules["industry_mapping"].get(industry, "other"), |
| "team_size_range": self._get_team_size_range(team_size), |
| "funding_category": self._get_funding_category(company_data.get("funding_raised", "0")), |
| "geography_region": self._get_geography_region(company_data.get("location", "unknown")), |
| "business_model_type": self._generalize_business_model(company_data.get("business_model", "")), |
| "revenue_range": self._get_revenue_range(company_data.get("revenue", 0)) |
| } |
| |
| |
| anonymized["cohort_id"] = self._generate_cohort_id(anonymized) |
| |
| |
| anonymized["profile_hash"] = self._create_hash(anonymized) |
| |
| return anonymized |
|
|
| def anonymize_tool_decision(self, tool_data: Dict[str, Any], company_context: Dict[str, Any]) -> Dict[str, Any]: |
| """Convert tool decision to anonymous pattern""" |
| |
| company_anon = self.anonymize_company_profile(company_context) |
| |
| return { |
| "tool_name": tool_data.get("tool_name"), |
| "tool_category": tool_data.get("tool_category"), |
| "cohort_context": company_anon["cohort_id"], |
| "stage_category": company_anon["stage_category"], |
| "industry_category": company_anon["industry_category"], |
| "team_size_range": company_anon["team_size_range"], |
| "satisfaction_score": tool_data.get("satisfaction_score", 5), |
| "use_cases": self._categorize_use_cases(tool_data.get("use_cases", [])), |
| "selection_reasons": self._categorize_reasons(tool_data.get("why_chosen", "")), |
| "alternatives_considered": tool_data.get("alternatives_evaluated", []) |
| } |
|
|
| def anonymize_decision(self, decision_data: Dict[str, Any], company_context: Dict[str, Any]) -> Dict[str, Any]: |
| """Convert business decision to anonymous pattern""" |
| |
| company_anon = self.anonymize_company_profile(company_context) |
| |
| return { |
| "decision_category": self._categorize_decision(decision_data.get("decision_type", "")), |
| "decision_type": decision_data.get("decision_type"), |
| "cohort_context": company_anon["cohort_id"], |
| "stage_category": company_anon["stage_category"], |
| "industry_category": company_anon["industry_category"], |
| "team_size_range": company_anon["team_size_range"], |
| "chosen_option": decision_data.get("decision_title", ""), |
| "alternative_options": decision_data.get("alternatives_considered", []), |
| "implementation_success": decision_data.get("implementation_status") == "completed", |
| "outcome_satisfaction": self._estimate_satisfaction(decision_data), |
| "time_to_outcome_days": self._calculate_implementation_time(decision_data) |
| } |
|
|
| def _get_team_size_range(self, team_size: int) -> str: |
| for (min_size, max_size), range_str in self.anonymization_rules["team_size_ranges"].items(): |
| if min_size <= team_size <= max_size: |
| return range_str |
| return "unknown" |
|
|
| def _get_revenue_range(self, revenue: float) -> str: |
| for (min_rev, max_rev), range_str in self.anonymization_rules["revenue_ranges"].items(): |
| if min_rev <= revenue <= max_rev: |
| return range_str |
| return "unknown" |
|
|
| def _get_funding_category(self, funding_str: str) -> str: |
| if not funding_str or funding_str == "0": |
| return "unfunded" |
| elif "pre-seed" in funding_str.lower(): |
| return "pre-seed" |
| elif "seed" in funding_str.lower() and "series" not in funding_str.lower(): |
| return "seed" |
| else: |
| return "series-a+" |
|
|
| def _get_geography_region(self, location: str) -> str: |
| location_lower = location.lower() |
| for country, region in self.anonymization_rules["geography_mapping"].items(): |
| if country in location_lower: |
| return region |
| return "other" |
|
|
| def _generalize_business_model(self, business_model: str) -> str: |
| if not business_model: |
| return "unknown" |
| |
| model_lower = business_model.lower() |
| if any(word in model_lower for word in ["subscription", "saas", "recurring"]): |
| return "subscription" |
| elif any(word in model_lower for word in ["marketplace", "platform", "commission"]): |
| return "marketplace" |
| elif any(word in model_lower for word in ["ecommerce", "retail", "selling"]): |
| return "ecommerce" |
| elif any(word in model_lower for word in ["consulting", "service", "agency"]): |
| return "services" |
| else: |
| return "other" |
|
|
| def _categorize_decision(self, decision_type: str) -> str: |
| decision_lower = decision_type.lower() |
| if any(word in decision_lower for word in ["hire", "hiring", "team", "employee"]): |
| return "hiring" |
| elif any(word in decision_lower for word in ["tool", "software", "platform"]): |
| return "tools" |
| elif any(word in decision_lower for word in ["price", "pricing", "monetiz"]): |
| return "pricing" |
| elif any(word in decision_lower for word in ["product", "feature", "development"]): |
| return "product" |
| elif any(word in decision_lower for word in ["marketing", "growth", "acquisition"]): |
| return "marketing" |
| elif any(word in decision_lower for word in ["funding", "investment", "raise"]): |
| return "funding" |
| else: |
| return "strategy" |
|
|
| def _categorize_use_cases(self, use_cases: List[str]) -> List[str]: |
| categories = [] |
| for use_case in use_cases: |
| use_case_lower = str(use_case).lower() |
| if any(word in use_case_lower for word in ["project", "task", "management"]): |
| categories.append("project_management") |
| elif any(word in use_case_lower for word in ["communication", "chat", "message"]): |
| categories.append("communication") |
| elif any(word in use_case_lower for word in ["document", "note", "wiki"]): |
| categories.append("documentation") |
| elif any(word in use_case_lower for word in ["design", "prototype", "visual"]): |
| categories.append("design") |
| elif any(word in use_case_lower for word in ["analytics", "data", "metrics"]): |
| categories.append("analytics") |
| return list(set(categories)) |
|
|
| def _categorize_reasons(self, reasons: str) -> List[str]: |
| if not reasons: |
| return [] |
| |
| categories = [] |
| reasons_lower = reasons.lower() |
| |
| if any(word in reasons_lower for word in ["cost", "cheap", "affordable", "free"]): |
| categories.append("cost_effective") |
| if any(word in reasons_lower for word in ["easy", "simple", "intuitive", "user-friendly"]): |
| categories.append("ease_of_use") |
| if any(word in reasons_lower for word in ["feature", "functionality", "powerful"]): |
| categories.append("features") |
| if any(word in reasons_lower for word in ["integration", "api", "connect"]): |
| categories.append("integration") |
| if any(word in reasons_lower for word in ["team", "collaboration", "sharing"]): |
| categories.append("collaboration") |
| if any(word in reasons_lower for word in ["recommended", "popular", "everyone"]): |
| categories.append("social_proof") |
| |
| return categories |
|
|
| def _estimate_satisfaction(self, decision_data: Dict) -> float: |
| |
| if decision_data.get("implementation_status") == "completed": |
| return 8.0 |
| elif decision_data.get("implementation_status") == "in_progress": |
| return 7.0 |
| else: |
| return 6.0 |
|
|
| def _calculate_implementation_time(self, decision_data: Dict) -> int: |
| |
| timeline_weeks = decision_data.get("timeline_weeks", 4) |
| return timeline_weeks * 7 |
|
|
| def _generate_cohort_id(self, anonymized_data: Dict) -> str: |
| """Generate ID for grouping similar companies""" |
| cohort_key = f"{anonymized_data['stage_category']}_{anonymized_data['industry_category']}_{anonymized_data['team_size_range']}" |
| return cohort_key |
|
|
| def _create_hash(self, data: Dict) -> str: |
| """Create deterministic hash of data""" |
| sorted_data = json.dumps(data, sort_keys=True, default=str) |
| return hashlib.sha256(sorted_data.encode()).hexdigest() |
| ===================================================================== |
| COMMUNITY INTELLIGENCE ENGINE |
| ===================================================================== |
| class CommunityIntelligenceEngine: |
| def init(self, db_session: Session): |
| self.db = db_session |
| self.anonymizer = AnonymizationEngine() |
| def contribute_company_data(self, company_data: Dict[str, Any]) -> bool: |
| """Add company data to community intelligence""" |
| try: |
| anonymized = self.anonymizer.anonymize_company_profile(company_data) |
| |
| |
| existing = self.db.query(AnonymizedCompanyProfile).filter( |
| AnonymizedCompanyProfile.profile_hash == anonymized["profile_hash"] |
| ).first() |
| |
| if existing: |
| existing.data_points_contributing += 1 |
| existing.last_updated = datetime.utcnow() |
| else: |
| new_profile = AnonymizedCompanyProfile(**anonymized) |
| self.db.add(new_profile) |
| |
| self.db.commit() |
| return True |
| |
| except Exception as e: |
| print(f"Error contributing company data: {e}") |
| return False |
|
|
| def contribute_tool_decision(self, tool_data: Dict[str, Any], company_context: Dict[str, Any]) -> bool: |
| """Add tool decision to community intelligence""" |
| try: |
| anonymized = self.anonymizer.anonymize_tool_decision(tool_data, company_context) |
| |
| |
| existing = self.db.query(CommunityToolPattern).filter( |
| CommunityToolPattern.tool_name == anonymized["tool_name"], |
| CommunityToolPattern.cohort_context == anonymized["cohort_context"] |
| ).first() |
| |
| if existing: |
| |
| self._update_tool_metrics(existing, anonymized) |
| else: |
| |
| new_pattern = CommunityToolPattern( |
| tool_name=anonymized["tool_name"], |
| tool_category=anonymized["tool_category"], |
| cohort_context=anonymized["cohort_context"], |
| stage_category=anonymized["stage_category"], |
| industry_category=anonymized["industry_category"], |
| team_size_range=anonymized["team_size_range"], |
| sample_size=1, |
| avg_satisfaction_score=anonymized["satisfaction_score"], |
| adoption_rate=1.0, |
| typical_use_cases=anonymized["use_cases"], |
| selection_reasons=anonymized["selection_reasons"], |
| alternative_tools=anonymized["alternatives_considered"], |
| confidence_level=self._calculate_confidence(1) |
| ) |
| self.db.add(new_pattern) |
| |
| self.db.commit() |
| return True |
| |
| except Exception as e: |
| print(f"Error contributing tool decision: {e}") |
| return False |
|
|
| def contribute_business_decision(self, decision_data: Dict[str, Any], company_context: Dict[str, Any]) -> bool: |
| """Add business decision to community intelligence""" |
| try: |
| anonymized = self.anonymizer.anonymize_decision(decision_data, company_context) |
| |
| |
| decision_context = f"{anonymized['decision_category']}_{anonymized['cohort_context']}" |
| |
| |
| existing = self.db.query(CommunityDecisionPattern).filter( |
| CommunityDecisionPattern.decision_context == decision_context, |
| CommunityDecisionPattern.chosen_option == anonymized["chosen_option"] |
| ).first() |
| |
| if existing: |
| self._update_decision_metrics(existing, anonymized) |
| else: |
| new_pattern = CommunityDecisionPattern( |
| decision_category=anonymized["decision_category"], |
| decision_type=anonymized["decision_type"], |
| decision_context=decision_context, |
| cohort_context=anonymized["cohort_context"], |
| stage_category=anonymized["stage_category"], |
| industry_category=anonymized["industry_category"], |
| team_size_range=anonymized["team_size_range"], |
| chosen_option=anonymized["chosen_option"], |
| alternative_options=anonymized["alternative_options"], |
| sample_size=1, |
| success_rate=1.0 if anonymized["implementation_success"] else 0.0, |
| outcome_satisfaction=anonymized["outcome_satisfaction"], |
| avg_time_to_outcome_days=anonymized["time_to_outcome_days"], |
| confidence_level=self._calculate_confidence(1) |
| ) |
| self.db.add(new_pattern) |
| |
| self.db.commit() |
| return True |
| |
| except Exception as e: |
| print(f"Error contributing business decision: {e}") |
| return False |
|
|
| def get_tool_intelligence(self, context: Dict[str, Any], tool_category: str = None) -> Dict[str, Any]: |
| """Get tool adoption intelligence for similar companies""" |
| |
| |
| anonymized_context = self.anonymizer.anonymize_company_profile(context) |
| cohort_id = anonymized_context["cohort_id"] |
| |
| |
| query = self.db.query(CommunityToolPattern).filter( |
| CommunityToolPattern.cohort_context == cohort_id |
| ) |
| |
| if tool_category: |
| query = query.filter(CommunityToolPattern.tool_category == tool_category) |
| |
| patterns = query.order_by(CommunityToolPattern.adoption_rate.desc()).limit(10).all() |
| |
| return { |
| "context": anonymized_context, |
| "tool_recommendations": [ |
| { |
| "tool_name": p.tool_name, |
| "tool_category": p.tool_category, |
| "adoption_rate": p.adoption_rate, |
| "satisfaction_score": p.avg_satisfaction_score, |
| "sample_size": p.sample_size, |
| "confidence": p.confidence_level, |
| "typical_use_cases": p.typical_use_cases, |
| "selection_reasons": p.selection_reasons, |
| "alternatives": p.alternative_tools |
| } for p in patterns |
| ], |
| "total_companies_in_cohort": self._get_cohort_size(cohort_id) |
| } |
|
|
| def get_decision_intelligence(self, context: Dict[str, Any], decision_category: str) -> Dict[str, Any]: |
| """Get decision intelligence for similar companies""" |
| |
| anonymized_context = self.anonymizer.anonymize_company_profile(context) |
| cohort_id = anonymized_context["cohort_id"] |
| |
| |
| patterns = self.db.query(CommunityDecisionPattern).filter( |
| CommunityDecisionPattern.cohort_context == cohort_id, |
| CommunityDecisionPattern.decision_category == decision_category |
| ).order_by(CommunityDecisionPattern.success_rate.desc()).limit(10).all() |
| |
| return { |
| "context": anonymized_context, |
| "decision_category": decision_category, |
| "decision_patterns": [ |
| { |
| "decision_type": p.decision_type, |
| "chosen_option": p.chosen_option, |
| "success_rate": p.success_rate, |
| "sample_size": p.sample_size, |
| "confidence": p.confidence_level, |
| "avg_satisfaction": p.outcome_satisfaction, |
| "typical_timeline_days": p.avg_time_to_outcome_days, |
| "alternatives_considered": p.alternative_options |
| } for p in patterns |
| ], |
| "total_companies_in_cohort": self._get_cohort_size(cohort_id) |
| } |
|
|
| def get_market_intelligence(self, context: Dict[str, Any]) -> Dict[str, Any]: |
| """Get broader market intelligence""" |
| |
| anonymized_context = self.anonymizer.anonymize_company_profile(context) |
| |
| |
| industry_tools = self.db.query(CommunityToolPattern).filter( |
| CommunityToolPattern.industry_category == anonymized_context["industry_category"] |
| ).order_by(CommunityToolPattern.adoption_rate.desc()).limit(5).all() |
| |
| stage_decisions = self.db.query(CommunityDecisionPattern).filter( |
| CommunityDecisionPattern.stage_category == anonymized_context["stage_category"] |
| ).order_by(CommunityDecisionPattern.success_rate.desc()).limit(5).all() |
| |
| return { |
| "industry_trending_tools": [ |
| { |
| "tool_name": t.tool_name, |
| "adoption_rate": t.adoption_rate, |
| "category": t.tool_category |
| } for t in industry_tools |
| ], |
| "stage_successful_decisions": [ |
| { |
| "decision_type": d.decision_type, |
| "success_rate": d.success_rate, |
| "sample_size": d.sample_size |
| } for d in stage_decisions |
| ] |
| } |
|
|
| def generate_community_insights_for_prompt(self, user_query: str, context: Dict[str, Any]) -> str: |
| """Generate community insights to enhance AI prompts""" |
| |
| |
| query_lower = user_query.lower() |
| |
| insights_text = "" |
| |
| |
| if any(word in query_lower for word in ["tool", "software", "platform", "app", "service"]): |
| tool_intel = self.get_tool_intelligence(context) |
| if tool_intel["tool_recommendations"]: |
| insights_text += "\nCOMMUNITY TOOL INTELLIGENCE:\n" |
| for tool in tool_intel["tool_recommendations"][:3]: |
| insights_text += f"โข {tool['tool_name']}: {tool['adoption_rate']:.0%} adoption, {tool['satisfaction_score']:.1f}/10 satisfaction ({tool['sample_size']} companies)\n" |
| |
| |
| elif any(word in query_lower for word in ["hire", "hiring", "team", "employee", "recruit"]): |
| decision_intel = self.get_decision_intelligence(context, "hiring") |
| if decision_intel["decision_patterns"]: |
| insights_text += "\nCOMMUNITY HIRING INTELLIGENCE:\n" |
| for pattern in decision_intel["decision_patterns"][:2]: |
| insights_text += f"โข {pattern['chosen_option']}: {pattern['success_rate']:.0%} success rate ({pattern['sample_size']} companies)\n" |
| |
| |
| elif any(word in query_lower for word in ["funding", "investment", "raise", "investor"]): |
| decision_intel = self.get_decision_intelligence(context, "funding") |
| if decision_intel["decision_patterns"]: |
| insights_text += "\nCOMMUNITY FUNDING INTELLIGENCE:\n" |
| for pattern in decision_intel["decision_patterns"][:2]: |
| insights_text += f"โข {pattern['chosen_option']}: {pattern['success_rate']:.0%} success rate, avg {pattern['typical_timeline_days']} days\n" |
| |
| |
| market_intel = self.get_market_intelligence(context) |
| if market_intel["industry_trending_tools"]: |
| insights_text += f"\nINDUSTRY TRENDS ({context.get('industry', 'tech').upper()}):\n" |
| for tool in market_intel["industry_trending_tools"][:2]: |
| insights_text += f"โข {tool['tool_name']}: {tool['adoption_rate']:.0%} adoption in {tool['category']}\n" |
| |
| return insights_text |
|
|
| def _update_tool_metrics(self, existing_pattern: CommunityToolPattern, new_data: Dict[str, Any]): |
| """Update aggregated tool metrics with new data point""" |
| |
| |
| old_size = existing_pattern.sample_size |
| new_size = old_size + 1 |
| existing_pattern.sample_size = new_size |
| |
| |
| old_satisfaction = existing_pattern.avg_satisfaction_score |
| new_satisfaction = new_data["satisfaction_score"] |
| existing_pattern.avg_satisfaction_score = (old_satisfaction * old_size + new_satisfaction) / new_size |
| |
| |
| existing_pattern.confidence_level = self._calculate_confidence(new_size) |
| |
| |
| existing_use_cases = set(existing_pattern.typical_use_cases or []) |
| new_use_cases = set(new_data["use_cases"]) |
| existing_pattern.typical_use_cases = list(existing_use_cases.union(new_use_cases)) |
| |
| existing_reasons = set(existing_pattern.selection_reasons or []) |
| new_reasons = set(new_data["selection_reasons"]) |
| existing_pattern.selection_reasons = list(existing_reasons.union(new_reasons)) |
| |
| existing_pattern.last_updated = datetime.utcnow() |
|
|
| def _update_decision_metrics(self, existing_pattern: CommunityDecisionPattern, new_data: Dict[str, Any]): |
| """Update aggregated decision metrics with new data point""" |
| |
| |
| old_size = existing_pattern.sample_size |
| new_size = old_size + 1 |
| existing_pattern.sample_size = new_size |
| |
| |
| old_success_count = existing_pattern.success_rate * old_size |
| new_success = 1 if new_data["implementation_success"] else 0 |
| existing_pattern.success_rate = (old_success_count + new_success) / new_size |
| |
| |
| old_satisfaction = existing_pattern.outcome_satisfaction |
| new_satisfaction = new_data["outcome_satisfaction"] |
| existing_pattern.outcome_satisfaction = (old_satisfaction * old_size + new_satisfaction) / new_size |
| |
| |
| old_timeline = existing_pattern.avg_time_to_outcome_days |
| new_timeline = new_data["time_to_outcome_days"] |
| existing_pattern.avg_time_to_outcome_days = int((old_timeline * old_size + new_timeline) / new_size) |
| |
| |
| existing_pattern.confidence_level = self._calculate_confidence(new_size) |
| |
| existing_pattern.last_updated = datetime.utcnow() |
|
|
| def _calculate_confidence(self, sample_size: int) -> str: |
| """Calculate confidence level based on sample size""" |
| if sample_size < 5: |
| return "low" |
| elif sample_size < 20: |
| return "medium" |
| else: |
| return "high" |
|
|
| def _get_cohort_size(self, cohort_id: str) -> int: |
| """Get total number of companies in a cohort""" |
| return self.db.query(AnonymizedCompanyProfile).filter( |
| AnonymizedCompanyProfile.cohort_id == cohort_id |
| ).count() |
| ===================================================================== |
| INTEGRATION WITH EXISTING SYSTEM |
| ===================================================================== |
| def create_community_tables(): |
| """Create community intelligence tables""" |
| COMMUNITY_DB_URL = os.getenv("COMMUNITY_DB_URL", os.getenv("ANONYMIZED_DB_URL", "postgresql://user:pass@localhost:5432/community_db")) |
| engine = create_engine(COMMUNITY_DB_URL) |
| CommunityBase.metadata.create_all(bind=engine) |
| print("โ
Community intelligence tables created") |
| def get_community_session(): |
| """Get database session for community intelligence""" |
| COMMUNITY_DB_URL = os.getenv("COMMUNITY_DB_URL", os.getenv("ANONYMIZED_DB_URL", "postgresql://user:pass@localhost:5432/community_db")) |
| engine = create_engine(COMMUNITY_DB_URL) |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) |
| return SessionLocal() |
| Example usage functions |
| def enhance_prompt_with_community_intelligence(user_query: str, company_context: Dict[str, Any]) -> str: |
| """Enhance AI prompt with community intelligence""" |
| try: |
| with get_community_session() as db: |
| community_engine = CommunityIntelligenceEngine(db) |
| community_insights = community_engine.generate_community_insights_for_prompt(user_query, company_context) |
| |
| if community_insights: |
| return f""" |
| COMPANY CONTEXT: {company_context.get('name', 'Startup')} - {company_context.get('stage', 'pre-seed')} {company_context.get('industry', 'tech')} |
| {community_insights} |
| USER QUERY: {user_query} |
| Provide advice that incorporates relevant community intelligence while being specific to this company's situation. |
| """ |
| else: |
| return f""" |
| COMPANY CONTEXT: {company_context.get('name', 'Startup')} - {company_context.get('stage', 'pre-seed')} {company_context.get('industry', 'tech')} |
| USER QUERY: {user_query} |
| Provide personalized startup advice. |
| """ |
| except Exception as e: |
| print(f"Community intelligence error: {e}") |
| return f""" |
| COMPANY CONTEXT: {company_context.get('name', 'Startup')} - {company_context.get('stage', 'pre-seed')} {company_context.get('industry', 'tech')} |
| USER QUERY: {user_query} |
| Provide personalized startup advice. |
| """ |
| Background task to contribute data to community intelligence |
| async def contribute_to_community_intelligence(company_data: Dict[str, Any], interaction_data: Dict[str, Any] = None, tool_data: Dict[str, Any] = None, decision_data: Dict[str, Any] = None): |
| """Background task to contribute data to community intelligence""" |
| try: |
| with get_community_session() as db: |
| community_engine = CommunityIntelligenceEngine(db) |
| |
| |
| community_engine.contribute_company_data(company_data) |
| |
| |
| if tool_data: |
| community_engine.contribute_tool_decision(tool_data, company_data) |
| |
| if decision_data: |
| community_engine.contribute_business_decision(decision_data, company_data) |
| |
| print(f"โ
Contributed data to community intelligence for {company_data.get('stage')} {company_data.get('industry')} company") |
|
|
| except Exception as e: |
| print(f"โ Failed to contribute to community intelligence: {e}") |
|
|