Alwrity version 0.5.4

This commit is contained in:
ajaysi
2025-08-11 10:54:50 +05:30
parent 13ca78f653
commit 39b96c44da
44 changed files with 10448 additions and 2119 deletions

View File

@@ -0,0 +1,8 @@
"""
AI Generation Module
AI-powered content strategy generation with comprehensive insights and recommendations.
"""
from .strategy_generator import AIStrategyGenerator, StrategyGenerationConfig
__all__ = ["AIStrategyGenerator", "StrategyGenerationConfig"]

View File

@@ -0,0 +1,699 @@
"""
AI-Powered Strategy Generation Service
Generates comprehensive content strategies using AI with enhanced insights and recommendations.
"""
import json
import logging
from typing import Any, Dict, List, Optional
from datetime import datetime
from dataclasses import dataclass
from services.ai_service_manager import AIServiceManager, AIServiceType
from ..autofill.ai_structured_autofill import AIStructuredAutofillService
logger = logging.getLogger(__name__)
@dataclass
class StrategyGenerationConfig:
"""Configuration for strategy generation."""
include_competitive_analysis: bool = True
include_content_calendar: bool = True
include_performance_predictions: bool = True
include_implementation_roadmap: bool = True
include_risk_assessment: bool = True
max_content_pieces: int = 50
timeline_months: int = 12
class AIStrategyGenerator:
"""
AI-Powered Content Strategy Generator
Generates comprehensive content strategies including:
- Strategic field autofill (leveraging existing 100% success system)
- Competitive analysis and positioning
- Content calendar and publishing schedule
- Performance predictions and KPIs
- Implementation roadmap
- Risk assessment and mitigation
"""
def __init__(self, config: Optional[StrategyGenerationConfig] = None):
"""Initialize the AI strategy generator."""
self.config = config or StrategyGenerationConfig()
self.ai_manager = AIServiceManager()
self.autofill_service = AIStructuredAutofillService()
self.logger = logger
async def generate_comprehensive_strategy(
self,
user_id: int,
context: Dict[str, Any],
strategy_name: Optional[str] = None
) -> Dict[str, Any]:
"""
Generate a comprehensive content strategy using AI.
Args:
user_id: User ID for personalization
context: User context and onboarding data
strategy_name: Optional custom strategy name
Returns:
Comprehensive strategy with all components
Raises:
RuntimeError: If any AI component fails to generate
"""
try:
self.logger.info(f"🚀 Generating comprehensive AI strategy for user: {user_id}")
# Step 1: Generate base strategy fields (using existing autofill system)
base_strategy = await self._generate_base_strategy_fields(user_id, context)
# Step 2: Generate strategic insights and recommendations
strategic_insights = await self._generate_strategic_insights(base_strategy, context)
# Step 3: Generate competitive analysis
competitive_analysis = await self._generate_competitive_analysis(base_strategy, context)
# Step 4: Generate content calendar
content_calendar = await self._generate_content_calendar(base_strategy, context)
# Step 5: Generate performance predictions
performance_predictions = await self._generate_performance_predictions(base_strategy, context)
# Step 6: Generate implementation roadmap
implementation_roadmap = await self._generate_implementation_roadmap(base_strategy, context)
# Step 7: Generate risk assessment
risk_assessment = await self._generate_risk_assessment(base_strategy, context)
# Step 8: Compile comprehensive strategy
comprehensive_strategy = {
"strategy_metadata": {
"generated_at": datetime.utcnow().isoformat(),
"user_id": user_id,
"strategy_name": strategy_name or f"AI-Generated Strategy {datetime.utcnow().strftime('%Y-%m-%d')}",
"generation_version": "2.0",
"ai_model": "gemini-pro",
"personalization_level": "high",
"ai_generated": True,
"comprehensive": True
},
"base_strategy": base_strategy,
"strategic_insights": strategic_insights,
"competitive_analysis": competitive_analysis,
"content_calendar": content_calendar,
"performance_predictions": performance_predictions,
"implementation_roadmap": implementation_roadmap,
"risk_assessment": risk_assessment,
"summary": {
"total_content_pieces": len(content_calendar.get("content_pieces", [])),
"estimated_roi": performance_predictions.get("estimated_roi", "15-25%"),
"implementation_timeline": implementation_roadmap.get("total_duration", "12 months"),
"risk_level": risk_assessment.get("overall_risk_level", "Medium"),
"success_probability": performance_predictions.get("success_probability", "85%")
}
}
self.logger.info(f"✅ Comprehensive AI strategy generated successfully for user: {user_id}")
return comprehensive_strategy
except Exception as e:
self.logger.error(f"❌ Error generating comprehensive strategy: {str(e)}")
raise RuntimeError(f"Failed to generate comprehensive strategy: {str(e)}")
async def _generate_base_strategy_fields(
self,
user_id: int,
context: Dict[str, Any]
) -> Dict[str, Any]:
"""Generate base strategy fields using existing autofill system."""
try:
self.logger.info(f"Generating base strategy fields for user: {user_id}")
# Use existing autofill service (100% success rate)
autofill_result = await self.autofill_service.generate_autofill_fields(user_id, context)
# Extract the fields from autofill result
base_strategy = autofill_result.get("fields", {})
# Add generation metadata
base_strategy["generation_metadata"] = {
"generated_by": "ai_autofill_system",
"success_rate": autofill_result.get("success_rate", 100),
"personalized": autofill_result.get("personalized", True),
"data_sources": autofill_result.get("data_sources", [])
}
return base_strategy
except Exception as e:
self.logger.error(f"Error generating base strategy fields: {str(e)}")
raise
async def _generate_strategic_insights(self, base_strategy: Dict[str, Any], context: Dict[str, Any], ai_manager: Optional[Any] = None) -> Dict[str, Any]:
"""Generate strategic insights using AI."""
try:
logger.info("🧠 Generating strategic insights...")
# Use provided AI manager or create default one
if ai_manager is None:
from services.ai_service_manager import AIServiceManager
ai_manager = AIServiceManager()
prompt = f"""
Generate comprehensive strategic insights for content strategy based on the following context:
CONTEXT:
{json.dumps(context, indent=2)}
BASE STRATEGY:
{json.dumps(base_strategy, indent=2)}
Please provide strategic insights including:
1. Market positioning analysis
2. Content opportunity identification
3. Competitive advantage mapping
4. Growth potential assessment
5. Strategic recommendations
Format as structured JSON with insights, reasoning, and confidence levels.
"""
schema = {
"type": "object",
"properties": {
"insights": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {"type": "string"},
"insight": {"type": "string"},
"reasoning": {"type": "string"},
"priority": {"type": "string"},
"estimated_impact": {"type": "string"},
"implementation_time": {"type": "string"},
"confidence_level": {"type": "string"}
}
}
}
}
}
response = await ai_manager.execute_structured_json_call(
AIServiceType.STRATEGIC_INTELLIGENCE,
prompt,
schema
)
if not response or not response.get("data"):
raise RuntimeError("AI service returned empty strategic insights")
logger.info("✅ Strategic insights generated successfully")
return response.get("data", {})
except Exception as e:
logger.error(f"❌ Error generating strategic insights: {str(e)}")
raise RuntimeError(f"Failed to generate strategic insights: {str(e)}")
async def _generate_competitive_analysis(self, base_strategy: Dict[str, Any], context: Dict[str, Any], ai_manager: Optional[Any] = None) -> Dict[str, Any]:
"""Generate competitive analysis using AI."""
try:
logger.info("🔍 Generating competitive analysis...")
# Use provided AI manager or create default one
if ai_manager is None:
from services.ai_service_manager import AIServiceManager
ai_manager = AIServiceManager()
prompt = f"""
Generate comprehensive competitive analysis for content strategy based on the following context:
CONTEXT:
{json.dumps(context, indent=2)}
BASE STRATEGY:
{json.dumps(base_strategy, indent=2)}
Please provide competitive analysis including:
1. Competitor identification and analysis
2. Market gap identification
3. Differentiation opportunities
4. Competitive positioning
5. Strategic recommendations
Format as structured JSON with detailed analysis and recommendations.
"""
schema = {
"type": "object",
"properties": {
"competitors": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"strengths": {"type": "array", "items": {"type": "string"}},
"weaknesses": {"type": "array", "items": {"type": "string"}},
"content_strategy": {"type": "string"},
"market_position": {"type": "string"}
}
}
},
"market_gaps": {"type": "array", "items": {"type": "string"}},
"opportunities": {"type": "array", "items": {"type": "string"}},
"recommendations": {"type": "array", "items": {"type": "string"}}
}
}
response = await ai_manager.execute_structured_json_call(
AIServiceType.MARKET_POSITION_ANALYSIS,
prompt,
schema
)
if not response or not response.get("data"):
raise RuntimeError("AI service returned empty competitive analysis")
logger.info("✅ Competitive analysis generated successfully")
return response.get("data", {})
except Exception as e:
logger.error(f"❌ Error generating competitive analysis: {str(e)}")
raise RuntimeError(f"Failed to generate competitive analysis: {str(e)}")
async def _generate_content_calendar(self, base_strategy: Dict[str, Any], context: Dict[str, Any], ai_manager: Optional[Any] = None) -> Dict[str, Any]:
"""Generate content calendar using AI."""
try:
logger.info("📅 Generating content calendar...")
# Use provided AI manager or create default one
if ai_manager is None:
from services.ai_service_manager import AIServiceManager
ai_manager = AIServiceManager()
prompt = f"""
Generate comprehensive content calendar for content strategy based on the following context:
CONTEXT:
{json.dumps(context, indent=2)}
BASE STRATEGY:
{json.dumps(base_strategy, indent=2)}
Please provide content calendar including:
1. Content pieces with titles and descriptions
2. Publishing schedule and timing
3. Content types and formats
4. Platform distribution strategy
5. Content themes and pillars
Format as structured JSON with detailed content schedule.
"""
schema = {
"type": "object",
"properties": {
"content_pieces": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {"type": "string"},
"description": {"type": "string"},
"content_type": {"type": "string"},
"platform": {"type": "string"},
"publishing_date": {"type": "string"},
"theme": {"type": "string"},
"priority": {"type": "string"}
}
}
},
"themes": {"type": "array", "items": {"type": "string"}},
"schedule": {"type": "object"},
"distribution_strategy": {"type": "object"}
}
}
response = await ai_manager.execute_structured_json_call(
AIServiceType.CONTENT_SCHEDULE_GENERATION,
prompt,
schema
)
if not response or not response.get("data"):
raise RuntimeError("AI service returned empty content calendar")
logger.info("✅ Content calendar generated successfully")
return response.get("data", {})
except Exception as e:
logger.error(f"❌ Error generating content calendar: {str(e)}")
raise RuntimeError(f"Failed to generate content calendar: {str(e)}")
async def _generate_performance_predictions(self, base_strategy: Dict[str, Any], context: Dict[str, Any], ai_manager: Optional[Any] = None) -> Dict[str, Any]:
"""Generate performance predictions using AI."""
try:
logger.info("📊 Generating performance predictions...")
# Use provided AI manager or create default one
if ai_manager is None:
from services.ai_service_manager import AIServiceManager
ai_manager = AIServiceManager()
prompt = f"""
Generate comprehensive performance predictions for content strategy based on the following context:
CONTEXT:
{json.dumps(context, indent=2)}
BASE STRATEGY:
{json.dumps(base_strategy, indent=2)}
Please provide performance predictions including:
1. Traffic growth projections
2. Engagement rate predictions
3. Conversion rate estimates
4. ROI projections
5. Success probability assessment
Format as structured JSON with detailed predictions and confidence levels.
"""
schema = {
"type": "object",
"properties": {
"traffic_predictions": {
"type": "object",
"properties": {
"monthly_traffic": {"type": "string"},
"growth_rate": {"type": "string"},
"peak_traffic": {"type": "string"}
}
},
"engagement_predictions": {
"type": "object",
"properties": {
"engagement_rate": {"type": "string"},
"time_on_page": {"type": "string"},
"bounce_rate": {"type": "string"}
}
},
"conversion_predictions": {
"type": "object",
"properties": {
"conversion_rate": {"type": "string"},
"lead_generation": {"type": "string"},
"sales_impact": {"type": "string"}
}
},
"roi_predictions": {
"type": "object",
"properties": {
"estimated_roi": {"type": "string"},
"cost_benefit": {"type": "string"},
"payback_period": {"type": "string"}
}
}
}
}
response = await ai_manager.execute_structured_json_call(
AIServiceType.PERFORMANCE_PREDICTION,
prompt,
schema
)
if not response or not response.get("data"):
raise RuntimeError("AI service returned empty performance predictions")
logger.info("✅ Performance predictions generated successfully")
return response.get("data", {})
except Exception as e:
logger.error(f"❌ Error generating performance predictions: {str(e)}")
raise RuntimeError(f"Failed to generate performance predictions: {str(e)}")
async def _generate_implementation_roadmap(self, base_strategy: Dict[str, Any], context: Dict[str, Any], ai_manager: Optional[Any] = None) -> Dict[str, Any]:
"""Generate implementation roadmap using AI."""
try:
logger.info("🗺️ Generating implementation roadmap...")
# Use provided AI manager or create default one
if ai_manager is None:
from services.ai_service_manager import AIServiceManager
ai_manager = AIServiceManager()
prompt = f"""
Generate comprehensive implementation roadmap for content strategy based on the following context:
CONTEXT:
{json.dumps(context, indent=2)}
BASE STRATEGY:
{json.dumps(base_strategy, indent=2)}
Please provide implementation roadmap including:
1. Phase-by-phase breakdown
2. Timeline with milestones
3. Resource allocation
4. Success metrics
5. Risk mitigation strategies
Format as structured JSON with detailed implementation plan.
"""
schema = {
"type": "object",
"properties": {
"phases": {
"type": "array",
"items": {
"type": "object",
"properties": {
"phase": {"type": "string"},
"duration": {"type": "string"},
"tasks": {"type": "array", "items": {"type": "string"}},
"milestones": {"type": "array", "items": {"type": "string"}},
"resources": {"type": "array", "items": {"type": "string"}}
}
}
},
"timeline": {"type": "object"},
"resource_allocation": {"type": "object"},
"success_metrics": {"type": "array", "items": {"type": "string"}},
"total_duration": {"type": "string"}
}
}
response = await ai_manager.execute_structured_json_call(
AIServiceType.STRATEGIC_INTELLIGENCE,
prompt,
schema
)
if not response or not response.get("data"):
raise RuntimeError("AI service returned empty implementation roadmap")
logger.info("✅ Implementation roadmap generated successfully")
return response.get("data", {})
except Exception as e:
logger.error(f"❌ Error generating implementation roadmap: {str(e)}")
raise RuntimeError(f"Failed to generate implementation roadmap: {str(e)}")
async def _generate_risk_assessment(self, base_strategy: Dict[str, Any], context: Dict[str, Any], ai_manager: Optional[Any] = None) -> Dict[str, Any]:
"""Generate risk assessment using AI."""
try:
logger.info("⚠️ Generating risk assessment...")
# Use provided AI manager or create default one
if ai_manager is None:
from services.ai_service_manager import AIServiceManager
ai_manager = AIServiceManager()
prompt = f"""
Generate comprehensive risk assessment for content strategy based on the following context:
CONTEXT:
{json.dumps(context, indent=2)}
BASE STRATEGY:
{json.dumps(base_strategy, indent=2)}
Please provide risk assessment including:
1. Risk identification and analysis
2. Probability and impact assessment
3. Mitigation strategies
4. Contingency planning
5. Risk monitoring framework
Format as structured JSON with detailed risk analysis and mitigation plans.
"""
schema = {
"type": "object",
"properties": {
"risks": {
"type": "array",
"items": {
"type": "object",
"properties": {
"risk": {"type": "string"},
"probability": {"type": "string"},
"impact": {"type": "string"},
"mitigation": {"type": "string"},
"contingency": {"type": "string"}
}
}
},
"overall_risk_level": {"type": "string"},
"risk_categories": {"type": "object"},
"mitigation_strategies": {"type": "array", "items": {"type": "string"}},
"monitoring_framework": {"type": "object"}
}
}
response = await ai_manager.execute_structured_json_call(
AIServiceType.STRATEGIC_INTELLIGENCE,
prompt,
schema
)
if not response or not response.get("data"):
raise RuntimeError("AI service returned empty risk assessment")
logger.info("✅ Risk assessment generated successfully")
return response.get("data", {})
except Exception as e:
logger.error(f"❌ Error generating risk assessment: {str(e)}")
raise RuntimeError(f"Failed to generate risk assessment: {str(e)}")
def _build_strategic_insights_prompt(self, base_strategy: Dict[str, Any], context: Dict[str, Any]) -> str:
"""Build prompt for strategic insights generation."""
return f"""
As an expert content strategy consultant with 15+ years of experience, analyze this content strategy and provide strategic insights:
STRATEGY CONTEXT:
{json.dumps(base_strategy, indent=2)}
USER CONTEXT:
{json.dumps(context, indent=2)}
Provide comprehensive strategic insights covering:
1. Key insights about the strategy's strengths and opportunities
2. Strategic recommendations with priority levels
3. Identified opportunity areas for growth
4. Competitive advantages to leverage
Focus on actionable, data-driven insights that will drive content strategy success.
"""
def _build_competitive_analysis_prompt(self, base_strategy: Dict[str, Any], context: Dict[str, Any]) -> str:
"""Build prompt for competitive analysis generation."""
return f"""
As a competitive intelligence expert, analyze the competitive landscape for this content strategy:
STRATEGY CONTEXT:
{json.dumps(base_strategy, indent=2)}
USER CONTEXT:
{json.dumps(context, indent=2)}
Provide comprehensive competitive analysis covering:
1. Competitive landscape analysis with key players
2. Positioning strategy and differentiation factors
3. Market gaps and opportunities
4. Competitive advantages and unique value propositions
Focus on actionable competitive intelligence that will inform strategic positioning.
"""
def _build_content_calendar_prompt(self, base_strategy: Dict[str, Any], context: Dict[str, Any]) -> str:
"""Build prompt for content calendar generation."""
return f"""
As a content strategy expert, create a comprehensive content calendar for this strategy:
STRATEGY CONTEXT:
{json.dumps(base_strategy, indent=2)}
USER CONTEXT:
{json.dumps(context, indent=2)}
Generate a {self.config.max_content_pieces}-piece content calendar covering {self.config.timeline_months} months including:
1. Diverse content pieces (blog posts, social media, videos, etc.)
2. Publishing schedule with optimal timing
3. Content mix distribution
4. Topic clusters and content pillars
5. Target audience alignment
Ensure content aligns with business objectives and audience preferences.
"""
def _build_performance_predictions_prompt(self, base_strategy: Dict[str, Any], context: Dict[str, Any]) -> str:
"""Build prompt for performance predictions generation."""
return f"""
As a data-driven content strategist, predict performance outcomes for this content strategy:
STRATEGY CONTEXT:
{json.dumps(base_strategy, indent=2)}
USER CONTEXT:
{json.dumps(context, indent=2)}
Provide realistic performance predictions covering:
1. Traffic growth projections (3, 6, 12 months)
2. Engagement metrics predictions
3. Conversion and lead generation forecasts
4. ROI estimates and success probability
5. Key performance indicators with targets
Base predictions on industry benchmarks and strategy characteristics.
"""
def _build_implementation_roadmap_prompt(self, base_strategy: Dict[str, Any], context: Dict[str, Any]) -> str:
"""Build prompt for implementation roadmap generation."""
return f"""
As a project management expert, create an implementation roadmap for this content strategy:
STRATEGY CONTEXT:
{json.dumps(base_strategy, indent=2)}
USER CONTEXT:
{json.dumps(context, indent=2)}
Create a detailed implementation roadmap covering:
1. Phased implementation approach
2. Resource requirements and budget allocation
3. Timeline with milestones and deliverables
4. Critical path and dependencies
5. Success metrics and evaluation criteria
Ensure roadmap is realistic and achievable given available resources.
"""
def _build_risk_assessment_prompt(self, base_strategy: Dict[str, Any], context: Dict[str, Any]) -> str:
"""Build prompt for risk assessment generation."""
return f"""
As a risk management expert, assess potential risks for this content strategy:
STRATEGY CONTEXT:
{json.dumps(base_strategy, indent=2)}
USER CONTEXT:
{json.dumps(context, indent=2)}
Provide comprehensive risk assessment covering:
1. Identified risks with probability and impact
2. Risk categorization (market, operational, competitive, resource)
3. Mitigation strategies for each risk
4. Contingency plans for high-impact scenarios
5. Overall risk level assessment
Focus on practical risk mitigation strategies.
"""