summaryrefslogtreecommitdiffstats
path: root/g4f/base_provider.py
blob: 84cbc3844d5fe1420689cddd5dfc74e3ba3868b8 (plain) (blame)
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
from abc import ABC, abstractmethod
from .typing import Messages, CreateResult, Union
    
class BaseProvider(ABC):
    url: str
    working: bool = False
    needs_auth: bool = False
    supports_stream: bool = False
    supports_gpt_35_turbo: bool = False
    supports_gpt_4: bool = False
    supports_message_history: bool = False
    params: str

    @classmethod
    @abstractmethod
    def create_completion(
        cls,
        model: str,
        messages: Messages,
        stream: bool,
        **kwargs
    ) -> CreateResult:
        raise NotImplementedError()

    @classmethod
    @abstractmethod
    async def create_async(
        cls,
        model: str,
        messages: Messages,
        **kwargs
    ) -> str:
        raise NotImplementedError()
    
    @classmethod
    def get_dict(cls):
        return {'name': cls.__name__, 'url': cls.url} 
    
class BaseRetryProvider(BaseProvider):
    __name__: str = "RetryProvider"
    supports_stream: bool = True

    def __init__(
        self,
        providers: list[type[BaseProvider]],
        shuffle: bool = True
    ) -> None:
        self.providers: list[type[BaseProvider]] = providers
        self.shuffle: bool = shuffle
        self.working: bool = True
        self.exceptions: dict[str, Exception] = {}
        self.last_provider: type[BaseProvider] = None
        
ProviderType = Union[type[BaseProvider], BaseRetryProvider]