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