Skip to content

Commit 3e47962

Browse files
committed
Add Vector Search Sample
1 parent e11867d commit 3e47962

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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_vector_search(
24+
vector_search_index_name: str,
25+
vector_search_index_endpoint_name: str,
26+
display_name: Optional[str] = None,
27+
description: Optional[str] = None,
28+
) -> RagCorpus:
29+
# [START generativeaionvertexai_rag_create_corpus_vector_search]
30+
31+
from vertexai.preview import rag
32+
import vertexai
33+
34+
# TODO(developer): Update and un-comment below lines
35+
# PROJECT_ID = "your-project-id"
36+
# vector_search_index_name = "projects/{PROJECT_ID}/locations/{LOCATION}/indexes/{INDEX_ID}"
37+
# vector_search_index_endpoint_name = "projects/{PROJECT_ID}/locations/{LOCATION}/indexEndpoints/{INDEX_ENDPOINT_ID}"
38+
# display_name = "test_corpus"
39+
# description = "Corpus Description"
40+
41+
# Initialize Vertex AI API once per session
42+
vertexai.init(project=PROJECT_ID, location="us-central1")
43+
44+
# Configure embedding model (Optional)
45+
embedding_model_config = rag.EmbeddingModelConfig(
46+
publisher_model="publishers/google/models/text-embedding-004"
47+
)
48+
49+
# Configure Vector DB
50+
vector_db = rag.VertexVectorSearch(
51+
index=vector_search_index_name, index_endpoint=vector_search_index_endpoint_name
52+
)
53+
54+
corpus = rag.create_corpus(
55+
display_name=display_name,
56+
description=description,
57+
embedding_model_config=embedding_model_config,
58+
vector_db=vector_db,
59+
)
60+
print(corpus)
61+
# Example response:
62+
# RagCorpus(name='projects/1234567890/locations/us-central1/ragCorpora/1234567890',
63+
# display_name='test_corpus', description='Corpus Description', embedding_model_config=...
64+
# ...
65+
66+
# [END generativeaionvertexai_rag_create_corpus_vector_search]
67+
return corpus
68+
69+
70+
if __name__ == "__main__":
71+
create_corpus_vector_search(
72+
vector_search_index_name="projects/{PROJECT_ID}/locations/{LOCATION}/indexes/{INDEX_ID}",
73+
vector_search_index_endpoint_name="projects/{PROJECT_ID}/locations/{LOCATION}/indexEndpoints/{INDEX_ENDPOINT_ID}",
74+
display_name="test_corpus",
75+
description="Corpus Description",
76+
)

0 commit comments

Comments
 (0)