智能撰写

智能撰写功能可在人工客服与最终用户进行文字聊天时提供帮助。它会建议短语来补全人工客服开始撰写的句子。您可以使用此功能为代理的聊天界面实现自定义建议功能。

人工客服在聊天界面中输入内容时收到文本建议。 本指南将引导您通过直接调用 API 来实现智能撰写功能。如果您愿意,可以使用 Agent Assist 控制台训练模型,并使用模拟器测试其性能。如需相关说明,请参阅 智能撰写模拟器教程

如需了解有关将智能撰写功能与您自己的系统集成以及提高建议质量的指南,请参阅智能撰写最佳实践文档

准备工作

在阅读本指南之前,请先完成以下操作:

  1. 为您的项目启用 Dialogflow API

个人身份信息和儿童数据

在您向此 API 发送数据时,该 API 会尝试隐去所有个人身份信息 (PII)。如果您需要确保模型不包含 PII,则应在向 API 发送数据之前先进行清理。将已遮盖的字词替换为占位符(例如 `REDACTED_NUMBER` 或 `REDACTED_NAME`),而不是直接移除这些字词。

此外,如果数据包含从儿童收集的信息,则应在向 API 发送儿童的数据之前先进行移除。

训练和部署模型

Agent Assist 智能回复模型使用对话数据集进行训练。对话数据集包含您自己上传的转录数据。本部分将引导您完成以下流程:创建对话数据集、将对话数据上传到该数据集,以及训练和部署模型。如果您不希望直接调用 API,还可以使用 Agent Assist 控制台来执行这些操作。

创建对话数据集

在开始上传对话转写内容之前,您必须先创建一个对话数据集来放入转写内容。对 ConversationDataset 资源调用 create 方法,以创建对话数据集。

响应包含对话数据集 ID。

将对话转录内容导入对话数据集

将聊天对话数据上传到对话数据集,以便 Agent Assist 对其进行处理。 确保每段对话的转写内容都采用 JSON 格式,并存储在 Cloud Storage 存储桶中。 一个对话数据集必须至少包含 30,000 个对话,否则模型训练将失败。一般来说,对话数量越多,模型质量就越好。我们建议您上传至少 3 个月的对话,以充分涵盖尽可能多的使用场景。一个对话数据集中的消息数量上限为 100 万。

ConversationDataset 资源调用 importConversationData 方法,以导入对话。

必填字段:

  • 您之前创建的对话数据集 ID。
  • inputConfig 路径指向 Cloud Storage 存储桶中的对话转写数据。

该响应是一个长时间运行的操作,您可以轮询该操作以检查是否完成。

创建对话模型

ConversationModel 资源调用 create 方法,以创建对话模型。此操作还会创建模型的许可名单

必填字段:

  • datasets 中,使用您之前创建的对话数据集 ID 提供单个数据集。
  • smartReplyModelMetadata 设置为空对象,或填充该字段以替换默认值。

该响应是一个长时间运行的操作,您可以轮询该操作以检查是否完成。完成后,模型 ID 和许可名单 ID 将包含在相应操作的元数据中。

  • 模型 ID:name
  • 许可名单 ID:smart_reply_model_metadata.associated_allowlist_info.document

部署对话模型

ConversationModel 资源调用 deploy 方法,以部署对话模型。

必填字段:

  • 使用 conversationModels:输入您之前创建的对话模型 ID。

管理许可名单

每个模型都关联有一个许可名单,该名单会在您创建对话模型时自动创建。许可名单包含在运行时可向人工客服显示的所有基于对话数据集生成的回答。本部分介绍了如何创建和管理许可名单。如果您不希望直接调用 API,还可以使用 Agent Assist 控制台来执行这些操作。

将许可名单内容导出到 CSV 文件

创建模型时,系统会自动创建一个与新模型相关联的许可名单。许可名单是具有唯一 ID 的 Document 资源。创建模型时,系统会在 smart_reply_model_metadata.associated_allowlist_info.document 中返回该 ID。如需查看并更改许可名单中的消息,您必须将其导出到 Cloud Storage 存储桶。

