2023-09-03 04:26:26 -04:00
|
|
|
from __future__ import annotations
|
2023-10-19 10:14:48 -04:00
|
|
|
from requests import get
|
|
|
|
from .models import Model, ModelUtils, _all_models
|
2023-11-20 08:02:51 -05:00
|
|
|
from .Provider import BaseProvider, AsyncGeneratorProvider, RetryProvider
|
|
|
|
from .typing import Messages, CreateResult, AsyncResult, Union, List
|
2023-10-22 18:00:31 -04:00
|
|
|
from . import debug
|
2023-06-23 21:47:00 -04:00
|
|
|
|
2023-12-06 07:14:49 -05:00
|
|
|
version = '0.1.9.2'
|
2023-10-08 08:31:46 -04:00
|
|
|
version_check = True
|
2023-09-27 14:07:12 -04:00
|
|
|
|
2023-10-07 04:17:43 -04:00
|
|
|
def check_pypi_version() -> None:
|
2023-09-27 14:07:12 -04:00
|
|
|
try:
|
2023-10-07 04:17:43 -04:00
|
|
|
response = get("https://pypi.org/pypi/g4f/json").json()
|
2023-09-27 14:07:12 -04:00
|
|
|
latest_version = response["info"]["version"]
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-09-27 14:07:12 -04:00
|
|
|
if version != latest_version:
|
|
|
|
print(f'New pypi version: {latest_version} (current: {version}) | pip install -U g4f')
|
2023-11-13 13:56:08 -05:00
|
|
|
return False
|
|
|
|
return True
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-09-27 14:07:12 -04:00
|
|
|
except Exception as e:
|
|
|
|
print(f'Failed to check g4f pypi version: {e}')
|
|
|
|
|
2023-10-08 08:31:46 -04:00
|
|
|
def get_model_and_provider(model : Union[Model, str],
|
|
|
|
provider : Union[type[BaseProvider], None],
|
2023-10-13 01:45:29 -04:00
|
|
|
stream : bool,
|
2023-10-20 19:52:19 -04:00
|
|
|
ignored : List[str] = None,
|
2023-12-06 03:35:36 -05:00
|
|
|
ignore_working: bool = False,
|
|
|
|
ignore_stream: bool = False) -> tuple[Model, type[BaseProvider]]:
|
2023-10-08 08:31:46 -04:00
|
|
|
|
2023-09-20 08:52:50 -04:00
|
|
|
if isinstance(model, str):
|
2023-10-07 04:17:43 -04:00
|
|
|
if model in ModelUtils.convert:
|
|
|
|
model = ModelUtils.convert[model]
|
2023-09-20 08:52:50 -04:00
|
|
|
else:
|
2023-10-09 04:22:17 -04:00
|
|
|
raise ValueError(f'The model: {model} does not exist')
|
2023-09-20 08:52:50 -04:00
|
|
|
|
|
|
|
if not provider:
|
2023-09-21 14:10:59 -04:00
|
|
|
provider = model.best_provider
|
2023-09-20 08:52:50 -04:00
|
|
|
|
2023-10-13 01:45:29 -04:00
|
|
|
if isinstance(provider, RetryProvider) and ignored:
|
|
|
|
provider.providers = [p for p in provider.providers if p.__name__ not in ignored]
|
|
|
|
|
2023-09-20 08:52:50 -04:00
|
|
|
if not provider:
|
2023-10-09 04:22:17 -04:00
|
|
|
raise RuntimeError(f'No provider found for model: {model}')
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-10-20 19:52:19 -04:00
|
|
|
if not provider.working and not ignore_working:
|
2023-10-09 04:22:17 -04:00
|
|
|
raise RuntimeError(f'{provider.__name__} is not working')
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-12-06 03:35:36 -05:00
|
|
|
if not ignore_stream and not provider.supports_stream and stream:
|
2023-10-09 04:22:17 -04:00
|
|
|
raise ValueError(f'{provider.__name__} does not support "stream" argument')
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-10-22 17:53:18 -04:00
|
|
|
if debug.logging:
|
2023-09-20 08:52:50 -04:00
|
|
|
print(f'Using {provider.__name__} provider')
|
|
|
|
|
|
|
|
return model, provider
|
|
|
|
|
2023-06-23 21:47:00 -04:00
|
|
|
class ChatCompletion:
|
|
|
|
@staticmethod
|
2023-10-19 10:14:48 -04:00
|
|
|
def create(model : Union[Model, str],
|
2023-10-12 09:51:11 -04:00
|
|
|
messages : Messages,
|
|
|
|
provider : Union[type[BaseProvider], None] = None,
|
|
|
|
stream : bool = False,
|
2023-10-13 01:45:29 -04:00
|
|
|
auth : Union[str, None] = None,
|
2023-10-20 19:52:19 -04:00
|
|
|
ignored : List[str] = None,
|
2023-12-06 03:35:36 -05:00
|
|
|
ignore_working: bool = False,
|
|
|
|
ignore_stream_and_auth: bool = False,
|
|
|
|
**kwargs) -> Union[CreateResult, str]:
|
2023-09-11 22:40:10 -04:00
|
|
|
|
2023-12-06 03:35:36 -05:00
|
|
|
model, provider = get_model_and_provider(model, provider, stream, ignored, ignore_working, ignore_stream_and_auth)
|
2023-07-28 06:07:17 -04:00
|
|
|
|
2023-12-06 03:35:36 -05:00
|
|
|
if not ignore_stream_and_auth and provider.needs_auth and not auth:
|
2023-10-09 04:22:17 -04:00
|
|
|
raise ValueError(
|
|
|
|
f'{provider.__name__} requires authentication (use auth=\'cookie or token or jwt ...\' param)')
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-12-06 03:35:36 -05:00
|
|
|
if auth:
|
2023-08-27 11:37:44 -04:00
|
|
|
kwargs['auth'] = auth
|
2023-07-28 06:07:17 -04:00
|
|
|
|
|
|
|
result = provider.create_completion(model.name, messages, stream, **kwargs)
|
2023-08-27 11:37:44 -04:00
|
|
|
return result if stream else ''.join(result)
|
2023-09-20 08:52:50 -04:00
|
|
|
|
|
|
|
@staticmethod
|
2023-10-19 10:14:48 -04:00
|
|
|
async def create_async(model : Union[Model, str],
|
|
|
|
messages : Messages,
|
|
|
|
provider : Union[type[BaseProvider], None] = None,
|
|
|
|
stream : bool = False,
|
2023-11-20 08:02:51 -05:00
|
|
|
ignored : List[str] = None,
|
|
|
|
**kwargs) -> Union[AsyncResult, str]:
|
2023-10-13 01:45:29 -04:00
|
|
|
model, provider = get_model_and_provider(model, provider, False, ignored)
|
2023-09-20 08:52:50 -04:00
|
|
|
|
2023-11-20 08:02:51 -05:00
|
|
|
if stream:
|
|
|
|
if isinstance(provider, type) and issubclass(provider, AsyncGeneratorProvider):
|
|
|
|
return await provider.create_async_generator(model.name, messages, **kwargs)
|
|
|
|
raise ValueError(f'{provider.__name__} does not support "stream" argument')
|
|
|
|
|
2023-09-20 08:52:50 -04:00
|
|
|
return await provider.create_async(model.name, messages, **kwargs)
|
2023-09-23 06:16:19 -04:00
|
|
|
|
|
|
|
class Completion:
|
|
|
|
@staticmethod
|
2023-10-19 10:14:48 -04:00
|
|
|
def create(model : Union[Model, str],
|
|
|
|
prompt : str,
|
|
|
|
provider : Union[type[BaseProvider], None] = None,
|
|
|
|
stream : bool = False,
|
|
|
|
ignored : List[str] = None, **kwargs) -> Union[CreateResult, str]:
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-09-23 06:16:19 -04:00
|
|
|
allowed_models = [
|
|
|
|
'code-davinci-002',
|
|
|
|
'text-ada-001',
|
|
|
|
'text-babbage-001',
|
|
|
|
'text-curie-001',
|
|
|
|
'text-davinci-002',
|
|
|
|
'text-davinci-003'
|
|
|
|
]
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-09-23 06:16:19 -04:00
|
|
|
if model not in allowed_models:
|
|
|
|
raise Exception(f'ValueError: Can\'t use {model} with Completion.create()')
|
2023-10-07 04:17:43 -04:00
|
|
|
|
2023-10-13 01:45:29 -04:00
|
|
|
model, provider = get_model_and_provider(model, provider, stream, ignored)
|
2023-09-23 06:16:19 -04:00
|
|
|
|
2023-10-07 04:17:43 -04:00
|
|
|
result = provider.create_completion(model.name, [{"role": "user", "content": prompt}], stream, **kwargs)
|
2023-09-23 06:16:19 -04:00
|
|
|
|
2023-10-08 08:31:46 -04:00
|
|
|
return result if stream else ''.join(result)
|
|
|
|
|
|
|
|
if version_check:
|
2023-12-01 17:11:52 -05:00
|
|
|
check_pypi_version()
|