__init__.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from __future__ import annotations
  2. import os
  3. import logging
  4. from typing import Union, Optional, Coroutine
  5. from . import debug, version
  6. from .models import Model
  7. from .client import Client, AsyncClient
  8. from .typing import Messages, CreateResult, AsyncResult, ImageType
  9. from .errors import StreamNotSupportedError
  10. from .cookies import get_cookies, set_cookies
  11. from .providers.types import ProviderType
  12. from .providers.helper import concat_chunks, async_concat_chunks
  13. from .client.service import get_model_and_provider
  14. #Configure "g4f" logger
  15. logger = logging.getLogger(__name__)
  16. log_handler = logging.StreamHandler()
  17. log_handler.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
  18. logger.addHandler(log_handler)
  19. logger.setLevel(logging.ERROR)
  20. class ChatCompletion:
  21. @staticmethod
  22. def create(model : Union[Model, str],
  23. messages : Messages,
  24. provider : Union[ProviderType, str, None] = None,
  25. stream : bool = False,
  26. image : ImageType = None,
  27. image_name: Optional[str] = None,
  28. ignore_working: bool = False,
  29. ignore_stream: bool = False,
  30. **kwargs) -> Union[CreateResult, str]:
  31. model, provider = get_model_and_provider(
  32. model, provider, stream,
  33. ignore_working,
  34. ignore_stream
  35. )
  36. if image is not None:
  37. kwargs["images"] = [(image, image_name)]
  38. if "proxy" not in kwargs:
  39. proxy = os.environ.get("G4F_PROXY")
  40. if proxy:
  41. kwargs["proxy"] = proxy
  42. if ignore_stream:
  43. kwargs["ignore_stream"] = True
  44. result = provider.get_create_function()(model, messages, stream=stream, **kwargs)
  45. return result if stream else concat_chunks(result)
  46. @staticmethod
  47. def create_async(model : Union[Model, str],
  48. messages : Messages,
  49. provider : Union[ProviderType, str, None] = None,
  50. stream : bool = False,
  51. image : ImageType = None,
  52. image_name: Optional[str] = None,
  53. ignore_stream: bool = False,
  54. ignore_working: bool = False,
  55. **kwargs) -> Union[AsyncResult, Coroutine[str]]:
  56. model, provider = get_model_and_provider(model, provider, False, ignore_working)
  57. if image is not None:
  58. kwargs["images"] = [(image, image_name)]
  59. if "proxy" not in kwargs:
  60. proxy = os.environ.get("G4F_PROXY")
  61. if proxy:
  62. kwargs["proxy"] = proxy
  63. if ignore_stream:
  64. kwargs["ignore_stream"] = True
  65. result = provider.get_async_create_function()(model, messages, stream=stream, **kwargs)
  66. if not stream:
  67. if hasattr(result, "__aiter__"):
  68. result = async_concat_chunks(result)
  69. return result