from __future__ import annotations
import os
import sys
from pathlib import Path
from typing import Optional
def get_config_dir() -> Path:
"""Get platform-appropriate config directory."""
def get_fallback_config_dir() -> Path:
if sys.platform == "win32":
return (
Path(os.environ.get("APPDATA", Path.home() / "AppData" / "Roaming"))
/ "g4f"
)
elif sys.platform == "darwin":
return Path.home() / "Library" / "Application Support" / "g4f"
return Path.home() / ".config" / "g4f"
config_dir = Path.home() / ".g4f"
if not config_dir.exists():
config_dir = get_fallback_config_dir()
if not config_dir.exists():
config_dir = Path.home() / ".g4f"
return config_dir
DEFAULT_PORT = 1337
DEFAULT_TIMEOUT = 600
DEFAULT_STREAM_TIMEOUT = 120
PACKAGE_NAME = "g4f"
CONFIG_DIR = get_config_dir()
COOKIES_DIR = CONFIG_DIR / "cookies"
CUSTOM_COOKIES_DIR = "./har_and_cookies"
ORGANIZATION = "gpt4free"
GITHUB_REPOSITORY = f"xtekky/{ORGANIZATION}"
STATIC_DOMAIN = f"{PACKAGE_NAME}.dev"
STATIC_URL = f"https://{STATIC_DOMAIN}/"
REFFERER_URL = f"https://{STATIC_DOMAIN}/"
SPACE_URL = f"https://g4f.space/"
DIST_DIR = f"./{STATIC_DOMAIN}/dist"
DEFAULT_MODEL = "openai/gpt-oss-120b"
JSDELIVR_URL = "https://cdn.jsdelivr.net/"
DOWNLOAD_URL = f"{JSDELIVR_URL}gh/{ORGANIZATION}/{STATIC_DOMAIN}/"
GITHUB_URL = (
f"https://raw.githubusercontent.com/{ORGANIZATION}/{STATIC_DOMAIN}/refs/heads/main/"
)
class AppConfig:
ignored_providers: Optional[list[str]] = None
g4f_api_key: Optional[str] = None
ignore_cookie_files: bool = False
model: str = None
provider: str = None
media_provider: str = None
proxy: str = None
gui: bool = False
demo: bool = False
timeout: int = DEFAULT_TIMEOUT
stream_timeout: int = DEFAULT_STREAM_TIMEOUT
disable_custom_api_key: bool = False
g4f_space_api_key: Optional[str] = None
@classmethod
def set_config(cls, **data):
for key, value in data.items():
if value is not None:
setattr(cls, key, value)
@classmethod
def load_from_env(cls):
cls.g4f_api_key = os.environ.get("G4F_API_KEY", cls.g4f_api_key)
cls.timeout = int(os.environ.get("G4F_TIMEOUT", cls.timeout))
cls.stream_timeout = int(
os.environ.get("G4F_STREAM_TIMEOUT", cls.stream_timeout)
)
cls.proxy = os.environ.get("G4F_PROXY", cls.proxy)
cls.model = os.environ.get("G4F_MODEL", cls.model)
cls.provider = os.environ.get("G4F_PROVIDER", cls.provider)
cls.disable_custom_api_key = os.environ.get(
"G4F_DISABLE_CUSTOM_API_KEY", str(cls.disable_custom_api_key)
).lower() in ("true", "1", "yes")
cls.g4f_space_api_key = os.environ.get(
"G4F_SPACE_API_KEY", cls.g4f_space_api_key
)
from __future__ import annotations
import os
import sys
from pathlib import Path
from typing import Optional
def get_config_dir() -> Path:
"""Get platform-appropriate config directory."""
def get_fallback_config_dir() -> Path:
if sys.platform == "win32":
return (
Path(os.environ.get("APPDATA", Path.home() / "AppData" / "Roaming"))
/ "g4f"
)
elif sys.platform == "darwin":
return Path.home() / "Library" / "Application Support" / "g4f"
return Path.home() / ".config" / "g4f"
config_dir = Path.home() / ".g4f"
if not config_dir.exists():
config_dir = get_fallback_config_dir()
if not config_dir.exists():
config_dir = Path.home() / ".g4f"
return config_dir
DEFAULT_PORT = 1337
DEFAULT_TIMEOUT = 600
DEFAULT_STREAM_TIMEOUT = 120
PACKAGE_NAME = "g4f"
CONFIG_DIR = get_config_dir()
COOKIES_DIR = CONFIG_DIR / "cookies"
CUSTOM_COOKIES_DIR = "./har_and_cookies"
ORGANIZATION = "gpt4free"
GITHUB_REPOSITORY = f"xtekky/{ORGANIZATION}"
STATIC_DOMAIN = f"{PACKAGE_NAME}.dev"
STATIC_URL = f"https://{STATIC_DOMAIN}/"
REFFERER_URL = f"https://{STATIC_DOMAIN}/"
SPACE_URL = f"https://g4f.space/"
DIST_DIR = f"./{STATIC_DOMAIN}/dist"
DEFAULT_MODEL = "openai/gpt-oss-120b"
JSDELIVR_URL = "https://cdn.jsdelivr.net/"
DOWNLOAD_URL = f"{JSDELIVR_URL}gh/{ORGANIZATION}/{STATIC_DOMAIN}/"
GITHUB_URL = (
f"https://raw.githubusercontent.com/{ORGANIZATION}/{STATIC_DOMAIN}/refs/heads/main/"
)
class AppConfig:
ignored_providers: Optional[list[str]] = None
g4f_api_key: Optional[str] = None
ignore_cookie_files: bool = False
model: str = None
provider: str = None
media_provider: str = None
proxy: str = None
gui: bool = False
demo: bool = False
timeout: int = DEFAULT_TIMEOUT
stream_timeout: int = DEFAULT_STREAM_TIMEOUT
disable_custom_api_key: bool = False
g4f_space_api_key: Optional[str] = None
@classmethod
def set_config(cls, **data):
for key, value in data.items():
if value is not None:
setattr(cls, key, value)
@classmethod
def load_from_env(cls):
cls.g4f_api_key = os.environ.get("G4F_API_KEY", cls.g4f_api_key)
cls.timeout = int(os.environ.get("G4F_TIMEOUT", cls.timeout))
cls.stream_timeout = int(
os.environ.get("G4F_STREAM_TIMEOUT", cls.stream_timeout)
)
cls.proxy = os.environ.get("G4F_PROXY", cls.proxy)
cls.model = os.environ.get("G4F_MODEL", cls.model)
cls.provider = os.environ.get("G4F_PROVIDER", cls.provider)
cls.disable_custom_api_key = os.environ.get(
"G4F_DISABLE_CUSTOM_API_KEY", str(cls.disable_custom_api_key)
).lower() in ("true", "1", "yes")
cls.g4f_space_api_key = os.environ.get(
"G4F_SPACE_API_KEY", cls.g4f_space_api_key
)