跟进搜索

本页面介绍了 Agent Search 的后续搜索功能,并展示了如何使用 API 调用来实现该功能。

如果您想向搜索微件添加后续搜索功能,请参阅为搜索微件配置结果。

跟进搜索适用于具有非结构化数据和网站的搜索应用。

关于跟进搜索

“搜索并跟进”功能基于生成式 AI 模型。带后续问题的搜索与常规非结构化数据搜索不同,因为带后续问题的搜索会考虑同一搜索会话中的先前查询。

跟进搜索支持以下功能:

  • 自然语言查询处理:它会处理和理解人类语言输入,识别查询背后的意图,并返回相关结果。

  • 上下文感知:它能理解先前互动的上下文,并提供上下文感知型回答。

  • 多轮对话:允许用户提出后续问题并获得相关回答。

跟进搜索示例

以下是跟进搜索的示例。假设您想了解在墨西哥度假的相关信息:

  • 您:一年中什么时候最适合去墨西哥度假?

  • 搜索并提出后续问题:去墨西哥度假的最佳时间是旱季,即 11 月至次年 4 月。

  • 您:汇率是多少?

  • 跟进搜索:1 美元约等于 17.65 墨西哥比索。

  • 您:12 月的平均气温是多少?

  • 跟进搜索:平均气温在 70-78°F 之间。 坎昆的平均气温约为 77°F。

如果使用常规搜索,您的问题“汇率是多少?”将无法回答,因为常规搜索不知道您想要的是墨西哥汇率。同样,常规搜索不会保留上下文来为您提供墨西哥的气温。

对话简介

在后续搜索中,对话由用户提供的文本查询和 Agent Search 提供的回答组成。

这些查询和回答对有时称为消息。在前面的示例中,第二条消息由“What is the exchange rate?”和“1 USD is equal to approximately 17.65 Mexican pesos.”组成。

对话存储在保存非结构化数据的同一数据存储区中。在数据存储区中,对话由对话资源表示。除了包含查询和响应消息之外,对话资源还具有:

  • 唯一名称(对话 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:这是用于跟踪搜索访问者的唯一标识符。例如,您可以通过 HTTP Cookie 实现此目的,该 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

如需了解详情,请参阅 Agent Search Python API 参考文档。

如需向 Agent Search 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。

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"
      ]
    …

忽略非摘要求取查询

非摘要寻求查询会返回不适合总结的结果。例如,“为什么天空是蓝色的”和“谁是世界上最优秀的足球运动员?”是寻求摘要的查询,但“SFO 机场”和“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 步中,运行此命令,其中包含设置 summaryResultCount 的 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": { "summary_result_count": NUMBER_OF_RESULTS }
    }'
    

    NUMBER_OF_RESULTS:用于生成搜索摘要的热门结果数量。如果返回的结果数量少于 summaryResultCount,则系统会根据所有结果生成摘要。 最大值为 5。

获取自定义摘要

您可以在请求摘要时,通过在 modelPromptSpec.preamble 字段中提供自然语言指令来获取自定义摘要。

您可以请求自定义,例如长度和详细程度、输出样式(例如“简单”)、输出语言、回答重点和格式(例如表格、项目符号和 XML)。

例如,对于查询“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:自定义指令,以字符串形式提供。

安全搜索功能可用于从摘要回答中滤除露骨、不安全、违反政策的输出内容。如需详细了解安全搜索,请参阅代理搜索的安全设置。

如需将安全搜索应用于对话回答,请执行以下操作:

  • 按照前面的存储对话并获取聊天回答过程执行操作,但在第 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_id 或 state(IN_PROGRESS 或 COMPLETED)。

列出用户的对话

如需列出与用户或访问者关联的对话,请执行以下操作:

  • 运行以下 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_PROGRESS 或 COMPLETED)

    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 中返回。

更多信息

  • 如需详细了解 summaryResultCount、includeCitations、ignoreAdversarialQuery、ignoreNonSummarySeekingQuery 字段,请参阅 Agent Search API 文档中的 SummarySpec。

  • 如需查看更多获取搜索摘要的示例,请参阅获取摘要。