-
Notifications
You must be signed in to change notification settings - Fork 6.7k
docs: Update RAG snippets to use latest RAG module from AgentPlatform SDK #14369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f791c2c
4af7bc9
4b41808
bb9cbc7
a8dfedd
7bdf192
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,6 @@ | |
|
|
||
| from typing import Optional | ||
|
|
||
| from vertexai.preview.rag import RagCorpus | ||
|
|
||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
|
|
||
|
|
@@ -26,30 +25,33 @@ def create_corpus( | |
| ) -> RagCorpus: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| # [START generativeaionvertexai_rag_create_corpus] | ||
|
|
||
| from vertexai import rag | ||
| import vertexai | ||
| import agentplatform | ||
| from agentplatform import types | ||
|
|
||
| # TODO(developer): Update and un-comment below lines | ||
| # PROJECT_ID = "your-project-id" | ||
| # display_name = "test_corpus" | ||
| # description = "Corpus Description" | ||
|
|
||
| # Initialize Vertex AI API once per session | ||
| vertexai.init(project=PROJECT_ID, location="us-central1") | ||
| # Initialize Agent Platform client once per session | ||
| client = agentplatform.Client(project=PROJECT_ID, location="us-central1") | ||
|
|
||
| # Configure backend_config | ||
| backend_config = rag.RagVectorDbConfig( | ||
| rag_embedding_model_config=rag.RagEmbeddingModelConfig( | ||
| vertex_prediction_endpoint=rag.VertexPredictionEndpoint( | ||
| publisher_model="publishers/google/models/text-embedding-005" | ||
| # Configure project-level config | ||
| backend_config = types.RagVectorDbConfig( | ||
| rag_embedding_model_config=types.RagEmbeddingModelConfig( | ||
| vertex_prediction_endpoint=types.RagEmbeddingModelConfigVertexPredictionEndpoint( | ||
| endpoint="publishers/google/models/text-embedding-005" | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| corpus = rag.create_corpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| backend_config=backend_config, | ||
| # Create a corpus | ||
| corpus = client.rag.create_corpus( | ||
| rag_corpus=types.RagCorpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| rag_vector_db_config=backend_config, | ||
| ) | ||
| ) | ||
| print(corpus) | ||
| # Example response: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,6 @@ | |
|
|
||
| from typing import Optional | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| from vertexai.preview.rag import RagCorpus | ||
|
|
||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
|
|
||
|
|
||
|
|
@@ -27,31 +25,36 @@ def create_corpus_feature_store( | |
| ) -> RagCorpus: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| # [START generativeaionvertexai_rag_create_corpus_feature_store] | ||
|
|
||
| from vertexai.preview import rag | ||
| import vertexai | ||
| import agentplatform | ||
| from agentplatform import types | ||
|
|
||
| # TODO(developer): Update and un-comment below lines | ||
| # PROJECT_ID = "your-project-id" | ||
| # feature_view_name = "projects/{PROJECT_ID}/locations/{LOCATION}/featureOnlineStores/{FEATURE_ONLINE_STORE_ID}/featureViews/{FEATURE_VIEW_ID}" | ||
| # display_name = "test_corpus" | ||
| # description = "Corpus Description" | ||
|
|
||
| # Initialize Vertex AI API once per session | ||
| vertexai.init(project=PROJECT_ID, location="us-central1") | ||
| # Initialize Agent Platform client once per session | ||
| client = agentplatform.Client(project=PROJECT_ID, location="us-central1") | ||
|
|
||
| # Configure embedding model (Optional) | ||
| embedding_model_config = rag.EmbeddingModelConfig( | ||
| publisher_model="publishers/google/models/text-embedding-004" | ||
| backend_config = types.RagVectorDbConfig( | ||
| rag_embedding_model_config=types.RagEmbeddingModelConfig( | ||
| vertex_prediction_endpoint=types.RagEmbeddingModelConfigVertexPredictionEndpoint( | ||
| endpoint="publishers/google/models/text-embedding-005" | ||
| ), | ||
| ), | ||
| vertex_feature_store=types.RagDbConfigVertexFeatureStore( | ||
| feature_view_resource_name=feature_view_name | ||
| ) | ||
| ) | ||
|
|
||
| # Configure Vector DB | ||
| vector_db = rag.VertexFeatureStore(resource_name=feature_view_name) | ||
|
|
||
| corpus = rag.create_corpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| embedding_model_config=embedding_model_config, | ||
| vector_db=vector_db, | ||
| corpus = client.rag.create_corpus( | ||
| rag_corpus=types.RagCorpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| rag_vector_db_config=backend_config, | ||
| ) | ||
| ) | ||
| print(corpus) | ||
| # Example response: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,6 @@ | |
|
|
||
| from typing import Optional | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| from vertexai.preview.rag import RagCorpus | ||
|
|
||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
|
|
||
|
|
||
|
|
@@ -28,39 +26,39 @@ def create_corpus_pinecone( | |
| ) -> RagCorpus: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| # [START generativeaionvertexai_rag_create_corpus_pinecone] | ||
|
|
||
| from vertexai import rag | ||
| import vertexai | ||
| import agentplatform | ||
| from agentplatform import types | ||
|
|
||
| # TODO(developer): Update and un-comment below lines | ||
| # PROJECT_ID = "your-project-id" | ||
| # pinecone_index_name = "pinecone-index-name" | ||
| # pinecone_api_key_secret_manager_version = "projects/{PROJECT_ID}/secrets/{SECRET_NAME}/versions/latest" | ||
| # display_name = "test_corpus" | ||
| # description = "Corpus Description" | ||
|
|
||
| # Initialize Vertex AI API once per session | ||
| vertexai.init(project=PROJECT_ID, location="us-central1") | ||
| # Initialize Agent Platform client once per session | ||
| client = agentplatform.Client(project=PROJECT_ID, location="us-central1") | ||
|
|
||
| # Configure embedding model (Optional) | ||
| embedding_model_config = rag.RagEmbeddingModelConfig( | ||
| vertex_prediction_endpoint=rag.VertexPredictionEndpoint( | ||
| publisher_model="publishers/google/models/text-embedding-005" | ||
| embedding_model_config = types.RagEmbeddingModelConfig( | ||
| vertex_prediction_endpoint=types.RagEmbeddingModelConfigVertexPredictionEndpoint( | ||
| endpoint="publishers/google/models/text-embedding-005" | ||
| ) | ||
| ) | ||
|
|
||
| # Configure Vector DB | ||
| vector_db = rag.Pinecone( | ||
| index_name=pinecone_index_name, | ||
| api_key=pinecone_api_key_secret_manager_version, | ||
| vector_db = types.RagVectorDbConfig( | ||
| pinecone=types.RagVectorDbConfigPinecone( | ||
| index_name=pinecone_index_name, | ||
| ), | ||
| rag_embedding_model_config=embedding_model_config, | ||
| ) | ||
|
|
||
| corpus = rag.create_corpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| backend_config=rag.RagVectorDbConfig( | ||
| rag_embedding_model_config=embedding_model_config, | ||
| vector_db=vector_db, | ||
| ), | ||
| corpus = client.rag.create_corpus( | ||
| rag_corpus=types.RagCorpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| rag_vector_db_config=vector_db, | ||
| ) | ||
| ) | ||
| print(corpus) | ||
| # Example response: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,6 @@ | |
|
|
||
| from typing import Optional | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| from vertexai.preview.rag import RagCorpus | ||
|
|
||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
|
|
||
|
|
||
|
|
@@ -28,8 +26,8 @@ def create_corpus_vector_search( | |
| ) -> RagCorpus: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| # [START generativeaionvertexai_rag_create_corpus_vector_search] | ||
|
|
||
| from vertexai import rag | ||
| import vertexai | ||
| import agentplatform | ||
| from agentplatform import types | ||
|
|
||
| # TODO(developer): Update and un-comment below lines | ||
| # PROJECT_ID = "your-project-id" | ||
|
|
@@ -38,28 +36,31 @@ def create_corpus_vector_search( | |
| # display_name = "test_corpus" | ||
| # description = "Corpus Description" | ||
|
|
||
| # Initialize Vertex AI API once per session | ||
| vertexai.init(project=PROJECT_ID, location="us-central1") | ||
| # Initialize Agent Platform client once per session | ||
| client = agentplatform.Client(project=PROJECT_ID, location="us-central1") | ||
|
|
||
| # Configure embedding model (Optional) | ||
| embedding_model_config = rag.RagEmbeddingModelConfig( | ||
| vertex_prediction_endpoint=rag.VertexPredictionEndpoint( | ||
| publisher_model="publishers/google/models/text-embedding-005" | ||
| embedding_model_config = types.RagEmbeddingModelConfig( | ||
| vertex_prediction_endpoint=types.RagEmbeddingModelConfigVertexPredictionEndpoint( | ||
| endpoint="publishers/google/models/text-embedding-005" | ||
| ) | ||
| ) | ||
|
|
||
| # Configure Vector DB | ||
| vector_db = rag.VertexVectorSearch( | ||
| index=vector_search_index_name, index_endpoint=vector_search_index_endpoint_name | ||
| vector_db = types.RagVectorDbConfigVertexVectorSearch( | ||
| index=vector_search_index_name, | ||
| index_endpoint=vector_search_index_endpoint_name | ||
| ) | ||
|
|
||
| corpus = rag.create_corpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| backend_config=rag.RagVectorDbConfig( | ||
| rag_embedding_model_config=embedding_model_config, | ||
| vector_db=vector_db, | ||
| ), | ||
| corpus = client.rag.create_corpus( | ||
| rag_corpus=types.RagCorpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| rag_vector_db_config=types.RagVectorDbConfig( | ||
| rag_embedding_model_config=embedding_model_config, | ||
| vertex_vector_search=vector_db, | ||
| ), | ||
| ) | ||
| ) | ||
| print(corpus) | ||
| # Example response: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,6 @@ | |
|
|
||
| from typing import Optional | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| from vertexai import rag | ||
|
|
||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
|
|
||
|
|
||
|
|
@@ -27,27 +25,29 @@ def create_corpus_vertex_ai_search( | |
| ) -> rag.RagCorpus: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| # [START generativeaionvertexai_rag_create_corpus_vertex_ai_search] | ||
|
|
||
| from vertexai import rag | ||
| import vertexai | ||
| import agentplatform | ||
| from agentplatform import types | ||
|
|
||
| # TODO(developer): Update and un-comment below lines | ||
| # PROJECT_ID = "your-project-id" | ||
| # vertex_ai_search_engine_name = "projects/{PROJECT_ID}/locations/{LOCATION}/collections/default_collection/engines/{ENGINE_ID}" | ||
| # display_name = "test_corpus" | ||
| # description = "Corpus Description" | ||
|
|
||
| # Initialize Vertex AI API once per session | ||
| vertexai.init(project=PROJECT_ID, location="us-central1") | ||
| # Initialize Agent Platform client once per session | ||
| client = agentplatform.Client(project=PROJECT_ID, location="us-central1") | ||
|
|
||
| # Configure Search | ||
| vertex_ai_search_config = rag.VertexAiSearchConfig( | ||
| vertex_ai_search_config = types.VertexAiSearchConfig( | ||
| serving_config=f"{vertex_ai_search_engine_name}/servingConfigs/default_search", | ||
| ) | ||
|
|
||
| corpus = rag.create_corpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| vertex_ai_search_config=vertex_ai_search_config, | ||
| corpus = client.rag.create_corpus( | ||
| rag_corpus=types.RagCorpus( | ||
| display_name=display_name, | ||
| description=description, | ||
| vertex_ai_search_config=vertex_ai_search_config, | ||
| ), | ||
| ) | ||
| print(corpus) | ||
| # Example response: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,8 +15,6 @@ | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| from vertexai.preview.rag import RagCorpus | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -29,8 +27,8 @@ def create_corpus_weaviate( | |||||||||||||||||||||||||||||||||||||||||
| ) -> RagCorpus: | ||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||||||||||
| # [START generativeaionvertexai_rag_create_corpus_weaviate] | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| from vertexai.preview import rag | ||||||||||||||||||||||||||||||||||||||||||
| import vertexai | ||||||||||||||||||||||||||||||||||||||||||
| import agentplatform | ||||||||||||||||||||||||||||||||||||||||||
| from agentplatform import types | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # TODO(developer): Update and un-comment below lines | ||||||||||||||||||||||||||||||||||||||||||
| # PROJECT_ID = "your-project-id" | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,26 +38,31 @@ def create_corpus_weaviate( | |||||||||||||||||||||||||||||||||||||||||
| # display_name = "test_corpus" | ||||||||||||||||||||||||||||||||||||||||||
| # description = "Corpus Description" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Initialize Vertex AI API once per session | ||||||||||||||||||||||||||||||||||||||||||
| vertexai.init(project=PROJECT_ID, location="us-central1") | ||||||||||||||||||||||||||||||||||||||||||
| # Initialize Agent Platform client once per session | ||||||||||||||||||||||||||||||||||||||||||
| client = agentplatform.Client(project=PROJECT_ID, location="us-central1") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Configure embedding model (Optional) | ||||||||||||||||||||||||||||||||||||||||||
| embedding_model_config = rag.EmbeddingModelConfig( | ||||||||||||||||||||||||||||||||||||||||||
| publisher_model="publishers/google/models/text-embedding-004" | ||||||||||||||||||||||||||||||||||||||||||
| embedding_model_config = types.RagEmbeddingModelConfig( | ||||||||||||||||||||||||||||||||||||||||||
| vertex_prediction_endpoint=types.RagEmbeddingModelConfigVertexPredictionEndpoint( | ||||||||||||||||||||||||||||||||||||||||||
| endpoint="publishers/google/models/text-embedding-004" | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Configure Vector DB | ||||||||||||||||||||||||||||||||||||||||||
| vector_db = rag.Weaviate( | ||||||||||||||||||||||||||||||||||||||||||
| weaviate_http_endpoint=weaviate_http_endpoint, | ||||||||||||||||||||||||||||||||||||||||||
| vector_db = types.RagVectorDbConfigWeaviate( | ||||||||||||||||||||||||||||||||||||||||||
| http_endpoint=weaviate_http_endpoint, | ||||||||||||||||||||||||||||||||||||||||||
| collection_name=weaviate_collection_name, | ||||||||||||||||||||||||||||||||||||||||||
| api_key=weaviate_api_key_secret_manager_version, | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| corpus = rag.create_corpus( | ||||||||||||||||||||||||||||||||||||||||||
| display_name=display_name, | ||||||||||||||||||||||||||||||||||||||||||
| description=description, | ||||||||||||||||||||||||||||||||||||||||||
| embedding_model_config=embedding_model_config, | ||||||||||||||||||||||||||||||||||||||||||
| vector_db=vector_db, | ||||||||||||||||||||||||||||||||||||||||||
| corpus = client.rag.create_corpus( | ||||||||||||||||||||||||||||||||||||||||||
| rag_corpus=types.RagCorpus( | ||||||||||||||||||||||||||||||||||||||||||
| display_name=display_name, | ||||||||||||||||||||||||||||||||||||||||||
| description=description, | ||||||||||||||||||||||||||||||||||||||||||
| rag_embedding_model_config=embedding_model_config, | ||||||||||||||||||||||||||||||||||||||||||
| rag_vector_db_config=types.RagVectorDbConfig( | ||||||||||||||||||||||||||||||||||||||||||
| weaviate=vector_db | ||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+57
to
66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct the field names and nesting for
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| print(corpus) | ||||||||||||||||||||||||||||||||||||||||||
| # Example response: | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
agentplatformandtypesglobally so they can be used in the function signature's return type annotation without raising aNameErrorat import time.