gpt4free/g4f/version.py
hlohaus c12227a1cd fix: enhance retry logic and parameter handling in commit and provider code
- Added `--max-retries` argument to `parse_arguments()` in `commit.py` with default `MAX_RETRIES`
- Updated `generate_commit_message()` to accept a `max_retries` parameter and iterate accordingly
- Included check to raise immediately if `max_retries` is 1 within `generate_commit_message()`
- Passed `args.max_retries` when calling `generate_commit_message()` in `main()`
- In `g4f/Provider/har/__init__.py`, imported `ResponseError` and added check for network error to raise `ResponseError`
- In `g4f/Provider/hf_space/Qwen_Qwen_3.py`, changed default model string and updated system prompt handling to use `get_system_prompt()`
- In `g4f/Provider/needs_auth/LMArenaBeta.py`, modified callback to wait for cookie and turnstile response
- In `g4f/Provider/needs_auth/PuterJS.py`, adjusted `get_models()` to filter out certain models
- In `g4f/gui/server/api.py`, adjusted `get_model_data()` to handle models starting with "openrouter:"
- In `g4f/providers/any_provider.py`, imported and used `ResponseError`; added logic to process `model_aliases` with updated model name resolution
- Refined model name cleaning logic to handle additional patterns and replaced multiple regex patterns to better match version strings
- Updated list of providers `PROVIERS_LIST_1`, `PROVIERS_LIST_2`, `PROVIERS_LIST_3`, and their usage to include new providers and adjust filtering
- In `g4f/version.py`, added `get_git_version()` function, retrieved version with `git describe` command, instead of only relying on `get_github_version()`, increasing robustness
2025-06-13 05:32:55 +02:00

123 lines
3.9 KiB
Python

from __future__ import annotations
from os import environ
import requests
from functools import cached_property
from importlib.metadata import version as get_package_version, PackageNotFoundError
from subprocess import check_output, CalledProcessError, PIPE
from .errors import VersionNotFoundError
from .constants import PACKAGE_NAME, GITHUB_REPOSITORY
from . import debug
def get_pypi_version(package_name: str) -> str:
"""
Retrieves the latest version of a package from PyPI.
Args:
package_name (str): The name of the package for which to retrieve the version.
Returns:
str: The latest version of the specified package from PyPI.
Raises:
VersionNotFoundError: If there is an error in fetching the version from PyPI.
"""
try:
response = requests.get(f"https://pypi.org/pypi/{package_name}/json").json()
return response["info"]["version"]
except requests.RequestException as e:
raise VersionNotFoundError(f"Failed to get PyPI version: {e}")
def get_github_version(repo: str) -> str:
"""
Retrieves the latest release version from a GitHub repository.
Args:
repo (str): The name of the GitHub repository.
Returns:
str: The latest release version from the specified GitHub repository.
Raises:
VersionNotFoundError: If there is an error in fetching the version from GitHub.
"""
try:
response = requests.get(f"https://api.github.com/repos/{repo}/releases/latest")
response.raise_for_status()
return response.json()["tag_name"]
except requests.RequestException as e:
raise VersionNotFoundError(f"Failed to get GitHub release version: {e}")
def get_git_version() -> str:
# Read from git repository
try:
command = ["git", "describe", "--tags", "--abbrev=0"]
return check_output(command, text=True, stderr=PIPE).strip()
except CalledProcessError:
return None
class VersionUtils:
"""
Utility class for managing and comparing package versions of 'g4f'.
"""
@cached_property
def current_version(self) -> str:
"""
Retrieves the current version of the 'g4f' package.
Returns:
str: The current version of 'g4f'.
Raises:
VersionNotFoundError: If the version cannot be determined from the package manager,
Docker environment, or git repository.
"""
if debug.version:
return debug.version
# Read from package manager
try:
return get_package_version(PACKAGE_NAME)
except PackageNotFoundError:
pass
# Read from docker environment
version = environ.get("G4F_VERSION")
if version:
return version
return get_git_version()
@property
def latest_version(self) -> str:
"""
Retrieves the latest version of the 'g4f' package.
Returns:
str: The latest version of 'g4f'.
"""
# Is installed via package manager?
try:
get_package_version(PACKAGE_NAME)
except PackageNotFoundError:
return get_github_version(GITHUB_REPOSITORY)
return get_pypi_version(PACKAGE_NAME)
@cached_property
def latest_version_cached(self) -> str:
return self.latest_version
def check_version(self) -> None:
"""
Checks if the current version of 'g4f' is up to date with the latest version.
Note:
If a newer version is available, it prints a message with the new version and update instructions.
"""
try:
if self.current_version != self.latest_version:
print(f'New g4f version: {self.latest_version} (current: {self.current_version}) | pip install -U g4f')
except Exception as e:
print(f'Failed to check g4f version: {e}')
utils = VersionUtils()