rasa 基于意图对话_用rasa构建一个基于ai的电报机器人

rasa 基于意图对话

In this post, I want to share the details of developing a Rasa assistant with custom actions (abilities) run locally from a Docker container and integrated into a Telegram bot.

在本文中,我想分享使用自定义操作(功能)开发Rasa助手的详细信息,这些自定义操作从Docker容器本地运行并集成到Telegram机器人中。

You will not need to install Rasa (which can be a bit challenging) or clone any repository. You will be able to reproduce the same results just by following the step-by-step guide.

您将不需要安装Rasa(这可能会有些挑战)或克隆任何存储库。 只要遵循分步指南,您就可以重现相同的结果。

This post will cover the following topics:

这篇文章将涵盖以下主题:

  1. A couple of words on Rasa

    关于Rasa的几句话
  2. Running and testing Rasa assistant locally from a container

    从容器本地运行和测试Rasa助手
  3. Integrating into a Telegram bot

    集成到Telegram机器人中
  4. Custom actions

    自定义动作

1.什么是Rasa? (1. What’s Rasa?)

As Rasa’s site states: “Rasa Open Source is a machine learning framework to automate text- and voice-based assistants.”. Indeed, it is a great tool to build smart assistants integrated seamlessly into almost any platform.

正如Rasa的网站所述:“ Rasa开源是一种机器学习框架,可自动执行基于文本和语音的助手。” 确实,这是将智能助手无缝集成到几乎所有平台中的绝佳工具。

It has a decent level of customisation providing you with a full set of ready-made solutions and also allowing you to incorporate and use your machine learning models, custom code, and external APIs.

它具有不错的自定义级别,可为您提供全套现成的解决方案,还允许您合并和使用机器学习模型,自定义代码和外部API。

2.在本地运行一个简单的助手 (2. Run a simple assistant locally)

Rasa provides developers with detailed tutorials. However, when I wanted to reproduce some of it I encountered several errors. So the purpose of the post is to help those struggling with the same issues.

Rasa为开发人员提供了详细的教程。 但是,当我想复制其中一些内容时,遇到了一些错误。 因此,该帖子的目的是帮助遇到同样问题的人。

I assume you have Docker installed in your system.

我假设您在系统中安装了Docker。

Image for post
https://www.docker.com https://www.docker.com的免费图片

2.1. First, we need to download the image and initialise our project. Run the following command from your project’s directory:

2.1。 首先,我们需要下载图像并初始化我们的项目。 从您的项目目录运行以下命令:

docker run -v $(pwd):/app rasa/rasa:1.10.8-full init --no-prompt

When executing the docker run command it will download the corresponding docker image if it can not find it in your system.

执行docker run命令时,如果无法在系统中找到对应的docker映像,则会下载相应的docker映像。

The command will initialise the project and generate all the needed files for a template assistant, let’s call it that. This bot will have just a few intents but you already can talk to it with a special tool — Rasa shell.

该命令将初始化项目并生成模板助手所需的所有文件,我们称其为。 该机器人只有几个意图,但是您已经可以使用特殊工具Rasa shell与它进行对话。

Run the following from the project’s directory with your command line:

使用命令行从项目目录运行以下命令:

docker run -it -v $(pwd):/app rasa/rasa:1.10.8-full shell

Your bot doesn’t know a lot yet, but it’s a good starting point. I recommend you walk through tutorials and get familiar with the different files your assistant need.

您的机器人还不很了解,但这是一个很好的起点。 我建议您逐步阅读教程,并熟悉助手所需的不同文件。

Now let’s make your bot available from the internet — it would be great to be able to give it for testing to friends without having any servers for deployment. For this, we’ll need Ngrok — a program that can provide you with a secure URL to your localhost server.

现在,让您的机器人可以从Internet上使用-能够将它提供给朋友进行测试而无需部署任何服务器将是很棒的。 为此,我们需要Ngrok —一个可以为您提供到本地服务器的安全URL的程序。

2.2. Download the archive from here, unzip it and run:

