含後續查詢的搜尋

本頁面介紹 Vertex AI Search 的後續搜尋功能,並說明如何透過 API 呼叫實作這項功能。

如要在搜尋小工具中加入後續追問功能,請參閱「設定搜尋小工具的結果」。

後續追問搜尋功能適用於含有非結構化資料網站的搜尋應用程式。

關於含後續查詢的搜尋

後續追問功能採用生成式 AI 模型,「搜尋並追問」與一般非結構化資料搜尋不同,因為「搜尋並追問」會將同一搜尋工作階段中的先前查詢納入考量。

含後續查詢的搜尋支援下列功能:

  • 自然語言查詢處理:處理及理解人類語言輸入內容、判斷查詢背後的意圖,並傳回相關結果。

  • 情境感知:瞭解先前互動的情境,並提供情境感知回覆。

  • 多輪對話:使用者可以提出後續問題,並獲得相關回覆。

含後續查詢的搜尋範例

以下是含後續查詢的搜尋範例。假設你想瞭解在墨西哥度假的相關資訊:

  • 你:一年中什麼時候最適合到墨西哥度假?

  • 後續搜尋:墨西哥的乾季是 11 月到 4 月,最適合度假。

  • 你:匯率是多少?

  • 後續搜尋:1 美元約等於 17.65 墨西哥披索。

  • 你:12 月的平均溫度是多少?

  • 使用後續查詢搜尋:平均溫度為 21 到 26 攝氏度。坎昆的平均溫度約為 25°C。

如果使用一般搜尋,系統無法回答「匯率是多少?」這個問題,因為一般搜尋不知道您想查詢墨西哥的匯率。同樣地,一般搜尋不會保留脈絡,因此無法提供墨西哥的溫度。

關於對話

在後續追問的搜尋中,對話是由使用者提供的文字查詢,以及 Vertex AI Search 提供的回覆所組成。

這些查詢和回應組合有時稱為「訊息」。在上述範例中,第二則訊息是由「匯率是多少?」和「1 美元約等於 17.65 墨西哥披索」組成。

對話會儲存在非結構化資料所在的資料儲存庫中。在資料儲存庫中,對話會以 Conversation 資源表示。 除了查詢和回應訊息外,對話資源還包含:

  • 專屬名稱 (對話 ID)。

  • 狀態 (進行中或已完成)。

  • 使用者虛擬 ID,也就是追蹤使用者的訪客 ID。可透過程式輔助方式指派。

  • 開始時間和結束時間。

事前準備

請確認您符合下列必要條件。相關規定視應用程式類型而異。

儲存對話並取得回覆

您可以使用指令列或用戶端程式庫產生搜尋結果,並儲存搜尋後續追問的對話。

REST

