File size: 1,799 Bytes
b313f94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Base LLM Provider interface"""
from abc import ABC, abstractmethod
from typing import List, Dict, Iterator, Optional


class BaseLLMProvider(ABC):
    """Abstract base class for LLM providers"""
    
    def __init__(self, api_key: Optional[str] = None, api_url: Optional[str] = None, **kwargs):
        """
        Initialize the LLM provider
        
        Args:
            api_key: API key for the provider (if needed)
            **kwargs: Additional provider-specific configuration
        """
        self.api_key = api_key
        self.api_url = api_url
        self.config = kwargs
    
    @abstractmethod
    def chat(
        self,
        messages: List[Dict[str, str]],
        model: str,
        temperature: float = 0.7,
        max_tokens: Optional[int] = None,
        stream: bool = False,
        **kwargs
    ) -> str | Iterator[str]:
        """
        Args:
            messages: List of message dicts with 'role' and 'content'
            model: Model identifier
            temperature: Sampling temperature (0.0 to 2.0)
            max_tokens: Maximum tokens to generate
            stream: Whether to stream the response
            **kwargs: Additional provider-specific parameters
            
        Returns:
            String response or iterator of response chunks if streaming
        """
        pass
    
    @abstractmethod
    def get_available_models(self) -> List[str]:
        pass
    
    @property
    @abstractmethod
    def name(self) -> str:
        """Provider name"""
        pass
    
    def is_available(self) -> bool:
        return self.api_key is not None or not self.requires_api_key() or self.api_url is not None
    
    def requires_api_key(self) -> bool:
        """Whether this provider requires an API key"""
        return True