Document 资源调用 export 方法,以将文档导出到 Cloud Storage 存储桶中的 CSV 文件。 smart_messaging_partial_update 字段是可选的,但会影响您日后更新此许可名单的方式。如果设置为 true,导出的 CSV 文件将包含一个列,其中包含每条消息的唯一 ID。您可以使用消息 ID 仅更新指定的消息,而不是整个文档。如果 smart_messaging_partial_update 设置为 false 或未设置,则文件中不会显示额外的列,并且对许可名单的任何更新都需要更新整个文档。

必填字段:

  • gcsDestination 路径指向您的 Cloud Storage 存储桶。

该响应是一个长时间运行的操作,您可以轮询该操作以检查是否完成。之后,您在请求中提供的 CSV 文件会填充候选响应。

审核许可清单

生成的许可名单包含智能回复根据您的对话数据自动生成的回复。现在,您可以查看并根据需要更新这些回答。从 Cloud Storage 存储桶下载 CSV 文件,根据需要进行修改,然后将该文件上传回 Cloud Storage 存储桶。只有许可名单中的回答可以显示给人工客服。

如果您要修改任何回答,我们建议您仅修改拼写和语法,不要更改消息的含义。修改后的文本与模型中的含义偏差越大,相应消息就越不可能显示。

您也可以根据需要创建新消息。与修改后的消息类似,创建的消息不太可能在运行时呈现。

更新许可名单

完成 CSV 文件的更新后,您可以使用该文件来更新 Document 资源。您可以选择更新整个许可名单,也可以仅更新指定的消息。如需仅更新指定的消息,您必须在导出许可名单时将 smart_messaging_partial_update 设置为 true。如果您已完成此操作,请使用导出的 CSV 文件中自动生成的列来指明要更新的消息。

Document 资源调用 reload 方法以更新许可名单。如需仅更新指定的消息,请在 ReloadDocumentRequest 中将 smart_messaging_partial_update 设置为 true。如需更新整个许可名单,请将 smart_messaging_partial_update 设置为未设置状态或设置为 false

必填字段:

  • gcsSourceCSV 文件的 Cloud Storage 路径。
  • 对于 name,请使用您创建对话模型时生成的许可名单资源名称。

示例请求:

{
  "name":"projects/project-id/knowledgeBases/knowledge-base-id/documents/allowlist-id",
  "gcsSource" {
      "uri": "gs://revised_smart_reply_allowlist_path"
   }
}

评估训练后模型的性能

部署模型并为其创建许可名单后,您可以测试模型的性能。您还必须提供测试数据集。系统会将训练后的智能回复模型及其关联的许可名单生成的回答与测试数据集中的实际客服人员消息进行比较。测试数据集应由真实对话数据组成,但不得包含用于训练模型的对话数据集中的任何数据。例如,假设您有 1 个月的对话流量,您可以使用 3 周的对话数据来创建对话数据集,并使用剩余 1 周的数据来创建测试数据集。测试数据集应至少包含 1, 000 个对话,但一般来说,测试数据集中的对话越多,评估指标就越可靠。测试数据集格式与对话数据集格式相同。

如需创建新的模型评估,请对 ConversationModel 资源调用 CreateConversationModelEvaluation 方法。此方法会返回长时间运行的操作。您可以轮询操作以检查其状态,该状态将返回 INITIALIZINGRUNNINGSUCCEEDEDCANCELLEDFAILED 中的一个。

必填字段:

  • InputDataset:将用于测试模型性能的测试数据集。
  • allowlist_document:与要测试的智能回复模型相关联的许可名单。

当长时间运行的操作完成后,系统会返回 ConversationModelEvaluation 资源。其中包括以下两个指标:

  • allowlist_coverage:测试数据集中受许可名单覆盖的代理消息所占的百分比。
  • recall:测试数据集中包含在许可名单中出现在智能回复模型显示的前 3 条建议中的代理消息所占的百分比。

