|
| 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_pinecone( |
| 24 | + pinecone_index_name: str, |
| 25 | + pinecone_api_key_secret_manager_version: str, |
| 26 | + display_name: Optional[str] = None, |
| 27 | + description: Optional[str] = None, |
| 28 | +) -> RagCorpus: |
| 29 | + # [START generativeaionvertexai_rag_create_corpus_pinecone] |
| 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 | + # pinecone_index_name = "pinecone-index-name" |
| 37 | + # pinecone_api_key_secret_manager_version = "projects/{PROJECT_ID}/secrets/{SECRET_NAME}/versions/latest" |
| 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.Pinecone( |
| 51 | + index_name=pinecone_index_name, |
| 52 | + api_key=pinecone_api_key_secret_manager_version, |
| 53 | + ) |
| 54 | + |
| 55 | + corpus = rag.create_corpus( |
| 56 | + display_name=display_name, |
| 57 | + description=description, |
| 58 | + embedding_model_config=embedding_model_config, |
| 59 | + vector_db=vector_db, |
| 60 | + ) |
| 61 | + print(corpus) |
| 62 | + # Example response: |
| 63 | + # RagCorpus(name='projects/1234567890/locations/us-central1/ragCorpora/1234567890', |
| 64 | + # display_name='test_corpus', description='Corpus Description', embedding_model_config=... |
| 65 | + # ... |
| 66 | + |
| 67 | + # [END generativeaionvertexai_rag_create_corpus_pinecone] |
| 68 | + return corpus |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + create_corpus_pinecone( |
| 73 | + pinecone_index_name="pinecone-index-name"," |
| 74 | + pinecone_api_key_secret_manager_version="projects/{PROJECT_ID}/secrets/{SECRET_NAME}/versions/latest", |
| 75 | + display_name="test_corpus", |
| 76 | + description="Corpus Description", |
| 77 | + ) |
0 commit comments