使用 Vertex AI RAG Engine 將 Vertex AI Search 做為檢索後端

本頁面將介紹 Vertex AI Search 與 Vertex AI RAG Engine 的整合方式。

Vertex AI Search 提供解決方案,可在 Vertex AI RAG 應用程式中擷取及管理資料。使用 Vertex AI Search 做為檢索後端,可提升效能、可擴充性和整合便利性。

  • 提升效能和擴充性:Vertex AI Search 可處理大量資料,且延遲時間極低。這可縮短 RAG 應用程式的回應時間,並提高效能,特別是在處理複雜或廣泛的知識庫時。

  • 簡化資料管理:從各種來源 (例如網站、BigQuery 資料集和 Cloud Storage 儲存桶) 匯入資料,以便簡化資料擷取程序

  • 無縫整合:Vertex AI 提供內建的 Vertex AI Search 整合功能,可讓您選取 Vertex AI Search 做為 RAG 應用程式的字元集後端。這可簡化整合程序,並有助於確保元件之間的最佳相容性。

  • 提升 LLM 輸出品質:使用 Vertex AI Search 的檢索功能,有助於確保 RAG 應用程式可從語料庫中擷取最相關的資訊,進而產生更準確且資訊豐富的 LLM 輸出內容。

Vertex AI Search 結合了深度資訊檢索、自然語言處理,以及最新的大型語言模型 (LLM) 處理技術,有助於瞭解使用者意圖,並傳回最相關的結果。

有了 Vertex AI Search,您就能運用有權控管的資料,建構品質媲美 Google 的搜尋應用程式。

如要設定 Vertex AI Search,請按照下列步驟操作:

  1. 建立搜尋資料儲存庫
  2. 建立搜尋應用程式

使用 Vertex AI Search 做為 Vertex AI RAG Engine 的檢索後端

設定 Vertex AI Search 後,請按照下列步驟將其設為 RAG 應用程式的擷取後端。

將 Vertex AI Search 設為檢索後端,以建立 RAG 語料庫

這些程式碼範例說明如何將 Vertex AI Search 設為 RAG 語料庫的檢索後端。

REST

如要使用指令列建立 RAG 語料庫,請按照下列步驟操作:

  1. 建立 RAG 語料庫

    請替換程式碼範例中使用的下列變數:

    • PROJECT_ID: Google Cloud 專案的 ID。
    • LOCATION:處理要求的區域。
    • DISPLAY_NAME:您要建立的 RAG 語料庫顯示名稱。
    • ENGINE_NAME:Vertex AI Search 引擎或 Vertex AI Search Datastore 的完整資源名稱。
    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/ragCorpora" \
    -d '{
      "display_name" : "DISPLAY_NAME",
      "vertex_ai_search_config" : {
        "serving_config": "ENGINE_NAME/servingConfigs/default_search"
      }
    }'
    
  2. 監控進度

    請替換程式碼範例中使用的下列變數:

    • PROJECT_ID: Google Cloud 專案的 ID。
    • LOCATION:處理要求的區域。
    • OPERATION_ID:RAG 語料庫建立作業的 ID。
    curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID"
    

Python

在試用這個範例之前,請先按照 Vertex AI 快速入門:使用用戶端程式庫中的操作說明設定 Python。詳情請參閱 Vertex AI Python API 參考說明文件

如要向 Vertex AI 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# vertex_ai_search_engine_name = "projects/{PROJECT_ID}/locations/{LOCATION}/collections/default_collection/engines/{ENGINE_ID}"
# display_name = "test_corpus"
# description = "Corpus Description"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

# Configure Search
vertex_ai_search_config = rag.VertexAiSearchConfig(
    serving_config=f"{vertex_ai_search_engine_name}/servingConfigs/default_search",
)

corpus = rag.create_corpus(
    display_name=display_name,
    description=description,
    vertex_ai_search_config=vertex_ai_search_config,
)
print(corpus)
# Example response:
# RagCorpus(name='projects/1234567890/locations/us-central1/ragCorpora/1234567890',
# display_name='test_corpus', description='Corpus Description'.
# ...

使用 RAG API 擷取背景資訊