配置对话配置文件

对话配置文件可配置一组参数,用于控制对话期间向客服人员提出的建议。以下步骤将使用 HumanAgentAssistantConfig 对象创建 ConversationProfile。如果您不希望直接调用 API,还可以使用 Agent Assist 控制台来执行这些操作。

创建对话配置文件

如需创建对话配置文件,请对 ConversationProfile 资源调用 create 方法。 提供知识库 ID、文档 ID、项目 ID 和模型 ID。

以下是一个 JSON 示例:
{
  "displayName": "smart_compose_assist",
  "humanAgentAssistantConfig": {
    "humanAgentSuggestionConfig": {
      "featureConfigs": [
        {
          "suggestionFeature": {
            "type": "SMART_COMPOSE"
          },
          "queryConfig": {
            "documentQuerySource": {
              "documents": "projects/PROJECT_ID/knowledgeBases/KNOWLEDGE_BASE_ID/documents/DOCUMENT_ID"
            },
           "maxResults": "1"
          },
         "conversationModelConfig": {
           "model": "projects/PROJECT_ID/conversationModels/MODEL_ID"
         }
        }
      ]
    }
  }
}

响应包含新对话配置文件 ID。

在运行时处理对话

当最终用户与人工客服之间开始对话时,您需要创建对话如需查看建议,您还必须创建最终用户参与者和人工客服参与者,并将其添加到对话中。以下部分将引导您完成此过程。

创建对话

如需创建对话,请对 Conversation 资源调用 create 方法。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 Cloud 项目 ID
  • LOCATION_ID:您的位置 ID
  • CONVERSATION_PROFILE_ID:您在创建对话配置文件时收到的 ID

HTTP 方法和网址:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/locations/LOCATION_ID/conversations

请求 JSON 正文:

{
  "conversationProfile": "projects/PROJECT_ID/locations/LOCATION_ID/conversationProfiles/CONVERSATION_PROFILE_ID",
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "name": "projects/PROJECT_ID/locations/LOCATION_ID/conversations/CONVERSATION_ID",
  "lifecycleState": "IN_PROGRESS",
  "conversationProfile": "projects/PROJECT_ID/locations/LOCATION_ID/conversationProfiles/CONVERSATION_PROFILE_ID",
  "startTime": "2018-11-05T21:05:45.622Z"
}

conversations 后面的路径段包含新对话 ID。

Python

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

def create_conversation(project_id, conversation_profile_id):
    """Creates a conversation with given values

    Args:
        project_id:  The GCP project linked with the conversation.
        conversation_profile_id: The conversation profile id used to create
        conversation."""

    client = dialogflow.ConversationsClient()
    conversation_profile_client = dialogflow.ConversationProfilesClient()
    project_path = client.common_project_path(project_id)
    conversation_profile_path = conversation_profile_client.conversation_profile_path(
        project_id, conversation_profile_id
    )
    conversation = {"conversation_profile": conversation_profile_path}
    response = client.create_conversation(
        parent=project_path, conversation=conversation
    )

    print("Life Cycle State: {}".format(response.lifecycle_state))
    print("Conversation Profile Name: {}".format(response.conversation_profile))
    print("Name: {}".format(response.name))
    return response

创建最终用户参与者

您必须将最终用户参与者和人工客服参与者添加到对话中才能查看建议。如需创建最终用户参与者,请对 Participant 资源调用 create 方法。为 role 字段提供对话 ID 和 END_USER

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 Cloud 项目 ID
  • LOCATION_ID:您的位置 ID
  • CONVERSATION_ID:您的对话 ID

HTTP 方法和网址:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/locations/LOCATION_ID/conversations/CONVERSATION_ID/participants

请求 JSON 正文:

{
  "role": "END_USER",
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "name": "projects/PROJECT_ID/locations/LOCATION_ID/conversations/CONVERSATION_ID/participants/PARTICIPANT_ID",
  "role": "END_USER"
}

