將 QueryData 整合至應用程式

本教學課程說明如何使用 Google Cloud 控制台,在與 MongoDB 相容的 Firestore 中設定及使用 QueryData,並將其整合至應用程式。瞭解如何建構脈絡資料集檔案、建立使用該檔案的脈絡資料集、透過 MCP Toolbox 呼叫 Conversational Analytics API 中的 QueryData 方法,為自然語言問題生成與 MongoDB 相容的查詢語言 (MQL) 查詢,並將其整合至應用程式。

詳情請參閱「QueryData 總覽」。

目標

  • 建立資料庫並填入資料。
  • 使用脈絡工程代理建立脈絡資料集檔案。
  • 建立脈絡資料集並上傳脈絡資料集檔案。
  • 在與 MongoDB 相容的 Firestore Studio 中,測試 QueryData 並生成 MQL 查詢。
  • 使用 MCP Toolbox 中的 Gemini 資料分析 QueryData 工具,將 QueryData 與應用程式整合。
  • 使用值搜尋查詢,為 LLM 回覆內容建立基準。

費用

在本文件中,您將使用下列 Google Cloud計費元件:

您可以使用 Pricing Calculator,根據預測用量估算費用。

初次使用 Google Cloud 的使用者可能符合免費試用期資格。

完成本文所述工作後,請刪除建立的資源,避免繼續計費。詳情請參閱「清除所用資源」。

事前準備

建立情境集前,請先完成下列前置作業。

啟用必要服務

為專案啟用下列服務:

準備 Firestore 資料庫

確認您有與 MongoDB 相容的 Firestore Enterprise 版資料庫。

如果您要試用本頁面的範例,建議您為此建立新的資料庫。詳情請參閱「建立及管理資料庫」。

所需角色和權限

  • 在專案層級將 roles/datastore.viewer 和 roles/geminidataanalytics.queryDataUser 角色授予 IAM 使用者。詳情請參閱「為專案新增 IAM 政策繫結」。

填入 flights 和 airports 集合

在本節中,您將使用提供的 MQL 指令碼填入 flights 和 airports 集合。

  1. 前往 Google Cloud 控制台的「Firestore」頁面。

    前往 Firestore

  2. 從清單中選取資料庫。舉例來說,如果您先前建立了新資料庫,請選取該資料庫。資料庫必須是與 MongoDB 相容的 Enterprise 版資料庫。

  3. 在導覽選單中,按一下「Firestore Studio」。

  4. 按一下「開啟新的 MQL 編輯器分頁」或「開啟新分頁」,開啟新分頁。

  5. 執行下列指令碼,填入 airports 集合。確認未選取「Read-only」(唯讀) 選項。

  6. 執行下列指令碼,填入 flights 集合。確認未選取「Read-only」(唯讀) 選項。

在 Firestore Studio 建立脈絡資料集

在本節中,請建立名為 flights-assistant 的內容集。這個情境組合不含任何上傳的情境組合檔案。

  1. 前往 Google Cloud 控制台的「Firestore」頁面。

    前往 Firestore

  2. 從清單中選取資料庫。資料庫必須是 MongoDB 相容模式的 Enterprise 版資料庫。

  3. 在導覽選單中,按一下「Firestore Studio」。

  4. 在「Explorer」窗格中,按一下「Context sets」旁邊的「更多動作」圖示 ()。

  5. 按一下「建立情境組合」。

  6. 在「脈絡資料集名稱」中輸入 flights-assistant。

  7. 點選「建立」。

在 Studio 測試 QueryData

