天天看點

Rasa 文檔 中英文翻譯版本 2 - Tutorial: Rasa BasicsRasa 文檔 中英文翻譯版本 2 - Tutorial: Rasa Basics Tutorial: Rasa Basics Rasa基礎知識

Rasa 文檔 中英文翻譯版本 2 - Tutorial: Rasa Basics 

https://rasa.com/docs/rasa/user-guide/rasa-tutorial/

T by yi 

Tutorial: Rasa Basics 

Rasa基礎知識

This page explains the basics of building an assistant with Rasa and shows the structure of a Rasa project. You can test it out right here without installing anything. You can also install Rasa and follow along in your command line.

本頁解釋使用 Rasa 建構助手的基礎知識,并展示 Rasa 項目的結構。您可以在這裡測試它,而無需安裝任何東西。您還可以指令行安裝 Rasa 。

The glossary contains an overview of the most common terms you’ll see in the Rasa documentation.

術語表包含您将在 Rasa 文檔中看到的最常見的術語的概述

目錄

1. Create a New Project  

建立項目

2. View Your NLU Training Data

檢視你的NLU訓練資料

3. Define Your Model Configuration

模型配置

4. Write Your First Stories

第一個Stories

5. Define a Domain

定義域

6. Train a Model

訓練模型

7. Test Your Assistant

訓練助手

8. Talk to Your Assistant

與助手對話

In this tutorial, you will build a simple, friendly assistant which will ask how you’re doing and send you a fun picture to cheer you up if you are sad.

在本教程中,您将建立一個簡單友好的助手,它會問你感覺怎麼樣,如果你選擇是傷心,它會送你一個有趣的圖檔讓你高興。

1. Create a New Project 建立項目

The first step is to create a new Rasa project. To do this, run:

第一步是建立一個Rasa項目

rasa init --no-prompt

run

The rasa init command creates all the files that a Rasa project needs and trains a simple bot on some sample data. If you leave out the --no-prompt flag you will be asked some questions about how you want your project to be set up.

rasa init 指令建立 Rasa 項目需要的所有檔案,并訓練一些示例資料上的簡單機器人。如果您不使用 --no-prompt,系統會詢問有關如何設定項目的一些問題。

This creates the following files: 

系統會自動建立以下檔案。

__init__.py

an empty file that helps python find your actions

一個空檔案幫助python找到actions

actions.py

code for your custom actions

自定義actions的代碼

config.yml ‘*’

configuration of your NLU and Core models

定義你的NLU和Core模型

credentials.yml

details for connecting to other services

與其他服務連結的配置

data/nlu.md ‘*’

your NLU training data

NLU訓練資料

data/stories.md ‘*’

your stories

Stories

domain.yml ‘*’

your assistant’s domain

機器人域定義

endpoints.yml

details for connecting to channels like fb messenger

類似facebook message的一個通道定義

models/<timestamp>.tar.gz

your initial model

初始化模型

The most important files are marked with a ‘*’. You will learn about all of these in this tutorial.

最重要的檔案打了星号“*”。你在後面的教程中會更多了解他們的内容。

2. View Your NLU Training Data 檢視你的NLU訓練資料

The first piece of a Rasa assistant is an NLU model. NLU stands for Natural Language Understanding, which means turning user messages into structured data. To do this with Rasa, you provide training examples that show how Rasa should understand user messages, and then train a model by showing it those examples.

Rasa 助手的第一部分是 NLU 模型。NLU自然語言了解意味着将使用者消息轉換為結構化資料。若要使用 Rasa 做到這一點,後面提供一些教育訓練示例,這些示例顯示 Rasa 應如何了解使用者消息,然後通過顯示這些示例來訓練模型。

Run the code cell below to see the NLU training data created by the rasa init command:

運作rasa init後會生成NLU 訓練示例資料,運作下面cat指令可以看到

cat data/nlu.md

run

The lines starting with ## define the names of your intents, which are groups of messages with the same meaning. Rasa’s job will be to predict the correct intent when your users send new, unseen messages to your assistant. You can find all the details of the data format in Training Data Format.

以 ## 開始的行定義意圖Intents的名稱,這些意圖是具有相同含義的消息組。Rasa 的工作将是在使用者向機器人發送新的、沒見過的消息時,預測出正确的意圖。您可以在訓練資料格式中查找資料格式的所有詳細資訊。

3. Define Your Model Configuration 定義模型配置

The configuration file defines the NLU and Core components that your model will use. In this example, your NLU model will use the supervised_embeddings pipeline. You can learn about the different NLU pipelines here.

配置檔案定義模型将使用的 NLU 和 Core 元件。在此示例中,NLU 模型将使用supervised_embeddings管道。

Let’s take a look at your model configuration file.

檢視模型定義檔案

cat config.yml

run

The language and pipeline keys specify how the NLU model should be built. The policies key defines the policies that the Core model will use.

language 和pipeline 鍵指定應如何建構 NLU 模型。

policies 鍵定義核心模型将使用的政策。

