Skip to content

Commit c0533bc

Browse files
committed
docs(samples): Add Create Corpus with Feature Store
1 parent 8f39ccc commit c0533bc

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

generative_ai/rag/create_corpus_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from typing import Optional
1717

18-
from google.cloud.aiplatform_v1beta1 import RagCorpus
18+
from vertexai.preview.rag import RagCorpus
1919

2020
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
2121

@@ -37,7 +37,7 @@ def create_corpus(
3737
# Initialize Vertex AI API once per session
3838
vertexai.init(project=PROJECT_ID, location="us-central1")
3939

40-
# Configure embedding model
40+
# Configure embedding model (Optional)
4141
embedding_model_config = rag.EmbeddingModelConfig(
4242
publisher_model="publishers/google/models/text-embedding-004"
4343
)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
from typing import Optional
17+
18+
from vertexai.preview.rag import RagCorpus
19+
20+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
21+
22+
23+
def create_corpus_feature_store(
24+
feature_view_name: str,
25+
display_name: Optional[str] = None,
26+
description: Optional[str] = None,
27+
) -> RagCorpus:
28+
# [START generativeaionvertexai_rag_create_corpus_feature_store]
29+
30+
from vertexai.preview import rag
31+
import vertexai
32+
33+
# TODO(developer): Update and un-comment below lines
34+
# PROJECT_ID = "your-project-id"
35+
# feature_view_name = "projects/{PROJECT_ID}/locations/{LOCATION}/featureOnlineStores/{FEATURE_ONLINE_STORE_ID}/featureViews/{FEATURE_VIEW_ID}"
36+
# display_name = "test_corpus"
37+
# description = "Corpus Description"
38+
39+
# Initialize Vertex AI API once per session
40+
vertexai.init(project=PROJECT_ID, location="us-central1")
41+
42+
# Configure embedding model (Optional)
43+
embedding_model_config = rag.EmbeddingModelConfig(
44+
publisher_model="publishers/google/models/text-embedding-004"
45+
)
46+
47+
# Configure Vector DB
48+
vector_db = rag.VertexFeatureStore(resource_name=feature_view_name)
49+
50+
corpus = rag.create_corpus(
51+
display_name=display_name,
52+
description=description,
53+
embedding_model_config=embedding_model_config,
54+
vector_db=vector_db,
55+
)
56+
print(corpus)
57+
# Example response:
58+
# RagCorpus(name='projects/1234567890/locations/us-central1/ragCorpora/1234567890',
59+
# display_name='test_corpus', description='Corpus Description', embedding_model_config=...
60+
# ...
61+
62+
# [END generativeaionvertexai_rag_create_corpus_feature_store]
63+
return corpus
64+
65+
66+
if __name__ == "__main__":
67+
create_corpus_feature_store(
68+
feature_view_name="projects/{PROJECT_ID}/locations/{LOCATION}/featureOnlineStores/{FEATURE_ONLINE_STORE_ID}/featureViews/{FEATURE_VIEW_ID}",
69+
display_name="test_corpus",
70+
description="Corpus Description",
71+
)

0 commit comments

Comments
 (0)