gateway/modules/interfaces/interfaceAiEngine.py
2025-09-02 18:58:30 +02:00

115 lines
3.6 KiB
Python

"""
Centralized AI Engine Interface for intelligent content processing
"""
from abc import ABC, abstractmethod
from typing import List, Dict, Any, Optional, Union, Tuple
from enum import Enum
from dataclasses import dataclass
from modules.interfaces.interfaceChatModel import ChatDocument, ExtractedContent
class AIModelType(Enum):
"""Available AI model types"""
OPENAI_GPT4 = "openai_gpt4"
OPENAI_GPT35 = "openai_gpt35"
ANTHROPIC_CLAUDE = "anthropic_claude"
OPENAI_VISION = "openai_vision"
ANTHROPIC_VISION = "anthropic_vision"
class ProcessingStrategy(Enum):
"""Content processing strategies"""
SINGLE_CALL = "single_call" # One AI call with full content
DOCUMENT_BY_DOCUMENT = "doc_by_doc" # One call per document, merge results
CHUNKED_PROCESSING = "chunked" # Process in chunks, merge results
SUMMARIZED_CONTENT = "summarized" # Summarize content first, then process
class ContentReductionStrategy(Enum):
"""Content reduction strategies"""
REDUCE_DOCUMENTS_ONLY = "reduce_docs" # Keep prompt, reduce documents
REDUCE_PROMPT_AND_DOCS = "reduce_both" # Reduce both prompt and documents
SUMMARIZE_DOCUMENTS = "summarize_docs" # Summarize documents to key points
EXTRACT_KEY_INFO = "extract_key" # Extract only relevant information
@dataclass
class AIRequest:
"""Standardized AI request structure"""
prompt: str
documents: List[ChatDocument]
context: Optional[str] = None
preferred_model: Optional[AIModelType] = None
max_tokens: Optional[int] = None
temperature: Optional[float] = None
processing_strategy: Optional[ProcessingStrategy] = None
reduction_strategy: Optional[ContentReductionStrategy] = None
metadata: Optional[Dict[str, Any]] = None
@dataclass
class AIResponse:
"""Standardized AI response structure"""
success: bool
content: str
model_used: AIModelType
processing_strategy: ProcessingStrategy
tokens_used: Optional[int] = None
processing_time: Optional[float] = None
error: Optional[str] = None
metadata: Optional[Dict[str, Any]] = None
@dataclass
class ModelCapabilities:
"""AI model capabilities and limits"""
max_tokens: int
max_input_tokens: int
supports_vision: bool
supports_function_calling: bool
cost_per_1k_tokens: float
processing_speed: str # "fast", "medium", "slow"
class AIEngine(ABC):
"""Abstract AI Engine interface"""
@abstractmethod
async def process_request(self, request: AIRequest) -> AIResponse:
"""Process an AI request with intelligent content management"""
pass
@abstractmethod
def get_model_capabilities(self, model: AIModelType) -> ModelCapabilities:
"""Get capabilities and limits for a specific model"""
pass
@abstractmethod
async def estimate_token_usage(self, request: AIRequest) -> int:
"""Estimate token usage for a request"""
pass
class ContentReducer(ABC):
"""Abstract content reduction interface"""
@abstractmethod
async def reduce_content(
self,
documents: List[ChatDocument],
prompt: str,
strategy: ContentReductionStrategy,
target_reduction: float = 0.5
) -> Tuple[List[ChatDocument], str]:
"""Reduce content size while preserving important information"""
pass
@abstractmethod
async def summarize_document(
self,
document: ChatDocument,
focus_prompt: str
) -> ChatDocument:
"""Create a summary of a document focused on specific aspects"""
pass