2.2。 从此处下载档案,解压缩并运行:

./ngrok http 5005

This command will generate a public url and attach it to port 5005. We’ll connect this port to the same port of our container later.

此命令将生成一个公共URL,并将其附加到端口5005。稍后,我们将将此端口连接到容器的同一端口。

2.3. Now we need to run the container with a special flag to be able to send requests to the bot:

2.3。 现在,我们需要使用特殊标志运行容器,以便能够将请求发送到机器人:

docker run -p 5005:5005 -v $(pwd):/app rasa/rasa:1.10.8-full run --enable-api

If everything is done correctly you can send post requests from any computer to the url that has been generated by Ngrok. You can use programs like Postman for this. Make sure you send the request body in json format. For example: {“sender”: “1”, “message”: “Hey”}

如果一切都正确完成,则可以将任何计算机的发帖请求发送到Ngrok生成的url。 您可以使用类似Postman的程序。 确保以json格式发送请求正文。 例如:{“发送者”:“ 1”,“消息”:“嘿”}

3.制作自己的基于AI的电报机器人 (3. Make own AI-based telegram bot)

3.1 To create a telegram bot you first need to text BotFather. Find it on telegram, text it, and follow its instructions. At the end of the conversation, you should have a token and a bot (not very mighty at the moment).

3.1要创建电报机器人,您首先需要给BotFather发短信。 在电报上找到它,发短信,然后按照说明进行操作。 在对话结束时,您应该有一个令牌和一个漫游器(目前还不是很强大)。

Image for post
BotFather. Image by author
BotFather。 图片作者

3.2. Now we need to make slight changes to credentials.yml which contains details for connecting to other services. We need to insert the following into the file:

3.2。 现在,我们需要对凭据.yml进行一些更改,其中包含用于连接其他服务的详细信息。 我们需要在文件中插入以下内容:

Do not forget to use your own info to fill up the fields.

不要忘记使用您自己的信息来填写字段。

telegram:
access_token: token received from BotFather
verify: specified bot name
webhook_url: “https://url_from_Ngrok/webhooks/telegram/webhook”

The bot will be run only after running Ngrok for us to be able to provide Rasa with the generated link.

该机器人只有在运行Ngrok之后才能运行,以便我们能够为Rasa提供生成的链接。

3.3. Run the container. It is important to specify ports:

3.3。 运行容器。 指定端口很重要:

docker run -p 5005:5005 -v $(pwd):/app rasa/rasa:1.10.8-full run --enable-api

3.4. Chat with your assistant :)

3.4。 与您的助手聊天:)

Image for post
Functionality demonstration. Image by author
功能演示。 图片作者

Great! Now we have at our disposal the fully-functioning chat-bot. But it can just cheer you up with a cute picture. What if we want to make it more capable. One way of doing this is to use custom actions.

大! 现在,我们可以使用功能齐全的聊天机器人。 但这可以用一张可爱的照片使您振作起来。 如果我们想使其更有能力该怎么办。 一种方法是使用自定义操作。

4.通过自定义操作扩展您的机器人功能 (4. Expand capabilities of your bot with custom actions)

“Actions are the things your bot runs in response to user input.”. There are several types of actions every bot can perform. For example, it can reply to you with a message or, which is more interesting, execute your custom code called custom actions.

“动作是您的机器人根据用户输入运行的东西。” 每个漫游器可以执行几种类型的动作。 例如,它可以通过消息答复您,或者更有趣的是,执行称为自定义操作的自定义代码。

Our custom code will be used as an endpoint run on a separate server which we will send requests to when classifying the user’s message as a corresponding intent.

我们的自定义代码将用作在单独的服务器上运行的终结点,当将用户消息分类为相应的意图时,我们会将请求发送到该终结点。

Rasa provides us with a convenient tool that will help us with it — Rasa SDK. Again, we don’t need to install anything.

Rasa为我们提供了一个方便的工具-Rasa SDK。 同样,我们不需要安装任何东西。

Let’s teach our bot to send a joke to a user to cheer them up.

