{"data":{"slug":"github-google-skills-skills-cloud-agent-platform-rag-engine-management-ca1d1a82a94d","title":"Manage Agent Platform RAG Engine Corpora and Contexts","summary":"Use this skill to list, inspect, and retrieve contexts from Agent Platform RAG Engine corpora using the Google GenAI SDK.","url":"https:\/\/skillshunter.net\/skills\/github-google-skills-skills-cloud-agent-platform-rag-engine-management-ca1d1a82a94d","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-google-skills-skills-cloud-agent-platform-rag-engine-management-ca1d1a82a94d","markdown_url":"https:\/\/skillshunter.net\/skills\/github-google-skills-skills-cloud-agent-platform-rag-engine-management-ca1d1a82a94d.md","download_url":"https:\/\/skillshunter.net\/skills\/github-google-skills-skills-cloud-agent-platform-rag-engine-management-ca1d1a82a94d\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Before running any Python snippets, ensure the environment is initialized by authenticating with Google Cloud credentials and installing the required dependencies (`google-cloud-aiplatform` and `google-genai`) in the same command as the retry.","instructions":"---\nname: agent-platform-rag-engine-management\nmetadata:\n  version: \"1.0.2\"\n  category: AiAndMachineLearning\ndescription: >-\n  Manage and query Agent Platform RAG Engine Corpora and retrieve grounded\n  contexts using the Google GenAI SDK. Use when listing RAG corpora or files,\n  inspecting a corpus, retrieving contexts, or generating content grounded in a\n  RAG corpus. Do not use for standard database queries (use SQL\/Spanner skills),\n  Google Workspace RAG, or other RAG products like gRAG.\n---\n\n# Agent Platform RAG Engine Management\n\nThis skill provides instructions on how to interact with Agent Platform RAG\nEngine using the Agent Platform Python SDK. You MUST use the `vertexai` Python\nSDK to perform RAG Engine operations, rather than raw REST calls or MCP tools,\nbecause this code is intended to be run by external clients.\n\n## Attribution\n\n**MUST READ**: Before executing any `gcloud`, REST API, or MCP command, you MUST\nread [Usage Attribution](references\/attribution.md). For commands executed\ndirectly in the terminal (never in generated scripts, Terraform, or user code),\nprefix `gcloud` with `CLOUDSDK_METRICS_REQUEST_ATTRIBUTION` and append the\nattribution suffix to `User-Agent` headers:\n\n```text\nCLOUDSDK_METRICS_REQUEST_ATTRIBUTION=\"agent-skills\/1.0 (skill:agent-platform-rag-engine-management)\"\nUser-Agent: <existing_user_agent> agent-skills\/1.0 (skill:agent-platform-rag-engine-management)\n```\n\n## Safety & Confirmation Tiers (CRITICAL)\n\nBefore executing any commands or scripts on behalf of the user, you must adhere\nto the following safety tiers based on the action requested:\n\n1.  **Tier R: Read-only (`list_corpora`, `list_files`, `get_corpus`,\n    `retrieval_query`)**\n    *   No confirmation needed. Execute immediately to gather information or\n        retrieve grounded contexts.\n2.  **Tier RC: Read-only but consumes Compute Resources\n    (`client.models.generate_content`)**\n\n    *   Requires **interactive confirmation** with 'Yes'\/'No' options before\n        executing grounded content generation. The confirmation prompt MUST\n        clearly explain the proposed generation execution and its key parameters\n        (e.g., target corpus ID, query text, target model). Natural-language\n        paraphrases without specifying exact parameters are insufficient, as\n        explicit parameter listing is required to ensure unambiguous user\n        approval of the specific resource and configuration.\n    *   **Same-turn restriction**: Do not execute the generation code in the\n        same turn as presenting the confirmation prompt. Stop and wait for the\n        user's reply; only execute after explicit 'Yes' \/ approval.\n    *   **Gold Standard Example**:\n\n        > I will perform grounded content generation with the following\n        > parameters. Please confirm this information before I proceed:\n        >\n        > *   **Target Corpus ID**: `projects\/123\/locations\/us\/ragCorpora\/abc`\n        > *   **Target Model**: `gemini-2.5-pro`\n        > *   **Query Text**: \"What are the company policies on remote work?\"\n        >\n        > Do you confirm? [Yes\/No]\n\n## Phase 0: Environment Setup\n\n**CRITICAL**: Before running any of the Python snippets below, you must ensure\nthe environment is correctly initialized by following these steps:\n\n1.  **Google Cloud Authentication**: Authenticate with your Google Cloud\n    credentials and configure active Application Default Credentials (ADC) for\n    Agent Platform access:\n\n    ```bash\n    gcloud auth login\n    gcloud auth application-default login\n    ```\n\n2.  **Python Dependencies**: This skill needs `google-cloud-aiplatform` and\n    `google-genai`, which the sandbox already provides. Do **not** create a\n    virtual environment \u2014 it starts empty and hides packages the environment\n    already provides, forcing a redundant install. Do not spend a separate\n    command checking for them: run the snippet directly, and only if it fails\n    with `ModuleNotFoundError`, install in the same command as the retry:\n\n    ```bash\n    pip install -q google-cloud-aiplatform google-genai && python3 - <<'PY'\n    ...\n    PY\n    ```\n\n3.  **Execution**: Run each snippet as a quoted heredoc, `python3 - <<'PY'` ...\n    `PY`, rather than `python3 -c '...'`, whose nested quotes break easily.\n    There is no environment to activate first. When one step needs several of\n    the snippets below (for example, list the corpora and then the files in\n    each), combine them into one script and one command. The\n    `vertexai.preview.rag` deprecation warning printed on stderr is expected;\n    keep using these snippets rather than switching clients because of it.\n\n## Workflow Decision Tree\n\n1.  **Information Gathering**: Has the user provided the Project ID, Region, and\n    Corpus ID?\n\n    *   **No** -> Proceed to [1. Listing Corpora and Files] to discover the\n        necessary Resource Names and IDs. Only ask the user if discovery fails.\n    *   **Yes** -> Proceed.\n\n2.  **Task Type**: What does the user want to do?\n\n    *   **List Corpora and Files** -> Proceed to [1. Listing Corpora and Files].\n    *   **Inspect a Corpus** -> Proceed to [2. Getting \/ Inspecting a RAG Engine\n        Corpus].\n    *   **Search for Contexts** -> Proceed to [3. Retrieving Contexts].\n    *   **Answer questions using RAG Engine** -> Proceed to [4. Answering the\n        User with Retrieved Context].\n\n> [!TIP]\n>\n> **Placeholder Parameter Replacement:** The Python scripts below use bracketed\n> string placeholders (like `\"{project_id}\"`, `\"{region}\"`, and\n> `\"{corpus_id}\"`). You **MUST** dynamically replace these placeholders with the\n> actual Project ID, Region, and Corpus ID values provided in the user's prompt\n> (or active context) before generating, providing, or executing the scripts.\n\n## 1. Listing Corpora and Files (Discovery)\n\nIf you do not know the Resource Name of the corpus or file, you MUST list them\nfirst to discover them. The SDK handles pagination automatically when converted\nto a list, but you can also use manual pagination for large sets.\n\n### 1.1 Listing and Discovering Corpora\n\n```python\nimport vertexai\nfrom vertexai.preview import rag\n\nvertexai.init(project=\"{project_id}\", location=\"{region}\")\n\n# Approach A: List ALL (Automatic Pagination)\n# The SDK's Pager iterates through all pages for you.\nall_corpora = list(rag.list_corpora())\nprint(f\"Found {len(all_corpora)} corpora in total.\")\nfor c in all_corpora:\n    print(f\"Corpus Name: {c.name} | Display Name: {c.display_name}\")\n\n# Approach B: Manual Pagination (for very large projects)\npager = rag.list_corpora(page_size=10)\n# Process first page\nfor c in pager:\n    print(f\"Corpus: {c.display_name}\")\n\n# Get next page if needed\nif pager.next_page_token:\n    second_page = rag.list_corpora(\n        page_size=10, page_token=pager.next_page_token\n    )\n```\n\n### 1.2 Listing and Discovering Files\n\nTo understand what files (and types) are in a corpus, list them and inspect the\n`display_name` (usually includes the extension).\n\n```python\nimport vertexai\nfrom vertexai.preview import rag\n\nvertexai.init(project=\"{project_id}\", location=\"{region}\")\ncorpus_name = (\n    \"projects\/{project_id}\/locations\/{region}\/ragCorpora\/{corpus_id}\"\n)\n\n# List files with automatic pagination\nfiles = list(rag.list_files(corpus_name=corpus_name))\nprint(f\"Found {len(files)} files.\")\n\nfor f in files:\n    # High-level SDK RagFile objects usually have name, display_name,\n    # description\n    print(f\"File: {f.display_name} | Resource: {f.name}\")\n    # Tip: Check extension to understand file type (PDF, TXT, etc.)\n    if f.display_name.lower().endswith(\".pdf\"):\n        print(\"  Type: PDF\")\n    elif f.display_name.lower().endswith(\".txt\"):\n        print(\"  Type: Plain Text\")\n```\n\n## 2. Getting \/ Inspecting an Agent Platform RAG Engine Corpus\n\nTo retrieve details about an existing Agent Platform RAG Engine corpus:\n\n```python\nimport vertexai\nfrom vertexai.preview import rag\n\nvertexai.init(project=\"{project_id}\", location=\"{region}\")\n\n# To get details of a specific corpus\ncorpus_name = (\n    \"projects\/{project_id}\/locations\/{region}\/ragCorpora\/{corpus_id}\"\n)\ncorpus = rag.get_corpus(name=corpus_name)\nprint(f\"Corpus Name: {corpus.name}\")\nprint(f\"Display Name: {corpus.display_name}\")\n```\n\n## 3. Retrieving Contexts\n\nTo retrieve relevant contexts from a RAG Engine corpus based on a query. If no\ncontexts come back, that is a valid answer: report it with the corpus and query\nyou used. If a broader search is reasonable, try it in the same script (for\nexample a larger `similarity_top_k`) rather than as a separate command.\n\n```python\nimport vertexai\nfrom vertexai.preview import rag\n\nvertexai.init(project=\"{project_id}\", location=\"{region}\")\n\ncorpus_name = (\n    \"projects\/{project_id}\/locations\/{region}\/ragCorpora\/{corpus_id}\"\n)\nquery = \"What is the speed of light?\"\n\n# Retrieve contexts\nresponse = rag.retrieval_query(\n    rag_corpora=[corpus_name],\n    text=query,\n    similarity_top_k=3\n)\n\nfor context in response.contexts.contexts:\n    print(f\"Context text: {context.text}\")\n    print(f\"Source: {context.source_uri}\")\n```\n\n## 4. Answering the User with Retrieved Context\n\nTo use the retrieved context alongside an Agent Platform model to generate a\ngrounded response:\n\n```python\nfrom google import genai\nfrom google.genai import types\n\nclient = genai.Client(enterprise=True, project=\"{project_id}\", location=\"{region}\")\ncorpus_name = (\n    \"projects\/{project_id}\/locations\/{region}\/ragCorpora\/{corpus_id}\"\n)\n\n# Define the Agent Platform RAG Engine tool pointing to the corpus\nrag_tool = types.Tool(\n    retrieval=types.Retrieval(\n        vertex_rag_store=types.VertexRagStore(\n            rag_resources=[types.VertexRagStoreRagResource(rag_corpus=corpus_name)],\n            rag_retrieval_config=types.RagRetrievalConfig(\n                top_k=3,\n                filter=types.RagRetrievalConfigFilter(\n                    vector_similarity_threshold=0.5,\n                ),\n            ),\n        )\n    )\n)\n\n# Generate content using the RAG Engine tool\nresponse = client.models.generate_content(\n    model=\"gemini-2.5-flash\",\n    contents=\"What is the speed of light?\",\n    config=types.GenerateContentConfig(\n        tools=[rag_tool]\n    )\n)\nprint(response.text)\n```\n","use_cases":["List all RAG corpora and files in a project","Inspect metadata and structure of a specific RAG corpus","Retrieve grounded contexts for a given query","Generate content using retrieved contexts from a RAG corpus"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/google\/skills\/blob\/e3c591afe426ea585e4c21799e5199d6d47cd1be\/skills\/cloud\/agent-platform-rag-engine-management\/SKILL.md","source_commit":"e3c591afe426ea585e4c21799e5199d6d47cd1be","license":null,"version":null,"listing_approved_at":"2026-11-16","created_at":"2026-11-16T07:35:12.000000Z","updated_at":"2026-11-16T10:49:37.000000Z","seo_title":"Manage Agent Platform RAG Engine Corpora","seo_description":"Efficiently list, inspect, and retrieve contexts from Agent Platform RAG Engine corpora using the Google GenAI SDK.","seo_keywords":["RAG Engine Management","List RAG Corpora","Inspect RAG Corpus","Retrieve RAG Contexts","Generate Content with RAG"]}}