今天看啥  ›  专栏  ›  桃子说产品

Rasa 入门教程 Core 系列(三)

桃子说产品  · 简书  ·  · 2019-12-15 19:26
rasa_tutorial_core_background.png

Rasa 入门教程 Core 系列包括十一个部分,前面介绍了 Rasa 框架 Core 系列的第二部分:Domains 。本文主要介绍 Rasa 框架 Core 系列的第三部分: 响应 。如果你希望助手响应用户信息,则需要对这些响应进行管理。在训练数据中,stories 文件中,制定了机器人应该执行的动作。这些动作可以使用话术将消息发送给用户。

有三种方式可以管理这些话术:

  • 将话术存储在 domain 文件中;
  • 将检索动作响应作为训练数据的一部分;
  • 创建自定义 NLG 服务以生成响应。

本文的目录结构:

  1. 将话术存储在 domain 文件中
  2. 为机器人响应创建 NLG 服务

1. 将话术存储在 domain 文件中

响应管理的默认方式是将话术存储在 domain 文件中,然后包含该文件中所有自定义动作、可用实体、槽位和意图的引用。

# all hashtags are comments :)
intents:
 - greet
 - default
 - goodbye
 - affirm
 - thank_you
 - change_bank_details
 - simple
 - hello
 - why
 - next_intent

entities:
 - name

slots:
  name:
    type: text

templates:
  utter_greet:
    - text: "hey there {name}!"  # {name} will be filled by slot (same name) or by custom action
  utter_channel:
    - text: "this is a default channel"
    - text: "you're talking to me on slack!"  # if you define channel-specific utterances, the bot will pick
      channel: "slack"                        # from those when talking on that specific channel
  utter_goodbye:
    - text: "goodbye 😢"   # multiple templates - bot will randomly pick one of them
    - text: "bye bye 😢"
  utter_default:   # utterance sent by action_default_fallback
    - text: "sorry, I didn't get that, can you rephrase it?"

actions:
  - utter_default
  - utter_greet
  - utter_goodbye

有关响应格式的更多详情,详见 Rasa 框架 Core 系列的第二部分:Domains 中的话术模板。

2. 为机器人响应创建 NLG 服务

在某些工作流程中,仅仅对机器人进行重新训练来更改文本副本可能不是最佳选择,因此,Rasa Core 提供了将生成响应和对话学习分开的功能。

助手仍将可以预测动作并根据过去的对话记录对用户的输入做出响应,助手发送给用户的响应信息是在 Rasa Core 之外生成的。

如果助手想向用户发送消息,它可以通过 post 请求调用外部 HTTP 服务器,你需要创建一个 endpoints.yml 文件并肩其传给 run server 脚本, endpoints.yml 文件内容如下:

nlg:
  url: http://localhost:5055/nlg    # url of the nlg endpoint
  # you can also specify additional parameters, if you need them:
  # headers:
  #   my-custom-header: value
  # token: "my_authentication_token"    # will be passed as a get parameter
  # basic_auth:
  #   username: user
  #   password: pass
# example of redis external tracker store config
tracker_store:
  type: redis
  url: localhost
  port: 6379
  db: 0
  password: password
  record_exp: 30000
# example of mongoDB external tracker store config
#tracker_store:
  #type: mongod
  #url: mongodb://localhost:27017
  #db: rasa
  #user: username
  #password: password

现在通过以下命令来启动服务器:

$ rasa run --enable-api -m examples/babi/models --log-file out.log --endpoints endpoints.yml

post 发送到端点的请求主体如下所示:

{
  "tracker": {
    "latest_message": {
      "text": "/greet",
      "intent_ranking": [
        {
          "confidence": 1.0,
          "name": "greet"
        }
      ],
      "intent": {
        "confidence": 1.0,
        "name": "greet"
      },
      "entities": []
    },
    "sender_id": "22ae96a6-85cd-11e8-b1c3-f40f241f6547",
    "paused": false,
    "latest_event_time": 1531397673.293572,
    "slots": {
      "name": null
    },
    "events": [
      {
        "timestamp": 1531397673.291998,
        "event": "action",
        "name": "action_listen"
      },
      {
        "timestamp": 1531397673.293572,
        "parse_data": {
          "text": "/greet",
          "intent_ranking": [
            {
              "confidence": 1.0,
              "name": "greet"
            }
          ],
          "intent": {
            "confidence": 1.0,
            "name": "greet"
          },
          "entities": []
        },
        "event": "user",
        "text": "/greet"
      }
    ]
  },
  "arguments": {},
  "template": "utter_greet",
  "channel": {
    "name": "collector"
  }
}

然后,端点需要使用生成的响应进行响应用户:

{
    "text": "hey there",
    "buttons": [],
    "image": null,
    "elements": [],
    "attachments": []
}

然后,Rasa 将使用此响应发送给用户。


作者: 关于我

备注:转载请注明出处。

如发现错误,欢迎留言指正。




原文地址:访问原文地址
快照地址: 访问文章快照