自然语言处理学习日记14

1.PolicyTrainer类
解析:Trains a policy on a domain using training data from a file.
[1]param augmentation_factor: how many stories should be created by randomly concatenating stories
[2]param filename: story file containing the training conversations
[3]param max_history: number of past actions to consider for the prediction of the next action
[4]param max_training_samples: specifies how many training samples to train on
[5]param max_number_of_trackers: limits the tracker generation during story file parsing
[6]param remove_duplicates: remove duplicates from the training set before training
[7]param kwargs: additional arguments passed to the underlying ML trainer

2.命名实体识别评估指标
解析:命名实体识别通常采用的评估指标主要有正确率、召回率和F值,如下所示:
[1]正确率 = 识别出的正确实体数 / 识别出的实体数
[2]召回率 = 识别出的正确实体数 / 样本的实体数
[3]F1值 = [2*正确率*召回率] / [正确率+召回率]

3.FallbackPolicy
解析:

class ActionDefaultFallback(Action):
    def name(self):
        return "action_default_fallback"
    def run(self, dispatcher, tracker, domain):
        text = tracker.latest_message.get('text')
        message = get_response(text)
        if message['code'] == 100000:
            dispatcher.utter_message("{}".format(message['text']))
        else:
            dispatcher.utter_template('utter_default', tracker)
        return [UserUtteranceReverted()]

如果意图和预测的action分别低于这两个阈值的时候,便会调用action_default_fallback这个自定义的action。action_default_fallback在rasa-core中的实现:

class ActionDefaultFallback(Action):
    def name(self):
        return "action_default_fallback"
    def run(self, dispatcher, tracker, domain):
        text = tracker.latest_message.get('text')
        message = get_response(text)
        if message['code'] == 100000:
            dispatcher.utter_message("{}".format(message['text']))
        else:
            dispatcher.utter_template('utter_default', tracker)
        return [UserUtteranceReverted()]

如果调用图灵机器人api,那么get_response实现:

def get_response(msg):
    key = apikey
    api = 'http://www.tuling123.com/openapi/api?key={}&info={}'.format(key, msg)
    return requests.get(api).json()

4.Tracker Stores
解析:
[1]InMemoryTrackerStore [default]
[2]SQLTrackerStore
[3]RedisTrackerStore
[4]MongoTrackerStore
[5]Custom Tracker Store

5.default_actions()
解析:
[1]ActionListen()
[2]ActionRestart()
[3]ActionDefaultFallback()
[4]ActionDeactivateForm()
[5]ActionRevertFallbackEvents()
[6]ActionDefaultAskAffirmation()
[7]ActionDefaultAskRephrase()
[8]ActionBack()

6.Fallback Actions
解析:action_default_fallback is a default action in Rasa Core which sends the utter_default template message to the user.

7.Actions
解析:
[1]Utterance actions: start with utter_ and send a specific message to the user
[2]Retrieval actions: start with respond_ and send a message selected by a retrieval model
[3]Custom actions: run arbitrary code and send any number of messages (or none).
[4]Default actions: e.g. action_listen, action_restart, action_default_fallback
说明:rasa_core_sdk已经改名为rasa_sdk。

8.rasa run actions
解析:使用Rasa SDK启动操作服务。

9.custom actions
解析:custom actions在Rasa中的使用方式并不是直接调用,而是采用服务的形式,所以如果想使用自定义的action,还需要定义一个endpoints.yml文件,文件内容如下:

action_endpoint:
  url: 'http://localhost:5055/webhook'

在启动会话的时候添加额外的命令–endpoints endpoints.yml,该命令会在5055端口启动一个服务,这个服务就是自定义的action。

10.Rasa词槽类型
解析:
[1]Text Slot:文本类型
[2]Boolean Slot:布尔类型
[3]Categorical Slot:分类类型
[4]Float Slot:浮点类型
[5]List Slot:列表类型
[6]Unfeaturized Slot:想保存下来的信息