在本節中,您將使用 flights-assistant 設定的背景資訊,以自然語言提問來生成 MQL 查詢。由於脈絡資料集沒有任何上傳的脈絡資料集檔案,即使在提問時提供脈絡 (例如 nighttime traffic),QueryData 仍會生成次佳查詢。

  1. 在「Explorer」窗格中,點選情境集旁邊的「更多動作」圖示 。
  2. 按一下「測試脈絡資料集」。
  3. 在查詢編輯器中,點選「Generate MQL using QueryData with: flights-assistant」。
  4. 輸入下列自然語言問題來生成 MQL 查詢,然後點選「生成」。

    Find flights from SFO to JFK.
    

    查看 MQL 查詢。請注意,QueryData 會為這個明確的問題生成正確的 MQL。

      db.flights.find({"departure_airport": "SFO", "arrival_airport": "JFK"})
    
  5. 在「使用 QueryData 和『flights-assistant』生成 MQL」視窗中,點選「編輯」。

  6. 輸入下列自然語言問題來生成 MQL 查詢,然後點選「更新」。

    Tell me flights that can help me beat nighttime traffic if traveling from New York
    

    資料庫無法理解「nighttime流量」一詞。這可能會導致系統無法生成 MQL 查詢,或生成的查詢忽略該字詞,如下列查詢所示。

    // The database schema does not contain information about traffic.
    // Returning all flights departing from New York airports.
    db.flights.find({
      $and: [
        {
          "departure_airport": {"$regex": "New York", "$options": "i"}
        },
        {
          "$or": [
            {"departure_time": {"$exists": true}},
            {"arrival_time": {"$exists": true}}
          ]
        }
      ]
    })
    

為脈絡資料集生成脈絡

在本節中,您將準備環境並建立結構定義檔案,提升結構定義集的查詢功能。

設定環境

您必須先準備環境,才能開始生成背景資訊。

如要設定環境,請按照下列步驟操作:

  1. 安裝 Antigravity CLI。詳情請參閱「開始使用 Antigravity CLI」。
  2. 安裝 Google Cloud CLI。
  3. 設定應用程式預設憑證 (ADC)。在終端機中執行下列指令,進行驗證並選取專案:

    gcloud auth application-default login
  4. 安裝 uv:這是 Python 套件和專案管理工具,也是脈絡工程代理的必要條件。如果系統尚未安裝 Python,uv 可以為您安裝。

  5. 安裝內容比對引擎代理程式:

    agy plugin install https://github.com/GoogleCloudPlatform/db-context-enrichment/tree/main/plugin
  6. 在終端機啟動 Antigravity CLI。

    agy
  7. 使用 MCP Toolbox 設定資料庫連線,以產生背景資訊。連線定義於 tools.yaml 設定檔中。

    如要在目前目錄中建立 tools.yaml 設定檔,請輸入類似以下的提示:

    Help me set up context generation for a Firestore database with MongoDB compatibility.
    

    按照技能提供的指示操作。如要進一步瞭解 tools.yaml 檔案,請參閱 MCP Toolbox 說明文件。

  8. 建立 tools.yaml 檔案後,請在 Antigravity CLI 執行下列指令,並按照指示重新載入 Toolbox MCP 伺服器,重新載入設定:

    /mcp
    1. 選取「db-context-engineering_toolbox」。

    2. 選取「Restart」。

  9. 確認 MCP 工具箱和情境工程代理外掛程式已連線,可供使用。

    /mcp

生成範本背景資訊

在上一節中,QueryData 無法辨識「nighttime traffic」一詞。為解決這個問題,請在本節的內容集檔案中,將該詞定義為 5:00 PM 到 7:00 PM 之間的流量。