如要使用指令列建立對話並根據使用者輸入內容生成回覆,請按照下列步驟操作:

  1. 指定要儲存對話記錄的資料儲存庫:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations" \
    -d '{
      "user_pseudo_id": "USER_PSEUDO_ID"
    }'
    
    • PROJECT_ID:您的 Google Cloud 專案編號或 ID

    • DATA_STORE_ID:與應用程式相關聯的資料儲存空間 ID。

    • USER_PSEUDO_ID:這是追蹤搜尋訪客的專屬 ID。舉例來說,您可以透過 HTTP Cookie 實作這項功能,在單一裝置上區分訪客。

    按一下即可查看 POST 指令的回覆範例。

    {
      "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/conversations/11078281986791420687",
      "state": "IN_PROGRESS",
      "userPseudoId": "test_id",
      "startTime": "2023-08-15T20:08:12.094639Z"
    }
  2. 生成搜尋回覆,並將其新增至資料儲存庫中的新對話或現有對話:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID:converse" \
    -d '{
      "query": { "input": "FREE_TEXT"},
      "filter": "FILTER"
    }'
    
    • PROJECT_ID:您的 Google Cloud 專案編號或 ID

    • DATA_STORE_ID:與應用程式相關聯的資料儲存空間 ID。

    • CONVERSATION_ID:對話的專屬 ID,例如 123456。如果是搜尋後續追問的對話,請在每一回合使用相同的對話 ID。

    • FREE_TEXT:包含使用者問題的任意文字字串,例如 what is bigquery?

    • FILTER:文字欄位,可使用篩選器運算式篩選搜尋結果。預設值為空字串。篩選器的建構方式會因您擁有網站資料或含有中繼資料的非結構化資料而異。詳情請參閱「使用後續追問篩選搜尋結果」。

    按一下即可查看 POST 指令的回覆範例。

    {
    "reply": {
    "summary": {
      "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data."
    }
    },
    "conversation": {
    "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/conversations/11078281986791420687",
    "state": "IN_PROGRESS",
    "userPseudoId": "test_id",
    "messages": [
      {
        "userInput": {
          "input": "what is bigquery?"
        }
      },
      {
        "reply": {
          "summary": {
            "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data."
          }
        }
      }
    ],
    "startTime": "2023-08-15T20:08:12.094639Z"
    },
    "searchResults": [
    {
      "id": "c86f19582746b56f71c9bb6929893835",
      "document": {
        "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/branches/0/documents/c86f19582746b56f71c9bb6929893835",
        "id": "c86f19582746b56f71c9bb6929893835",
        "derivedStructData": {
          "link": "gs://aquamuse-data-ucs-eval-dev/documents/94627ee0249dfdfda25b1b158c717bca.txt",
          "snippets": [
            {
              "snippet_status": "SUCCESS",
              "snippet": "For larger websites, talk to the IT team and/or utilize a big data solution such as Google \u003cb\u003eBigQuery\u003c/b\u003e to extract all URLs. It may also be necessary to ask the ..."
            }
          ],
          "extractive_answers": [
            {
              "content": "Alternatively, load the Server Log Files into Google BigQuery and use the Google BigQuery interface or the 360 Data Studio to analyze the data. To monitor the indexation numbers of the HTTP and the HTTPS version, keep an eye on all submitted XML Sitemaps."
            }
          ]
        }
      }
    },
    {
      "id": "774bd7ce2a3509ab4bbd1fc876f39dc2",
      "document": {
        "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/branches/0/documents/774bd7ce2a3509ab4bbd1fc876f39dc2",
        "id": "774bd7ce2a3509ab4bbd1fc876f39dc2",
        "derivedStructData": {
          "snippets": [
            {
              "snippet": "No snippet is available for this page.",
              "snippet_status": "NO_SNIPPET_AVAILABLE"
            }
          ],
          "extractive_answers": [
            {
              "content": "This consists of a collection of virtual tables. A virtual table exists for every queryable object type (content type if you prefer) in the repository. Each row in these virtual tables correspond to an instance of the corresponding object type (or of one of its subtypes)."
            }
          ],
          "link": "gs://aquamuse-data-ucs-eval-dev/documents/28841ef8590a105e9415f1390648a811.txt"
        }
      }
    },
    {
      "id": "3e1d306e49aefd9e23f2d5f7a66e6c76",
      "document": {
        "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/branches/0/documents/3e1d306e49aefd9e23f2d5f7a66e6c76",
        "id": "3e1d306e49aefd9e23f2d5f7a66e6c76",
        "derivedStructData": {
          "snippets": [
            {
              "snippet": "No snippet is available for this page.",
              "snippet_status": "NO_SNIPPET_AVAILABLE"
            }
          ],
          "extractive_answers": [
            {
              "content": "Other logo switches are based on search terms. For instance, if the term "ASCII art" is searched, an ASCII art version of the Google logo will appear next to the search box."
            }
          ],
          "link": "gs://aquamuse-data-ucs-eval-dev/documents/98008df3eef5d3ee1661c52f23189190.txt"
        }
      }
    },
    {
      "id": "cf94e24aacd47cd2c2f5effcbdeda832",
      "document": {
        "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/branches/0/documents/cf94e24aacd47cd2c2f5effcbdeda832",
        "id": "cf94e24aacd47cd2c2f5effcbdeda832",
        "derivedStructData": {
          "extractive_answers": [
            {
              "content": "The company is listed on the NASDAQ stock exchange under the ticker symbols GOOGL and GOOG, and on the Frankfurt Stock Exchange under the ticker symbol GGQ1. These ticker symbols now refer to Alphabet Inc., Google's holding company, since the fourth quarter of 2015."
            }
          ],
          "snippets": [
            {
              "snippet": "No snippet is available for this page.",
              "snippet_status": "NO_SNIPPET_AVAILABLE"
            }
          ],
          "link": "gs://aquamuse-data-ucs-eval-dev/documents/d80204083ef1096799fa4b7257548b33.txt"
        }
      }
    },
    {
      "id": "05bc6497a4e7e6ca36b2e495b354b764",
      "document": {
        "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/branches/0/documents/05bc6497a4e7e6ca36b2e495b354b764",
        "id": "05bc6497a4e7e6ca36b2e495b354b764",
        "derivedStructData": {
          "extractive_answers": [
            {
              "content": "SQL injection countermeasures are designed to utilize secure programming methods. By changing the variables used by the application code, weaknesses in applications can be greatly minimized. This report will detail how to perform a SQL injection and explore the best countermeasures to prevent the attack."
            }
          ],
          "link": "gs://aquamuse-data-ucs-eval-dev/documents/7cba75d646f5774a21d96801bec68bb3.txt",
          "snippets": [
            {
              "snippet_status": "NO_SNIPPET_AVAILABLE",
              "snippet": "No snippet is available for this page."
            }
          ]
        }
      }
    }
    ]
    }
  3. 針對對話中的每個新問題重複步驟 2。

