返回提交历史
Modified
g4f/api/__init__.py
+5
-8
Modified
g4f/cli/__init__.py
+14
-9
Modified
g4f/config.py
+3
-0
XFEstudio/gpt4free
Fix handling of stream_timeout
4c1da4e6
代码差异
3 个文件
+22
-17
@@ -60,9 +60,10 @@ except ImportError:
60
60
61
61
import g4f
62
62
import g4f.debug
63
from g4f.client import AsyncClient, ChatCompletion, ImagesResponse, ClientResponse
63
from g4f.client import AsyncClient, ChatCompletion, ImagesResponse
64
64
from g4f.providers.response import BaseConversation, JsonConversation
65
65
from g4f.client.helper import filter_none
66
from g4f.config import DEFAULT_PORT, DEFAULT_TIMEOUT, DEFAULT_STREAM_TIMEOUT
66
67
from g4f.image import EXTENSIONS_MAP, is_data_an_media, process_image
67
68
from g4f.image.copy_images import get_media_dir, copy_media, get_source_url
68
69
from g4f.errors import ProviderNotFoundError, ModelNotFoundError, MissingAuthError, NoValidHarFileError, MissingRequirementsError
@@ -90,18 +91,14 @@ except ImportError:
90
91
91
92
logger = logging.getLogger(__name__)
92
93
93
DEFAULT_PORT = 1337
94
DEFAULT_TIMEOUT = 600
95
DEFAULT_STREAM_TIMEOUT = 15
96
97
94
@asynccontextmanager
98
95
async def lifespan(app: FastAPI):
99
96
# Read cookie files if not ignored
100
97
if not AppConfig.ignore_cookie_files:
101
98
read_cookie_files()
102
99
AppConfig.g4f_api_key = os.environ.get("G4F_API_KEY", AppConfig.g4f_api_key)
103
AppConfig.timeout = os.environ.get("G4F_TIMEOUT", AppConfig.timeout)
104
AppConfig.stream_timeout = os.environ.get("G4F_STREAM_TIMEOUT", AppConfig.stream_timeout)
100
AppConfig.timeout = int(os.environ.get("G4F_TIMEOUT", AppConfig.timeout))
101
AppConfig.stream_timeout = int(os.environ.get("G4F_STREAM_TIMEOUT", AppConfig.stream_timeout))
105
102
yield
106
103
if has_nodriver:
107
104
for browser in util.get_registered_instances():
@@ -442,7 +439,7 @@ class Api:
442
439
config.conversation_id = conversation_id
443
440
if config.timeout is None:
444
441
config.timeout = AppConfig.timeout
445
if config.stream_timeout is None:
442
if config.stream_timeout is None and config.stream:
446
443
config.stream_timeout = AppConfig.stream_timeout
447
444
if credentials is not None and credentials.credentials != "secret":
448
445
config.api_key = credentials.credentials
@@ -2,17 +2,18 @@ from __future__ import annotations
2
2
3
3
import argparse
4
4
from argparse import ArgumentParser
5
5
6
from .client import get_parser, run_client_args
6
7
from ..requests import BrowserConfig
7
8
from g4f import Provider
9
from g4f.gui.run import gui_parser, run_gui_args
10
import g4f.cookies
8
from ..gui.run import gui_parser, run_gui_args
9
from ..config import DEFAULT_PORT, DEFAULT_TIMEOUT, DEFAULT_STREAM_TIMEOUT
10
from .. import Provider
11
from .. import cookies
11
12
12
13
def get_api_parser():
13
14
api_parser = ArgumentParser(description="Run the API and GUI")
14
api_parser.add_argument("--bind", default=None, help="The bind string. (Default: 0.0.0.0:1337)")
15
api_parser.add_argument("--port", "-p", default=None, help="Change the port of the server.")
15
api_parser.add_argument("--bind", default=None, help=f"The bind string. (Default: 0.0.0.0:{DEFAULT_PORT})")
16
api_parser.add_argument("--port", "-p", default=None, help=f"Change the port of the server. (Default: {DEFAULT_PORT})")
16
17
api_parser.add_argument("--debug", "-d", action="store_true", help="Enable verbose logging.")
17
18
api_parser.add_argument("--gui", "-g", default=None, action="store_true", help="(deprecated)")
18
19
api_parser.add_argument("--no-gui", "-ng", default=False, action="store_true", help="Start without the gui.")
@@ -28,11 +29,12 @@ def get_api_parser():
28
29
api_parser.add_argument("--g4f-api-key", type=str, default=None, help="Sets an authentication key for your API. (incompatible with --reload and --workers)")
29
30
api_parser.add_argument("--ignored-providers", nargs="+", choices=[provider.__name__ for provider in Provider.__providers__ if provider.working],
30
31
default=[], help="List of providers to ignore when processing request. (incompatible with --reload and --workers)")
31
api_parser.add_argument("--cookie-browsers", nargs="+", choices=[browser.__name__ for browser in g4f.cookies.BROWSERS],
32
api_parser.add_argument("--cookie-browsers", nargs="+", choices=[browser.__name__ for browser in cookies.BROWSERS],
32
33
default=[], help="List of browsers to access or retrieve cookies from. (incompatible with --reload and --workers)")
33
34
api_parser.add_argument("--reload", action="store_true", help="Enable reloading.")
34
35
api_parser.add_argument("--demo", action="store_true", help="Enable demo mode.")
35
api_parser.add_argument("--timeout", type=int, default=600, help="Default timeout for requests in seconds. (incompatible with --reload and --workers)")
36
api_parser.add_argument("--timeout", type=int, default=DEFAULT_TIMEOUT, help="Default timeout for requests in seconds. (incompatible with --reload and --workers)")
37
api_parser.add_argument("--stream-timeout", type=int, default=DEFAULT_STREAM_TIMEOUT, help="Default timeout for streaming requests in seconds. (incompatible with --reload and --workers)")
36
38
api_parser.add_argument("--ssl-keyfile", type=str, default=None, help="Path to SSL key file for HTTPS.")
37
39
api_parser.add_argument("--ssl-certfile", type=str, default=None, help="Path to SSL certificate file for HTTPS.")
38
40
api_parser.add_argument("--log-config", type=str, default=None, help="Custom log config.")
@@ -55,12 +57,15 @@ def run_api_args(args):
55
57
gui=not args.no_gui,
56
58
demo=args.demo,
57
59
timeout=args.timeout,
60
stream_timeout=args.stream_timeout
58
61
)
62
59
63
if args.browser_port:
60
64
BrowserConfig.port = args.browser_port
61
65
BrowserConfig.host = args.browser_host
62
66
if args.cookie_browsers:
63
g4f.cookies.BROWSERS = [g4f.cookies[browser] for browser in args.cookie_browsers]
67
cookies.BROWSERS = [cookies[browser] for browser in args.cookie_browsers]
68
64
69
run_api(
65
70
bind=args.bind,
66
71
port=args.port,
@@ -14,6 +14,9 @@ def get_config_dir() -> Path:
14
14
return Path.home() / "Library" / "Application Support"
15
15
return Path.home() / ".config"
16
16
17
DEFAULT_PORT = 1337
18
DEFAULT_TIMEOUT = 600
19
DEFAULT_STREAM_TIMEOUT = 15
17
20
18
21
PACKAGE_NAME = "g4f"
19
22
CONFIG_DIR = get_config_dir() / PACKAGE_NAME