summaryrefslogtreecommitdiffstats
path: root/g4f/base_provider.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-01-14 15:32:51 +0100
committerGitHub <noreply@github.com>2024-01-14 15:32:51 +0100
commit1ca80ed48b55d6462b4bd445e66d4f7de7442c2b (patch)
tree05a94b53b83461b8249de965e093b4fd3722e2d1 /g4f/base_provider.py
parentMerge pull request #1466 from hlohaus/upp (diff)
parentChange doctypes style to Google (diff)
downloadgpt4free-1ca80ed48b55d6462b4bd445e66d4f7de7442c2b.tar
gpt4free-1ca80ed48b55d6462b4bd445e66d4f7de7442c2b.tar.gz
gpt4free-1ca80ed48b55d6462b4bd445e66d4f7de7442c2b.tar.bz2
gpt4free-1ca80ed48b55d6462b4bd445e66d4f7de7442c2b.tar.lz
gpt4free-1ca80ed48b55d6462b4bd445e66d4f7de7442c2b.tar.xz
gpt4free-1ca80ed48b55d6462b4bd445e66d4f7de7442c2b.tar.zst
gpt4free-1ca80ed48b55d6462b4bd445e66d4f7de7442c2b.zip
Diffstat (limited to 'g4f/base_provider.py')
-rw-r--r--g4f/base_provider.py81
1 files changed, 71 insertions, 10 deletions
diff --git a/g4f/base_provider.py b/g4f/base_provider.py
index 1863f6bc..03ae64d6 100644
--- a/g4f/base_provider.py
+++ b/g4f/base_provider.py
@@ -1,7 +1,22 @@
from abc import ABC, abstractmethod
-from .typing import Messages, CreateResult, Union
-
+from typing import Union, List, Dict, Type
+from .typing import Messages, CreateResult
+
class BaseProvider(ABC):
+ """
+ Abstract base class for a provider.
+
+ Attributes:
+ url (str): URL of the provider.
+ working (bool): Indicates if the provider is currently working.
+ needs_auth (bool): Indicates if the provider needs authentication.
+ supports_stream (bool): Indicates if the provider supports streaming.
+ supports_gpt_35_turbo (bool): Indicates if the provider supports GPT-3.5 Turbo.
+ supports_gpt_4 (bool): Indicates if the provider supports GPT-4.
+ supports_message_history (bool): Indicates if the provider supports message history.
+ params (str): List parameters for the provider.
+ """
+
url: str = None
working: bool = False
needs_auth: bool = False
@@ -20,6 +35,18 @@ class BaseProvider(ABC):
stream: bool,
**kwargs
) -> CreateResult:
+ """
+ Create a completion with the given parameters.
+
+ Args:
+ model (str): The model to use.
+ messages (Messages): The messages to process.
+ stream (bool): Whether to use streaming.
+ **kwargs: Additional keyword arguments.
+
+ Returns:
+ CreateResult: The result of the creation process.
+ """
raise NotImplementedError()
@classmethod
@@ -30,25 +57,59 @@ class BaseProvider(ABC):
messages: Messages,
**kwargs
) -> str:
+ """
+ Asynchronously create a completion with the given parameters.
+
+ Args:
+ model (str): The model to use.
+ messages (Messages): The messages to process.
+ **kwargs: Additional keyword arguments.
+
+ Returns:
+ str: The result of the creation process.
+ """
raise NotImplementedError()
@classmethod
- def get_dict(cls):
+ def get_dict(cls) -> Dict[str, str]:
+ """
+ Get a dictionary representation of the provider.
+
+ Returns:
+ Dict[str, str]: A dictionary with provider's details.
+ """
return {'name': cls.__name__, 'url': cls.url}
class BaseRetryProvider(BaseProvider):
+ """
+ Base class for a provider that implements retry logic.
+
+ Attributes:
+ providers (List[Type[BaseProvider]]): List of providers to use for retries.
+ shuffle (bool): Whether to shuffle the providers list.
+ exceptions (Dict[str, Exception]): Dictionary of exceptions encountered.
+ last_provider (Type[BaseProvider]): The last provider used.
+ """
+
__name__: str = "RetryProvider"
supports_stream: bool = True
def __init__(
self,
- providers: list[type[BaseProvider]],
+ 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
+ """
+ Initialize the BaseRetryProvider.
+
+ Args:
+ providers (List[Type[BaseProvider]]): List of providers to use.
+ shuffle (bool): Whether to shuffle the providers list.
+ """
+ self.providers = providers
+ self.shuffle = shuffle
+ self.working = True
+ self.exceptions: Dict[str, Exception] = {}
+ self.last_provider: Type[BaseProvider] = None
-ProviderType = Union[type[BaseProvider], BaseRetryProvider] \ No newline at end of file
+ProviderType = Union[Type[BaseProvider], BaseRetryProvider] \ No newline at end of file