11.MemoizationPolicy
解析:MemoizationPolicy只记住训练数据中的对话。如果训练数据中存在这样的对话,那么它将以置信度为1.0预测下一个动作,否则将预测为None,此时置信度为0.0。其中,max_history决定了模型查看多少个对话历史以决定下一个执行的action。

12.EmbeddingPolicy
解析:循环嵌入式对话策略[Recurrent Embedding Dialogue Policy,REDP],它通过将actions和对话状态嵌入到相同的向量空间[vector space]能够获得较好的效果,REDP包含一个基于改进的Neural Turing Machine的记忆组件和注意机制,在该任务上显著优于基线LSTM分类器。

13.FormPolicy
解析:FormPolicy是MemoizationPolicy的扩展,用于处理表单的填充事项。当一个FormAction被调用时,FormPolicy将持续预测表单动作,直到表单中的所有槽都被填满,然后再执行对应的FormAction。

14.MappingPolicy
解析:MappingPolicy可用于直接将意图映射到要执行的action,从而实现被映射的action总会被执行,其中,这种映射是通过triggers属性实现的。[domain.yml文件]如下所示:

intents:
 - greet: {triggers: utter_goodbye}

其中,greet是意图,utter_goodbye是action。一个意图最多只能映射到一个action,机器人一旦收到映射意图的消息,它将执行对应的action。然后继续监听下一条message。需要注意的是,对于上述映射,还需要要在story.md文件中添加如下样本,否则任何机器学习策略都可能被预测的action_greet在dialouge历史中突然出现而混淆。

15.credentials.yml
解析:使用credentials.yml来存储对应的访问权限信息。如下所示:

twilio:
account_sid: "ACbc2dxxxxxxxxxxxx19d54bdcd6e41186"
auth_token: "e231c197493a7122d475b4xxxxxxxxxx"
twilio_number: "+440123456789"

slack:
slack_token: "xoxb-286425452756-safjasdf7sl38KLls"
slack_channel: "@my_channel"

telegram:
access_token: "490161424:AAGlRxinBRtKGb21_rlOEMtDFZMXBl6EC0o"
verify: "your_bot"
webhook_url: "your_url.com/webhook"

mattermost:
url: "https://chat.example.com/api/v4"
team: "community"
user: "user@user.com"
pw: "password"

facebook:
verify: "rasa-bot"
secret: "3e34709d01ea89032asdebfe5a74518"
page - access - token: "EAAbHPa7H9rEBAAuFk4Q3gPKbDedQnx4djJJ1JmQ7CAqO4iJKrQcNT0wtD"

webexteams:
access_token: "ADD-YOUR-BOT-ACCESS-TOKEN"
room: "YOUR-WEBEXTEAMS-ROOM-ID"

rocketchat:
user: "yourbotname"
password: "YOUR_PASSWORD"
server_url: "https://demo.rocket.chat"

# socket通道
# 前两个配置值定义Rasa Core在通过socket.io发送或接收消息时使用的事件名称
socketio:
user_message_evt: user_uttered
bot_message_evt: bot_uttered
session_persistence: true / false

# rest通道
rest:
# you don't need to provide anything here - this channel doesn't
# require any credentials

# CallbackInput通道
callback:
# URL to which Core will send the bot responses
url: "http://localhost:5034/bot"

当需要从开发的客户端访问人机对话系统,可以通过使用socket和http通道来实现,它们分别对应socketio输入通道和rest输入通道,而callback与rest通道类似,均是走HTTP协议,但是它不会直接将bot消息返回给发送消息的HTTP请求,而是调用一个URL,可以指定该URL来发送bot消息。

16.endpoints.yml
解析:在Rasa Core项目创建endpoint.yml文件,该文件用于指定RasaCore将要访问的CustomeAction web和nlu web,当rasa core需要执行意图、实体提取和执行action时,就会根据url找到对应的web进行执行。

