Starting April 29, 2025, Gemini 1.5 Pro and Gemini 1.5 Flash models are not available in projects that have no prior usage of these models, including new projects. For details, see Model versions and lifecycle.
Stay organized with collections
Save and categorize content based on your preferences.
This quickstart shows you how to use the Vertex AI SDK to perform Vertex AI RAG Engine tasks. You will set up your environment and then use a Python code sample to retrieve and generate content.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# RAG quickstart for Python\n\n| The [VPC-SC security controls](/vertex-ai/generative-ai/docs/security-controls) and\n| CMEK are supported by Vertex AI RAG Engine. Data residency and AXT security controls aren't\nsupported. \n| To see an example of using RAG Engine,\n| run the \"Intro to RAG Engine in Vertex AI\" notebook in one of the following\n| environments:\n|\n| [Open in Colab](https://colab.research.google.com/github/GoogleCloudPlatform/generative-ai/blob/main/gemini/rag-engine/intro_rag_engine.ipynb)\n|\n|\n| \\|\n|\n| [Open in Colab Enterprise](https://console.cloud.google.com/vertex-ai/colab/import/https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Frag-engine%2Fintro_rag_engine.ipynb)\n|\n|\n| \\|\n|\n| [Open\n| in Vertex AI Workbench](https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https%3A%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Frag-engine%2Fintro_rag_engine.ipynb)\n|\n|\n| \\|\n|\n| [View on GitHub](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/rag-engine/intro_rag_engine.ipynb)\n\nThis page shows you how to use the Vertex AI SDK to run\nVertex AI RAG Engine tasks.\n\nYou can also follow along using this notebook [Intro to Vertex AI RAG Engine](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/rag-engine/intro_rag_engine.ipynb).\n\nRequired roles\n--------------\n\n\nGrant roles to your user account. Run the following command once for each of the following\nIAM roles:\n`roles/aiplatform.user` \n\n```bash\ngcloud projects add-iam-policy-binding PROJECT_ID --member=\"user:\u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e\" --role=ROLE\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: your project ID.\n- \u003cvar translate=\"no\"\u003eUSER_IDENTIFIER\u003c/var\u003e: the identifier for your user account---for example, `myemail@example.com`.\n- \u003cvar translate=\"no\"\u003eROLE\u003c/var\u003e: the IAM role that you grant to your user account.\n\nPrepare your Google Cloud console\n---------------------------------\n\nTo use Vertex AI RAG Engine, do the following:\n\n1. [Install the Vertex AI SDK for Python](/vertex-ai/docs/start/install-sdk).\n\n2. Run this command in the Google Cloud console to set up your project.\n\n `gcloud config set {project}`\n3. Run this command to authorize your login.\n\n `gcloud auth application-default login`\n\nRun Vertex AI RAG Engine\n------------------------\n\nCopy and paste this sample code into the Google Cloud console to run Vertex AI RAG Engine. \n\n### Python\n\nTo learn how to install or update the Vertex AI SDK for Python, see [Install the Vertex AI SDK for Python](/vertex-ai/docs/start/use-vertex-ai-python-sdk).\n\nFor more information, see the\n[Python API reference documentation](/python/docs/reference/aiplatform/latest).\n\n from vertexai import rag\n from vertexai.generative_models import GenerativeModel, Tool\n import vertexai\n\n # Create a RAG Corpus, Import Files, and Generate a response\n\n # TODO(developer): Update and un-comment below lines\n # PROJECT_ID = \"your-project-id\"\n # display_name = \"test_corpus\"\n # paths = [\"https://drive.google.com/file/d/123\", \"gs://my_bucket/my_files_dir\"] # Supports Google Cloud Storage and Google Drive Links\n\n # Initialize Vertex AI API once per session\n vertexai.init(project=PROJECT_ID, location=\"us-central1\")\n\n # Create RagCorpus\n # Configure embedding model, for example \"text-embedding-005\".\n embedding_model_config = rag.RagEmbeddingModelConfig(\n vertex_prediction_endpoint=rag.VertexPredictionEndpoint(\n publisher_model=\"publishers/google/models/text-embedding-005\"\n )\n )\n\n rag_corpus = rag.create_corpus(\n display_name=display_name,\n backend_config=rag.RagVectorDbConfig(\n rag_embedding_model_config=embedding_model_config\n ),\n )\n\n # Import Files to the RagCorpus\n rag.import_files(\n rag_corpus.name,\n paths,\n # Optional\n transformation_config=rag.TransformationConfig(\n chunking_config=rag.ChunkingConfig(\n chunk_size=512,\n chunk_overlap=100,\n ),\n ),\n max_embedding_requests_per_min=1000, # Optional\n )\n\n # Direct context retrieval\n rag_retrieval_config = rag.RagRetrievalConfig(\n top_k=3, # Optional\n filter=rag.Filter(vector_distance_threshold=0.5), # Optional\n )\n response = rag.retrieval_query(\n rag_resources=[\n rag.RagResource(\n rag_corpus=rag_corpus.name,\n # Optional: supply IDs from `rag.list_files()`.\n # rag_file_ids=[\"rag-file-1\", \"rag-file-2\", ...],\n )\n ],\n text=\"What is RAG and why it is helpful?\",\n rag_retrieval_config=rag_retrieval_config,\n )\n print(response)\n\n # Enhance generation\n # Create a RAG retrieval tool\n rag_retrieval_tool = Tool.from_retrieval(\n retrieval=rag.Retrieval(\n source=rag.VertexRagStore(\n rag_resources=[\n rag.RagResource(\n rag_corpus=rag_corpus.name, # Currently only 1 corpus is allowed.\n # Optional: supply IDs from `rag.list_files()`.\n # rag_file_ids=[\"rag-file-1\", \"rag-file-2\", ...],\n )\n ],\n rag_retrieval_config=rag_retrieval_config,\n ),\n )\n )\n\n # Create a Gemini model instance\n rag_model = GenerativeModel(\n model_name=\"gemini-2.0-flash-001\", tools=[rag_retrieval_tool]\n )\n\n # Generate response\n response = rag_model.generate_content(\"What is RAG and why it is helpful?\")\n print(response.text)\n # Example response:\n # RAG stands for Retrieval-Augmented Generation.\n # It's a technique used in AI to enhance the quality of responses\n # ...\n\n\u003cbr /\u003e\n\nWhat's next\n-----------\n\n- To learn more about the RAG API, see [Vertex AI RAG Engine\n API](/vertex-ai/generative-ai/docs/model-reference/rag-api).\n- To learn more about the responses from RAG, see [Retrieval and Generation Output of Vertex AI RAG Engine](/vertex-ai/generative-ai/docs/model-reference/rag-output-explained).\n- To learn about the Vertex AI RAG Engine, see the [Vertex AI RAG Engine overview](/vertex-ai/generative-ai/docs/rag-overview)."]]