types.py 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. from __future__ import annotations
  2. import os
  3. from .stubs import ChatCompletion, ChatCompletionChunk
  4. from ..providers.types import BaseProvider, ProviderType, FinishReason
  5. from typing import Union, Iterator, AsyncIterator
  6. ImageProvider = Union[BaseProvider, object]
  7. Proxies = Union[dict, str]
  8. IterResponse = Iterator[Union[ChatCompletion, ChatCompletionChunk]]
  9. AsyncIterResponse = AsyncIterator[Union[ChatCompletion, ChatCompletionChunk]]
  10. class ClientProxyMixin():
  11. def get_proxy(self) -> Union[str, None]:
  12. if isinstance(self.proxies, str):
  13. return self.proxies
  14. elif self.proxies is None:
  15. return os.environ.get("G4F_PROXY")
  16. elif "all" in self.proxies:
  17. return self.proxies["all"]
  18. elif "https" in self.proxies:
  19. return self.proxies["https"]
  20. class Client(ClientProxyMixin):
  21. def __init__(
  22. self,
  23. api_key: str = None,
  24. proxies: Proxies = None,
  25. **kwargs
  26. ) -> None:
  27. self.api_key: str = api_key
  28. self.proxies: Proxies = proxies