如要產生範本內容,請按照下列步驟操作:

  1. 在 Antigravity CLI 輸入下列提示詞:

    Generate a template for the question: "Tell me flights that can help me beat nighttime traffic if traveling from New York" with the following MQL query:
    
    db.flights.aggregate([
      {
        "$lookup": {
          "from": "airports",
          "localField": "departure_airport",
          "foreignField": "iata",
          "as": "departure_airport_info",
          "pipeline": [{ "$project": { "_id": 0, "name": 1, "city": 1 } }]
        }
      },
      { "$unwind": { "path": "$departure_airport_info" } },
      {
        "$match": {
          "$expr": {
            "$and": [
              { "$eq": ["$departure_airport_info.city", "New York"] },
              {
                "$or": [
                  { "$lte": [{ "$hour": "$departure_time" }, 17] },
                  { "$gte": [{ "$hour": "$departure_time" }, 19] }
                ]
              }
            ]
          }
        }
      },
    ])
    
  2. 代理程式會將您的輸入內容轉換為特定格式,以提升內容集在各種使用者查詢中的效能。詳情請參閱「情境組合總覽」。

  3. 查看生成的查詢範本。您可以將查詢範本儲存為新的代理結構定義檔案,或附加至現有的代理結構定義檔案。

  4. 提示代理儲存範本:

    Save it to context_set.json.
    

    代理會在同一個目錄中建立檔案,並加入以下內容:

    {
      "templates": [
        {
          "nl_query": "Tell me flights that can help me beat nighttime traffic if traveling from New York",
          "sql": "db.flights.aggregate([\n  {\n    \"$lookup\": {\n      \"from\": \"airports\",\n      \"localField\": \"departure_airport\",\n      \"foreignField\": \"iata\",\n      \"as\": \"departure_airport_info\",\n      \"pipeline\": [{ \"$project\": { \"_id\": 0, \"name\": 1, \"city\": 1 } }]\n    }\n  },\n  { \"$unwind\": { \"path\": \"$departure_airport_info\" } },\n  {\n    \"$match\": {\n      \"$expr\": {\n        \"$and\": [\n          { \"$eq\": [\"$departure_airport_info.city\", \"New York\"] },\n          {\n            \"$or\": [\n              { \"$lte\": [{ \"$hour\": \"$departure_time\" }, 17] },\n              { \"$gte\": [{ \"$hour\": \"$departure_time\" }, 19] }\n            ]\n          }\n        ]\n      }\n    }\n  }\n])",
          "intent": "Tell me flights that can help me beat nighttime traffic if traveling from New York",
          "manifest": "Tell me flights that can help me beat nighttime traffic if traveling from a given city",
          "parameterized": {
            "parameterized_sql": "db.flights.aggregate([\n  {\n    \"$lookup\": {\n      \"from\": \"airports\",\n      \"localField\": \"departure_airport\",\n      \"foreignField\": \"iata\",\n      \"as\": \"departure_airport_info\",\n      \"pipeline\": [{ \"$project\": { \"_id\": 0, \"name\": 1, \"city\": 1 } }]\n    }\n  },\n  { \"$unwind\": { \"path\": \"$departure_airport_info\" } },\n  {\n    \"$match\": {\n      \"$expr\": {\n        \"$and\": [\n          { \"$eq\": [\"$departure_airport_info.city\", \"$1\"] },\n          {\n            \"$or\": [\n              { \"$lte\": [{ \"$hour\": \"$departure_time\" }, 17] },\n              { \"$gte\": [{ \"$hour\": \"$departure_time\" }, 19] }\n            ]\n          }\n        ]\n      }\n    }\n  }\n])",
            "parameterized_intent": "Tell me flights that can help me beat nighttime traffic if traveling from $1"
          }
        }
      ]
    }
    

將脈絡資料集檔案上傳至 QueryData

在本節中,您會將脈絡資料集檔案上傳至 QueryData,提升 QueryData 在資料庫生成 MQL 的能力。

如要上傳背景資訊,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Firestore」頁面。

    前往 Firestore

  2. 從清單中選取資料庫。資料庫必須是 MongoDB 相容模式的 Enterprise 版資料庫。

  3. 在導覽選單中,按一下「Firestore Studio」。

  4. 在「Explorer」窗格中,點選「Context sets」旁邊的「Actions」(動作) 圖示 ()。

  5. 按一下「編輯內容集」。

  6. 按一下「Upload context set file」部分的「Browse」,然後選取先前產生的情境集檔案。

  7. 按一下 [儲存]。

使用 QueryData 生成 MQL 查詢

在本節中,您將使用上傳的內容集檔案,以自然語言提問。這樣一來,您就能確認 QueryData 是否正確理解並套用「nighttime traffic」等字詞和其他相關詞組的定義。

