mocks.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from g4f.providers.base_provider import AbstractProvider, AsyncProvider, AsyncGeneratorProvider
  2. from g4f.image import ImageResponse
  3. from g4f.errors import MissingAuthError
  4. class ProviderMock(AbstractProvider):
  5. working = True
  6. def create_completion(
  7. model, messages, stream, **kwargs
  8. ):
  9. yield "Mock"
  10. class AsyncProviderMock(AsyncProvider):
  11. working = True
  12. async def create_async(
  13. model, messages, **kwargs
  14. ):
  15. return "Mock"
  16. class AsyncGeneratorProviderMock(AsyncGeneratorProvider):
  17. working = True
  18. async def create_async_generator(
  19. model, messages, stream, **kwargs
  20. ):
  21. yield "Mock"
  22. class ModelProviderMock(AbstractProvider):
  23. working = True
  24. def create_completion(
  25. model, messages, stream, **kwargs
  26. ):
  27. yield model
  28. class YieldProviderMock(AsyncGeneratorProvider):
  29. working = True
  30. async def create_async_generator(
  31. model, messages, stream, **kwargs
  32. ):
  33. for message in messages:
  34. yield message["content"]
  35. class YieldImageResponseProviderMock(AsyncGeneratorProvider):
  36. working = True
  37. @classmethod
  38. async def create_async_generator(
  39. cls, model, messages, stream, prompt: str, **kwargs
  40. ):
  41. yield ImageResponse(prompt, "")
  42. class MissingAuthProviderMock(AbstractProvider):
  43. working = True
  44. @classmethod
  45. def create_completion(
  46. cls, model, messages, stream, **kwargs
  47. ):
  48. raise MissingAuthError(cls.__name__)
  49. yield cls.__name__
  50. class RaiseExceptionProviderMock(AbstractProvider):
  51. working = True
  52. @classmethod
  53. def create_completion(
  54. cls, model, messages, stream, **kwargs
  55. ):
  56. raise RuntimeError(cls.__name__)
  57. yield cls.__name__
  58. class AsyncRaiseExceptionProviderMock(AsyncGeneratorProvider):
  59. working = True
  60. @classmethod
  61. async def create_async_generator(
  62. cls, model, messages, stream, **kwargs
  63. ):
  64. raise RuntimeError(cls.__name__)
  65. yield cls.__name__
  66. class YieldNoneProviderMock(AsyncGeneratorProvider):
  67. working = True
  68. async def create_async_generator(
  69. model, messages, stream, **kwargs
  70. ):
  71. yield None