17.Event对象
解析:Event对象是Rasa Core描述会话中发生的所有事件和明确rasa.core.trackers.DialogueStateTracker该如何更新其状态的基类,因此不能被直接使用,而是通过它包含的具体事件[event]实现。

18.rasa run actions和rasa x --endpoints ENDPOINTS
解析:
[1]rasa run actions --actions ACTIONS:name of action package to be load [default: None]。
[2]rasa x --endpoints ENDPOINTS:configuration file for the model server and the connectors as a yml file [default: None]。

19.class Dispatcher(sender_id, output_channel, domain)
解析:Send messages back to user,如下所示:
[1]utter_response(message):Send a message to the client.
[2]utter_message(text):Send a text to the output channel
[3]utter_custom_message(*elements):Sends a message with custom elements to the output channel.
[4]utter_button_message(text, buttons, **kwargs):Sends a message with buttons to the output channel.
[5]utter_attachment(attachment):Send a message to the client with attachments.
[6]utter_button_template(template, buttons, filled_slots=None, **kwargs):Sends a message template with buttons to the output channel.
[7]utter_template(template, filled_slots=None, **kwargs):Send a message to the client based on a template.
[8]_template_variables(kwargs):Combine slot values and key word arguments to fill templates.
[9]_fill_template_text(template, filled_slots=None, **kwargs)
[10]retrieve_template(template_name, filled_slots=None, **kwargs):Retrieve a named template from the domain.

20.Python Logging Options
解析:
[1]-v[–verbose]:Be verbose. Sets logging level to INFO. [default: None]
[2]-vv[–debug]:Print lots of debugging statements. Sets logging level to DEBUG. [default: None]
[3]–quiet:Be quiet! Sets logging level to WARNING. [default: None]

21.rasa-ui
解析:Rasa UI is a frontend for the Rasa Framework。如下所示:
[1]UI for creating and managing training data - Examples, Intents, Entities, Synonyms, Regex, Stories, Actions, Responses
[2]Manage multiple bots from a single UI/instance of Rasa UI
[3]Create, manage and load different versions of your models for testing and optimizing
[4]Log requests for usage tracking, history and improvements to your models
[5]Easily execute intent parsing using different models
[6]Data is stored in a SQLite DB for backing up/sharing
[7]Can be used with or without a Rasa backend to manage your training data

22.get_slot(key)
解析:retrieves the value of a slot, the return type is Optional[Any].

23.ActionCheckRestaurants
解析:

from rasa_sdk import Action
from rasa_sdk.events import SlotSet

class ActionCheckRestaurants(Action):
   def name(self) -> Text:
      return "action_check_restaurants"
   def run(self,
           dispatcher: CollectingDispatcher,
           tracker: Tracker,
           domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
      cuisine = tracker.get_slot('cuisine')
      q = "select * from restaurants where cuisine='{0}' limit 1".format(cuisine)
      result = db.query(q)
      return [SlotSet("matches", result if result is not None else [])]

24.dispatcher对象
解析:
[1]dispatcher.utter_template()
[2]dispatcher.utter_message()
[3]rasa_sdk.executor.CollectingDispatcher()

25.typing常用类型
解析:
[1]int,long,float:整型,长整形,浮点型
[2]bool,str:布尔型,字符串类型
[3]List,Tuple,Dict,Set:列表,元组,字典,集合
[4]Iterable,Iterator:可迭代类型,迭代器类型
[5]Generator:生成器类型

参考文献:
[1]rasa对话系统踩坑记(四):https://www.jianshu.com/p/9393d319e698
[2]Fallback Actions:https://rasa.com/docs/rasa/core/fallback-actions/#fallback-actions
[3]Rasa命令行:https://panchuang.net/2019/08/31/rasa_command_line_interface/
[4]dispatcher:https://www.pydoc.io/pypi/rasa-core-0.9.0a1/autoapi/dispatcher/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NLP工程化

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值