如要生成 MQL 查詢,請按照下列步驟操作:

  1. 在「Explorer」窗格中,點選情境集旁邊的「更多動作」圖示 。
  2. 按一下「測試脈絡資料集」。
  3. 在查詢編輯器中,點選「Generate MQL using QueryData with: flights-assistant」。
  4. 輸入下列自然語言問題來生成 MQL 查詢,然後點選「生成」。

    Tell me flights that can help me beat nighttime traffic if traveling from New York
    

    生成的 MQL 查詢大致如下:

    // Tell me flights that can help me beat nighttime traffic if traveling from New York
    db.flights.aggregate([
      {
        "$lookup": {
          "from": "airports",
          "localField": "departure_airport",
          "foreignField": "iata",
          "as": "departure_airport_info",
          "pipeline": [{ "$project": { "_id": 0, "name": 1, "city": 1 } }]
        }
      },
      { "$unwind": { "path": "$departure_airport_info" } },
      {
        "$match": {
          "$expr": {
            "$and": [
              { "$eq": ["$departure_airport_info.city", "New York"] },
              {
                "$or": [
                  { "$lte": [{ "$hour": "$departure_time" }, 17] },
                  { "$gte": [{ "$hour": "$departure_time" }, 19] }
                ]
              }
            ]
          }
        }
      }
    ])
    

    這個問題與您新增至 QueryData 內容的問題相同。您會發現 QueryData 現在可以準確解讀「nighttime traffic」一詞。

    雖然脈絡來自特定問題,但 QueryData 會使用該脈絡,強化各種相似問題的 MQL 生成作業。

  5. 在「使用 QueryData 和『flights-assistant』生成 MQL」視窗中,點選「編輯」。

  6. 輸入下列類似問題來生成 MQL 查詢,然後點選「更新」。

    What are the flights that can help me avoid evening traffic if departing from Boston
    

    由於問題將「nighttime traffic」一詞替換為類似的字詞「evening traffic」,QueryData 會套用相同的解讀方式,為這個問題提供一致的答案。

    生成的 MQL 查詢大致如下:

    // What are the flights that can help me avoid evening traffic if departing from Boston
    db.flights.aggregate([
      {
        "$lookup": {
          "from": "airports",
          "localField": "departure_airport",
          "foreignField": "iata",
          "as": "departure_airport_info",
          "pipeline": [{ "$project": { "_id": 0, "name": 1, "city": 1 } }]
        }
      },
      { "$unwind": { "path": "$departure_airport_info" } },
      {
        "$match": {
          "$expr": {
            "$and": [
              { "$eq": ["$departure_airport_info.city", "Boston"] },
              {
                "$or": [
                  { "$lte": [{ "$hour": "$departure_time" }, 17] },
                  { "$gte": [{ "$hour": "$departure_time" }, 19] }
                ]
              }
            ]
          }
        }
      }
    ])
    

將 QueryData 與應用程式整合

