智慧撰寫

智慧撰寫功能可在真人服務專員與使用者進行文字對話時提供協助。系統會根據真人服務專員輸入的開頭,建議完整句子。您可以使用這項功能,為代理程式的即時通訊介面實作自訂建議功能。

真人服務專員在即時通訊介面中輸入文字時,會收到文字建議。本指南會逐步說明如何直接呼叫 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 bucket 中。 對話資料集必須包含至少 30,000 個對話,否則模型訓練會失敗。一般來說,對話越多,模型品質越好。建議上傳至少 3 個月的對話記錄,盡可能涵蓋各種用途。對話資料集中的訊息數上限為 1,000,000 則。

呼叫 ConversationDataset 資源的 importConversationData 方法,匯入對話。

必填欄位:

  • 您先前建立的對話資料集 ID。
  • inputConfig 路徑會指向 Cloud Storage bucket 中的對話轉錄稿資料。

回應是長時間執行的作業,您可以輪詢檢查作業是否完成。

建立對話模型

呼叫 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 的文件資源。建立模型時,系統會在 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 bucket。

回應是長時間執行的作業,您可以輪詢檢查作業是否完成。之後,您在要求中提供的 CSV 檔案就會填入候選回覆。

查看許可清單

系統會根據對話資料,使用智慧回覆功能自動生成允許清單中的回覆。現在可以視需要查看及更新這些回覆。從 Cloud Storage bucket 下載 CSV 檔案,根據需求編輯,然後將檔案上傳回 Cloud Storage bucket。只有允許清單中的回覆可以顯示給真人服務專員。

如果編輯建議回覆,請僅修正拼字和文法,不要改變訊息的含義。編輯後的文字與模型中的含義差異越大,該訊息就越不可能顯示。

您也可以視需要建立新訊息。與編輯過的訊息類似,建立的訊息在執行階段不太可能顯示。

更新許可清單

更新 CSV 檔案後,您可以使用該檔案更新 Document 資源。您可以選擇更新整個許可清單,或只更新特定訊息。如要只更新特定郵件,您必須在匯出允許清單時,將 smart_messaging_partial_update 設為 true。 如果已完成這項操作,請使用匯出 CSV 檔案中自動產生的資料欄,指出要更新的訊息。

呼叫 Document 資源的 reload 方法,即可更新許可清單。如要只更新特定訊息,請將 smart_messaging_partial_update 設為 ReloadDocumentRequest 中的 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 週的資料建立測試資料集。測試資料集至少應包含 1000 筆對話,但一般而言,測試資料集中的對話越多,評估指標就越可靠。測試資料集格式與對話資料集格式相同。

如要建立新的模型評估作業,請在 ConversationModel 資源上呼叫 CreateConversationModelEvaluation 方法。這個方法會傳回長時間執行的作業。您可以輪詢作業來檢查狀態,這會傳回 INITIALIZINGRUNNINGSUCCEEDEDCANCELLEDFAILED 其中之一。

必填欄位:

  • InputDataset:用於測試模型效能的測試資料集。
  • allowlist_document:與要測試的智慧回覆模型相關聯的允許清單。

長時間執行的作業完成後,系統會傳回 ConversationModelEvaluation 資源。這兩項指標包括:

  • allowlist_coverage:測試資料集中,允許清單涵蓋的客服人員訊息百分比。
  • recall:測試資料集中,服務專員訊息出現在允許清單列為智慧回覆模型前 3 項建議的百分比。

設定對話設定檔

對話設定檔會設定一組參數,控管對話期間向服務專員提供的建議。下列步驟會建立 ConversationProfile 物件,其中包含 HumanAgentAssistantConfig 物件。如果您不想直接呼叫 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:您的雲端專案 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:您的雲端專案 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:您的雲端專案 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