4. Write Your First Stories 定義第一個Stories

At this stage, you will teach your assistant how to respond to your messages. This is called dialogue management, and is handled by your Core model.

在此階段,您将教您的助手如何回複您的消息。這稱為對話管理,由核心模型處理。

Core models learn from real conversational data in the form of training “stories”. A story is a real conversation between a user and an assistant. Lines with intents and entities reflect the user’s input and action names show what the assistant should do in response.

核心模型以教育訓練"stories"的形式從真實的對話資料中學習。stories是使用者和機器人之間的真實對話。包含意圖和實體的行反映使用者的輸入和操作名稱,顯示機器人在響應時應執行哪些操作。

Below is an example of a simple conversation. The user says hello, and the assistant says hello back. This is how it looks as a story:

下面是一個簡單的對話示例。使用者打招呼,機器人打招呼回來。這就是它作為一個故事的樣子:

## story1* greet   - utter_greet

You can see the full details in Stories.

你可以點選連結檢視Stories詳解。

Lines that start with - are actions taken by the assistant. In this tutorial, all of our actions are messages sent back to the user, like utter_greet, but in general, an action can do anything, including calling an API and interacting with the outside world.

以 - 開始的行是機器人執行的操作。在本教程中,我們所有的操作都是發送給使用者的消息,就如utter_greet,但一般來說,操作可以執行任何操作,包括調用 API 和與外部世界互動。

Run the command below to view the example stories inside the file data/stories.md:

運作下面的指令以檢視檔案data/stories.md中的示例情景:

cat data/stories.md

run

5. Define a Domain 定義域

The next thing we need to do is define a Domain. The domain defines the universe your assistant lives in: what user inputs it should expect to get, what actions it should be able to predict, how to respond, and what information to store. The domain for our assistant is saved in a file called domain.yml:

接下來我們需要做的就是定義一個域Domain。域定義了您的助手所生活的宇宙universe。它應該期望獲得哪些使用者輸入、應該能夠預測哪些操作、如何響應以及存儲哪些資訊。我們的助手的域儲存在名為domain.yml的檔案中:

cat domain.yml

run

So what do the different parts mean?

不同含義的定義

Intents

意圖

things you expect users to say

你希望使用者說的内容

Actions

動作

things your assistant can do and say

你的機器人可以說和做的事情

Responses

響應

response strings for the things your assistant can say

你的機器人可以說的響應内容的字元串

How does this fit together? Rasa Core’s job is to choose the right action to execute at each step of the conversation. In this case, our actions simply send a message to the user. These simple utterance actions are the actions in the domain that start with utter_. The assistant will respond with a message based on a response from the responses section. See Custom Actions to build actions that do more than just send a message.

這怎麼結合在一起?Rasa Core 的工作是選擇正确的action 在對話的每一步執行。在簡單的情況下,我們的操作一般隻是向使用者發送一條消息。這些簡單的話語操作是在域配置中,讀取從utter_開始的actions中得到的。助理将根據responses 定義部分的内容進行響應。請參閱Custom Actions以生成除了執行傳遞消息之外的複雜操作。

6. Train a Model 訓練模型

Anytime we add new NLU or Core data, or update the domain or configuration, we need to re-train a neural network on our example stories and NLU data. To do this, run the command below. This command will call the Rasa Core and NLU train functions and store the trained model into the models/ directory. The command will automatically only retrain the different model parts if something has changed in their data or configuration.

每當我們添加新的 NLU 或 Core 資料,或更新域或配置時,我們都需要在示例情景和 NLU 資料上重新訓練神經網絡。為此,請運作下面的指令。此指令将調用 Rasa Core 和 NLU 訓練函數,并将訓練模型存儲到模型/目錄中。如果其資料或配置中發生了更改該指令将僅自動重新訓練不同的模型部件。

rasa train

echo "Finished training."

run

The rasa train command will look for both NLU and Core data and will train a combined model.

rasa train指令将查找 NLU 和 Core 資料,并将訓練組合模型。

7. Test Your Assistant 測試機器人

After you train a model, you always want to check that your assistant still behaves as you expect. In Rasa Open Source, you use end-to-end tests defined in your tests/ directory to run through test conversations that ensure both NLU and Core make correct predictions.

訓練模型後,您始終想要檢查您的助手是否仍按預期行事。在 Rasa開源代碼中,您可以使用tests/ 目錄中定義的端到端測試來運作測試對話,以確定 NLU 和 Core 做出正确的預測。

rasa test

echo "Finished running tests."

run

See Testing Your Assistant to learn more about how to evaluate your model as you improve it.

請參閱Testing Your Assistant,詳細了解如何在改進模型時對其進行評估。

8. Talk to Your Assistant 與機器人對話

Congratulations!

恭喜!

The next step is to try it out! If you’re following this tutorial on your local machine, start talking to your assistant by running:

下一步是試一試!如果您在本地計算機上學習本教程,請通過運作以下條件開始與助手交談:

rasa shell

繼續閱讀