retry_provider.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. from __future__ import annotations
  2. import random
  3. from ..typing import Type, List, CreateResult, Messages, AsyncResult
  4. from .types import BaseProvider, BaseRetryProvider, ProviderType
  5. from .response import MediaResponse, AudioResponse, ProviderInfo
  6. from .. import debug
  7. from ..errors import RetryProviderError, RetryNoProviderError
  8. def is_content(chunk):
  9. return isinstance(chunk, (str, MediaResponse, AudioResponse))
  10. class IterListProvider(BaseRetryProvider):
  11. def __init__(
  12. self,
  13. providers: List[Type[BaseProvider]],
  14. shuffle: bool = True
  15. ) -> None:
  16. """
  17. Initialize the BaseRetryProvider.
  18. Args:
  19. providers (List[Type[BaseProvider]]): List of providers to use.
  20. shuffle (bool): Whether to shuffle the providers list.
  21. single_provider_retry (bool): Whether to retry a single provider if it fails.
  22. max_retries (int): Maximum number of retries for a single provider.
  23. """
  24. self.providers = providers
  25. self.shuffle = shuffle
  26. self.working = True
  27. self.last_provider: Type[BaseProvider] = None
  28. def create_completion(
  29. self,
  30. model: str,
  31. messages: Messages,
  32. stream: bool = False,
  33. ignore_stream: bool = False,
  34. ignored: list[str] = [],
  35. **kwargs,
  36. ) -> CreateResult:
  37. """
  38. Create a completion using available providers, with an option to stream the response.
  39. Args:
  40. model (str): The model to be used for completion.
  41. messages (Messages): The messages to be used for generating completion.
  42. stream (bool, optional): Flag to indicate if the response should be streamed. Defaults to False.
  43. Yields:
  44. CreateResult: Tokens or results from the completion.
  45. Raises:
  46. Exception: Any exception encountered during the completion process.
  47. """
  48. exceptions = {}
  49. started: bool = False
  50. for provider in self.get_providers(stream and not ignore_stream, ignored):
  51. self.last_provider = provider
  52. debug.log(f"Using {provider.__name__} provider")
  53. yield ProviderInfo(**provider.get_dict(), model=model if model else getattr(provider, "default_model"))
  54. try:
  55. response = provider.get_create_function()(model, messages, stream=stream, **kwargs)
  56. for chunk in response:
  57. if chunk:
  58. yield chunk
  59. if is_content(chunk):
  60. started = True
  61. if started:
  62. return
  63. except Exception as e:
  64. exceptions[provider.__name__] = e
  65. debug.error(f"{provider.__name__} {type(e).__name__}: {e}")
  66. if started:
  67. raise e
  68. yield e
  69. raise_exceptions(exceptions)
  70. async def create_async_generator(
  71. self,
  72. model: str,
  73. messages: Messages,
  74. stream: bool = True,
  75. ignore_stream: bool = False,
  76. ignored: list[str] = [],
  77. **kwargs
  78. ) -> AsyncResult:
  79. exceptions = {}
  80. started: bool = False
  81. for provider in self.get_providers(stream and not ignore_stream, ignored):
  82. self.last_provider = provider
  83. debug.log(f"Using {provider.__name__} provider")
  84. yield ProviderInfo(**provider.get_dict(), model=model if model else getattr(provider, "default_model"))
  85. try:
  86. response = provider.get_async_create_function()(model, messages, stream=stream, **kwargs)
  87. if hasattr(response, "__aiter__"):
  88. async for chunk in response:
  89. if chunk:
  90. yield chunk
  91. if is_content(chunk):
  92. started = True
  93. elif response:
  94. response = await response
  95. if response:
  96. yield response
  97. started = True
  98. if started:
  99. return
  100. except Exception as e:
  101. exceptions[provider.__name__] = e
  102. debug.error(f"{provider.__name__} {type(e).__name__}: {e}")
  103. if started:
  104. raise e
  105. yield e
  106. raise_exceptions(exceptions)
  107. def get_create_function(self) -> callable:
  108. return self.create_completion
  109. def get_async_create_function(self) -> callable:
  110. return self.create_async_generator
  111. def get_providers(self, stream: bool, ignored: list[str]) -> list[ProviderType]:
  112. providers = [p for p in self.providers if (p.supports_stream or not stream) and p.__name__ not in ignored]
  113. if self.shuffle:
  114. random.shuffle(providers)
  115. return providers
  116. class RetryProvider(IterListProvider):
  117. def __init__(
  118. self,
  119. providers: List[Type[BaseProvider]],
  120. shuffle: bool = True,
  121. single_provider_retry: bool = False,
  122. max_retries: int = 3,
  123. ) -> None:
  124. """
  125. Initialize the BaseRetryProvider.
  126. Args:
  127. providers (List[Type[BaseProvider]]): List of providers to use.
  128. shuffle (bool): Whether to shuffle the providers list.
  129. single_provider_retry (bool): Whether to retry a single provider if it fails.
  130. max_retries (int): Maximum number of retries for a single provider.
  131. """
  132. super().__init__(providers, shuffle)
  133. self.single_provider_retry = single_provider_retry
  134. self.max_retries = max_retries
  135. def create_completion(
  136. self,
  137. model: str,
  138. messages: Messages,
  139. stream: bool = False,
  140. **kwargs,
  141. ) -> CreateResult:
  142. """
  143. Create a completion using available providers, with an option to stream the response.
  144. Args:
  145. model (str): The model to be used for completion.
  146. messages (Messages): The messages to be used for generating completion.
  147. stream (bool, optional): Flag to indicate if the response should be streamed. Defaults to False.
  148. Yields:
  149. CreateResult: Tokens or results from the completion.
  150. Raises:
  151. Exception: Any exception encountered during the completion process.
  152. """
  153. if self.single_provider_retry:
  154. exceptions = {}
  155. started: bool = False
  156. provider = self.providers[0]
  157. self.last_provider = provider
  158. for attempt in range(self.max_retries):
  159. try:
  160. if debug.logging:
  161. print(f"Using {provider.__name__} provider (attempt {attempt + 1})")
  162. response = provider.get_create_function()(model, messages, stream=stream, **kwargs)
  163. for chunk in response:
  164. yield chunk
  165. if is_content(chunk):
  166. started = True
  167. if started:
  168. return
  169. except Exception as e:
  170. exceptions[provider.__name__] = e
  171. if debug.logging:
  172. print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
  173. if started:
  174. raise e
  175. raise_exceptions(exceptions)
  176. else:
  177. yield from super().create_completion(model, messages, stream, **kwargs)
  178. async def create_async_generator(
  179. self,
  180. model: str,
  181. messages: Messages,
  182. stream: bool = True,
  183. **kwargs
  184. ) -> AsyncResult:
  185. exceptions = {}
  186. started = False
  187. if self.single_provider_retry:
  188. provider = self.providers[0]
  189. self.last_provider = provider
  190. for attempt in range(self.max_retries):
  191. try:
  192. debug.log(f"Using {provider.__name__} provider (attempt {attempt + 1})")
  193. response = provider.get_async_create_function()(model, messages, stream=stream, **kwargs)
  194. if hasattr(response, "__aiter__"):
  195. async for chunk in response:
  196. yield chunk
  197. if is_content(chunk):
  198. started = True
  199. else:
  200. response = await response
  201. if response:
  202. yield response
  203. started = True
  204. if started:
  205. return
  206. except Exception as e:
  207. exceptions[provider.__name__] = e
  208. if debug.logging:
  209. print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
  210. raise_exceptions(exceptions)
  211. else:
  212. async for chunk in super().create_async_generator(model, messages, stream, **kwargs):
  213. yield chunk
  214. def raise_exceptions(exceptions: dict) -> None:
  215. """
  216. Raise a combined exception if any occurred during retries.
  217. Raises:
  218. RetryProviderError: If any provider encountered an exception.
  219. RetryNoProviderError: If no provider is found.
  220. """
  221. if exceptions:
  222. raise RetryProviderError("RetryProvider failed:\n" + "\n".join([
  223. f"{p}: {type(exception).__name__}: {exception}" for p, exception in exceptions.items()
  224. ])) from list(exceptions.values())[0]
  225. raise RetryNoProviderError("No provider found")