Rasa 文档 中英文翻译版本 3 - Tutorial: Building Assistants

本文档是 Rasa 创建对话机器人助手的教程,包括构建简单 FAQ 助理和上下文助理。首先介绍了如何使用 MemoizationPolicy 回应简单意图,然后使用 ResponseSelector 处理常见问题。接着讲解了如何处理业务逻辑、意外用户输入、优雅地处理失败情况和构建更复杂的上下文对话。最后提到了增强记忆策略和使用机器学习进行概括。
摘要由CSDN通过智能技术生成

Rasa 文档 中英文翻译版本 3 - Tutorial: Building Assistants

 

https://rasa.com/docs/rasa/user-guide/building-assistants/

 

T by yi 

 

Tutorial: Building Assistants  创建对话机器人助理

After following the basics of setting up an assistant in the Rasa Tutorial, we’ll now walk through building a basic FAQ chatbot and then build a bot that can handle contextual conversations.

FAQ是英文Frequently Asked Questions的缩写

学完了前面的教程基础知识后,我们现在将演练构建一个基本的常见问题聊天机器人,然后构建一个可以处理上下文对话的机器人。

目录

Building a simple FAQ assistant 创建FAQ 常见问题机器人助理

Memoization Policy 记忆策略

Response Selectors  响应选择器

Building a contextual assistant   构建上下文助理

Handling business logic 处理业务逻辑

Handling unexpected user input 处理意外用户输入

Generic interjections 通用插话

Contextual questions 上下文问题

Failing gracefully 优雅地回复失败问题

Fallback policy 回退策略

Out of scope intent 范围外意图处理

More complex contextual conversations 更复杂的上下文对话

AugmentedMemoizationPolicy 增强记忆策略

Using ML to generalise 使用ML机器学习来生成

Building a simple FAQ assistant 创建FAQ 常见问题机器人助手

FAQ assistants are the simplest assistants to build and a good place to get started. These assistants allow the user to ask a simple question and get a response. We’re going to build a basic FAQ assistant using features of Rasa designed specifically for this type of assistant.

常见问题助理是最简单的助手,也是好的入门教程。这些助手允许用户提出一个简单的问题并得到答复。我们将使用 Rasa 专为此类助手设计的功能构建一个基本的常见问题解答助手。

In this section we’re going to cover the following topics:

这个章节我们覆盖一下内容

Responding to simple intents with the MemoizationPolicy

使用MemoizationPolicy回应简单意图对话

 

Handling FAQs using the ResponseSelector

使用ResponseSelector处理常见问题

We’re going to use content from Sara, the Rasa assistant that, amongst other things, helps the user get started with the Rasa products. You should first install Rasa using the Step-by-step Installation Guide and then follow the Rasa Tutorial to make sure you know the basics.

我们将使用来自Rasa机器人助手示例Sara 的内容和其他一些内容,帮助用户开始使用 Rasa 产品。您应该首先使用分步安装指南安装 Rasa,然后按照 Rasa 教程操作,以确保了解基础知识。

To prepare for this tutorial, we’re going to create a new directory and start a new Rasa project.

为了准备本教程,我们将创建一个新目录,并开始一个新的 Rasa 项目。

mkdir rasa-assistant

rasa init

Let’s remove the default content from this bot, so that the nlu.mdstories.md and domain.yml files are empty.

我们需要删除默认生成的文件内容,所以 nlu.mdstories.md domain.yml三个文件是空的

Memoization Policy 记忆策略

The MemoizationPolicy remembers examples from training stories for up to a max_history of turns. The number of “turns” includes messages the user sent, and actions the assistant performed. For the purpose of a simple, context-less FAQ bot, we only need to pay attention to the last message the user sent, and therefore we’ll set that to 1.

MemoizationPolicy 记忆策略可以设置用几轮对话的内容来进行训练进行。

配置中max_history标记来标记轮次(turns) 

对话轮次的数量包括用户发送的消息以及助手执行的操作。为了简单、无上下文的常见问题程序,我们只需要注意用户发送的最后一条消息,因此我们将它设置为 1。

You can do this by editing your config.yml file as follows (you can remove TEDPolicy for now):