在本節中,您將為航班搜尋應用程式建立資料代理。這個資料代理提供對話式介面,可存取您先前建立的 flights 和 airports 資料表。課程內容也說明如何使用 Agent Development Kit (ADK)、Gemini 資料分析 QueryData MCP 工具和脈絡資料集,建立這個資料代理並整合至應用程式,提升回覆品質。

  1. 下載 MCP Toolbox 1.14.0 以上版本。MCP Toolbox 會將資料代理公開為工具,供應用程式連線。MCP Toolbox 與您先前安裝的脈絡工程代理程式不同,後者會產生脈絡。

  2. 設定應用程式預設憑證 (ADC)。

    gcloud auth application-default login
  3. 找出您先前建立的情境集 ID。如要進一步瞭解如何找出內容集 ID,請參閱「找出內容集 ID」。

  4. 將下列內容附加至 tools.yaml 設定,即可使用 MCP Toolbox 連線至資料代理程式。詳情請參閱「Gemini 資料分析來源」和「Gemini 資料分析 QueryData 工具」。

    kind: source
    name: gda-api-source
    type: cloud-gemini-data-analytics
    projectId: "PROJECT_ID"
    ---
    kind: tool
    name: cloud_gda_query_tool
    type: cloud-gemini-data-analytics-query
    source: gda-api-source
    description: Use this tool to send natural language queries to the Gemini Data Analytics API and receive MongoDB-compatible query language (MQL), natural language answers, and explanations.
    location: "REGION_ID"
    context:
      datasourceReferences:
        firestoreReference:
          databaseReference:
            projectId: "PROJECT_ID"
            databaseId: "DATABASE_ID"
          agentContextReference:
            contextSetId: "CONTEXT_SET_ID"
    generationOptions:
      generateQueryResult: true
      generateNaturalLanguageAnswer: true
      generateExplanation: true
      generateDisambiguationQuestion: true
    

    更改下列內容:

    • PROJECT_ID: Google Cloud 專案 ID。
    • DATABASE_ID:要連線的資料庫名稱。
    • REGION_ID:資料庫所在的區域 (例如 us-central1)。
    • CONTEXT_SET_ID:脈絡資料集 ID。如要進一步瞭解如何找出內容集 ID,請參閱「找出內容集 ID」。
  5. 使用 tools.yaml 檔案執行 MCP Toolbox 伺服器。

    ./toolbox --config autoctx/tools.yaml
  6. 使用 MCP Toolbox 的 Python SDK,建立會叫用 Gemini 資料分析 QueryData 工具的 ADK 應用程式。如要進一步瞭解如何使用 MCP Toolbox 的 Python SDK,請參閱 Toolbox 快速入門導覽課程;如要瞭解 Python ADK,請參閱 ADK 快速入門導覽課程。

    1. 建立目錄來儲存應用程式,例如 flight-assistant-app。
    2. 將目錄變更為 flight-assistant-app 目錄。

      mkdir flight-assistant-app
      cd flight-assistant-app
    3. 在 flight-assistant-app 目錄下執行下列指令,建立虛擬環境並安裝必要元件。

      uv init
      uv add google-adk toolbox-core google-genai
    4. 設定 ADK 代理。

      1. 建立 ADK 代理。

        uv run adk create my_agent
      2. 選取 gemini-3.8-flash 以上版本。

      3. 選取「Google AI」,然後輸入 Gemini API 金鑰。如要進一步瞭解如何找出 API 金鑰,請參閱「使用 Gemini API 金鑰」。(或者,如果不需要免費試用,可以選取「Vertex AI」,並指定已啟用計費功能的 Google Cloud 專案)。

    5. 將 agent.py 檔案的內容替換為下列 Flight Data Assistant 範例應用程式程式碼。

      from typing import cast
      
      from google.adk.agents.llm_agent import Agent
      from google.adk.agents.llm_agent import ToolUnion
      
      from toolbox_core import ToolboxSyncClient
      
      TOOLBOX_URL = "http://127.0.0.1:5000"
      
      INSTRUCTION = """
      # ROLE
      You are a friendly and factual flight data assistant. Your goal is to help users find the best flights for their needs by providing accurate information with a helpful, professional tone.
      - use the Query Data Tool to answer the user's question, if the tool fails to generate a valid query, ask the user to clarify their question.
      
      # OPERATIONAL CONSTRAINTS
      - TOOL LIMITATION: You only have access to the Query Data Tool. Do not claim to have capabilities beyond what this tool provides.
      - TRANSPARENCY POLICY: Maintain a seamless user experience. Never mention that you are using a tool, querying a database, or generating MQL. Frame all responses as your own direct assistance.
      - SCOPE MANAGEMENT: If a user asks for something beyond your capabilities, politely state that you cannot perform that specific task. Guide the user towards what you can help with.
      
      # COMMUNICATION STYLE
      - Be concise and scannable when listing answers.
      - Maintain a helpful, professional persona.
      
      =====
      
      # QUERY DATA TOOL
      
      Inputs:
      1. query: A natural language formulation of a database query.
      
      Outputs: (all optional)
      1. disambiguation_question: Clarification questions or comments where the tool needs the users' input.
      2. generated_query: The generated query for the user query.
      3. intent_explanation: An explanation for why the tool produced `generated_query`.
      4. query_result: The result of executing `generated_query`.
      5. natural_language_answer: The natural language answer that summarizes the `query` and `query_result`.
      
      Usage guidance:
      1. If `disambiguation_question` is produced, then solicit the needed inputs from the user and try the tool with a new `query` that has the needed clarification.
      2. If `natural_language_answer` is produced, use `intent_explanation` and `generated_query` to see if you need to clarify any assumptions for the user.
      3. If the tool output indicates failure or empty results, explain that clearly using the provided reasoning.
      """
      
      client = ToolboxSyncClient(TOOLBOX_URL)
      
      mcp_tool = client.load_tool("cloud_gda_query_tool")
      
      root_agent = Agent(
          model="gemini-3.8-flash",  # Or newer.
          name="root_agent",
          instruction=INSTRUCTION,
          tools=cast(list[ToolUnion], [mcp_tool]),
      )
      
  7. 在 flight-assistant-app 目錄下執行下列指令,啟動應用程式並存取 ADK 網路伺服器 (http://127.0.0.1:8000)。

    uv run adk web --port 8000
  8. 輸入任意文字 (例如 hello),即可開始與服務專員互動。

    ADK 代理會回答一般問題,並呼叫必要的 MCP 工具。

  9. 輸入下列與航班相關的問題。

    How many flights depart from the west side?
    

    系統呼叫 MCP 工具來回答這個問題。不過,由於「the west」一詞模稜兩可,且未指定任何機場,MCP 工具會傳回消歧問題,代理程式會使用該問題建構回應。

    I cannot determine how many flights depart from the 'west side' as the database does not contain information about which airports are considered to be on the 'west side'. However, I can help you with questions like:
    
    1. How many flights depart from a specific airport?
    
    2. What are the departure airports for all flights?
    
    3. How many flights depart from each airport? Would you like to rephrase your question based on these options?
    
  10. 輸入與為代理程式生成的查詢範本類似的問題。

    Help me find flights from San Francisco that avoid the evening rush hour.
    

    根據先前新增的 QueryData 脈絡,MCP 工具會知道 evening traffic 發生在下午 5 點到 7 點之間。MCP 工具會傳回相關資料,供代理建構回覆。

    Here are the flights departing from San Francisco that avoid the evening rush hour (defined as 5 PM to 7 PM):
    
    * UA 1532 departing at 05:50:00
    * UA 1158 departing at 05:57:00
    * CY 922 departing at 06:38:00
    * OO 5441 departing at 07:08:00
    * UA 616 departing at 07:14:00
    * AA 24 departing at 07:14:00
    * B6 434 departing at 08:00:00
    * AA 242 departing at 08:18:00
    * UA 1739 departing at 08:22:00
    * OO 6336 departing at 08:32:00
    * US 1784 departing at 08:47:00
    * DL 1631 departing at 09:00:00
    * DL 1106 departing at 09:06:00
    * OO 5427 departing at 09:06:00
    * CY 352 departing at 09:25:00
    

反覆提升代理程式效能

您可以在 ADK 網頁 UI 中,查看 Gemini 資料分析 QueryData MCP 工具的要求和回應。您可以透過這項回覆觀察工具回覆,例如生成的 MQL 查詢、結果集、意圖說明、消歧問題和自然語言答案,確認代理回覆是否正確。

舉例來說,針對您先前輸入的文字 How many flights depart from the west side?,請點選代理泡泡。在左側導覽列的「Event」(事件) 分頁中,展開 functionResponse 即可查看下列回應。

"{"disambiguationQuestion": ["[NOT_ENOUGH_INFO] The database schema does not
contain information about which airports are on the 'west side'. Therefore, I
cannot determine how many flights depart from the west side.Possible alternative
questions: 1. How many flights depart from a specific airport? 2. What are the
departure airports for all flights? 3. How many flights depart from each
airport?"]}"

提升回覆準確度

您可以新增更多背景資訊,持續提升 Gemini 資料分析 QueryData 工具的回覆準確度。使用脈絡工程代理生成脈絡資料,然後將更新後的脈絡資料集檔案上傳至現有的 flights-assistant 資料代理。詳情請參閱「在 Firestore Studio 中管理情境集」。上傳後,控制台會立即擷取新的背景資訊,讓您在不中斷應用程式的情況下,提高代理的準確度。

多個代理

在開發環境中,您可以為 tools.yaml 檔案中的工具指派不同名稱,對多個資料代理程式環境執行 A/B 測試。舉例來說,您可以定義兩個名稱不同的 cloud-gemini-data-analytics-query 工具 (例如 cloud_gda_query_tool_v1 和 cloud_gda_query_tool_v2),建立不重複的 tools.yaml 設定。這樣一來,您就能實作應用程式邏輯,透過選擇對應的工具名稱,以程式輔助方式選取所需的內容版本。

以下範例 tools.yaml 說明如何為資料庫來源設定多個資料代理程式:

kind: source
name: gda-api-source
type: cloud-gemini-data-analytics
projectId: PROJECT_ID
---
kind: tool
name: cloud_gda_query_tool_v1
type: cloud-gemini-data-analytics-query
source: gda-api-source
context:
  datasourceReferences:
    DB_SOURCE:
      databaseReference: ...
      agentContextReference:
        contextSetId: "V1_YOUR_CONTEXT_SET_ID"
generationOptions: ...
---
kind: tool
name: cloud_gda_query_tool_v2
type: cloud-gemini-data-analytics-query
source: gda-api-source
context:
  datasourceReferences:
    DB_SOURCE:
      databaseReference: ...
      agentContextReference:
        contextSetId: "V2_YOUR_CONTEXT_SET_ID"
generationOptions: ...

更改下列內容:

  • PROJECT_ID: Google Cloud 專案 ID。
  • V1_YOUR_CONTEXT_SET_ID:版本 1 的情境集 ID。
  • V2_YOUR_CONTEXT_SET_ID:版本 2 的情境集 ID。

存取控管和安全性考量

在預先發布版中,與 MongoDB 相容的 Firestore QueryData 不支援精細存取控管。將 QueryData 與應用程式整合時,請考量資料存取權的控管方式:

  • 資料庫層級精細度:您可以使用資料庫層級的 IAM 條件,控管存取權精細度。不過,集合、文件或欄位層級沒有精細的存取控管機制。
  • 資料庫完整讀取權限:資料代理程式具備整個資料庫的讀取權限。代理生成的任何查詢,都能讀取該資料庫中的所有集合、文件和欄位。資料代理人不受 Firebase 安全性規則限制。
  • 應用程式使用者範圍:由於系統不支援精細的存取控管機制,您無法根據應用程式使用者身分,限制特定集合、文件或欄位的資料存取權。QueryData 產生的查詢會以應用程式使用的資料庫使用者或服務帳戶的完整讀取的權限執行。

清除所用資源

完成本教學課程後,請刪除您建立的資源,以免持續產生費用。下列各節將說明如何刪除這些資源。

刪除脈絡資料集

刪除資料庫前,請先刪除您建立的情境集。

  1. 前往 Google Cloud 控制台的「Firestore」頁面。

    前往 Firestore

  2. 從清單中選取資料庫。資料庫必須是處於 MongoDB 相容模式的 Enterprise 版資料庫。

  3. 在導覽選單中,按一下「Firestore Studio」。

  4. 在「Explorer」窗格中,點選情境集旁邊的「更多動作」圖示 。

  5. 在「Delete context set」(刪除情境集) 視窗的確認方塊中,輸入 flight-assistant。

  6. 按一下「確認」。

刪除資料庫

刪除「事前準備」一節中建立的資料庫時,您建立的所有物件也會一併刪除。

  1. 前往 Google Cloud 控制台的 Firestore 頁面。

    前往 Firestore

  2. 找出要刪除的資料庫名稱所在資料列。

  3. 按一下「動作」 () 圖示,然後按一下「刪除」。

  4. 輸入資料庫名稱,然後按一下「刪除」,確認要刪除資料庫。

後續步驟