让我们教我们的机器人向用户发送玩笑以使他们振作起来。

4.1. Create a new folder called actions inside your project’s directory and move actions.py there.

4.1。 在项目目录中创建一个名为actions的新文件夹,然后将actions.py移到该文件夹​​中。

4.2. Create an empty file __init__.py inside the actions directory because Rasa SDK expects a python module.

4.2。 在action目录内创建一个空文件__init__.py,因为Rasa SDK需要python模块。

4.3. Insert into the actions.py file:

4.3。 插入actions.py文件:

import requests
import json
from rasa_sdk import Action
class ActionJoke(Action):
def name(self):
return "action_joke"
def run(self, dispatcher, tracker, domain):
request = requests.get(
'http://api.icndb.com/jokes/random'
).json() # make an api call
joke = request['value']['joke'] # extract a joke
dispatcher.utter_message(text=joke) # send the message back
return []

4.4. In data/stories.md, replace utter_cheer_up actions with our custom action action_joke to tell your bot to use this new action.

4.4。 在data / stories.md中,将utter_cheer_up操作替换为我们的自定义操作action_joke,以告诉您的机器人使用此新操作。

4.5. Add a section for custom actions in domain.yml, including your new action:

4.5。 在domain.yml中添加自定义操作的部分,包括您的新操作:

actions:
— action_joke

4.6. After updating your domain and stories, you have to retrain your model:

4.6。 更新域和故事后,您必须重新训练模型:

docker run -v $(pwd):/app rasa/rasa:1.10.8-full train

4.7. To instruct the Rasa server to use the action server, you have to tell Rasa its location. Add this endpoint to your endpoints.yml, referencing the name you want to give the server:

4.7。 要指示Rasa服务器使用动作服务器,您必须告诉Rasa其位置。 将此端点添加到您的endpoints.yml,并引用您要赋予服务器的名称:

action_endpoint:
url: “http://action-server:5055/webhook"

Notice that the action server is run on a different port than the assistant itself.

请注意,动作服务器在与助手本身不同的端口上运行。

4.8. As we use a different server for our action we need to create a new container for it. The simplest way for us to handle more than one container is to use docker network:

4.8。 当我们使用其他服务器进行操作时,我们需要为其创建一个新的容器。 我们处理多个容器的最简单方法是使用docker网络:

docker network create rasa_bot_network

4.9. Let’s run the action server at first. It is important to specify the ports:

4.9。 首先让我们运行动作服务器。 指定端口很重要:

docker run -d -p 5055:5055 -v $(pwd)/actions:/app/actions --net rasa_bot_network —-name action-server rasa/rasa-sdk:2.0.0a1

4.10. Finally, we need to run the assistant with the following command:

4.10。 最后,我们需要使用以下命令运行助手:

Don’t forget to run Ngrok and specify the provided link in credentials.yml like before.

不要忘记像以前一样运行Ngrok并在凭据.yml中指定提供的链接。

docker run -p 5005:5005 --net rasa_bot_network -v $(pwd):/app rasa/rasa:1.10.8-full run --enable-api
Image for post
Functionality demonstration. Image by author
功能演示。 图片作者

Notice that in the screenshot we have buttons for a faster choice between different options. These are pretty easy to implement. Follow this Rasa’s tutorial.

请注意,在屏幕截图中,我们具有用于在不同选项之间进行更快选择的按钮。 这些非常容易实现。 遵循 Rasa的教程。

We went over just a part of the available features of Rasa’s assistants. Check out their tutorials to learn more about those.

我们仅介绍了Rasa助手的部分可用功能。 查看他们的教程以了解更多有关它们的信息。

As always, I hope this post will be useful for someone. Please let me know if you have any questions. You can also reach out to me via LinkedIn

和往常一样,我希望这篇文章对某人有用。 请让我知道,如果你有任何问题。 您也可以通过LinkedIn与我联系

翻译自: https://medium.com/roonyx/build-an-ai-based-telegram-bot-with-rasa-b710869351f0

rasa 基于意图对话

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值