summaryrefslogtreecommitdiffstats
path: root/g4f/__init__.py
diff options
context:
space:
mode:
authorTekky <98614666+xtekky@users.noreply.github.com>2023-10-07 22:03:50 +0200
committerGitHub <noreply@github.com>2023-10-07 22:03:50 +0200
commit6334dac211898fb607d0281e9f23176172de4feb (patch)
tree45fad72c866cb03db744ae53e2993a4f31b29f0d /g4f/__init__.py
parent ~ | g4f `v-0.1.5.4` [new gui & interference] (diff)
parentRemove Aivvm from working providers (diff)
downloadgpt4free-6334dac211898fb607d0281e9f23176172de4feb.tar
gpt4free-6334dac211898fb607d0281e9f23176172de4feb.tar.gz
gpt4free-6334dac211898fb607d0281e9f23176172de4feb.tar.bz2
gpt4free-6334dac211898fb607d0281e9f23176172de4feb.tar.lz
gpt4free-6334dac211898fb607d0281e9f23176172de4feb.tar.xz
gpt4free-6334dac211898fb607d0281e9f23176172de4feb.tar.zst
gpt4free-6334dac211898fb607d0281e9f23176172de4feb.zip
Diffstat (limited to '')
-rw-r--r--g4f/__init__.py73
1 files changed, 36 insertions, 37 deletions
diff --git a/g4f/__init__.py b/g4f/__init__.py
index a5cee80b..5d0b47d8 100644
--- a/g4f/__init__.py
+++ b/g4f/__init__.py
@@ -1,31 +1,30 @@
from __future__ import annotations
-from g4f import models
-from .Provider import BaseProvider
-from .typing import CreateResult, Union
-from .debug import logging
-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.5.4'
-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')
@@ -34,14 +33,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')
@@ -50,11 +48,11 @@ def get_model_and_provider(model: Union[models.Model, str], provider: type[BaseP
class ChatCompletion:
@staticmethod
def create(
- model : Union[models.Model, str],
- messages : list[dict[str, str]],
- provider : Union[type[BaseProvider], None] = None,
- stream : bool = False,
- auth : Union[str, None] = None,
+ 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]:
@@ -63,7 +61,7 @@ class ChatCompletion:
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
@@ -72,9 +70,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)
@@ -84,11 +82,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',
@@ -97,13 +97,12 @@ 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)
+ return result if stream else ''.join(result) \ No newline at end of file