Upload 4 files
Browse files
base_interfaces/__init__.py
CHANGED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Base interfaces package for Wildnerve TLM framework.
|
| 3 |
+
Contains abstract base classes and type definitions for the entire system.
|
| 4 |
+
"""
|
| 5 |
+
from base_interfaces.common_types import *
|
| 6 |
+
|
| 7 |
+
# (package marker—no code needed)
|
base_interfaces/common_types.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Common type definitions and imports for the entire Wildnerve TLM framework.
|
| 3 |
+
This module should be imported first by all other modules to ensure consistent type definitions.
|
| 4 |
+
"""
|
| 5 |
+
import re
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
import json
|
| 9 |
+
import time
|
| 10 |
+
import math
|
| 11 |
+
import torch
|
| 12 |
+
import logging
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
from threading import Lock
|
| 15 |
+
from typing import Dict, List, Optional, Union, Any, Tuple, Set, Callable, Generator, TypeVar, Generic, Iterable, TYPE_CHECKING
|
| 16 |
+
|
| 17 |
+
# Configure logging for the module
|
| 18 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
# Type aliases for cleaner annotations
|
| 22 |
+
ModelType = TypeVar('ModelType')
|
| 23 |
+
TokenizerType = TypeVar('TokenizerType')
|
| 24 |
+
DatasetType = TypeVar('DatasetType')
|
| 25 |
+
EmbeddingType = Union[torch.Tensor, List[float], List[List[float]]]
|
| 26 |
+
|
| 27 |
+
# Constants for registry keys
|
| 28 |
+
MODEL_KEY = "model"
|
| 29 |
+
TOKENIZER_KEY = "tokenizer"
|
| 30 |
+
MODEL_MANAGER_KEY = "model_manager"
|
| 31 |
+
COMMUNICATOR_KEY = "communicator"
|
| 32 |
+
PROMPT_ANALYZER_KEY = "prompt_analyzer"
|
| 33 |
+
PIPELINE_KEY = "pipeline"
|
| 34 |
+
EVENT_DISPATCHER_KEY = "event_dispatcher"
|
| 35 |
+
EVENT_BUS_KEY = "event_bus"
|
| 36 |
+
|
| 37 |
+
# Common exception types
|
| 38 |
+
class InitializationError(Exception):
|
| 39 |
+
"""Raised when a component fails to initialize properly."""
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
class ResourceNotFoundError(Exception):
|
| 43 |
+
"""Raised when a required resource is not found."""
|
| 44 |
+
pass
|
| 45 |
+
|
| 46 |
+
class ServiceNotAvailableError(Exception):
|
| 47 |
+
"""Raised when a required service is not available."""
|
| 48 |
+
pass
|
| 49 |
+
|
| 50 |
+
# Provide a mechanism to check if all necessary imports are available
|
| 51 |
+
def check_imports() -> Tuple[bool, List[str]]:
|
| 52 |
+
"""Check if all necessary imports are available.
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
Tuple of (success, missing_imports)
|
| 56 |
+
"""
|
| 57 |
+
required_imports = [
|
| 58 |
+
"torch", "transformers", "numpy", "pandas",
|
| 59 |
+
"sklearn", "sentence_transformers"
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
missing = []
|
| 63 |
+
for imp in required_imports:
|
| 64 |
+
try:
|
| 65 |
+
__import__(imp)
|
| 66 |
+
except ImportError:
|
| 67 |
+
missing.append(imp)
|
| 68 |
+
|
| 69 |
+
return len(missing) == 0, missing
|
| 70 |
+
|
| 71 |
+
logger.debug(f"Loaded common types module. Python version: {sys.version}")
|
base_interfaces/communicator_interface.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Abstract base class for communicator components.
|
| 3 |
+
"""
|
| 4 |
+
from typing import Dict, List, Optional, Any, Union, Tuple
|
| 5 |
+
import torch
|
| 6 |
+
from base_interfaces.common_types import *
|
| 7 |
+
from base_interfaces.model_interface import AbstractModel
|
| 8 |
+
from abc import ABC, abstractmethod
|
| 9 |
+
|
| 10 |
+
class AbstractCommunicator(ABC):
|
| 11 |
+
"""Minimal communicator interface"""
|
| 12 |
+
@abstractmethod
|
| 13 |
+
def process_input(self, *args, **kwargs):
|
| 14 |
+
pass
|
| 15 |
+
|
| 16 |
+
def process_input(self, input_text: str, context: Optional[Dict] = None) -> Dict[str, Any]:
|
| 17 |
+
"""Process user input through the appropriate model(s) and generate response."""
|
| 18 |
+
raise NotImplementedError("Communicator must implement process_input")
|
| 19 |
+
|
| 20 |
+
def process_request(self, prompt: str, model: Any) -> str:
|
| 21 |
+
"""Process a user request through the selected model"""
|
| 22 |
+
raise NotImplementedError("Communicator must implement process_request")
|
| 23 |
+
|
| 24 |
+
def route_input(self, input_text: str, query: Optional[str] = None) -> List[tuple]:
|
| 25 |
+
"""Route input to most relevant specializations, returning top-k matches."""
|
| 26 |
+
raise NotImplementedError("Communicator must implement route_input")
|
| 27 |
+
|
| 28 |
+
def _extract_subject(self, text: str) -> str:
|
| 29 |
+
"""Extract the primary subject from a text prompt."""
|
| 30 |
+
raise NotImplementedError("Communicator must implement _extract_subject")
|
| 31 |
+
|
| 32 |
+
def prepare_model_input(self, text: str, model) -> Dict:
|
| 33 |
+
"""Prepare input text for model processing."""
|
| 34 |
+
raise NotImplementedError("Communicator must implement prepare_model_input")
|
| 35 |
+
|
| 36 |
+
def _get_fallback_response(self, prompt: str) -> str:
|
| 37 |
+
"""Get a fallback response when primary model processing fails."""
|
| 38 |
+
raise NotImplementedError("Communicator must implement _get_fallback_response")
|
| 39 |
+
|
| 40 |
+
def clear_conversation_history(self):
|
| 41 |
+
"""Clear the conversation history"""
|
| 42 |
+
raise NotImplementedError("Communicator must implement clear_conversation_history")
|
| 43 |
+
|
| 44 |
+
def get_conversation_history(self) -> List[Dict]:
|
| 45 |
+
"""Get the current conversation history"""
|
| 46 |
+
raise NotImplementedError("Communicator must implement get_conversation_history")
|
base_interfaces/model_interface.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Abstract base classes and interfaces for model implementations.
|
| 3 |
+
"""
|
| 4 |
+
from base_interfaces.common_types import *
|
| 5 |
+
from abc import ABC, abstractmethod
|
| 6 |
+
|
| 7 |
+
class AbstractModel(ABC):
|
| 8 |
+
"""Minimal model interface"""
|
| 9 |
+
pass
|