code: from unittest.mock import MagicMock, patch
  
  import pytest
  from llama_index.core.response.schema import Response
  from llama_index.schema import Document
  
  try:
      import google.ai.generativelanguage as genai
  
      has_google = True
  except ImportError:
      has_google = False
  
  from llama_index.indices.managed.google.generativeai import (
      GoogleIndex,
      set_google_config,
  )
  
  SKIP_TEST_REASON = "Google GenerativeAI is not installed"
  
  
  if has_google:
      import llama_index.vector_stores.google.generativeai.genai_extension as genaix
  
      set_google_config(
          api_endpoint="No-such-endpoint-to-prevent-hitting-real-backend",
          testing=True,
      )
  
  
  @pytest.mark.skipif(not has_google, reason=SKIP_TEST_REASON)
  @patch("google.auth.credentials.Credentials")
  def test_set_google_config(mock_credentials: MagicMock) -> None:
      set_google_config(auth_credentials=mock_credentials)
      config = genaix.get_config()
      assert config.auth_credentials == mock_credentials
  
  
  @pytest.mark.skipif(not has_google, reason=SKIP_TEST_REASON)
  @patch("google.ai.generativelanguage.RetrieverServiceClient.get_corpus")
  def test_from_corpus(mock_get_corpus: MagicMock) -> None:
      # Arrange
      mock_get_corpus.return_value = genai.Corpus(name="corpora/123")
  
      # Act
      store = GoogleIndex.from_corpus(corpus_id="123")
  
      # Assert
      assert store.corpus_id == "123"
  
  
  @pytest.mark.skipif(not has_google, reason=SKIP_TEST_REASON)
  @patch("google.ai.generativelanguage.RetrieverServiceClient.create_corpus")
  def test_create_corpus(mock_create_corpus: MagicMock) -> None:
      def fake_create_corpus(request: genai.CreateCorpusRequest) -> genai.Corpus:
          return request.corpus
  
      # Arrange
      mock_create_corpus.side_effect = fake_create_corpus
  
      # Act
      store = GoogleIndex.create_corpus(display_name="My first corpus")
  
      # Assert
      assert len(store.corpus_id) > 0
      assert mock_create_corpus.call_count == 1
  
      request = mock_create_corpus.call_args.args[0]
      assert request.corpus.name == f"corpora/{store.corpus_id}"
      assert request.corpus.display_name == "My first corpus"
  
  
  @pytest.mark.skipif(not has_google, reason=SKIP_TEST_REASON)
  @patch("google.ai.generativelanguage.RetrieverServiceClient.create_corpus")
  @patch("google.ai.generativelanguage.RetrieverServiceClient.create_document")
  @patch("google.ai.generativelanguage.RetrieverServiceClient.batch_create_chunks")
  @patch("google.ai.generativelanguage.RetrieverServiceClient.get_document")
  def test_from_documents(
      mock_get_document: MagicMock,
      mock_batch_create_chunk: MagicMock,
      mock_create_document: MagicMock,
      mock_create_corpus: MagicMock,
  ) -> None:
      from google.api_core import exceptions as gapi_exception
  
      def fake_create_corpus(request: genai.CreateCorpusRequest) -> genai.Corpus:
          return request.corpus
  
      # Arrange
      mock_get_document.side_effect = gapi_exception.NotFound("")
      mock_create_corpus.side_effect = fake_create_corpus
      mock_create_document.return_value = genai.Document(name="corpora/123/documents/456")
      mock_batch_create_chunk.side_effect = [
          genai.BatchCreateChunksResponse(
              chunks=[
                  genai.Chunk(name="corpora/123/documents/456/chunks/777"),
              ]
          ),
          genai.BatchCreateChunksResponse(
              chunks=[
                  genai.Chunk(name="corpora/123/documents/456/chunks/888"),
              ]
          ),
      ]
  
      # Act
      index = GoogleIndex.from_documents(
          [
              Document(text="Hello, my darling"),
              Document(text="Goodbye, my baby"),
          ]
      )
  
      # Assert
      assert mock_create_corpus.call_count == 1
      create_corpus_request = mock_create_corpus.call_args.args[0]
      assert create_corpus_request.corpus.name == f"corpora/{index.corpus_id}"
  
      create_document_request = mock_create_document.call_args.args[0]
      assert create_document_request.parent == f"corpora/{index.corpus_id}"
  
      assert mock_batch_create_chunk.call_count == 2
  
      first_batch_request = mock_batch_create_chunk.call_args_list[0].args[0]
      assert (
          first_batch_request.requests[0].chunk.data.string_value == "Hello, my darling"
      )
  
      second_batch_request = mock_batch_create_chunk.call_args_list[1].args[0]
      assert (
          second_batch_request.requests[0].chunk.data.string_value == "Goodbye, my baby"
      )
  
  
  @pytest.mark.skipif(not has_google, reason=SKIP_TEST_REASON)
  @patch("google.ai.generativelanguage.RetrieverServiceClient.query_corpus")
  @patch("google.ai.generativelanguage.GenerativeServiceClient.generate_answer")
  @patch("google.ai.generativelanguage.RetrieverServiceClient.get_corpus")
  def test_as_query_engine(
      mock_get_corpus: MagicMock,
      mock_generate_answer: MagicMock,
      mock_query_corpus: MagicMock,
  ) -> None:
      # Arrange
      mock_get_corpus.return_value = genai.Corpus(name="corpora/123")
      mock_query_corpus.return_value = genai.QueryCorpusResponse(
          relevant_chunks=[
              genai.RelevantChunk(
                  chunk=genai.Chunk(
                      name="corpora/123/documents/456/chunks/789",
                      data=genai.ChunkData(string_value="It's 42"),
                  ),
                  chunk_relevance_score=0.9,
              )
          ]
      )
      mock_generate_answer.return_value = genai.GenerateAnswerResponse(
          answer=genai.Candidate(
              content=genai.Content(parts=[genai.Part(text="42")]),
              grounding_attributions=[
                  genai.GroundingAttribution(
                      content=genai.Content(
                          parts=[genai.Part(text="Meaning of life is 42")]
                      ),
                      source_id=genai.AttributionSourceId(
                          grounding_passage=genai.AttributionSourceId.GroundingPassageId(
                              passage_id="corpora/123/documents/456/chunks/777",
                              part_index=0,
                          )
                      ),
                  ),
                  genai.GroundingAttribution(
                      content=genai.Content(parts=[genai.Part(text="Or maybe not")]),
                      source_id=genai.AttributionSourceId(
                          grounding_passage=genai.AttributionSourceId.GroundingPassageId(
                              passage_id="corpora/123/documents/456/chunks/888",
                              part_index=0,
                          )
                      ),
                  ),
              ],
              finish_reason=genai.Candidate.FinishReason.STOP,
          ),
          answerable_probability=0.9,
      )
  
      # Act
      index = GoogleIndex.from_corpus(corpus_id="123")
      query_engine = index.as_query_engine(
          answer_style=genai.GenerateAnswerRequest.AnswerStyle.EXTRACTIVE
      )
      response = query_engine.query("What is the meaning of life?")
  
      # Assert
      assert mock_query_corpus.call_count == 1
      query_corpus_request = mock_query_corpus.call_args.args[0]
      assert query_corpus_request.name == "corpora/123"
      assert query_corpus_request.query == "What is the meaning of life?"
  
      assert isinstance(response, Response)
  
      assert response.response == "42"
  
      assert mock_generate_answer.call_count == 1
      generate_answer_request = mock_generate_answer.call_args.args[0]
      assert (
          generate_answer_request.contents[0].parts[0].text
          == "What is the meaning of life?"
      )
      assert (
          generate_answer_request.answer_style
          == genai.GenerateAnswerRequest.AnswerStyle.EXTRACTIVE
      )
  
      passages = generate_answer_request.inline_passages.passages
      assert len(passages) == 1
      passage = passages[0]
      assert passage.content.parts[0].text == "It's 42"
  