participants 后面的路径段包含新的最终用户参与者 ID。

Python

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

def create_participant(project_id: str, conversation_id: str, role: str):
    from google.cloud import dialogflow_v2beta1 as dialogflow

    """Creates a participant in a given conversation.

    Args:
        project_id: The GCP project linked with the conversation profile.
        conversation_id: Id of the conversation.
        participant: participant to be created."""

    client = dialogflow.ParticipantsClient()
    conversation_path = dialogflow.ConversationsClient.conversation_path(
        project_id, conversation_id
    )
    if role in ROLES:
        response = client.create_participant(
            parent=conversation_path, participant={"role": role}, timeout=600
        )
        print("Participant Created.")
        print(f"Role: {response.role}")
        print(f"Name: {response.name}")

        return response

创建人工客服参与者

如需创建人工客服参与者,请对 Participant 资源调用 create 方法。为 role 字段提供对话 ID 和 HUMAN_AGENT

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 Cloud 项目 ID
  • LOCATION_ID:您的位置 ID
  • CONVERSATION_ID:您的对话 ID

HTTP 方法和网址:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/locations/LOCATION_ID/conversations/CONVERSATION_ID/participants

请求 JSON 正文:

{
  "role": "HUMAN_AGENT",
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "name": "projects/PROJECT_ID/locations/LOCATION_ID/conversations/CONVERSATION_ID/participants/PARTICIPANT_ID",
  "role": "HUMAN_AGENT"
}

participants 后面的路径段包含新的人工客服参与者 ID。

Python

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

def create_participant(project_id: str, conversation_id: str, role: str):
    from google.cloud import dialogflow_v2beta1 as dialogflow

    """Creates a participant in a given conversation.

    Args:
        project_id: The GCP project linked with the conversation profile.
        conversation_id: Id of the conversation.
        participant: participant to be created."""

    client = dialogflow.ParticipantsClient()
    conversation_path = dialogflow.ConversationsClient.conversation_path(
        project_id, conversation_id
    )
    if role in ROLES:
        response = client.create_participant(
            parent=conversation_path, participant={"role": role}, timeout=600
        )
        print("Participant Created.")
        print(f"Role: {response.role}")
        print(f"Name: {response.name}")

        return response

添加和分析人工客服的消息

每次任一参与者在对话中输入消息时,您都需要向 API 发送该消息以进行处理。Agent Assist 的建议基于对人工客服和最终用户消息的分析。如需为对话添加和分析人工客服消息,请对 Participant 资源调用 analyzeContent 方法。提供对话 ID 和人工客服参与者 ID。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 GCP 项目 ID
  • CONVERSATION_ID:您的对话 ID
  • PARTICIPANT_ID:您的人工客服参与者 ID

HTTP 方法和网址:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/conversations/CONVERSATION_ID/participants/PARTICIPANT_ID:analyzeContent

请求 JSON 正文:

{
  "textInput": {
    "text": "How may I help you?",
    "languageCode": "en-US"
  }
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "message": {
    "name": "projects/PROJECT_ID/conversations/CONVERSATION_ID/messages/MESSAGE_ID",
    "content": "How may I help you?",
    "languageCode": "en-US",
    "participant": "PARTICIPANT_ID",
    "participantRole": "HUMAN_AGENT",
    "createTime": "2020-02-13T00:01:30.683Z"
  },
  "humanAgentSuggestionResults": [
    {
      "suggestSmartRepliesResponse": {
      "smartReplyAnswers": [
          {
            "reply": "I am here to help you.",
            "confidence": 0.5,
            "answerRecord": "projects/PROJECT_ID/answerRecords/ANSWER_RECORD_ID_1"
          },
          {
            "reply": "Sorry for the wait, we have a high volume of chats right now.",
            "confidence": 0.3,
            "answerRecord": "projects/PROJECT_ID/answerRecords/ANSWER_RECORD_ID_2"
          },
          {
            "reply": "Thank you for contacting us!",
            "confidence": 0.1,
            "answerRecord": "projects/PROJECT_ID/answerRecords/ANSWER_RECORD_ID_3"
          }
        ]
      }
    }
  ]
}

