summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/retry_provider.py
diff options
context:
space:
mode:
authorCommenter123321 <36051603+Commenter123321@users.noreply.github.com>2023-10-09 18:02:06 +0200
committerCommenter123321 <36051603+Commenter123321@users.noreply.github.com>2023-10-09 18:02:06 +0200
commit119817c96349807efaf87ee432ce46446542b66a (patch)
tree1dbdf4d4dbf4f6c8a8247274ef500a2f1de765d1 /g4f/Provider/retry_provider.py
parentaivvm's no life creator keeps patching it, but I'm just better 😉 (diff)
parentMerge branch 'main' of https://github.com/xtekky/gpt4free (diff)
downloadgpt4free-119817c96349807efaf87ee432ce46446542b66a.tar
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.gz
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.bz2
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.lz
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.xz
gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.zst
gpt4free-119817c96349807efaf87ee432ce46446542b66a.zip
Diffstat (limited to 'g4f/Provider/retry_provider.py')
-rw-r--r--g4f/Provider/retry_provider.py41
1 files changed, 24 insertions, 17 deletions
diff --git a/g4f/Provider/retry_provider.py b/g4f/Provider/retry_provider.py
index e1a9cd1f..b49020b2 100644
--- a/g4f/Provider/retry_provider.py
+++ b/g4f/Provider/retry_provider.py
@@ -1,32 +1,33 @@
from __future__ import annotations
import random
-
+from typing import List, Type, Dict
from ..typing import CreateResult
from .base_provider import BaseProvider, AsyncProvider
+from ..debug import logging
class RetryProvider(AsyncProvider):
- __name__ = "RetryProvider"
- working = True
- needs_auth = False
- supports_stream = True
- supports_gpt_35_turbo = False
- supports_gpt_4 = False
+ __name__: str = "RetryProvider"
+ working: bool = True
+ needs_auth: bool = False
+ supports_stream: bool = True
+ supports_gpt_35_turbo: bool = False
+ supports_gpt_4: bool = False
def __init__(
self,
- providers: list[type[BaseProvider]],
+ providers: List[Type[BaseProvider]],
shuffle: bool = True
) -> None:
- self.providers = providers
- self.shuffle = shuffle
+ self.providers: List[Type[BaseProvider]] = providers
+ self.shuffle: bool = shuffle
def create_completion(
self,
model: str,
- messages: list[dict[str, str]],
+ messages: List[Dict[str, str]],
stream: bool = False,
**kwargs
) -> CreateResult:
@@ -37,10 +38,12 @@ class RetryProvider(AsyncProvider):
if self.shuffle:
random.shuffle(providers)
- self.exceptions = {}
- started = False
+ self.exceptions: Dict[str, Exception] = {}
+ started: bool = False
for provider in providers:
try:
+ if logging:
+ print(f"Using {provider.__name__} provider")
for token in provider.create_completion(model, messages, stream, **kwargs):
yield token
started = True
@@ -48,6 +51,8 @@ class RetryProvider(AsyncProvider):
return
except Exception as e:
self.exceptions[provider.__name__] = e
+ if logging:
+ print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
if started:
break
@@ -56,23 +61,25 @@ class RetryProvider(AsyncProvider):
async def create_async(
self,
model: str,
- messages: list[dict[str, str]],
+ messages: List[Dict[str, str]],
**kwargs
) -> str:
- providers = [provider for provider in self.providers if issubclass(provider, AsyncProvider)]
+ providers = [provider for provider in self.providers]
if self.shuffle:
random.shuffle(providers)
- self.exceptions = {}
+ self.exceptions: Dict[str, Exception] = {}
for provider in providers:
try:
return await provider.create_async(model, messages, **kwargs)
except Exception as e:
self.exceptions[provider.__name__] = e
+ if logging:
+ print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
self.raise_exceptions()
- def raise_exceptions(self):
+ def raise_exceptions(self) -> None:
if self.exceptions:
raise RuntimeError("\n".join(["All providers failed:"] + [
f"{p}: {self.exceptions[p].__class__.__name__}: {self.exceptions[p]}" for p in self.exceptions