apis: llama_index.schema.Document llama_index.indices.managed.google.generativeai.GoogleIndex.from_corpus llama_index.indices.managed.google.generativeai.set_google_config llama_index.vector_stores.google.generativeai.genai_extension.get_config llama_index.indices.managed.google.generativeai.GoogleIndex.create_corpus
extract_api: [((665, 724), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not has_google)'], {'reason': 'SKIP_TEST_REASON'}), '(not has_google, reason=SKIP_TEST_REASON)\n', (683, 724), False, 'import pytest\n'), ((726, 770), 'unittest.mock.patch', 'patch', (['"""google.auth.credentials.Credentials"""'], {}), "('google.auth.credentials.Credentials')\n", (731, 770), False, 'from unittest.mock import MagicMock, patch\n'), ((984, 1043), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not has_google)'], {'reason': 'SKIP_TEST_REASON'}), '(not has_google, reason=SKIP_TEST_REASON)\n', (1002, 1043), False, 'import pytest\n'), ((1045, 1116), 'unittest.mock.patch', 'patch', (['"""google.ai.generativelanguage.RetrieverServiceClient.get_corpus"""'], {}), "('google.ai.generativelanguage.RetrieverServiceClient.get_corpus')\n", (1050, 1116), False, 'from unittest.mock import MagicMock, patch\n'), ((1374, 1433), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not has_google)'], {'reason': 'SKIP_TEST_REASON'}), '(not has_google, reason=SKIP_TEST_REASON)\n', (1392, 1433), False, 'import pytest\n'), ((1435, 1509), 'unittest.mock.patch', 'patch', (['"""google.ai.generativelanguage.RetrieverServiceClient.create_corpus"""'], {}), "('google.ai.generativelanguage.RetrieverServiceClient.create_corpus')\n", (1440, 1509), False, 'from unittest.mock import MagicMock, patch\n'), ((2109, 2168), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not has_google)'], {'reason': 'SKIP_TEST_REASON'}), '(not has_google, reason=SKIP_TEST_REASON)\n', (2127, 2168), False, 'import pytest\n'), ((2170, 2244), 'unittest.mock.patch', 'patch', (['"""google.ai.generativelanguage.RetrieverServiceClient.create_corpus"""'], {}), "('google.ai.generativelanguage.RetrieverServiceClient.create_corpus')\n", (2175, 2244), False, 'from unittest.mock import MagicMock, patch\n'), ((2246, 2322), 'unittest.mock.patch', 'patch', (['"""google.ai.generativelanguage.RetrieverServiceClient.create_document"""'], {}), "('google.ai.generativelanguage.RetrieverServiceClient.create_document')\n", (2251, 2322), False, 'from unittest.mock import MagicMock, patch\n'), ((2324, 2409), 'unittest.mock.patch', 'patch', (['"""google.ai.generativelanguage.RetrieverServiceClient.batch_create_chunks"""'], {}), "('google.ai.generativelanguage.RetrieverServiceClient.batch_create_chunks'\n    )\n", (2329, 2409), False, 'from unittest.mock import MagicMock, patch\n'), ((2406, 2479), 'unittest.mock.patch', 'patch', (['"""google.ai.generativelanguage.RetrieverServiceClient.get_document"""'], {}), "('google.ai.generativelanguage.RetrieverServiceClient.get_document')\n", (2411, 2479), False, 'from unittest.mock import MagicMock, patch\n'), ((4370, 4429), 'pytest.mark.skipif', 'pytest.mark.skipif', (['(not has_google)'], {'reason': 'SKIP_TEST_REASON'}), '(not has_google, reason=SKIP_TEST_REASON)\n', (4388, 4429), False, 'import pytest\n'), ((4431, 4504), 'unittest.mock.patch', 'patch', (['"""google.ai.generativelanguage.RetrieverServiceClient.query_corpus"""'], {}), "('google.ai.generativelanguage.RetrieverServiceClient.query_corpus')\n", (4436, 4504), False, 'from unittest.mock import MagicMock, patch\n'), ((4506, 4583), 'unittest.mock.patch', 'patch', (['"""google.ai.generativelanguage.GenerativeServiceClient.generate_answer"""'], {}), "('google.ai.generativelanguage.GenerativeServiceClient.generate_answer')\n", (4511, 4583), False, 'from unittest.mock import MagicMock, patch\n'), ((4585, 4656), 'unittest.mock.patch', 'patch', (['"""google.ai.generativelanguage.RetrieverServiceClient.get_corpus"""'], {}), "('google.ai.generativelanguage.RetrieverServiceClient.get_corpus')\n", (4590, 4656), False, 'from unittest.mock import MagicMock, patch\n'), ((542, 643), 'llama_index.indices.managed.google.generativeai.set_google_config', 'set_google_config', ([], {'api_endpoint': '"""No-such-endpoint-to-prevent-hitting-real-backend"""', 'testing': '(True)'}), "(api_endpoint=\n    'No-such-endpoint-to-prevent-hitting-real-backend', testing=True)\n", (559, 643), False, 'from llama_index.indices.managed.google.generativeai import GoogleIndex, set_google_config\n'), ((840, 892), 'llama_index.indices.managed.google.generativeai.set_google_config', 'set_google_config', ([], {'auth_credentials': 'mock_credentials'}), '(auth_credentials=mock_credentials)\n', (857, 892), False, 'from llama_index.indices.managed.google.generativeai import GoogleIndex, set_google_config\n'), ((906, 925), 'llama_index.vector_stores.google.generativeai.genai_extension.get_config', 'genaix.get_config', ([], {}), '()\n', (923, 925), True, 'import llama_index.vector_stores.google.generativeai.genai_extension as genaix\n'), ((1224, 1256), 'google.ai.generativelanguage.Corpus', 'genai.Corpus', ([], {'name': '"""corpora/123"""'}), "(name='corpora/123')\n", (1236, 1256), True, 'import google.ai.generativelanguage as genai\n'), ((1280, 1320), 'llama_index.indices.managed.google.generativeai.GoogleIndex.from_corpus', 'GoogleIndex.from_corpus', ([], {'corpus_id': '"""123"""'}), "(corpus_id='123')\n", (1303, 1320), False, 'from llama_index.indices.managed.google.generativeai import GoogleIndex, set_google_config\n'), ((1777, 1834), 'llama_index.indices.managed.google.generativeai.GoogleIndex.create_corpus', 'GoogleIndex.create_corpus', ([], {'display_name': '"""My first corpus"""'}), "(display_name='My first corpus')\n", (1802, 1834), False, 'from llama_index.indices.managed.google.generativeai import GoogleIndex, set_google_config\n'), ((2885, 2912), 'google.api_core.exceptions.NotFound', 'gapi_exception.NotFound', (['""""""'], {}), "('')\n", (2908, 2912), True, 'from google.api_core import exceptions as gapi_exception\n'), ((3009, 3057), 'google.ai.generativelanguage.Document', 'genai.Document', ([], {'name': '"""corpora/123/documents/456"""'}), "(name='corpora/123/documents/456')\n", (3023, 3057), True, 'import google.ai.generativelanguage as genai\n'), ((4846, 4878), 'google.ai.generativelanguage.Corpus', 'genai.Corpus', ([], {'name': '"""corpora/123"""'}), "(name='corpora/123')\n", (4858, 4878), True, 'import google.ai.generativelanguage as genai\n'), ((6596, 6636), 'llama_index.indices.managed.google.generativeai.GoogleIndex.from_corpus', 'GoogleIndex.from_corpus', ([], {'corpus_id': '"""123"""'}), "(corpus_id='123')\n", (6619, 6636), False, 'from llama_index.indices.managed.google.generativeai import GoogleIndex, set_google_config\n'), ((3503, 3537), 'llama_index.schema.Document', 'Document', ([], {'text': '"""Hello, my darling"""'}), "(text='Hello, my darling')\n", (3511, 3537), False, 'from llama_index.schema import Document\n'), ((3551, 3584), 'llama_index.schema.Document', 'Document', ([], {'text': '"""Goodbye, my baby"""'}), "(text='Goodbye, my baby')\n", (3559, 3584), False, 'from llama_index.schema import Document\n'), ((3180, 3236), 'google.ai.generativelanguage.Chunk', 'genai.Chunk', ([], {'name': '"""corpora/123/documents/456/chunks/777"""'}), "(name='corpora/123/documents/456/chunks/777')\n", (3191, 3236), True, 'import google.ai.generativelanguage as genai\n'), ((3341, 3397), 'google.ai.generativelanguage.Chunk', 'genai.Chunk', ([], {'name': '"""corpora/123/documents/456/chunks/888"""'}), "(name='corpora/123/documents/456/chunks/888')\n", (3352, 3397), True, 'import google.ai.generativelanguage as genai\n'), ((5127, 5166), 'google.ai.generativelanguage.ChunkData', 'genai.ChunkData', ([], {'string_value': '"""It\'s 42"""'}), '(string_value="It\'s 42")\n', (5142, 5166), True, 'import google.ai.generativelanguage as genai\n'), ((5403, 5424), 'google.ai.generativelanguage.Part', 'genai.Part', ([], {'text': '"""42"""'}), "(text='42')\n", (5413, 5424), True, 'import google.ai.generativelanguage as genai\n'), ((5747, 5861), 'google.ai.generativelanguage.AttributionSourceId.GroundingPassageId', 'genai.AttributionSourceId.GroundingPassageId', ([], {'passage_id': '"""corpora/123/documents/456/chunks/777"""', 'part_index': '(0)'}), "(passage_id=\n    'corpora/123/documents/456/chunks/777', part_index=0)\n", (5791, 5861), True, 'import google.ai.generativelanguage as genai\n'), ((6209, 6323), 'google.ai.generativelanguage.AttributionSourceId.GroundingPassageId', 'genai.AttributionSourceId.GroundingPassageId', ([], {'passage_id': '"""corpora/123/documents/456/chunks/888"""', 'part_index': '(0)'}), "(passage_id=\n    'corpora/123/documents/456/chunks/888', part_index=0)\n", (6253, 6323), True, 'import google.ai.generativelanguage as genai\n'), ((5583, 5623), 'google.ai.generativelanguage.Part', 'genai.Part', ([], {'text': '"""Meaning of life is 42"""'}), "(text='Meaning of life is 42')\n", (5593, 5623), True, 'import google.ai.generativelanguage as genai\n'), ((6075, 6106), 'google.ai.generativelanguage.Part', 'genai.Part', ([], {'text': '"""Or maybe not"""'}), "(text='Or maybe not')\n", (6085, 6106), True, 'import google.ai.generativelanguage as genai\n')]