google.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from collections.abc import Generator
  2. from unittest.mock import MagicMock
  3. import google.generativeai.types.generation_types as generation_config_types
  4. import pytest
  5. from _pytest.monkeypatch import MonkeyPatch
  6. from google.ai import generativelanguage as glm
  7. from google.ai.generativelanguage_v1beta.types import content as gag_content
  8. from google.generativeai import GenerativeModel
  9. from google.generativeai.types import GenerateContentResponse, content_types, safety_types
  10. from google.generativeai.types.generation_types import BaseGenerateContentResponse
  11. from extensions import ext_redis
  12. class MockGoogleResponseClass:
  13. _done = False
  14. def __iter__(self):
  15. full_response_text = "it's google!"
  16. for i in range(0, len(full_response_text) + 1, 1):
  17. if i == len(full_response_text):
  18. self._done = True
  19. yield GenerateContentResponse(
  20. done=True, iterator=None, result=glm.GenerateContentResponse({}), chunks=[]
  21. )
  22. else:
  23. yield GenerateContentResponse(
  24. done=False, iterator=None, result=glm.GenerateContentResponse({}), chunks=[]
  25. )
  26. class MockGoogleResponseCandidateClass:
  27. finish_reason = "stop"
  28. @property
  29. def content(self) -> gag_content.Content:
  30. return gag_content.Content(parts=[gag_content.Part(text="it's google!")])
  31. class MockGoogleClass:
  32. @staticmethod
  33. def generate_content_sync() -> GenerateContentResponse:
  34. return GenerateContentResponse(done=True, iterator=None, result=glm.GenerateContentResponse({}), chunks=[])
  35. @staticmethod
  36. def generate_content_stream() -> Generator[GenerateContentResponse, None, None]:
  37. return MockGoogleResponseClass()
  38. def generate_content(
  39. self: GenerativeModel,
  40. contents: content_types.ContentsType,
  41. *,
  42. generation_config: generation_config_types.GenerationConfigType | None = None,
  43. safety_settings: safety_types.SafetySettingOptions | None = None,
  44. stream: bool = False,
  45. **kwargs,
  46. ) -> GenerateContentResponse:
  47. if stream:
  48. return MockGoogleClass.generate_content_stream()
  49. return MockGoogleClass.generate_content_sync()
  50. @property
  51. def generative_response_text(self) -> str:
  52. return "it's google!"
  53. @property
  54. def generative_response_candidates(self) -> list[MockGoogleResponseCandidateClass]:
  55. return [MockGoogleResponseCandidateClass()]
  56. def mock_configure(api_key: str):
  57. if len(api_key) < 16:
  58. raise Exception("Invalid API key")
  59. class MockFileState:
  60. def __init__(self):
  61. self.name = "FINISHED"
  62. class MockGoogleFile:
  63. def __init__(self, name: str = "mock_file_name"):
  64. self.name = name
  65. self.state = MockFileState()
  66. def mock_get_file(name: str) -> MockGoogleFile:
  67. return MockGoogleFile(name)
  68. def mock_upload_file(path: str, mime_type: str) -> MockGoogleFile:
  69. return MockGoogleFile()
  70. @pytest.fixture
  71. def setup_google_mock(request, monkeypatch: MonkeyPatch):
  72. monkeypatch.setattr(BaseGenerateContentResponse, "text", MockGoogleClass.generative_response_text)
  73. monkeypatch.setattr(BaseGenerateContentResponse, "candidates", MockGoogleClass.generative_response_candidates)
  74. monkeypatch.setattr(GenerativeModel, "generate_content", MockGoogleClass.generate_content)
  75. monkeypatch.setattr("google.generativeai.configure", mock_configure)
  76. monkeypatch.setattr("google.generativeai.get_file", mock_get_file)
  77. monkeypatch.setattr("google.generativeai.upload_file", mock_upload_file)
  78. yield
  79. monkeypatch.undo()
  80. @pytest.fixture
  81. def setup_mock_redis() -> None:
  82. ext_redis.redis_client.get = MagicMock(return_value=None)
  83. ext_redis.redis_client.setex = MagicMock(return_value=None)
  84. ext_redis.redis_client.exists = MagicMock(return_value=True)