2023-09-03 11:26:26 +03:00
|
|
|
from __future__ import annotations
|
2023-06-24 04:47:00 +03:00
|
|
|
|
2023-12-07 09:18:05 +03:00
|
|
|
import os
|
2024-11-15 09:38:51 +03:00
|
|
|
import logging
|
2024-12-16 01:22:36 +03:00
|
|
|
from typing import Union, Optional, Coroutine
|
2023-09-27 21:07:12 +03:00
|
|
|
|
2024-04-07 11:36:13 +03:00
|
|
|
from . import debug, version
|
|
|
|
from .models import Model
|
2024-10-30 11:57:48 +03:00
|
|
|
from .client import Client, AsyncClient
|
2024-12-14 00:20:58 +03:00
|
|
|
from .typing import Messages, CreateResult, AsyncResult, ImageType
|
2024-12-16 01:22:36 +03:00
|
|
|
from .errors import StreamNotSupportedError
|
2024-04-07 11:36:13 +03:00
|
|
|
from .cookies import get_cookies, set_cookies
|
|
|
|
from .providers.types import ProviderType
|
|
|
|
from .client.service import get_model_and_provider, get_last_provider
|
2023-09-20 15:52:50 +03:00
|
|
|
|
2024-11-15 09:38:51 +03:00
|
|
|
#Configure "g4f" logger
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
log_handler = logging.StreamHandler()
|
|
|
|
log_handler.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
|
|
|
|
logger.addHandler(log_handler)
|
|
|
|
|
|
|
|
logger.setLevel(logging.ERROR)
|
|
|
|
|
2023-06-24 04:47:00 +03:00
|
|
|
class ChatCompletion:
|
|
|
|
@staticmethod
|
2023-10-19 17:14:48 +03:00
|
|
|
def create(model : Union[Model, str],
|
2023-10-12 16:51:11 +03:00
|
|
|
messages : Messages,
|
2024-01-01 19:48:57 +03:00
|
|
|
provider : Union[ProviderType, str, None] = None,
|
2023-10-12 16:51:11 +03:00
|
|
|
stream : bool = False,
|
2024-12-14 00:20:58 +03:00
|
|
|
image : ImageType = None,
|
|
|
|
image_name: Optional[str] = None,
|
2023-12-06 11:35:36 +03:00
|
|
|
ignore_working: bool = False,
|
2024-02-09 00:02:52 +03:00
|
|
|
ignore_stream: bool = False,
|
2023-12-06 11:35:36 +03:00
|
|
|
**kwargs) -> Union[CreateResult, str]:
|
2024-02-09 00:02:52 +03:00
|
|
|
model, provider = get_model_and_provider(
|
|
|
|
model, provider, stream,
|
2024-12-16 01:22:36 +03:00
|
|
|
ignore_working,
|
|
|
|
ignore_stream
|
2024-02-09 00:02:52 +03:00
|
|
|
)
|
2024-12-14 00:20:58 +03:00
|
|
|
if image is not None:
|
|
|
|
kwargs["images"] = [(image, image_name)]
|
2023-12-11 02:56:06 +03:00
|
|
|
if "proxy" not in kwargs:
|
|
|
|
proxy = os.environ.get("G4F_PROXY")
|
|
|
|
if proxy:
|
|
|
|
kwargs['proxy'] = proxy
|
2023-07-28 13:07:17 +03:00
|
|
|
|
2024-10-22 12:13:58 +03:00
|
|
|
result = provider.create_completion(model, messages, stream=stream, **kwargs)
|
|
|
|
|
2024-12-14 00:20:58 +03:00
|
|
|
return result if stream else ''.join([str(chunk) for chunk in result if chunk])
|
2023-09-20 15:52:50 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2024-01-01 19:48:57 +03:00
|
|
|
def create_async(model : Union[Model, str],
|
|
|
|
messages : Messages,
|
|
|
|
provider : Union[ProviderType, str, None] = None,
|
|
|
|
stream : bool = False,
|
2024-04-07 11:36:13 +03:00
|
|
|
ignore_working: bool = False,
|
2024-12-16 01:22:36 +03:00
|
|
|
**kwargs) -> Union[AsyncResult, Coroutine[str]]:
|
|
|
|
model, provider = get_model_and_provider(model, provider, False, ignore_working)
|
2023-09-20 15:52:50 +03:00
|
|
|
|
2023-11-20 16:02:51 +03:00
|
|
|
if stream:
|
2024-12-14 00:20:58 +03:00
|
|
|
if hasattr(provider, "create_async_generator"):
|
2024-01-01 19:48:57 +03:00
|
|
|
return provider.create_async_generator(model, messages, **kwargs)
|
2023-12-11 02:56:06 +03:00
|
|
|
raise StreamNotSupportedError(f'{provider.__name__} does not support "stream" argument in "create_async"')
|
2023-11-20 16:02:51 +03:00
|
|
|
|
2024-12-14 00:20:58 +03:00
|
|
|
return provider.create_async(model, messages, **kwargs)
|