Python

詳情請參閱 AI Applications Python API 參考說明文件

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

from typing import List

from google.api_core.client_options import ClientOptions
from google.cloud import discoveryengine_v1 as discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_LOCATION"                    # Values: "global", "us", "eu"
# data_store_id = "YOUR_DATA_STORE_ID"
# search_queries = ["YOUR_FIRST_SEARCH_QUERY", "YOUR_SECOND_SEARCH_QUERY"]


def multi_turn_search_sample(
    project_id: str,
    location: str,
    data_store_id: str,
    search_queries: List[str],
) -> List[discoveryengine.ConverseConversationResponse]:
    #  For more information, refer to:
    # https://cloud.google.com/generative-ai-app-builder/docs/locations#specify_a_multi-region_for_your_data_store
    client_options = (
        ClientOptions(api_endpoint=f"{location}-discoveryengine.googleapis.com")
        if location != "global"
        else None
    )

    # Create a client
    client = discoveryengine.ConversationalSearchServiceClient(
        client_options=client_options
    )

    # Initialize Multi-Turn Session
    conversation = client.create_conversation(
        # The full resource name of the data store
        # e.g. projects/{project_id}/locations/{location}/dataStores/{data_store_id}
        parent=client.data_store_path(
            project=project_id, location=location, data_store=data_store_id
        ),
        conversation=discoveryengine.Conversation(),
    )


    for search_query in search_queries:
        # Add new message to session
        request = discoveryengine.ConverseConversationRequest(
            name=conversation.name,
            query=discoveryengine.TextInput(input=search_query),
            serving_config=client.serving_config_path(
                project=project_id,
                location=location,
                data_store=data_store_id,
                serving_config="default_config",
            ),
            # Options for the returned summary
            summary_spec=discoveryengine.SearchRequest.ContentSearchSpec.SummarySpec(
                # Number of results to include in summary
                summary_result_count=3,
                include_citations=True,
            ),
        )
        response = client.converse_conversation(request)

        print(f"Reply: {response.reply.summary.summary_text}\n")

        for i, result in enumerate(response.search_results, 1):
            result_data = result.document.derived_struct_data
            print(f"[{i}]")
            print(f"Link: {result_data['link']}")
            print(f"First Snippet: {result_data['snippets'][0]['snippet']}")
            print(
                "First Extractive Answer: \n"
                f"\tPage: {result_data['extractive_answers'][0]['pageNumber']}\n"
                f"\tContent: {result_data['extractive_answers'][0]['content']}\n\n"
            )
        print("\n\n")

篩選含後續查詢的搜尋

使用「搜尋並追問」功能進行查詢時,您可以加入 filter 欄位,限制系統從哪些文件衍生回覆。您可以使用篩選運算式建構篩選器。使用的篩選運算式會因您擁有網站資料或含有中繼資料的非結構化資料而異。

網站資料的篩選器運算式

如果資料儲存庫包含網站資料,您可以使用進階網站索引建立功能中的篩選運算式,透過後續查詢篩選搜尋結果。建構篩選運算式後,請在「儲存對話並取得回覆」的步驟 2 中,將該運算式做為 filter 欄位的值。

含中繼資料的非結構化資料的篩選運算式

