返回提交历史
Modified
g4f/Provider/__init__.py
+65
-42
Modified
g4f/Provider/needs_auth/GigaChat.py
+11
-7
Modified
g4f/debug.py
+5
-8
XFEstudio/gpt4free
Catch import errors
5bf80ec2
代码差异
3 个文件
+81
-57
@@ -4,48 +4,71 @@ from ..providers.types import BaseProvider, ProviderType
4
4
from ..providers.retry_provider import RetryProvider, IterListProvider
5
5
from ..providers.base_provider import AsyncProvider, AsyncGeneratorProvider
6
6
from ..providers.create_images import CreateImagesProvider
7
8
from .deprecated import *
7
from .. import debug
8
try:
9
from .deprecated import *
10
except ImportError as e:
11
debug.error("Deprecated providers not loaded:", e)
9
12
from .needs_auth import *
10
from .not_working import *
11
from .local import *
12
from .hf import HuggingFace, HuggingChat, HuggingFaceAPI, HuggingFaceInference, HuggingFaceMedia
13
from .hf_space import *
14
from .mini_max import HailuoAI, MiniMax
15
13
from .template import OpenaiTemplate, BackendApi
14
from .hf import HuggingFace, HuggingChat, HuggingFaceAPI, HuggingFaceInference, HuggingFaceMedia
15
try:
16
from .not_working import *
17
except ImportError as e:
18
debug.error("Not working providers not loaded:", e)
19
try:
20
from .local import *
21
except ImportError as e:
22
debug.error("Local providers not loaded:", e)
23
try:
24
from .hf_space import *
25
except ImportError as e:
26
debug.error("HuggingFace Space providers not loaded:", e)
27
try:
28
from .mini_max import HailuoAI, MiniMax
29
except ImportError as e:
30
debug.error("MiniMax providers not loaded:", e)
16
31
17
from .AllenAI import AllenAI
18
from .ARTA import ARTA
19
from .Blackbox import Blackbox
20
from .ChatGLM import ChatGLM
21
from .ChatGpt import ChatGpt
22
from .ChatGptEs import ChatGptEs
23
from .Cloudflare import Cloudflare
24
from .Copilot import Copilot
25
from .DDG import DDG
26
from .DeepInfraChat import DeepInfraChat
27
from .Dynaspark import Dynaspark
28
from .Free2GPT import Free2GPT
29
from .FreeGpt import FreeGpt
30
from .GizAI import GizAI
31
from .Glider import Glider
32
from .Goabror import Goabror
33
from .ImageLabs import ImageLabs
34
from .Jmuz import Jmuz
35
from .LambdaChat import LambdaChat
36
from .Liaobots import Liaobots
37
from .OIVSCode import OIVSCode
38
from .PerplexityLabs import PerplexityLabs
39
from .Pi import Pi
40
from .Pizzagpt import Pizzagpt
41
from .PollinationsAI import PollinationsAI
42
from .PollinationsImage import PollinationsImage
43
from .TeachAnything import TeachAnything
44
from .TypeGPT import TypeGPT
45
from .You import You
46
from .Websim import Websim
47
from .Yqcloud import Yqcloud
48
32
try:
33
from .AllenAI import AllenAI
34
from .ARTA import ARTA
35
from .Blackbox import Blackbox
36
from .ChatGLM import ChatGLM
37
from .ChatGpt import ChatGpt
38
from .ChatGptEs import ChatGptEs
39
from .Cloudflare import Cloudflare
40
from .Copilot import Copilot
41
from .DDG import DDG
42
from .DeepInfraChat import DeepInfraChat
43
from .Dynaspark import Dynaspark
44
except ImportError as e:
45
debug.error("Providers not loaded (A-D):", e)
46
try:
47
from .Free2GPT import Free2GPT
48
from .FreeGpt import FreeGpt
49
from .GizAI import GizAI
50
from .Glider import Glider
51
from .Goabror import Goabror
52
from .ImageLabs import ImageLabs
53
from .Jmuz import Jmuz
54
from .LambdaChat import LambdaChat
55
from .Liaobots import Liaobots
56
from .OIVSCode import OIVSCode
57
except ImportError as e:
58
debug.error("Providers not loaded (F-L):", e)
59
try:
60
from .PerplexityLabs import PerplexityLabs
61
from .Pi import Pi
62
from .Pizzagpt import Pizzagpt
63
from .PollinationsAI import PollinationsAI
64
from .PollinationsImage import PollinationsImage
65
from .TeachAnything import TeachAnything
66
from .TypeGPT import TypeGPT
67
from .You import You
68
from .Websim import Websim
69
from .Yqcloud import Yqcloud
70
except ImportError as e:
71
debug.error("Providers not loaded (M-Z):", e)
49
72
50
73
import sys
51
74
@@ -61,9 +84,9 @@ __providers__: list[ProviderType] = [
61
84
__all__: list[str] = [
62
85
provider.__name__ for provider in __providers__
63
86
]
64
__map__: dict[str, ProviderType] = dict([
65
(provider.__name__, provider) for provider in __providers__
66
])
87
__map__: dict[str, ProviderType] = {
88
provider.__name__: provider for provider in __providers__
89
}
67
90
68
91
class ProviderUtils:
69
92
convert: dict[str, ProviderType] = __map__
@@ -1,7 +1,11 @@
1
1
from __future__ import annotations
2
2
3
3
import os
4
import ssl
4
try:
5
import ssl
6
has_ssl = True
7
except ImportError:
8
has_ssl = False
5
9
import time
6
10
import uuid
7
11
from pathlib import Path
@@ -80,19 +84,19 @@ class GigaChat(AsyncGeneratorProvider, ProviderModelMixin):
80
84
model = cls.get_model(model)
81
85
if not api_key:
82
86
raise MissingAuthError('Missing "api_key"')
83
87
84
88
# Create certificate file in cookies directory
85
89
cookies_dir = Path(get_cookies_dir())
86
90
cert_file = cookies_dir / 'russian_trusted_root_ca.crt'
87
91
88
92
# Write certificate if it doesn't exist
89
93
if not cert_file.exists():
90
94
cert_file.write_text(RUSSIAN_CA_CERT)
91
92
ssl_context = ssl.create_default_context(cafile=str(cert_file))
93
if connector is None:
95
96
if has_ssl and connector is None:
97
ssl_context = ssl.create_default_context(cafile=str(cert_file))
94
98
connector = TCPConnector(ssl_context=ssl_context)
95
99
96
100
async with ClientSession(connector=get_connector(connector, proxy)) as session:
97
101
if token_expires_at - int(time.time() * 1000) < 60000:
98
102
async with session.post(url="https://ngw.devices.sberbank.ru:9443/api/v2/oauth",
@@ -1,21 +1,18 @@
1
1
import sys
2
2
from typing import Callable, List, Optional, Any
3
3
4
# Warning: name could conflict with Python's built-in logging module
5
4
logging: bool = False
6
5
version_check: bool = True
7
6
version: Optional[str] = None
8
7
log_handler: Callable = print # More specifically: Callable[[Any, Optional[Any]], None]
9
8
logs: List[str] = []
10
9
11
def log(text: Any, file: Optional[Any] = None) -> None:
10
def log(*text: Any, file: Optional[Any] = None) -> None:
12
11
"""Log a message if logging is enabled."""
13
12
if logging:
14
log_handler(text, file=file)
13
log_handler(*text, file=file)
15
14
16
def error(error: Any, name: Optional[str] = None) -> None:
15
def error(*error: Any, name: Optional[str] = None) -> None:
17
16
"""Log an error message to stderr."""
18
log(
19
error if isinstance(error, str) else f"{type(error).__name__ if name is None else name}: {error}",
20
file=sys.stderr
21
)
17
error = [e if isinstance(e, str) else f"{type(e).__name__ if name is None else name}: {e}" for e in error]
18
log(*error, file=sys.stderr)