Dialogue knowledge extraction

更新时间:
复制 MD 格式

The dialogue knowledge extraction service is designed for online chat scenarios between customer service agents and consumers. It extracts customer service scripts and user questions from conversations, such as question-and-answer pairs between agents and customers. This data can be used to analyze common user issues, build a library of customer service scripts, and optimize customer service chatbots.

Note

This service is provided by the NLP Self-Learning Platform and can be used by calling its API directly.

Service activation and resource plan purchase

You must activate the service before you can use it. You can purchase a resource plan after activation.

Service invocation and testing

For more information about model invocation, see Model invocation.

For more information about software development kit (SDK) examples, see SDK examples.

Debugging

You can call this API operation directly in OpenAPI Explorer to simplify signature calculation. After a successful call, OpenAPI Explorer automatically generates sample SDK code.

Configure access credentials using environment variables

  1. Note:

    1. An AccessKey pair for an Alibaba Cloud account has all API access permissions, which poses a high security threat. We recommend that you create and use a Resource Access Management (RAM) user to call APIs or perform routine O&M. You can log on to the RAM console to create a RAM user.

    2. To prevent security threats that can arise from leaked keys, do not hard-code your AccessKey ID and AccessKey secret in your code. This topic describes how to use environment variables to store and access your AccessKey pair.

  2. Configure on Linux and macOS

    export NLP_AK_ENV=<access_key_id>
    export NLP_SK_ENV=<access_key_secret>

    Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret. For more information about how to obtain an AccessKey pair, see Step 2: Obtain an AccessKey pair.

  3. Configure on Windows

    1. Create a file to configure environment variables. In the file, add the NLP_AK_ENV and NLP_SK_ENV environment variables and set them to your AccessKey ID and AccessKey secret.

    2. Restart your Windows system.

Java code example

class MsgDO{
            private String role;
            private String content;
            public MsgDO(String role,String content) {
                this.role = role;
                this.content = content;
            }
            public String getRole() {
                return role;
            }
            public void setRole(String role) {
                this.role = role;
            }
            public String getContent() {
                return content;
            }
            public void setContent(String content) {
                this.content = content;
            }
        }
/**
 * An AccessKey pair of an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a RAM user to make API calls or perform routine O&M. Log on to the RAM console to create a RAM user.
 * This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
 * To prevent security threats from key leaks, do not hard-code your AccessKey ID and AccessKey secret into your code.
 */
String accessKeyId = System.getenv("NLP_AK_ENV");
String accessKeySecret = System.getenv("NLP_SK_ENV");
DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessKeySecret);
IAcsClient client = new DefaultAcsClient(defaultProfile);
Map<String, Object> obj = new HashMap<String, Object>();
List<MsgDO> msgs = new ArrayList<MsgDO>();
msgs.add(new MsgDO("agent","Hello."));
msgs.add(new MsgDO("customer","Hello."));
msgs.add(new MsgDO("agent","I am a service specialist from Cainiao Post. You have a package that has been delivered to our station. When are you available to pick it up?"));
msgs.add(new MsgDO("customer","Which Cainiao Post station?"));
msgs.add(new MsgDO("agent","One moment, I will give you the address. We are next to the clinic at the entrance of Luchun Community, Building 2-16."));
msgs.add(new MsgDO("agent","I will send the address to you in a text message later. You can come and pick it up anytime. Is that okay?"));
msgs.add(new MsgDO("customer","Okay."));
msgs.add(new MsgDO("customer","Where is the station?"));
msgs.add(new MsgDO("agent","How about this: you can check the address in the text message to see if it's convenient for you. If you have any questions, please contact me again. Is that okay?"));
msgs.add(new MsgDO("customer","Okay."));
obj.put("msgs",msgs);
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("NLP-dialog-knowledge");
request.setPredictContent(JSON.toJSONString(obj));
RunPreTrainServiceResponse response = client.getAcsResponse(request);
System.out.println(response.getPredictResult());

Python code example

# Install dependencies
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-nlp-automl
# -*- coding: utf-8 -*-
import json
import os

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdknlp_automl.request.v20191111 import RunPreTrainServiceRequest

/**
 * An AccessKey pair of an Alibaba Cloud account has all API access permissions, which poses a high security threat. Create and use a RAM user to make API calls or perform routine O&M. Log on to the RAM console to create a RAM user.
 * This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
 * To prevent security threats from key leaks, do not hard-code your AccessKey ID and AccessKey secret into your code.
 */
