diff options
author | Commenter123321 <36051603+Commenter123321@users.noreply.github.com> | 2023-10-09 18:02:06 +0200 |
---|---|---|
committer | Commenter123321 <36051603+Commenter123321@users.noreply.github.com> | 2023-10-09 18:02:06 +0200 |
commit | 119817c96349807efaf87ee432ce46446542b66a (patch) | |
tree | 1dbdf4d4dbf4f6c8a8247274ef500a2f1de765d1 /g4f/__init__.py | |
parent | aivvm's no life creator keeps patching it, but I'm just better 😉 (diff) | |
parent | Merge branch 'main' of https://github.com/xtekky/gpt4free (diff) | |
download | gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.gz gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.bz2 gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.lz gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.xz gpt4free-119817c96349807efaf87ee432ce46446542b66a.tar.zst gpt4free-119817c96349807efaf87ee432ce46446542b66a.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/__init__.py | 82 |
1 files changed, 42 insertions, 40 deletions
diff --git a/g4f/__init__.py b/g4f/__init__.py index 017c4dd0..4b1e4b80 100644 --- a/g4f/__init__.py +++ b/g4f/__init__.py @@ -1,29 +1,31 @@ from __future__ import annotations -from g4f import models -from .Provider import BaseProvider, AsyncProvider -from .typing import Any, CreateResult, Union -from requests import get +from requests import get +from g4f.models import Model, ModelUtils +from .Provider import BaseProvider +from .typing import CreateResult, Union +from .debug import logging -logging = False -version = '0.1.4.2' +version = '0.1.5.6' +version_check = True -def check_pypi_version(): +def check_pypi_version() -> None: try: - response = get(f"https://pypi.org/pypi/g4f/json").json() + response = get("https://pypi.org/pypi/g4f/json").json() latest_version = response["info"]["version"] - + if version != latest_version: print(f'New pypi version: {latest_version} (current: {version}) | pip install -U g4f') - + except Exception as e: print(f'Failed to check g4f pypi version: {e}') -check_pypi_version() - -def get_model_and_provider(model: Union[models.Model, str], provider: type[BaseProvider], stream: bool): +def get_model_and_provider(model : Union[Model, str], + provider : Union[type[BaseProvider], None], + stream : bool) -> tuple[Model, type[BaseProvider]]: + if isinstance(model, str): - if model in models.ModelUtils.convert: - model = models.ModelUtils.convert[model] + if model in ModelUtils.convert: + model = ModelUtils.convert[model] else: raise Exception(f'The model: {model} does not exist') @@ -32,14 +34,13 @@ def get_model_and_provider(model: Union[models.Model, str], provider: type[BaseP if not provider: raise Exception(f'No provider found for model: {model}') - + if not provider.working: raise Exception(f'{provider.__name__} is not working') - + if not provider.supports_stream and stream: - raise Exception( - f'ValueError: {provider.__name__} does not support "stream" argument') - + raise Exception(f'ValueError: {provider.__name__} does not support "stream" argument') + if logging: print(f'Using {provider.__name__} provider') @@ -47,21 +48,18 @@ def get_model_and_provider(model: Union[models.Model, str], provider: type[BaseP class ChatCompletion: @staticmethod - def create( - model : Union[models.Model, str], + def create(model: Union[Model, str], messages : list[dict[str, str]], provider : Union[type[BaseProvider], None] = None, - stream : bool = False, - auth : Union[str, None] = None, - **kwargs - ) -> Union[CreateResult, str]: + stream : bool = False, + auth : Union[str, None] = None, **kwargs) -> Union[CreateResult, str]: model, provider = get_model_and_provider(model, provider, stream) if provider.needs_auth and not auth: raise Exception( f'ValueError: {provider.__name__} requires authentication (use auth=\'cookie or token or jwt ...\' param)') - + if provider.needs_auth: kwargs['auth'] = auth @@ -70,9 +68,9 @@ class ChatCompletion: @staticmethod async def create_async( - model : Union[models.Model, str], - messages : list[dict[str, str]], - provider : Union[type[BaseProvider], None] = None, + model: Union[Model, str], + messages: list[dict[str, str]], + provider: Union[type[BaseProvider], None] = None, **kwargs ) -> str: model, provider = get_model_and_provider(model, provider, False) @@ -82,11 +80,13 @@ class ChatCompletion: class Completion: @staticmethod def create( - model : Union[models.Model, str], - prompt : str, - provider : Union[type[BaseProvider], None] = None, - stream : bool = False, **kwargs) -> Union[CreateResult, str]: - + model: str, + prompt: str, + provider: Union[type[BaseProvider], None] = None, + stream: bool = False, + **kwargs + ) -> Union[CreateResult, str]: + allowed_models = [ 'code-davinci-002', 'text-ada-001', @@ -95,13 +95,15 @@ class Completion: 'text-davinci-002', 'text-davinci-003' ] - + if model not in allowed_models: raise Exception(f'ValueError: Can\'t use {model} with Completion.create()') - + model, provider = get_model_and_provider(model, provider, stream) - result = provider.create_completion(model.name, - [{"role": "user", "content": prompt}], stream, **kwargs) + result = provider.create_completion(model.name, [{"role": "user", "content": prompt}], stream, **kwargs) - return result if stream else ''.join(result)
\ No newline at end of file + return result if stream else ''.join(result) + +if version_check: + check_pypi_version()
\ No newline at end of file |