Skip to content

Commit

Permalink
feat: Update v1beta1 sdk for RagVectorDbConfig & RagCorpus protos. Ba…
Browse files Browse the repository at this point in the history
…ckward compatible with older fields.

PiperOrigin-RevId: 702069384
  • Loading branch information
vertex-sdk-bot authored and copybara-github committed Dec 2, 2024
1 parent 00d93bd commit 216a30f
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 29 deletions.
85 changes: 76 additions & 9 deletions tests/unit/vertex_rag/test_rag_constants_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
VertexAiSearchConfig,
VertexVectorSearch,
VertexFeatureStore,
RagEmbeddingModelConfig,
VertexPredictionEndpoint,
RagVectorDbConfig,
)
from google.cloud.aiplatform_v1beta1 import (
GoogleDriveSource,
Expand All @@ -56,7 +59,7 @@
SlackSource as GapicSlackSource,
RagContexts,
RetrieveContextsResponse,
RagVectorDbConfig,
RagVectorDbConfig as GapicRagVectorDbConfig,
VertexAiSearchConfig as GapicVertexAiSearchConfig,
)
from google.cloud.aiplatform_v1beta1.types import api_auth
Expand Down Expand Up @@ -112,8 +115,8 @@
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
description=TEST_CORPUS_DISCRIPTION,
rag_vector_db_config=RagVectorDbConfig(
weaviate=RagVectorDbConfig.Weaviate(
rag_vector_db_config=GapicRagVectorDbConfig(
weaviate=GapicRagVectorDbConfig.Weaviate(
http_endpoint=TEST_WEAVIATE_HTTP_ENDPOINT,
collection_name=TEST_WEAVIATE_COLLECTION_NAME,
),
Expand All @@ -128,8 +131,8 @@
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
description=TEST_CORPUS_DISCRIPTION,
rag_vector_db_config=RagVectorDbConfig(
vertex_feature_store=RagVectorDbConfig.VertexFeatureStore(
rag_vector_db_config=GapicRagVectorDbConfig(
vertex_feature_store=GapicRagVectorDbConfig.VertexFeatureStore(
feature_view_resource_name=TEST_VERTEX_FEATURE_STORE_RESOURCE_NAME
),
),
Expand All @@ -138,8 +141,8 @@
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
description=TEST_CORPUS_DISCRIPTION,
rag_vector_db_config=RagVectorDbConfig(
vertex_vector_search=RagVectorDbConfig.VertexVectorSearch(
rag_vector_db_config=GapicRagVectorDbConfig(
vertex_vector_search=GapicRagVectorDbConfig.VertexVectorSearch(
index_endpoint=TEST_VERTEX_VECTOR_SEARCH_INDEX_ENDPOINT,
index=TEST_VERTEX_VECTOR_SEARCH_INDEX,
),
Expand All @@ -149,8 +152,8 @@
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
description=TEST_CORPUS_DISCRIPTION,
rag_vector_db_config=RagVectorDbConfig(
pinecone=RagVectorDbConfig.Pinecone(index_name=TEST_PINECONE_INDEX_NAME),
rag_vector_db_config=GapicRagVectorDbConfig(
pinecone=GapicRagVectorDbConfig.Pinecone(index_name=TEST_PINECONE_INDEX_NAME),
api_auth=api_auth.ApiAuth(
api_key_config=api_auth.ApiAuth.ApiKeyConfig(
api_key_secret_version=TEST_PINECONE_API_KEY_SECRET_VERSION
Expand All @@ -161,6 +164,14 @@
TEST_EMBEDDING_MODEL_CONFIG = EmbeddingModelConfig(
publisher_model="publishers/google/models/textembedding-gecko",
)
TEST_RAG_EMBEDDING_MODEL_CONFIG = RagEmbeddingModelConfig(
vertex_prediction_endpoint=VertexPredictionEndpoint(
publisher_model="publishers/google/models/textembedding-gecko",
),
)
TEST_BACKEND_CONFIG_EMBEDDING_MODEL_CONFIG = RagVectorDbConfig(
rag_embedding_model_config=TEST_RAG_EMBEDDING_MODEL_CONFIG,
)
TEST_VERTEX_FEATURE_STORE_CONFIG = VertexFeatureStore(
resource_name=TEST_VERTEX_FEATURE_STORE_RESOURCE_NAME,
)
Expand Down Expand Up @@ -195,6 +206,62 @@
vector_db=TEST_VERTEX_VECTOR_SEARCH_CONFIG,
)
TEST_PAGE_TOKEN = "test-page-token"
# Backend Config
TEST_GAPIC_RAG_CORPUS_BACKEND_CONFIG = GapicRagCorpus(
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
description=TEST_CORPUS_DISCRIPTION,
)
TEST_GAPIC_RAG_CORPUS_BACKEND_CONFIG.vector_db_config.rag_embedding_model_config.vertex_prediction_endpoint.endpoint = "projects/{}/locations/{}/publishers/google/models/textembedding-gecko".format(
TEST_PROJECT, TEST_REGION
)
TEST_GAPIC_RAG_CORPUS_VERTEX_VECTOR_SEARCH_BACKEND_CONFIG = GapicRagCorpus(
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
description=TEST_CORPUS_DISCRIPTION,
vector_db_config=GapicRagVectorDbConfig(
vertex_vector_search=GapicRagVectorDbConfig.VertexVectorSearch(
index_endpoint=TEST_VERTEX_VECTOR_SEARCH_INDEX_ENDPOINT,
index=TEST_VERTEX_VECTOR_SEARCH_INDEX,
),
),
)
TEST_GAPIC_RAG_CORPUS_PINECONE_BACKEND_CONFIG = GapicRagCorpus(
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
description=TEST_CORPUS_DISCRIPTION,
vector_db_config=GapicRagVectorDbConfig(
pinecone=GapicRagVectorDbConfig.Pinecone(index_name=TEST_PINECONE_INDEX_NAME),
api_auth=api_auth.ApiAuth(
api_key_config=api_auth.ApiAuth.ApiKeyConfig(
api_key_secret_version=TEST_PINECONE_API_KEY_SECRET_VERSION
),
),
),
)
TEST_RAG_CORPUS_BACKEND = RagCorpus(
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
backend_config=TEST_BACKEND_CONFIG_EMBEDDING_MODEL_CONFIG,
)
TEST_BACKEND_CONFIG_PINECONE_CONFIG = RagVectorDbConfig(
vector_db=TEST_PINECONE_CONFIG,
)
TEST_RAG_CORPUS_PINECONE_BACKEND = RagCorpus(
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
description=TEST_CORPUS_DISCRIPTION,
backend_config=TEST_BACKEND_CONFIG_PINECONE_CONFIG,
)
TEST_BACKEND_CONFIG_VERTEX_VECTOR_SEARCH_CONFIG = RagVectorDbConfig(
vector_db=TEST_VERTEX_VECTOR_SEARCH_CONFIG,
)
TEST_RAG_CORPUS_VERTEX_VECTOR_SEARCH_BACKEND = RagCorpus(
name=TEST_RAG_CORPUS_RESOURCE_NAME,
display_name=TEST_CORPUS_DISPLAY_NAME,
description=TEST_CORPUS_DISCRIPTION,
backend_config=TEST_BACKEND_CONFIG_VERTEX_VECTOR_SEARCH_CONFIG,
)
# Vertex AI Search Config
TEST_VERTEX_AI_SEARCH_ENGINE_SERVING_CONFIG = f"projects/{TEST_PROJECT_NUMBER}/locations/{TEST_REGION}/collections/test-collection/engines/test-engine/servingConfigs/test-serving-config"
TEST_VERTEX_AI_SEARCH_DATASTORE_SERVING_CONFIG = f"projects/{TEST_PROJECT_NUMBER}/locations/{TEST_REGION}/collections/test-collection/dataStores/test-datastore/servingConfigs/test-serving-config"
Expand Down
116 changes: 116 additions & 0 deletions tests/unit/vertex_rag/test_rag_data_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ def create_rag_corpus_mock():
yield create_rag_corpus_mock


@pytest.fixture
def create_rag_corpus_mock_backend():
with mock.patch.object(
VertexRagDataServiceClient,
"create_rag_corpus",
) as create_rag_corpus_mock:
create_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_corpus_lro_mock.done.return_value = True
create_rag_corpus_lro_mock.result.return_value = (
test_rag_constants_preview.TEST_GAPIC_RAG_CORPUS_BACKEND_CONFIG
)
create_rag_corpus_mock.return_value = create_rag_corpus_lro_mock
yield create_rag_corpus_mock


@pytest.fixture
def create_rag_corpus_mock_weaviate():
with mock.patch.object(
Expand Down Expand Up @@ -102,6 +117,23 @@ def create_rag_corpus_mock_vertex_vector_search():
yield create_rag_corpus_mock_vertex_vector_search


@pytest.fixture
def create_rag_corpus_mock_vertex_vector_search_backend():
with mock.patch.object(
VertexRagDataServiceClient,
"create_rag_corpus",
) as create_rag_corpus_mock_vertex_vector_search:
create_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_corpus_lro_mock.done.return_value = True
create_rag_corpus_lro_mock.result.return_value = (
test_rag_constants_preview.TEST_GAPIC_RAG_CORPUS_VERTEX_VECTOR_SEARCH_BACKEND_CONFIG
)
create_rag_corpus_mock_vertex_vector_search.return_value = (
create_rag_corpus_lro_mock
)
yield create_rag_corpus_mock_vertex_vector_search


@pytest.fixture
def create_rag_corpus_mock_pinecone():
with mock.patch.object(
Expand All @@ -117,6 +149,21 @@ def create_rag_corpus_mock_pinecone():
yield create_rag_corpus_mock_pinecone


@pytest.fixture
def create_rag_corpus_mock_pinecone_backend():
with mock.patch.object(
VertexRagDataServiceClient,
"create_rag_corpus",
) as create_rag_corpus_mock_pinecone:
create_rag_corpus_lro_mock = mock.Mock(ga_operation.Operation)
create_rag_corpus_lro_mock.done.return_value = True
create_rag_corpus_lro_mock.result.return_value = (
test_rag_constants_preview.TEST_GAPIC_RAG_CORPUS_PINECONE_BACKEND_CONFIG
)
create_rag_corpus_mock_pinecone.return_value = create_rag_corpus_lro_mock
yield create_rag_corpus_mock_pinecone


@pytest.fixture
def create_rag_corpus_mock_vertex_ai_engine_search_config():
with mock.patch.object(
Expand Down Expand Up @@ -407,6 +454,15 @@ def test_create_corpus_success(self):

rag_corpus_eq(rag_corpus, test_rag_constants_preview.TEST_RAG_CORPUS)

@pytest.mark.usefixtures("create_rag_corpus_mock_backend")
def test_create_corpus_backend_success(self):
rag_corpus = rag.create_corpus(
display_name=test_rag_constants_preview.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants_preview.TEST_BACKEND_CONFIG_EMBEDDING_MODEL_CONFIG,
)

rag_corpus_eq(rag_corpus, test_rag_constants_preview.TEST_RAG_CORPUS_BACKEND)

@pytest.mark.usefixtures("create_rag_corpus_mock_weaviate")
def test_create_corpus_weaviate_success(self):
rag_corpus = rag.create_corpus(
Expand Down Expand Up @@ -438,6 +494,18 @@ def test_create_corpus_vertex_vector_search_success(self):
rag_corpus, test_rag_constants_preview.TEST_RAG_CORPUS_VERTEX_VECTOR_SEARCH
)

@pytest.mark.usefixtures("create_rag_corpus_mock_vertex_vector_search_backend")
def test_create_corpus_vertex_vector_search_backend_success(self):
rag_corpus = rag.create_corpus(
display_name=test_rag_constants_preview.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants_preview.TEST_BACKEND_CONFIG_VERTEX_VECTOR_SEARCH_CONFIG,
)

rag_corpus_eq(
rag_corpus,
test_rag_constants_preview.TEST_RAG_CORPUS_VERTEX_VECTOR_SEARCH_BACKEND,
)

@pytest.mark.usefixtures("create_rag_corpus_mock_pinecone")
def test_create_corpus_pinecone_success(self):
rag_corpus = rag.create_corpus(
Expand All @@ -447,6 +515,43 @@ def test_create_corpus_pinecone_success(self):

rag_corpus_eq(rag_corpus, test_rag_constants_preview.TEST_RAG_CORPUS_PINECONE)

@pytest.mark.usefixtures("create_rag_corpus_mock_pinecone_backend")
def test_create_corpus_pinecone_backend_success(self):
rag_corpus = rag.create_corpus(
display_name=test_rag_constants_preview.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants_preview.TEST_BACKEND_CONFIG_PINECONE_CONFIG,
)

rag_corpus_eq(
rag_corpus, test_rag_constants_preview.TEST_RAG_CORPUS_PINECONE_BACKEND
)

def test_create_corpus_backend_config_with_embedding_model_config_failure(
self,
):
with pytest.raises(ValueError) as e:
rag.create_corpus(
display_name=test_rag_constants_preview.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants_preview.TEST_BACKEND_CONFIG_EMBEDDING_MODEL_CONFIG,
embedding_model_config=test_rag_constants_preview.TEST_EMBEDDING_MODEL_CONFIG,
)
e.match(
"Only one of backend_config or embedding_model_config and vector_db can be set. embedding_model_config and vector_db are deprecated, use backend_config instead."
)

def test_create_corpus_backend_config_with_vector_db_failure(
self,
):
with pytest.raises(ValueError) as e:
rag.create_corpus(
display_name=test_rag_constants_preview.TEST_CORPUS_DISPLAY_NAME,
backend_config=test_rag_constants_preview.TEST_BACKEND_CONFIG_EMBEDDING_MODEL_CONFIG,
vector_db=test_rag_constants_preview.TEST_PINECONE_CONFIG,
)
e.match(
"Only one of backend_config or embedding_model_config and vector_db can be set. embedding_model_config and vector_db are deprecated, use backend_config instead."
)

@pytest.mark.usefixtures("create_rag_corpus_mock_vertex_ai_engine_search_config")
def test_create_corpus_vais_engine_search_config_success(self):
rag_corpus = rag.create_corpus(
Expand Down Expand Up @@ -480,6 +585,17 @@ def test_create_corpus_vais_datastore_search_config_with_vector_db_failure(self)
)
e.match("Only one of vertex_ai_search_config or vector_db can be set.")

def test_create_corpus_vais_datastore_search_config_with_backend_config_failure(
self,
):
with pytest.raises(ValueError) as e:
rag.create_corpus(
display_name=test_rag_constants_preview.TEST_CORPUS_DISPLAY_NAME,
vertex_ai_search_config=test_rag_constants_preview.TEST_VERTEX_AI_SEARCH_CONFIG_DATASTORE,
backend_config=test_rag_constants_preview.TEST_BACKEND_CONFIG_EMBEDDING_MODEL_CONFIG,
)
e.match("Only one of vertex_ai_search_config or backend_config can be set.")

def test_create_corpus_vais_datastore_search_config_with_embedding_model_config_failure(
self,
):
Expand Down
6 changes: 6 additions & 0 deletions vertexai/preview/rag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
VertexFeatureStore,
VertexVectorSearch,
Weaviate,
RagEmbeddingModelConfig,
VertexPredictionEndpoint,
RagVectorDbConfig,
)

__all__ = (
Expand Down Expand Up @@ -87,6 +90,9 @@
"VertexRagStore",
"VertexVectorSearch",
"Weaviate",
"RagEmbeddingModelConfig",
"VertexPredictionEndpoint",
"RagVectorDbConfig",
"create_corpus",
"delete_corpus",
"delete_file",
Expand Down
Loading

0 comments on commit 216a30f

Please sign in to comment.