建立 RAG 語料庫後,您可以透過 RetrieveContexts API 從 Vertex AI Search 擷取相關的內容。

REST

本程式碼範例示範如何使用 REST 擷取內容。

請替換程式碼範例中使用的下列變數:

  • PROJECT_ID: Google Cloud 專案的 ID。
  • LOCATION:處理要求的區域。
  • RAG_CORPUS_RESOURCE:RAG 語料庫資源的名稱。

    格式: projects/{project}/locations/{location}/ragCorpora/{rag_corpus}.

  • TEXT:用於取得相關內容的查詢文字。
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts" \
  -d '{
    "vertex_rag_store": {
      "rag_resources": {
        "rag_corpus": "RAG_CORPUS_RESOURCE"
      }
    },
    "query": {
      "text": "TEXT"
    }
  }'

Python 適用的 Vertex AI SDK

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/[PROJECT_ID]/locations/us-central1/ragCorpora/[rag_corpus_id]"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

response = rag.retrieval_query(
    rag_resources=[
        rag.RagResource(
            rag_corpus=corpus_name,
            # Optional: supply IDs from `rag.list_files()`.
            # rag_file_ids=["rag-file-1", "rag-file-2", ...],
        )
    ],
    text="Hello World!",
    rag_retrieval_config=rag.RagRetrievalConfig(
        top_k=10,
        filter=rag.utils.resources.Filter(vector_distance_threshold=0.5),
    ),
)
print(response)
# Example response:
# contexts {
#   contexts {
#     source_uri: "gs://your-bucket-name/file.txt"
#     text: "....
#   ....

使用 Vertex AI Gemini API 生成內容

REST

如要使用 Gemini 模型產生內容,請呼叫 Vertex AI GenerateContent API。只要在要求中指定 RAG_CORPUS_RESOURCE,系統就會自動從 Vertex AI Search 擷取資料。

請替換範例程式碼中使用的下列變數:

  • PROJECT_ID: Google Cloud 專案的 ID。
  • LOCATION:處理要求的區域。
  • MODEL_ID:用於產生內容的大型語言模型。例如:gemini-2.0-flash
  • GENERATION_METHOD:用於產生內容的 LLM 方法。例如:generateContentstreamGenerateContent
  • INPUT_PROMPT:傳送至大型語言模型的文字,用於產生內容。請嘗試使用與 Vertex AI Search 中文件相關的提示。
  • RAG_CORPUS_RESOURCE:RAG 語料庫資源的名稱。格式:projects/{project}/locations/{location}/ragCorpora/{rag_corpus}
  • SIMILARITY_TOP_K:選用:要擷取的熱門情境數量。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD" \
    -d '{
      "contents": {
        "role": "user",
        "parts": {
          "text": "INPUT_PROMPT"
        }
      },
      "tools": {
        "retrieval": {
          "disable_attribution": false,
          "vertex_rag_store": {
            "rag_resources": {
                "rag_corpus": "RAG_CORPUS_RESOURCE"
              },
            "similarity_top_k": SIMILARITY_TOP_K
          }
        }
      }
    }'
    

Python 適用的 Vertex AI SDK

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件


from vertexai import rag
from vertexai.generative_models import GenerativeModel, Tool
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_retrieval_tool = Tool.from_retrieval(
    retrieval=rag.Retrieval(
        source=rag.VertexRagStore(
            rag_resources=[
                rag.RagResource(
                    rag_corpus=corpus_name,
                    # Optional: supply IDs from `rag.list_files()`.
                    # rag_file_ids=["rag-file-1", "rag-file-2", ...],
                )
            ],
            rag_retrieval_config=rag.RagRetrievalConfig(
                top_k=10,
                filter=rag.utils.resources.Filter(vector_distance_threshold=0.5),
            ),
        ),
    )
)

rag_model = GenerativeModel(
    model_name="gemini-2.0-flash-001", tools=[rag_retrieval_tool]
)
response = rag_model.generate_content("Why is the sky blue?")
print(response.text)
# Example response:
#   The sky appears blue due to a phenomenon called Rayleigh scattering.
#   Sunlight, which contains all colors of the rainbow, is scattered
#   by the tiny particles in the Earth's atmosphere....
#   ...

後續步驟