access_key_id = os.environ['NLP_AK_ENV']
access_key_secret = os.environ['NLP_SK_ENV']

# Initialize AcsClient instance
client = AcsClient(
  access_key_id,
  access_key_secret,
  "cn-hangzhou"
);
content = {
    "msgs": [
      {
        "role": "agent",
        "content": "Hello."
      },
      {
        "role": "customer",
        "content": "Hello."
      },
      {
        "role": "agent",
        "content": "I am a service specialist from Cainiao Post. You have a package that has been delivered to our station. When are you available to pick it up?"
      },
      {
        "role": "customer",
        "content": "Which Cainiao Post station?"
      },
      {
        "role": "agent",
        "content": "One moment, I will give you the address. We are next to the clinic at the entrance of a certain community, Building 2-16."
      },
      {
        "role": "agent",
        "content": "I will send the address to you in a text message later. You can come and pick it up anytime. Is that okay?"
      },
      {
        "role": "customer",
        "content": "Okay."
      },
      {
        "role": "customer",
        "content": "Where is the station?"
      },
      {
        "role": "agent",
        "content": "How about this: you can check the address in the text message to see if it's convenient for you. If you have any questions, please contact me again. Is that okay?"
      },
      {
        "role": "customer",
        "content": "Can you check the order for my glasses on my phone for me?"
      },
      {
        "role": "agent",
        "content": "Okay, sure. Just show the text message when you come to pick up the package. If you have any questions, feel free to contact us."
      }
    ]
}
# Initialize a request and set parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('NLP-dialog-knowledge')
request.set_PredictContent(json.dumps(content))
# Print response
response = client.do_action_with_exception(request)
resp_obj = json.loads(response)
predict_result = json.loads(resp_obj['PredictResult'])
print(predict_result)

PredictContent example

{
    "msgs": [
      {
        "role": "agent",
        "content": "Hello."
      },
      {
        "role": "customer",
        "content": "Hello."
      },
      {
        "role": "agent",
        "content": "I am a service specialist from Cainiao Post. You have a package that has been delivered to our station. When are you available to pick it up?"
      },
      {
        "role": "customer",
        "content": "Which Cainiao Post station?"
      },
      {
        "role": "agent",
        "content": "One moment, I will give you the address. We are next to the clinic at the entrance of a certain community, Building 2-16."
      },
      {
        "role": "agent",
        "content": "I will send the address to you in a text message later. You can come and pick it up anytime. Is that okay?"
      },
      {
        "role": "customer",
        "content": "Okay."
      },
      {
        "role": "customer",
        "content": "Where is the station?"
      },
      {
        "role": "agent",
        "content": "How about this: you can check the address in the text message to see if it's convenient for you. If you have any questions, please contact me again. Is that okay?"
      },
      {
        "role": "customer",
        "content": "Can you check the order for my glasses on my phone for me?"
      },
      {
        "role": "agent",
        "content": "Okay, sure. Just show the text message when you come to pick up the package. If you have any questions, feel free to contact us."
      }
    ]
}

PredictResult example

{
  "code": 1000,
  "data": {
    "cost": "14.822ms",
    "knowledges": [
      {
        "hits": [
          [
            "Which Cainiao Post station?",
            "One moment, I will give you the address. We are next to the clinic at the entrance of a certain community, Building 2-16."
          ],
          [
            "Where is the station?",
            "How about this: you can check the address in the text message to see if it's convenient for you. If you have any questions, please contact me again. Is that okay?"
          ]
        ],
        "name": "customer-question-agent-answer"
      },
      {
        "hits": [
          [
            "I am a service specialist from Cainiao Post. You have a package that has been delivered to our station. When are you available to pick it up?",
            "Which Cainiao Post station?"
          ]
        ],
        "name": "agent-question-customer-answer"
      }
    ]
  },
  "message": "OK",
  "tracerID": "2020-10-30 06:17:55.485426"
}

Request parameters

Parameter

Description

msgs

A list of messages from the online chat between the customer service agent and the customer. The list can contain a maximum of 20 messages.

role

The role of the speaker. Valid values are agent and customer.

content

The content of the message from the speaker.

Response parameters

Parameter

Description

knowledges

The extracted knowledge.

name

The name of the knowledge type. Valid values are agent-question-customer-answer and customer-question-agent-answer.

hits

A list of specific content that matches the current knowledge type. For example, if the value of name is "agent-question-customer-answer", each element in the hits list is a pair that contains the agent's question and the customer's answer.