你可以修改config.yml来配置。(现在可以删除TEDPolicy 

policies:- name: MemoizationPolicy

  max_history: 1- name: MappingPolicy

Note

The MappingPolicy is there because it handles the logic of the /restart intent, which allows you to clear the conversation history and start fresh.

MappingPolicy 策略之所以存在,是因为它处理 /restart 意图的逻辑,它允许您清除对话历史记录并开始重新开始。

Now that we’ve defined our policies, we can add some stories for the goodbyethank and greet intents to the stories.md file:

现在,我们已经定义了我们的策略,我们可以添加一些故事的goodbyethank greet 的意图stories.md文件:

 

## greet* greet

  - utter_greet

## thank* thank

  - utter_noworries

## goodbye* bye

  - utter_bye

We’ll also need to add the intents, actions and responses to our domain.yml file in the following sections:

我们还需要在以下部分中向域.yml 文件添加intents, actions and responses

intents:

  - greet

  - bye

  - thank

 

responses:

  utter_noworries:

    - text: No worries!

  utter_greet:

    - text: Hi

  utter_bye:

    - text: Bye!

Finally, we’ll copy over some NLU data from Sara into our nlu.md file (more can be found here):

最后,我们将从 Sara 复制一些 NLU 数据到我们的nlu.md文件:

## intent:greet- Hi- Hey- Hi bot- Hey bot- Hello- Good morning- hi again- hi folks

## intent:bye- goodbye- goodnight- good bye- good night- see ya- toodle-oo- bye bye- gotta go- farewell

## intent:thank- Thanks- Thank you- Thank you so much- Thanks bot- Thanks for that- cheers

You can now train a first model and test the bot, by running the following commands:

现在,您可以通过运行以下命令来训练第一个模型并测试自动程序:

rasa train

rasa shell

This bot should now be able to reply to the intents we defined consistently, and in any order.

此机器人现在应该能够以任何顺序回复我们定义的意图。

For example:

 

While it’s good to test the bot interactively, we should also add end to end test cases that can later be included as part of a CI/CD system. End-to-end test conversations include NLU data, so that both components of Rasa can be tested. The file tests/conversation_tests.md contains example test conversations. Delete all the test conversations and replace them with some test conversations for your assistant so far:

虽然以交互方式测试自动程序是好事,但我们还应添加可稍后作为 CI/CD 系统一部分的端到端测试用例。端到端测试对话包括 NLU 数据,以便可以测试 Rasa 的两个组件。tests/conversation_tests.md 包含示例测试会话。删除之前所有测试对话,并将其替换为新的一些测试对话:

## greet + goodbye* greet: Hi!

  - utter_greet* bye: Bye

  - utter_bye

## greet + thanks* greet: Hello there

  - utter_greet* thank: thanks a bunch

  - utter_noworries

## greet + thanks + goodbye* greet: Hey

  - utter_greet* thank: thank you

  - utter_noworries* bye: bye bye

  - utter_bye

To test our model against the test file, run the command:

要针对测试文件测试我们的模型,请运行以下命令:

rasa test --stories tests/conversation_tests.md

The test command will produce a directory named results. It should contain a file called failed_stories.md, where any test cases that failed will be printed. It will also specify whether it was an NLU or Core prediction that went wrong. As part of a CI/CD pipeline, the test option --fail-on-prediction-errors can be used to throw an exception that stops the pipeline.

test命令将生成一个名为results的目录。它应该包含一个名为 failed_stories.md 的文件,其中将打印任何对话失败的测试用例。它还将指出出错的是 NLU 还是 Core 预测出错。作为 CI/CD 管道的一部分,测试选项 --fail-on-prediction-errors 可用于抛出停止管道的异常。

Response Selectors 响应选择器

The ResponseSelector NLU component is designed to make it easier to handle dialogue elements like Small Talk and FAQ messages in a simple manner. By using the ResponseSelector, you only need one story to handle all FAQs, instead of adding new stories every time you want to increase your bot’s scope.

ResponseSelector  NLU 组件旨在更轻松地以简单的方式处理对话元素,如小谈话和常见问题消息。通过使用响应程序,您只需要一个故事来处理所有常见问题解答,而不是每次增加机器人范围的时候还需要添加新故事。

People often ask Sara different questions surrounding the Rasa products, so let’s start with three intents: ask_channelsask_languages, and ask_rasax. We’re going to copy over some NLU data from the Sara training data into our nlu.md. It’s important that these intents have an faq/ prefix, so they’re recognised as the faq intent by the ResponseSelector:

人们经常问Sara围绕Rasa产品的不同问题,所以让我们从三个意图开始:ask_channels,ask_languages和ask_rasax。我们将从 Sara 培训数据中复制一些 NLU 数据导入到nlu.md。具有 faq/前缀非常重要,ResponseSelector  将它们识别为 faq 意图:

## intent: faq/ask_channels- What channels of communication does rasa support?- what channels do you support?- what chat channels does rasa uses- channels supported by Rasa- which messaging channels does rasa support?

## intent: faq/ask_languages- what language does rasa support?- which language do you support?- which languages supports rasa- can I use rasa also for another laguage?- languages supported

## intent: faq/ask_rasax- I want information about rasa x- i want to learn more about Rasa X- what is rasa x?- Can you tell me about rasa x?- Tell me about rasa x- tell me what is rasa x

Next, we’ll need to define the responses associated with these FAQs in a new file called responses.md in the data/ directory:

接下来,我们需要在data/ 目录中responses.md中定义常见问题相关的响应回复

## ask channels* faq/ask_channels

  - We have a comprehensive list of [supported connectors](https://rasa.com/docs/core/connectors/), but if

    you don't see the one you're looking for, you can always create a custom connector by following

    [this guide](https://rasa.com/docs/rasa/user-guide/connectors/custom-connectors/).

## ask languages* faq/ask_languages

  - You can use Rasa to build assistants in any language you want!

## ask rasa x* faq/ask_rasax

 - Rasa X is a tool to learn from real conversations and improve your assistant. Read more [here](https://rasa.com/docs/rasa-x/)

The ResponseSelector should already be at the end of the NLU pipeline in our config.yml:

ResponseSelector 应该配置在 config.yml中的 NLU 管道的末尾:

language: enpipeline:

  - name: WhitespaceTokenizer

  - name: RegexFeaturizer

  - name: LexicalSyntacticFeaturizer

  - name: CountVectorsFeaturizer

  - name: CountVectorsFeaturizer

    analyzer: "char_wb"

    min_ngram: 1

    max_ngram: 4

  - name: DIETClassifier

    epochs: 100

  - name: EntitySynonymMapper

  - name: ResponseSelector

    epochs: 100

Now that we’ve defined the NLU side, we need to make Core aware of these changes. Open your domain.yml file and add the faq intent:

现在,我们已经定义了 NLU 端的内容,我们需要让 Core 意识到这些更改。打domain.yml 文件并添加 faq 意图:

intents:

  - greet

  - bye

  - thank

  - faq

We’ll also need to add a retrieval action, which takes care of sending the response predicted from the ResponseSelector back to the user, to the list of actions. These actions always have to start with the respond_ prefix:

我们还需要添加一个retrieval action检索操作,该操作负责将来自响应ResponseSelector 的响应发送回用户,并将其发送到操作列表中。这些操作始终必须从前缀respond_开始:

actions:

  - respond_faq

Next we’ll write a story so that Core knows which action to predict:

接下来,我们将写一个story ,以便 Core 知道要预测的操作:

## Some question from FAQ* faq

    - respond_faq

This prediction is handled by the MemoizationPolicy, as we described earlier.

After all of the changes are done, train a new model and test the modified FAQs:

正如我们前面描述的那样此预测由MemoizationPolicy处理。

完成所有更改后,训练新模型并测试修改后的常见问题解答:

rasa train

rasa shell

At this stage it makes sense to add a few test cases to your test_stories.md file again:

在此阶段,可以再次向 test_stories.md 文件添加一些测试用例:

## ask channels* faq: What messaging channels does Rasa support?

  - respond_faq

## ask languages* faq: Which languages can I build assistants in?

  - respond_faq

## ask rasa x* faq: What’s Rasa X?

  - respond_faq

You can read more in this blog post and the Retrieval Actions page.

Using the features we described in this tutorial, you can easily build a context-less assistant. When you’re ready to enhance your assistant with context, check out Building a contextual assistant.

使用本教程中介绍的功能,您可以轻松地构建无上下文助手。当您准备好使用上下文增强助手时,请查看Building a contextual assistant.

Note

Here’s a minimal checklist of files we modified to build a basic FAQ assistant:

以下是我们构建基本常见问题助手所需要文件的最小清单:

data/nlu.md: Add NLU training data for faq/ intents

faq/ 意图增加NLU训练数据

data/responses.md: Add responses associated with faq/ intents

 faq/ intents关联回复

config.yml: Add ReponseSelector in your NLU pipeline

为NLU通道增加ReponseSelector 

domain.yml: Add a retrieval action respond_faq and intent faq

增加一个检索respond_faq 和意图faq

data/stories.md: Add a simple story for FAQs

FAQs增加一个简单的story

test_stories.md: Add E2E test stories for your FAQs

FAQs增加E2E的测试story

 

Building a contextual assistant 构建上下文助理

Whether you’ve just created an FAQ bot or are starting from scratch, the next step is to expand your bot to handle contextual conversations.

无论您是刚刚创建了FAQ机器人还是从头开始,下一步是扩展自动程序以处理上下文对话。

In this tutorial we’re going to cover a variety of topics:

在本教程中,我们将介绍以下的几个主题:

Handling business logic

Handling unexpected user input

Failing gracefully

More complex contextual conversations

处理业务逻辑

处理意外用户输入

优雅地处理失败

更复杂的上下文对话

Please make sure you’ve got all the data from the Building a simple FAQ assistant section before starting this part. You will need to make some adjustments to your configuration file, since we now need to pay attention to context:

在开始此部分之前,请确保你已经从"构建一个简单的FAQ助理"章节中获得所需要的数据和代码。因为我们现在需要注意上下文的部分,您需要对配置文件进行一些调整,:

policies:- name: MemoizationPolicy- name: MappingPolicy

We removed the max_history: 1 configuration. The default is 5, meaning Core will pay attention to the past 5 turns when making a prediction (see explanation of max history).

我们删除了max_history:1 配置。默认值为 5,这意味着 Core 在进行预测时将注意过去 5 个对话轮次(请参阅max history最大历史记录的解释)。

Handling business logic 处理业务逻辑

A lot of conversational assistants have user goals that involve collecting a bunch of information from the user before being able to do something for them. This is called slot filling. For example, in the banking industry you may have a user goal of transferring money, where you need to collect information about which account to transfer from, whom to transfer to and the amount to transfer. This type of behavior can and should be handled in a rule based way, as it is clear how this information should be collected.

许多会话助理都有用户目标,这些目标涉及在能够为他们执行某一操作之前,先从用户那里收集大量信息。这称为插槽填充。例如,在银行业中,您可能有一个用户转账的目标,即您需要收集有关要从哪个账户转移、向谁转账以及转账金额的信息。这种类型的行为可以而且应该以基于规则的方式处理,因为应该如何收集此信息。

For this type of use case, we can use Forms and our FormPolicy. The FormPolicy works by predicting the form as the next action until all information is gathered from the user.

对于这种类型的用例,我们可以使用窗体Forms 和表单政策FormPolicy。FormPolicy 的工作原理是表单内容来预测为下一个操作,直到从用户那里收集所有所需要的信息。

 

As an example, we will build out the SalesForm from Sara. The user wants to contact our sales team, and for this we need to gather the following pieces of information:

例如,我们将从 Sara 构建销售表单SalesForm 。用户希望联系我们的销售团队,为此我们需要收集以下信息:

Their job

Their bot use case

Their name

Their email

Their budget

Their company

他们的工作

他们的机器人用例

他们的名字

他们的电子邮件

他们的预算

他们的公司

We will start by defining the SalesForm as a new class in the file called actions.py. The first method we need to define is the name, which like in a regular Action returns the name that will be used in our stories:

我们将首先将 SalesForm 定义为一个新的类class,名为"actions.py"SalesForm这个类我们需要定义的第一个方法是名称name,就像在常规操作中一样,它返回将在我们的故事中使用的名称:

from rasa_sdk.forms import FormAction

class SalesForm(FormAction):

    """Collects sales information and adds it to the spreadsheet"""

 

    def name(self):

        return "sales_form"

Next we have to define the required_slots method which specifies which pieces of information to ask for, i.e. which slots to fill.

接下来,我们必须定义required_slots(必填词槽),该方法指定要请求哪些信息,即要填充哪些槽。

@staticmethoddef required_slots(tracker):

    return [

        "job_function",

        "use_case",

        "budget",

        "person_name",

        "company",

        "business_email",

        ]

Note: you can customise the required slots function not to be static. E.g. if the job_function is a developer, you could add a required_slot about the users experience level with Rasa

注意:您可以自定义所需的词槽slots 功能,词槽slots 可以不是一个静态变量。例如,如果job_function的值是开发者developer,您可以添加一required_slot,用来增加了解 用户对Rasa 的体验级别

Once you’ve done that, you’ll need to specify how the bot should ask for this information. This is done by specifying utter_ask_{slotname} responses in your domain.yml file. For the above we’ll need to specify the following:

完成此工作后,您需要指定自动程序应如何请求此信息。这是通过在domain.yml utter_ask_{slotname} 来指定响应。对于上面例子,我们需要指定以下内容:

utter_ask_business_email:

  - text: What's your business email?utter_ask_company:

  - text: What company do you work for?utter_ask_budget:

  - text: "What's your annual budget for conversational AI? utter_ask_job_function:

  - text: "What's your job? utter_ask_person_name:

  - text: What's your name?utter_ask_use_case:

  - text: What's your use case?

We’ll also need to define all these slots in our domain.yml file:

我们同事需要在domain.yml文件中定义这些词槽

slots:

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值