forked from satishskid/AIRdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta_platform_implementation.py
More file actions
764 lines (621 loc) · 28.5 KB
/
meta_platform_implementation.py
File metadata and controls
764 lines (621 loc) · 28.5 KB
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#!/usr/bin/env python3
"""
AIRDOCS Meta-Platform Implementation
Intelligent AI Orchestration Layer for Specialized AI Services
"""
import asyncio
import aiohttp
import time
from typing import Dict, Any, List, Optional
from dataclasses import dataclass
from enum import Enum
import json
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class ServiceStatus(Enum):
HEALTHY = "healthy"
DEGRADED = "degraded"
UNAVAILABLE = "unavailable"
class ContentCategory(Enum):
ACADEMIC_PAPERS = "academic_papers"
PRESENTATIONS = "presentations"
BUSINESS_REPORTS = "business_reports"
RESEARCH_REPORTS = "research_reports"
MARKETING_CAMPAIGNS = "marketing_campaigns"
@dataclass
class AIServiceConfig:
"""Configuration for external AI service."""
name: str
api_endpoint: str
auth_type: str # oauth2, api_key, bearer
credentials: Dict[str, str]
rate_limit: int # requests per minute
timeout: int # seconds
retry_attempts: int
quality_score: int # 1-100
specialization: str
cost_per_request: float
free_credits: int
@dataclass
class RequestContext:
"""Context for AI service request."""
user_id: str
content_category: ContentCategory
prompt: str
quality_level: int # 1-3
output_formats: List[str]
budget_limit: float
deadline: Optional[int] # seconds
class AIOrchestrator:
"""
Intelligent AI service orchestration and routing.
Core component of the meta-platform architecture.
"""
def __init__(self):
self.services = self._initialize_services()
self.service_health = {}
self.rate_limiters = {}
self.circuit_breakers = {}
self.session = None
def _initialize_services(self) -> Dict[str, Dict[str, AIServiceConfig]]:
"""Initialize AI service configurations."""
return {
ContentCategory.ACADEMIC_PAPERS.value: {
"paperpal": AIServiceConfig(
name="paperpal",
api_endpoint="https://api.paperpal.com/v1/generate",
auth_type="oauth2",
credentials={"client_id": "airdocs_client", "scope": "research"},
rate_limit=10, # 10 requests per minute
timeout=30,
retry_attempts=3,
quality_score=96,
specialization="academic_writing",
cost_per_request=0.0, # Free tier
free_credits=10
),
"jenni_ai": AIServiceConfig(
name="jenni_ai",
api_endpoint="https://api.jenni.ai/v1/research",
auth_type="oauth2",
credentials={"client_id": "airdocs_client", "scope": "research_papers"},
rate_limit=8,
timeout=25,
retry_attempts=3,
quality_score=94,
specialization="research_papers",
cost_per_request=0.0,
free_credits=15
)
},
ContentCategory.PRESENTATIONS.value: {
"genspark": AIServiceConfig(
name="genspark",
api_endpoint="https://api.genspark.ai/v1/presentations",
auth_type="oauth2",
credentials={"client_id": "airdocs_client", "scope": "presentations"},
rate_limit=15,
timeout=20,
retry_attempts=3,
quality_score=95,
specialization="executive_presentations",
cost_per_request=0.0,
free_credits=50
),
"manus": AIServiceConfig(
name="manus",
api_endpoint="https://api.manus.ai/v1/generate",
auth_type="oauth2",
credentials={"client_id": "airdocs_client", "scope": "business_presentations"},
rate_limit=12,
timeout=25,
retry_attempts=3,
quality_score=93,
specialization="business_presentations",
cost_per_request=0.0,
free_credits=30
)
},
ContentCategory.BUSINESS_REPORTS.value: {
"pitchbook_ai": AIServiceConfig(
name="pitchbook_ai",
api_endpoint="https://api.pitchbook.com/v1/analysis",
auth_type="oauth2",
credentials={"client_id": "airdocs_client", "scope": "market_analysis"},
rate_limit=6,
timeout=45,
retry_attempts=3,
quality_score=96,
specialization="market_analysis",
cost_per_request=0.0,
free_credits=15
)
}
}
async def route_request(self, context: RequestContext) -> Dict[str, Any]:
"""
Intelligent routing of requests to optimal AI service.
Core orchestration logic.
"""
logger.info(f"🎯 Routing request for {context.content_category.value}")
# 1. Get available services for content category
available_services = await self._get_available_services(context.content_category)
if not available_services:
return await self._handle_no_services_available(context)
# 2. Select optimal service based on requirements
selected_service = await self._select_optimal_service(available_services, context)
# 3. Execute request with selected service
result = await self._execute_request(selected_service, context)
# 4. Post-process and standardize output
standardized_result = await self._standardize_output(result, selected_service)
return standardized_result
async def _get_available_services(self, category: ContentCategory) -> List[AIServiceConfig]:
"""Get healthy, available services for content category."""
category_services = self.services.get(category.value, {})
available_services = []
for service_name, config in category_services.items():
# Check service health
health_status = await self._check_service_health(config)
if health_status == ServiceStatus.HEALTHY:
# Check rate limits
if await self._check_rate_limit(service_name):
# Check credits availability
if await self._check_credits_available(service_name):
available_services.append(config)
return available_services
async def _select_optimal_service(self, services: List[AIServiceConfig],
context: RequestContext) -> AIServiceConfig:
"""Select optimal service based on requirements and constraints."""
# Scoring algorithm for service selection
scored_services = []
for service in services:
score = 0
# Quality score (40% weight)
quality_weight = 0.4
quality_score = (service.quality_score / 100) * quality_weight
score += quality_score
# Cost optimization (30% weight)
cost_weight = 0.3
if service.cost_per_request == 0: # Free tier
cost_score = cost_weight
else:
cost_score = max(0, cost_weight - (service.cost_per_request * 10))
score += cost_score
# Performance (20% weight)
performance_weight = 0.2
performance_score = (1 / max(service.timeout, 1)) * performance_weight * 100
score += performance_score
# Specialization match (10% weight)
specialization_weight = 0.1
# Simple keyword matching for specialization
if context.content_category.value in service.specialization:
score += specialization_weight
scored_services.append((service, score))
# Select service with highest score
best_service = max(scored_services, key=lambda x: x[1])
logger.info(f"✅ Selected {best_service[0].name} (score: {best_service[1]:.3f})")
return best_service[0]
async def _execute_request(self, service: AIServiceConfig,
context: RequestContext) -> Dict[str, Any]:
"""Execute request to selected AI service with retry logic."""
if not self.session:
self.session = aiohttp.ClientSession()
# Prepare request payload
payload = await self._prepare_request_payload(service, context)
# Execute with retry logic
for attempt in range(service.retry_attempts):
try:
logger.info(f"🔄 Attempt {attempt + 1} for {service.name}")
async with self.session.post(
service.api_endpoint,
json=payload,
timeout=aiohttp.ClientTimeout(total=service.timeout),
headers=await self._get_auth_headers(service)
) as response:
if response.status == 200:
result = await response.json()
logger.info(f"✅ Success from {service.name}")
return {
"success": True,
"service": service.name,
"data": result,
"attempt": attempt + 1
}
elif response.status == 429: # Rate limited
logger.warning(f"⚠️ Rate limited by {service.name}")
await asyncio.sleep(2 ** attempt) # Exponential backoff
continue
else:
logger.error(f"❌ HTTP {response.status} from {service.name}")
if attempt == service.retry_attempts - 1:
return {
"success": False,
"service": service.name,
"error": f"HTTP {response.status}",
"attempt": attempt + 1
}
except asyncio.TimeoutError:
logger.error(f"⏰ Timeout for {service.name}")
if attempt == service.retry_attempts - 1:
return {
"success": False,
"service": service.name,
"error": "timeout",
"attempt": attempt + 1
}
except Exception as e:
logger.error(f"💥 Error with {service.name}: {str(e)}")
if attempt == service.retry_attempts - 1:
return {
"success": False,
"service": service.name,
"error": str(e),
"attempt": attempt + 1
}
return {"success": False, "service": service.name, "error": "max_retries_exceeded"}
async def _prepare_request_payload(self, service: AIServiceConfig,
context: RequestContext) -> Dict[str, Any]:
"""Prepare service-specific request payload."""
# Base payload structure
payload = {
"prompt": context.prompt,
"quality_level": context.quality_level,
"output_formats": context.output_formats
}
# Service-specific customizations
if service.name == "paperpal":
payload.update({
"citation_style": "apa",
"academic_level": "research",
"word_count_target": 2000
})
elif service.name == "genspark":
payload.update({
"presentation_type": "executive",
"slide_count": 10,
"include_charts": True
})
elif service.name == "pitchbook_ai":
payload.update({
"analysis_type": "market_research",
"include_financials": True,
"data_sources": ["public_filings", "market_data"]
})
return payload
async def _get_auth_headers(self, service: AIServiceConfig) -> Dict[str, str]:
"""Get authentication headers for service."""
if service.auth_type == "oauth2":
# In production, this would handle OAuth token refresh
return {
"Authorization": f"Bearer {service.credentials.get('access_token', 'mock_token')}",
"Content-Type": "application/json"
}
elif service.auth_type == "api_key":
return {
"X-API-Key": service.credentials.get("api_key", "mock_key"),
"Content-Type": "application/json"
}
return {"Content-Type": "application/json"}
async def _standardize_output(self, result: Dict[str, Any],
service: AIServiceConfig) -> Dict[str, Any]:
"""Standardize output format across all services."""
if not result.get("success"):
return result
# Extract content from service-specific response format
raw_data = result["data"]
# Standardized response format
standardized = {
"success": True,
"content": {
"text": self._extract_text_content(raw_data, service.name),
"metadata": {
"service": service.name,
"quality_score": service.quality_score,
"specialization": service.specialization,
"generation_time": raw_data.get("generation_time", 0),
"word_count": self._count_words(raw_data),
"timestamp": time.time()
}
},
"routing_info": {
"selected_service": service.name,
"attempt_count": result.get("attempt", 1),
"cost": service.cost_per_request
}
}
return standardized
def _extract_text_content(self, raw_data: Dict[str, Any], service_name: str) -> str:
"""Extract text content from service-specific response."""
# Service-specific content extraction
if service_name == "paperpal":
return raw_data.get("academic_paper", {}).get("content", "")
elif service_name == "genspark":
return raw_data.get("presentation", {}).get("slides_content", "")
elif service_name == "pitchbook_ai":
return raw_data.get("report", {}).get("analysis", "")
# Generic extraction
return raw_data.get("content", raw_data.get("text", str(raw_data)))
def _count_words(self, raw_data: Dict[str, Any]) -> int:
"""Count words in generated content."""
content = str(raw_data)
return len(content.split())
async def _check_service_health(self, service: AIServiceConfig) -> ServiceStatus:
"""Check health status of AI service."""
# Simplified health check - in production would ping health endpoint
return ServiceStatus.HEALTHY
async def _check_rate_limit(self, service_name: str) -> bool:
"""Check if service is within rate limits."""
# Simplified rate limiting - in production would use Redis
return True
async def _check_credits_available(self, service_name: str) -> bool:
"""Check if service has available credits."""
# Simplified credit check - in production would check database
return True
async def _handle_no_services_available(self, context: RequestContext) -> Dict[str, Any]:
"""Handle case when no services are available."""
logger.warning(f"⚠️ No services available for {context.content_category.value}")
return {
"success": False,
"error": "no_services_available",
"message": f"No AI services currently available for {context.content_category.value}",
"fallback_suggestion": "Please try again later or contact support"
}
# Example usage and testing
async def test_meta_platform():
"""Test the meta-platform orchestration."""
orchestrator = AIOrchestrator()
# Test academic paper request
context = RequestContext(
user_id="test_user",
content_category=ContentCategory.ACADEMIC_PAPERS,
prompt="Analyze the impact of AI on healthcare delivery",
quality_level=3,
output_formats=["pdf", "docx"],
budget_limit=5.0,
deadline=300
)
result = await orchestrator.route_request(context)
print(f"Academic Paper Result: {result}")
# Test presentation request
context.content_category = ContentCategory.PRESENTATIONS
result = await orchestrator.route_request(context)
print(f"Presentation Result: {result}")
class ReliabilityManager:
"""
Manages reliability, performance, and failover for the meta-platform.
Ensures 99.9% uptime despite third-party service dependencies.
"""
def __init__(self):
self.circuit_breakers = {}
self.health_monitors = {}
self.performance_metrics = {}
self.failover_chains = self._initialize_failover_chains()
def _initialize_failover_chains(self) -> Dict[str, List[str]]:
"""Initialize failover chains for each content category."""
return {
"academic_papers": [
"paperpal", # Primary (highest quality)
"jenni_ai", # Secondary
"scispace", # Tertiary
"gpt-4o", # Generic fallback
"claude-3-opus" # Final fallback
],
"presentations": [
"genspark", # Primary
"manus", # Secondary
"gamma_app", # Tertiary
"gpt-4o", # Generic fallback
"claude-3-opus" # Final fallback
],
"business_reports": [
"pitchbook_ai", # Primary
"cb_insights", # Secondary
"analyst_ai", # Tertiary
"gpt-4o", # Generic fallback
"claude-3-opus" # Final fallback
]
}
async def execute_with_failover(self, category: str, context: RequestContext) -> Dict[str, Any]:
"""Execute request with automatic failover through service chain."""
failover_chain = self.failover_chains.get(category, [])
for i, service_name in enumerate(failover_chain):
try:
logger.info(f"🔄 Trying {service_name} (attempt {i+1}/{len(failover_chain)})")
# Check circuit breaker
if self._is_circuit_open(service_name):
logger.warning(f"⚡ Circuit breaker open for {service_name}")
continue
# Attempt service call
result = await self._call_service(service_name, context)
if result.get("success"):
# Record success
self._record_success(service_name)
# Add failover metadata
result["failover_info"] = {
"attempt_number": i + 1,
"service_used": service_name,
"is_primary": i == 0,
"fallback_level": "primary" if i == 0 else "secondary" if i < 3 else "generic"
}
return result
else:
# Record failure
self._record_failure(service_name, result.get("error"))
continue
except Exception as e:
logger.error(f"💥 Service {service_name} failed: {str(e)}")
self._record_failure(service_name, str(e))
continue
# All services failed
return {
"success": False,
"error": "all_services_failed",
"message": "All AI services in failover chain are currently unavailable",
"attempted_services": failover_chain
}
def _is_circuit_open(self, service_name: str) -> bool:
"""Check if circuit breaker is open for service."""
breaker = self.circuit_breakers.get(service_name, {})
if not breaker:
return False
# Simple circuit breaker logic
failure_rate = breaker.get("failure_rate", 0)
last_failure = breaker.get("last_failure", 0)
# Open circuit if failure rate > 50% and recent failures
if failure_rate > 0.5 and (time.time() - last_failure) < 300: # 5 minutes
return True
return False
def _record_success(self, service_name: str):
"""Record successful service call."""
if service_name not in self.circuit_breakers:
self.circuit_breakers[service_name] = {
"success_count": 0,
"failure_count": 0,
"failure_rate": 0,
"last_success": time.time()
}
breaker = self.circuit_breakers[service_name]
breaker["success_count"] += 1
breaker["last_success"] = time.time()
# Update failure rate
total_calls = breaker["success_count"] + breaker["failure_count"]
breaker["failure_rate"] = breaker["failure_count"] / max(total_calls, 1)
def _record_failure(self, service_name: str, error: str):
"""Record failed service call."""
if service_name not in self.circuit_breakers:
self.circuit_breakers[service_name] = {
"success_count": 0,
"failure_count": 0,
"failure_rate": 0,
"last_failure": time.time()
}
breaker = self.circuit_breakers[service_name]
breaker["failure_count"] += 1
breaker["last_failure"] = time.time()
# Update failure rate
total_calls = breaker["success_count"] + breaker["failure_count"]
breaker["failure_rate"] = breaker["failure_count"] / max(total_calls, 1)
logger.warning(f"⚠️ {service_name} failure rate: {breaker['failure_rate']:.2%}")
class ValueAddProcessor:
"""
Provides unique value-add services beyond simple API routing.
This is where AIRDOCS differentiates from basic API aggregation.
"""
def __init__(self):
self.quality_validators = {}
self.format_converters = {}
self.enhancement_engines = {}
async def enhance_output(self, raw_result: Dict[str, Any],
context: RequestContext) -> Dict[str, Any]:
"""Apply value-add enhancements to AI-generated content."""
if not raw_result.get("success"):
return raw_result
enhanced_result = raw_result.copy()
# 1. Quality validation and scoring
quality_analysis = await self._validate_quality(raw_result, context)
enhanced_result["quality_analysis"] = quality_analysis
# 2. Multi-format conversion
format_outputs = await self._convert_formats(raw_result, context.output_formats)
enhanced_result["format_outputs"] = format_outputs
# 3. Content enhancement
enhanced_content = await self._enhance_content(raw_result, context)
enhanced_result["enhanced_content"] = enhanced_content
# 4. Metadata enrichment
metadata = await self._enrich_metadata(raw_result, context)
enhanced_result["enriched_metadata"] = metadata
return enhanced_result
async def _validate_quality(self, result: Dict[str, Any],
context: RequestContext) -> Dict[str, Any]:
"""Validate and score content quality."""
content = result.get("content", {}).get("text", "")
quality_metrics = {
"word_count": len(content.split()),
"readability_score": self._calculate_readability(content),
"completeness_score": self._assess_completeness(content, context),
"accuracy_score": self._estimate_accuracy(content),
"professional_score": self._assess_professionalism(content)
}
# Overall quality score (weighted average)
overall_score = (
quality_metrics["readability_score"] * 0.2 +
quality_metrics["completeness_score"] * 0.3 +
quality_metrics["accuracy_score"] * 0.3 +
quality_metrics["professional_score"] * 0.2
)
return {
"overall_score": overall_score,
"metrics": quality_metrics,
"recommendations": self._generate_quality_recommendations(quality_metrics)
}
async def _convert_formats(self, result: Dict[str, Any],
formats: List[str]) -> Dict[str, str]:
"""Convert content to multiple output formats."""
content = result.get("content", {}).get("text", "")
format_outputs = {}
for format_type in formats:
if format_type == "pdf":
format_outputs["pdf"] = await self._convert_to_pdf(content)
elif format_type == "docx":
format_outputs["docx"] = await self._convert_to_docx(content)
elif format_type == "pptx":
format_outputs["pptx"] = await self._convert_to_pptx(content)
elif format_type == "html":
format_outputs["html"] = await self._convert_to_html(content)
return format_outputs
async def _enhance_content(self, result: Dict[str, Any],
context: RequestContext) -> Dict[str, Any]:
"""Apply content enhancements and optimizations."""
content = result.get("content", {}).get("text", "")
enhancements = {
"grammar_check": await self._check_grammar(content),
"fact_verification": await self._verify_facts(content),
"citation_validation": await self._validate_citations(content),
"style_consistency": await self._check_style_consistency(content),
"brand_alignment": await self._align_with_brand(content, context)
}
return enhancements
def _calculate_readability(self, content: str) -> float:
"""Calculate readability score (simplified)."""
words = content.split()
sentences = content.split('.')
if not sentences or not words:
return 0.0
avg_words_per_sentence = len(words) / len(sentences)
# Simplified readability score (0-100)
if avg_words_per_sentence < 15:
return 90.0
elif avg_words_per_sentence < 25:
return 75.0
else:
return 60.0
def _assess_completeness(self, content: str, context: RequestContext) -> float:
"""Assess content completeness based on requirements."""
# Simple completeness check based on word count
word_count = len(content.split())
if context.content_category == ContentCategory.ACADEMIC_PAPERS:
target_words = 2000
elif context.content_category == ContentCategory.PRESENTATIONS:
target_words = 500
else:
target_words = 1000
completeness = min(word_count / target_words, 1.0) * 100
return completeness
def _estimate_accuracy(self, content: str) -> float:
"""Estimate content accuracy (simplified)."""
# In production, this would use fact-checking APIs
return 85.0 # Placeholder
def _assess_professionalism(self, content: str) -> float:
"""Assess professional tone and language."""
# Simple professionalism check
professional_indicators = [
"analysis", "research", "findings", "recommendations",
"methodology", "conclusions", "objectives", "strategy"
]
content_lower = content.lower()
matches = sum(1 for indicator in professional_indicators if indicator in content_lower)
professionalism_score = min(matches * 10, 100)
return professionalism_score
if __name__ == "__main__":
asyncio.run(test_meta_platform())