File size: 2,041 Bytes
59f10cb
084503a
adf8222
 
59f10cb
 
084503a
59f10cb
 
adf8222
 
 
 
 
 
e7063a6
adf8222
 
e7063a6
adf8222
 
 
 
 
 
 
 
 
e7063a6
adf8222
e7063a6
 
 
 
972f028
e7063a6
 
 
084503a
e7063a6
 
972f028
e7063a6
 
 
 
 
 
 
 
 
 
 
adf8222
 
 
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
import logging
from typing import Optional
from src.llm.base_provider import LLMProvider
from src.llm.hf_provider import HuggingFaceProvider
from src.llm.ollama_provider import OllamaProvider
from utils.config import config
from src.services.hf_monitor import hf_monitor

logger = logging.getLogger(__name__)

class ProviderNotAvailableError(Exception):
    """Raised when no provider is available"""
    pass

class LLMFactory:
    """Factory for creating LLM providers with intelligent priority"""
    
    _instance = None
    _providers = {}

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(LLMFactory, cls).__new__(cls)
        return cls._instance

    def get_provider(self, preferred_provider: Optional[str] = None) -> LLMProvider:
        """
        Get an LLM provider based on preference and availability.
        NEW PRIORITY: HF Endpoint > Ollama > Error
        """
        # Check if HF should be primary (when token available and endpoint ready)
        if config.hf_token:
            hf_status = hf_monitor.get_endpoint_status()
            if hf_status["available"]:
                try:
                    logger.info("🎯 Using HF Endpoint as PRIMARY provider")
                    return HuggingFaceProvider(
                        model_name="DavidAU/OpenAi-GPT-oss-20b-abliterated-uncensored-NEO-Imatrix-gguf"
                    )
                except Exception as e:
                    logger.warning(f"Failed to initialize HF provider: {e}")
        
        # Try Ollama as secondary
        if config.ollama_host:
            try:
                logger.info("🦙 Using Ollama as SECONDARY provider")
                return OllamaProvider(
                    model_name=config.local_model_name
                )
            except Exception as e:
                logger.warning(f"Failed to initialize Ollama provider: {e}")
        
        raise ProviderNotAvailableError("No LLM providers are available")

# Global factory instance
llm_factory = LLMFactory()