Python

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

def analyze_content_text(
    project_id: str, conversation_id: str, participant_id: str, text: str
):
    from google.cloud import dialogflow_v2beta1 as dialogflow

    """Analyze text message content from a participant.

    Args:
        project_id: The GCP project linked with the conversation profile.
        conversation_id: Id of the conversation.
        participant_id: Id of the participant.
        text: the text message that participant typed."""

    client = dialogflow.ParticipantsClient()
    participant_path = client.participant_path(
        project_id, conversation_id, participant_id
    )
    text_input = {"text": text, "language_code": "en-US"}
    response = client.analyze_content(
        participant=participant_path, text_input=text_input
    )
    print("AnalyzeContent Response:")
    print(f"Reply Text: {response.reply_text}")

    for suggestion_result in response.human_agent_suggestion_results:
        if suggestion_result.error is not None:
            print(f"Error: {suggestion_result.error.message}")
        if suggestion_result.suggest_articles_response:
            for answer in suggestion_result.suggest_articles_response.article_answers:
                print(f"Article Suggestion Answer: {answer.title}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_faq_answers_response:
            for answer in suggestion_result.suggest_faq_answers_response.faq_answers:
                print(f"Faq Answer: {answer.answer}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_smart_replies_response:
            for (
                answer
            ) in suggestion_result.suggest_smart_replies_response.smart_reply_answers:
                print(f"Smart Reply: {answer.reply}")
                print(f"Answer Record: {answer.answer_record}")

    for suggestion_result in response.end_user_suggestion_results:
        if suggestion_result.error:
            print(f"Error: {suggestion_result.error.message}")
        if suggestion_result.suggest_articles_response:
            for answer in suggestion_result.suggest_articles_response.article_answers:
                print(f"Article Suggestion Answer: {answer.title}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_faq_answers_response:
            for answer in suggestion_result.suggest_faq_answers_response.faq_answers:
                print(f"Faq Answer: {answer.answer}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_smart_replies_response:
            for (
                answer
            ) in suggestion_result.suggest_smart_replies_response.smart_reply_answers:
                print(f"Smart Reply: {answer.reply}")
                print(f"Answer Record: {answer.answer_record}")

    return response

添加和分析最终用户的消息

如需为对话添加和分析最终用户消息,请对 Participant 资源调用 analyzeContent 方法。提供对话 ID 和最终用户参与者 ID。

响应包含消息 ID。 建议将在下一步中获取。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 GCP 项目 ID
  • CONVERSATION_ID:您的对话 ID
  • PARTICIPANT_ID:最终用户参与者 ID

HTTP 方法和网址:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/conversations/CONVERSATION_ID/participants/PARTICIPANT_ID:analyzeContent

请求 JSON 正文:

{
  "textInput": {
    "text": "I want to reserve a room.",
    "languageCode": "en-US"
  }
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "message": {
    "name": "projects/PROJECT_ID/conversations/CONVERSATION_ID/messages/MESSAGE_ID",
    "content": "I want to reserve a room.",
    "languageCode": "en-US",
    "participant": "PARTICIPANT_ID",
    "participantRole": "END_USER",
    "createTime": "2020-02-13T00:07:35.925Z"
  },
  "humanAgentSuggestionResults": [
    {
      "suggestSmartRepliesResponse": {
      "smartReplyAnswers": [
          {
            "reply": "Where would you like to reserve a room?",
            "confidence": 0.5,
            "answerRecord": "projects/PROJECT_ID/answerRecords/ANSWER_RECORD_ID_1"
          },
          {
            "reply": "What type of rooms would you like to reserve?",
            "confidence": 0.3,
            "answerRecord": "projects/PROJECT_ID/answerRecords/ANSWER_RECORD_ID_2"
          },
          {
            "reply": "How long do you want to stay?",
            "confidence": 0.1,
            "answerRecord": "projects/PROJECT_ID/answerRecords/ANSWER_RECORD_ID_3"
          }
        ]
      }
    }
  ]
}

Python

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

def analyze_content_text(
    project_id: str, conversation_id: str, participant_id: str, text: str
):
    from google.cloud import dialogflow_v2beta1 as dialogflow

    """Analyze text message content from a participant.

    Args:
        project_id: The GCP project linked with the conversation profile.
        conversation_id: Id of the conversation.
        participant_id: Id of the participant.
        text: the text message that participant typed."""

    client = dialogflow.ParticipantsClient()
    participant_path = client.participant_path(
        project_id, conversation_id, participant_id
    )
    text_input = {"text": text, "language_code": "en-US"}
    response = client.analyze_content(
        participant=participant_path, text_input=text_input
    )
    print("AnalyzeContent Response:")
    print(f"Reply Text: {response.reply_text}")

    for suggestion_result in response.human_agent_suggestion_results:
        if suggestion_result.error is not None:
            print(f"Error: {suggestion_result.error.message}")
        if suggestion_result.suggest_articles_response:
            for answer in suggestion_result.suggest_articles_response.article_answers:
                print(f"Article Suggestion Answer: {answer.title}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_faq_answers_response:
            for answer in suggestion_result.suggest_faq_answers_response.faq_answers:
                print(f"Faq Answer: {answer.answer}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_smart_replies_response:
            for (
                answer
            ) in suggestion_result.suggest_smart_replies_response.smart_reply_answers:
                print(f"Smart Reply: {answer.reply}")
                print(f"Answer Record: {answer.answer_record}")

    for suggestion_result in response.end_user_suggestion_results:
        if suggestion_result.error:
            print(f"Error: {suggestion_result.error.message}")
        if suggestion_result.suggest_articles_response:
            for answer in suggestion_result.suggest_articles_response.article_answers:
                print(f"Article Suggestion Answer: {answer.title}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_faq_answers_response:
            for answer in suggestion_result.suggest_faq_answers_response.faq_answers:
                print(f"Faq Answer: {answer.answer}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_smart_replies_response:
            for (
                answer
            ) in suggestion_result.suggest_smart_replies_response.smart_reply_answers:
                print(f"Smart Reply: {answer.reply}")
                print(f"Answer Record: {answer.answer_record}")

    return response

获取建议(可选)

您可以随时获取建议。您可以选择指定消息 ID,以便接收基于该消息的建议。如果未设置此字段,则默认情况下,建议基于任一参与者的最新消息。如需获取建议,请对 Suggestion 资源调用 suggestSmartReplies 方法。提供对话 ID、人工客服参与者 ID 以及任一参与者的消息 ID(可选)。

响应包含面向人工客服的智能回复建议。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 GCP 项目 ID
  • CONVERSATION_ID:您的对话 ID
  • PARTICIPANT_ID:最终用户参与者 ID

HTTP 方法和网址:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/conversations/CONVERSATION_ID/participants/PARTICIPANT_ID:analyzeContent

请求 JSON 正文:

{
  "textInput": {
    "text": "I want to reserve a room.",
    "languageCode": "en-US"
  }
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "message": {
    "name": "projects/PROJECT_ID/conversations/CONVERSATION_ID/messages/MESSAGE_ID",
    "content": "I want to reserve a room.",
    "languageCode": "en-US",
    "participant": "PARTICIPANT_ID",
    "participantRole": "END_USER",
    "createTime": "2020-02-13T00:07:35.925Z"
  },
  "humanAgentSuggestionResults": [
    {
      "suggestSmartRepliesResponse": {
      "smartReplyAnswers": [
          {
            "reply": "Where would you like to reserve a room?",
            "confidence": 0.5,
            "answerRecord": "projects/PROJECT_ID/answerRecords/ANSWER_RECORD_ID_1"
          },
          {
            "reply": "What type of rooms would you like to reserve?",
            "confidence": 0.3,
            "answerRecord": "projects/PROJECT_ID/answerRecords/ANSWER_RECORD_ID_2"
          },
          {
            "reply": "How long do you want to stay?",
            "confidence": 0.1,
            "answerRecord": "projects/PROJECT_ID/answerRecords/ANSWER_RECORD_ID_3"
          }
        ]
      }
    }
  ]
}

Python

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

def analyze_content_text(
    project_id: str, conversation_id: str, participant_id: str, text: str
):
    from google.cloud import dialogflow_v2beta1 as dialogflow

    """Analyze text message content from a participant.

    Args:
        project_id: The GCP project linked with the conversation profile.
        conversation_id: Id of the conversation.
        participant_id: Id of the participant.
        text: the text message that participant typed."""

    client = dialogflow.ParticipantsClient()
    participant_path = client.participant_path(
        project_id, conversation_id, participant_id
    )
    text_input = {"text": text, "language_code": "en-US"}
    response = client.analyze_content(
        participant=participant_path, text_input=text_input
    )
    print("AnalyzeContent Response:")
    print(f"Reply Text: {response.reply_text}")

    for suggestion_result in response.human_agent_suggestion_results:
        if suggestion_result.error is not None:
            print(f"Error: {suggestion_result.error.message}")
        if suggestion_result.suggest_articles_response:
            for answer in suggestion_result.suggest_articles_response.article_answers:
                print(f"Article Suggestion Answer: {answer.title}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_faq_answers_response:
            for answer in suggestion_result.suggest_faq_answers_response.faq_answers:
                print(f"Faq Answer: {answer.answer}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_smart_replies_response:
            for (
                answer
            ) in suggestion_result.suggest_smart_replies_response.smart_reply_answers:
                print(f"Smart Reply: {answer.reply}")
                print(f"Answer Record: {answer.answer_record}")

    for suggestion_result in response.end_user_suggestion_results:
        if suggestion_result.error:
            print(f"Error: {suggestion_result.error.message}")
        if suggestion_result.suggest_articles_response:
            for answer in suggestion_result.suggest_articles_response.article_answers:
                print(f"Article Suggestion Answer: {answer.title}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_faq_answers_response:
            for answer in suggestion_result.suggest_faq_answers_response.faq_answers:
                print(f"Faq Answer: {answer.answer}")
                print(f"Answer Record: {answer.answer_record}")
        if suggestion_result.suggest_smart_replies_response:
            for (
                answer
            ) in suggestion_result.suggest_smart_replies_response.smart_reply_answers:
                print(f"Smart Reply: {answer.reply}")
                print(f"Answer Record: {answer.answer_record}")

    return response

完成对话

如需完成对话,请对 conversations 资源调用 complete 方法。提供对话 ID。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 GCP 项目 ID
  • CONVERSATION_ID:您在创建对话时收到的 ID

HTTP 方法和网址:

POST https://dialogflow.googleapis.com/v2/projects/PROJECT_ID/conversations/CONVERSATION_ID:complete

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:

{
  "name": "projects/PROJECT_ID/conversations/CONVERSATION_ID",
  "lifecycleState": "COMPLETED",
  "conversationProfile": "projects/PROJECT_ID/conversationProfiles/CONVERSATION_PROFILE_ID",
  "startTime": "2018-11-05T21:05:45.622Z",
  "endTime": "2018-11-06T03:50:26.930Z"
}

Python

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

def complete_conversation(project_id, conversation_id):
    """Completes the specified conversation. Finished conversations are purged from the database after 30 days.

    Args:
        project_id: The GCP project linked with the conversation.
        conversation_id: Id of the conversation."""

    client = dialogflow.ConversationsClient()
    conversation_path = client.conversation_path(project_id, conversation_id)
    conversation = client.complete_conversation(name=conversation_path)
    print("Completed Conversation.")
    print("Life Cycle State: {}".format(conversation.lifecycle_state))
    print("Conversation Profile Name: {}".format(conversation.conversation_profile))
    print("Name: {}".format(conversation.name))
    return conversation