如果資料存放區含有中繼資料的非結構化資料,您可以透過後續查詢篩選搜尋結果,根據文件包含的中繼資料欄位傳回文件。請參閱「篩選結構化或非結構化資料的搜尋結果」,瞭解如何使用中繼資料篩選一般搜尋結果 (不含後續追問)。您可以使用相同的原則,透過後續追蹤的元資料篩選搜尋結果。建構篩選運算式後,請在「儲存對話並取得回覆」的步驟 2 中,將該運算式做為 filter 欄位的值。

設定摘要

搜尋結果的回覆訊息會是系統生成的摘要,並以 summaryText 形式傳回。您可以透過多種方式設定生成的摘要。以下各節將說明這些項目:

在搜尋結果中取得引用內容

如果指定引用內容,則為搜尋摘要中內嵌的數字。這些數字代表摘要中的特定句子是從哪個搜尋結果擷取而來。

如要取得引用內容:

  • 請按照上述「儲存對話並取得回覆」程序操作,但請在步驟 2 執行下列指令,其中包含將 includeCitations 設為 true 的 summarySpec 欄位。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID:converse" \
    -d '{
      "query": { "input": "FREE_TEXT"},
      "summarySpec": { "include_citations": true }
    }'
    

    按一下範例指令的回應部分。

    {
    "reply": {
    "summary": {
    }
    "reply": {
    "summary": {
      "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly [1]. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data [2, 3].",
      "safetyAttributes": {
        "categories": [
          "Finance",
          "Legal"
        ]

摘要文字會包含引用編號。引用編號是指傳回的搜尋結果,並以 1 為索引。舉例來說,[1] 表示該句子歸因於第一個搜尋結果。[2, 3] 表示該句子歸因於第二和第三個搜尋結果。

忽略對抗查詢

這類查詢包括負面留言,或旨在生成不安全/違反政策的輸出內容。您可以指定不針對對抗性查詢傳回任何搜尋摘要。如果系統忽略惡意查詢,summaryText 屬性會包含公式化文字,指出系統未傳回任何搜尋摘要。即使搜尋摘要未傳回,系統仍會針對對抗性查詢傳回搜尋文件。

如要指定不為對抗性查詢傳回搜尋摘要,請執行下列操作:

  • 請按照上述「儲存對話並取得回覆」程序操作,但請在步驟 2 執行下列指令,其中包含將 ignoreAdversarialQuery 設為 true 的 summarySpec 欄位。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID:converse" \
    -d '{
      "query": { "input": "FREE_TEXT"},
      "summarySpec": { "ignoreAdversarialQuery": true }
    }'
    

    按一下來自對抗性查詢的回覆部分內容。

    "reply": {
    "summary": {
      "summaryText": "A summary could not be generated for your search query. Here are some search results.",
      "summarySkippedReasons": [
        "ADVERSARIAL_QUERY_IGNORED"
      ]

忽略非摘要查詢

如果查詢並非尋找摘要,系統會傳回不適合用於摘要的結果。舉例來說,「為什麼天空是藍的」和「誰是世界上最厲害的足球員?」是尋求摘要的查詢,但「舊金山國際機場」和「2026 年世界盃」則不是。這類查詢最有可能屬於導覽查詢。您可以指定不為非摘要搜尋查詢傳回搜尋摘要。即使搜尋摘要未傳回,系統仍會針對非摘要搜尋查詢傳回搜尋文件。

如要指定不為非摘要搜尋查詢傳回搜尋摘要,請執行下列操作:

  • 請按照上述「儲存對話並取得回覆」程序操作,但請在步驟 2 執行下列指令,其中包含將 ignoreNonSummarySeekingQuery 設為 true 的 summarySpec 欄位。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID:converse" \
    -d '{
      "query": { "input": "FREE_TEXT"},
      "summarySpec": { "ignore_non_summary_seeking_query": true }
    }'
    

    按一下來自對抗性查詢的回覆部分內容。

     "reply": {
    "summary": {
      "summaryText": "A summary could not be generated for your search query. Here are some search results.",
      "summarySkippedReasons": [
        "NON_SUMMARY_SEEKING_QUERY_IGNORED"
      ]

設定摘要中要使用的結果數量

您可以指定要用於生成摘要的結果數量。根據預設,如果未設定 summaryResultCount 欄位,系統會使用傳回的所有結果產生摘要。

如要設定摘要使用的最相關結果數量,請按照下列步驟操作:

  • 請按照先前的「儲存對話並取得回覆」程序操作,但請在步驟 2 執行這個指令,其中包含設定 summaryResultCountsummarySpec 欄位。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID:converse" \
    -d '{
      "query": { "input": "FREE_TEXT"},
      "summarySpec": { "summary_result_count": NUMBER_OF_RESULTS }
    }'
    

    NUMBER_OF_RESULTS:用來生成搜尋摘要的頂端結果數量。如果傳回的結果數少於 summaryResultCount,系統會根據所有結果生成摘要。最大值為 5

取得自訂摘要

要求摘要時,您可以在 modelPromptSpec.preamble 欄位中提供自然語言指示,取得自訂摘要。

你可以要求自訂長度、詳細程度、輸出內容的風格 (例如「簡單」)、輸出內容的語言、答案的重點和格式 (例如表格、項目符號和 XML)。

舉例來說,如果查詢是「What are the benefits of bigquery?」(BigQuery 的優點是什麼?),您可以輸入下列自訂指令:

  • 範例指令:"please show the answer format in an ordered list"

    回答範例:

    The benefits of BigQuery are:
    
    1. Increased business productivity
    2. IT staff productivity
    3. Reduced infrastructure costs
    4. Loading and exporting data are free
    
  • 範例指令:give a very detailed answer

    回答範例:

    BigQuery is a serverless, highly scalable, and cost-effective cloud data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that takes care of provisioning, scaling, and managing the underlying infrastructure, so you can focus on analyzing your data. BigQuery is also integrated with other Google Cloud services, such as Google Kubernetes Engine, Cloud Data Fusion, and Cloud Dataproc, making it easy to build and deploy data pipelines.
    
    Here are some of the benefits of using BigQuery:
    
    * **Fast and scalable:** BigQuery can process petabytes of data very quickly, and it can scale to handle even the most demanding workloads. * **Cost-effective:** BigQuery is a very cost-effective way to store and analyze data. You only pay for the data that you use, and there are no upfront costs or commitments. * **Secure:** BigQuery is a secure platform that meets the needs of even the most security-conscious organizations. * **Easy to use:** BigQuery is easy to use, even for non-technical users. It has a simple and intuitive user interface, and it supports a variety of data sources. * **Integrated with other Google Cloud services:** BigQuery is integrated with other Google Cloud services, making it easy to build and deploy data pipelines.
    
    If you are looking for a fast, scalable, and cost-effective way to analyze your data, then BigQuery is a great option.
    

如要取得自訂摘要,請按照下列步驟操作:

  • 按照上述「儲存對話並取得即時通訊回覆」程序操作,但請在步驟 2 執行下列指令,其中包含 summarySpec 欄位,用於指定 modelPromptSpec.preamble 中的自訂指示。

      curl -X POST \
      -H "Authorization: Bearer $(gcloud auth print-access-token)" \
      -H "Content-Type: application/json" \
      "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID:converse" \
      -d '{
        "query": { "input": "FREE_TEXT"},
        "summarySpec": {
          "modelPromptSpec": {
            "preamble": "CUSTOMIZATION_INSTRUCTIONS"
          }
        }
      }'
    
    • CUSTOMIZATION_INSTRUCTIONS:自訂指示,以字串表示。

安全搜尋功能可濾除摘要回覆中的情色露骨、不安全或違反政策的內容。如要進一步瞭解安全搜尋,請參閱「Vertex AI Search 的安全設定」。

如要對對話回覆套用安全搜尋功能,請按照下列步驟操作:

  • 按照先前的「儲存對話並取得即時通訊回覆」程序操作,但步驟 2 例外,請在查詢下方指定 safe_search

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID:converse" \
    -d '{
      "query": { "input": "FREE_TEXT"},
      "safe_search": true
    }'
    

查看及修改已儲存的對話

您可以使用指令列取得、刪除、更新及列出已儲存的對話。

從資料儲存庫取得對話

如要從資料存放區取得特定對話的所有詳細資料:

  • 執行下列 curl 指令:

    curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID"
    
    • PROJECT_ID:您的 Google Cloud 專案編號或 ID

    • DATA_STORE_ID:與應用程式相關聯的資料儲存空間 ID。

    • CONVERSATION_ID:對話的 ID

    按一下即可查看 GET 指令的回覆範例。

    {
    "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/conversations/2040473575290303058",
    "state": "IN_PROGRESS",
    "userPseudoId": "2040473575290303058",
    "messages": [
    {
      "userInput": {
        "input": "what is bigquery?"
      }
    },
    {
      "reply": {
        "summary": {
          "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data."
        }
      }
    }
    ],
    "startTime": "2023-08-15T20:11:24.046735Z"
    }

從資料儲存庫刪除對話

系統預設會自動刪除 60 天前的對話。不過,如果您想刪除特定對話 (例如對話中意外含有私密內容),可以使用這個 API 呼叫刪除對話。

如要從資料儲存庫刪除對話:

  • 執行下列 curl 指令:

    curl -X DELETE \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID"
    
    • PROJECT_ID:您的 Google Cloud 專案編號或 ID

    • DATA_STORE_ID:與應用程式相關聯的資料儲存空間 ID。

    • CONVERSATION_ID:對話的 ID

    DELETE 指令的回應如下所示:

    {}
    

更新對話

您可能會基於各種原因而想更新對話。舉例來說,如要執行下列任一操作:

  • 將會話群組標示為完成

  • 將一個對話中的訊息併入另一個對話

  • 變更 user_pseudo_id

如要更新對話中的 state

  • 執行下列 curl 指令:

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID?updateMask=state" \
    -d '{
      "state": "NEW_STATE"
    }'
    
    • PROJECT_ID:您的 Google Cloud 專案編號或 ID

    • DATA_STORE_ID:與應用程式相關聯的資料儲存空間 ID。

    • CONVERSATION_ID:要更新的對話 ID

    • NEW_STATE:狀態的新值,例如: COMPLETED

    按一下即可查看 PATCH 指令的回覆範例。

    {
    "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/conversations/11078281986791420687",
    "state": "COMPLETED",
    "userPseudoId": "test_id1",
    "messages": [
    {
      "userInput": {
        "input": "what is bigquery?"
      }
    },
    {
      "reply": {
        "summary": {
          "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data."
        }
      }
    }
    ],
    "startTime": "2023-08-15T20:08:12.094639Z"
    }

如要更新對話中的 user_pseudo_id

  • 執行下列 curl 指令:

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations/CONVERSATION_ID?updateMask=user_pseudo_id" \
    -d '{
      "user_pseudo_id": "NEW_USER_PSEUDO_ID"
    }'
    
    • PROJECT_ID:您的 Google Cloud 專案編號或 ID

    • DATA_STORE_ID:與應用程式相關聯的資料儲存空間 ID。

    • CONVERSATION_ID:要更新的對話 ID

    • NEW_USER_PSEUDO_ID:使用者虛擬 ID 的新值

    按一下即可查看 PATCH 指令的回覆範例。

    {
    "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/conversations/11078281986791420687",
    "state": "IN_PROGRESS",
    "userPseudoId": "test_id1",
    "messages": [
    {
      "userInput": {
        "input": "what is bigquery?"
      }
    },
    {
      "reply": {
        "summary": {
          "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data."
        }
      }
    }
    ],
    "startTime": "2023-08-15T20:08:12.094639Z"
    }

上述指令說明如何變更 user_pseudo_id.,但您可以將 user_pseudo_id 替換為對話資源中的其他欄位,藉此更新對話中的其他欄位。

列出所有對話

如要列出資料存放區中的所有對話:

  • 執行下列 curl 指令:

    curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations"
    
    • PROJECT_ID:您的 Google Cloud 專案編號或 ID

    • DATA_STORE_ID:與應用程式相關聯的資料儲存空間 ID。

    按一下即可查看 GET 指令的回覆範例。

    {
    "conversations": [
    {
      "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/conversations/11078281986791420687",
      "state": "IN_PROGRESS",
      "userPseudoId": "test_id",
      "messages": [
        {
          "userInput": {
            "input": "what is bigquery?"
          }
        },
        {
          "reply": {
            "summary": {
              "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data."
            }
          }
        }
      ],
      "startTime": "2023-08-15T20:08:12.094639Z"
    },
    {
      "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/conversations/2040473575290303058",
      "state": "IN_PROGRESS",
      "userPseudoId": "2040473575290303058",
      "messages": [
        {
          "userInput": {
            "input": "what is bigquery?"
          }
        },
        {
          "reply": {
            "summary": {
              "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data."
            }
          }
        }
      ]
    }
    ]
    }

回覆會列出對話和 next_page_token。如果未傳回任何 next_page_token,表示沒有更多對話可列出。

預設頁面大小為 50。

依篩選條件列出對話

您可能不想列出資料存放區中的所有對話,而是想列出所有開啟的對話,或是與特定使用者相關聯的所有對話。

舉例來說,您可以向使用者顯示已關閉的搜尋結果,並提供重新開啟的選項。

如要執行這項操作,請列出符合指定篩選條件的對話: user_pseudo_idstate (IN_PROGRESSCOMPLETED)。

列出使用者的對話

如要列出與使用者或訪客相關聯的對話:

  • 執行下列 curl 指令:

    curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations?filter=user_pseudo_id=USER_PSEUDO_ID"
    
    • PROJECT_ID:您的 Google Cloud 專案編號或 ID

    • DATA_STORE_ID:與應用程式相關聯的資料儲存空間 ID。

    • USER_PSEUDO_ID:要列出對話的使用者虛擬 ID。

    GET 指令的回應如下:

    按一下即可查看 GET 指令的回覆範例。

    {
    "conversations": [
    {
      "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/conversations/11078281986791420687",
      "state": "IN_PROGRESS",
      "userPseudoId": "test_id",
      "messages": [
        {
          "userInput": {
            "input": "what is bigquery?"
          }
        },
        {
          "reply": {
            "summary": {
              "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data."
            }
          }
        }
      ],
      "startTime": "2023-08-15T20:08:12.094639Z"
    }
    ]
    }

列出使用者和狀態的對話

如要列出特定狀態 (開啟或關閉) 與使用者或訪客相關聯的對話:

  • 執行下列 curl 指令:

    curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/collections/default_collection/dataStores/DATA_STORE_ID/conversations?filter=user_pseudo_id=USER_PSEUDO_ID%20AND%20state=STATE"
    
    • PROJECT_ID:您的 Google Cloud 專案編號或 ID

    • DATA_STORE_ID:與應用程式相關聯的資料儲存空間 ID。

    • USER_PSEUDO_ID:要列出對話的使用者虛擬 ID。

    • STATE:對話是否已開啟或關閉 (IN_PROGRESSCOMPLETED)

    GET 指令的回應如下:

    按一下即可查看 GET 指令的回覆範例。

    {
    "conversations": [
    {
      "name": "projects/12345/locations/global/collections/default_collection/dataStores/my-data-store_4321/conversations/11078281986791420687",
      "state": "IN_PROGRESS",
      "userPseudoId": "test_id",
      "messages": [
        {
          "userInput": {
            "input": "what is bigquery?"
          }
        },
        {
          "reply": {
            "summary": {
              "summaryText": "BigQuery is a cloud-based data warehouse that enables businesses to analyze all their data very quickly. It is a fully managed service that provides a simple and cost-effective way to store and analyze large amounts of data."
            }
          }
        }
      ],
      "startTime": "2023-08-15T20:08:12.094639Z"
    }
    ]
    }

如需篩選語法的一般資訊,請參閱 AIP-160 篩選

「相關問題」是允許清單功能,屬於預覽版,除了搜尋結果外,還會傳回相關問題。

舉例來說,當你詢問「一年中什麼時候最適合到墨西哥度假?」時,除了回答你的問題,搜尋結果還會建議你可能想問的其他問題,例如「一年中哪個月到墨西哥度假最便宜?」和「墨西哥的旅遊旺季是哪幾個月?」。

如要讓搜尋應用程式傳回相關問題,請與 Google 帳戶團隊聯絡,並告知要為哪些專案和應用程式啟用相關問題。如果未使用預設的供應設定,您也需要提供供應設定的名稱。

啟用相關問題功能後,系統會以 ConverseConversationResponse 中的字串形式傳回問題。

更多資訊

  • 如要進一步瞭解 summaryResultCountincludeCitationsignoreAdversarialQueryignoreNonSummarySeekingQuery 欄位,請參閱 AI 應用程式 API 說明文件中的 SummarySpec

  • 如需更多取得搜尋摘要的範例,請參閱「取得摘要」。