summaryrefslogtreecommitdiffstats
path: root/g4f/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'g4f/__init__.py')
-rw-r--r--g4f/__init__.py57
1 files changed, 38 insertions, 19 deletions
diff --git a/g4f/__init__.py b/g4f/__init__.py
index 8bfed8e3..92bce194 100644
--- a/g4f/__init__.py
+++ b/g4f/__init__.py
@@ -1,25 +1,44 @@
from __future__ import annotations
-from requests import get
-from .models import Model, ModelUtils, _all_models
-from .Provider import BaseProvider, AsyncGeneratorProvider, RetryProvider
-from .typing import Messages, CreateResult, AsyncResult, Union, List
-from . import debug
-version = '0.1.9.2'
-version_check = True
+import os
+from requests import get
+from importlib.metadata import version as get_package_version, PackageNotFoundError
+from subprocess import check_output, CalledProcessError, PIPE
-def check_pypi_version() -> None:
- try:
- response = get("https://pypi.org/pypi/g4f/json").json()
- latest_version = response["info"]["version"]
+from .models import Model, ModelUtils, _all_models
+from .Provider import BaseProvider, AsyncGeneratorProvider, RetryProvider
+from .typing import Messages, CreateResult, AsyncResult, Union, List
+from . import debug
- if version != latest_version:
- print(f'New pypi version: {latest_version} (current: {version}) | pip install -U g4f')
- return False
- return True
+def get_version() -> str:
+ # Read from package manager
+ try:
+ return get_package_version("g4f")
+ except PackageNotFoundError:
+ pass
+ # Read from docker environment
+ current_version = os.environ.get("G4F_VERSION")
+ if current_version:
+ return current_version
+ # Read from git repository
+ try:
+ command = ["git", "describe", "--tags", "--abbrev=0"]
+ return check_output(command, text=True, stderr=PIPE).strip()
+ except CalledProcessError:
+ pass
+
+def get_lastet_version() -> str:
+ response = get("https://pypi.org/pypi/g4f/json").json()
+ return response["info"]["version"]
+def check_pypi_version() -> None:
+ try:
+ version = get_version()
+ latest_version = get_lastet_version()
except Exception as e:
print(f'Failed to check g4f pypi version: {e}')
+ if version != latest_version:
+ print(f'New pypi version: {latest_version} (current: {version}) | pip install -U g4f')
def get_model_and_provider(model : Union[Model, str],
provider : Union[type[BaseProvider], None],
@@ -27,6 +46,9 @@ def get_model_and_provider(model : Union[Model, str],
ignored : List[str] = None,
ignore_working: bool = False,
ignore_stream: bool = False) -> tuple[Model, type[BaseProvider]]:
+ if debug.version_check:
+ check_pypi_version()
+ debug.version_check = False
if isinstance(model, str):
if model in ModelUtils.convert:
@@ -118,7 +140,4 @@ class Completion:
result = provider.create_completion(model.name, [{"role": "user", "content": prompt}], stream, **kwargs)
- return result if stream else ''.join(result)
-
-if version_check:
- check_pypi_version() \ No newline at end of file
+ return result if stream else ''.join(result) \ No newline at end of file