基于事件的 API#

您可以使用基于事件的 API 通过 LLMRails.generate_events_async`LLMRails.generate_events 来使用 Guardrails 配置。

使用示例

import json
from nemoguardrails import LLMRails, RailsConfig

config = RailsConfig.from_path("path/to/config")
app = LLMRails(config)

new_events = app.generate_events(events=[{
    "type": "UtteranceUserActionFinished",
    "final_transcript": "Hello! What can you do for me?"
}])
print(json.dumps(new_events, indent=True))

输出示例

[
  {
    "type": "StartInternalSystemAction",
    "action_name": "generate_user_intent",
    "action_params": {},
    "action_result_key": null,
    "is_system_action": true,
  },
  {
    "type": "InternalSystemActionFinished",
    "action_name": "generate_user_intent",
    "action_params": {},
    "action_result_key": null,
    "status": "success",
    "return_value": null,
    "events": [{ "type": "UserIntent", "intent": "express greeting" }],
    "is_system_action": true,
  },
  { "type": "UserIntent", "intent": "express greeting" },
  { "type": "BotIntent", "intent": "express greeting" },
  {
    "type": "StartInternalSystemAction",
    "action_name": "retrieve_relevant_chunks",
    "action_params": {},
    "action_result_key": null,
    "is_system_action": true,
  },
  { "type": "ContextUpdate", "data": { "relevant_chunks": "" } },
  {
    "type": "InternalSystemActionFinished",
    "action_name": "retrieve_relevant_chunks",
    "action_params": {},
    "action_result_key": null,
    "status": "success",
    "return_value": "",
    "events": null,
    "is_system_action": true,
  },
  {
    "type": "StartInternalSystemAction",
    "action_name": "generate_bot_message",
    "action_params": {},
    "action_result_key": null,
    "is_system_action": true,
  },
  {
    "type": "ContextUpdate",
    "data": { "_last_bot_prompt": "<<REMOVED FOR READABILITY>>>" },
  },
  {
    "type": "InternalSystemActionFinished",
    "action_name": "generate_bot_message",
    "action_params": {},
    "action_result_key": null,
    "status": "success",
    "return_value": null,
    "events": [{ "type": "StartUtteranceBotAction", "script": "Hello!" }],
    "is_system_action": true,
  },
  { "type": "StartUtteranceBotAction", "script": "Hello!" },
  { "type": "Listen" },
]

事件类型#

NeMo Guardrails 支持多种类型的事件。有些用于内部使用(例如,UserIntentBotIntent),而另一些则代表“公共”接口(例如,UtteranceUserActionFinishedStartUtteranceBotAction)。

UtteranceUserActionFinished#

用户的原始消息。

示例

{
  "type": "UtteranceUserActionFinished",
  "final_transcript": "Hello!"
}

UserIntent#

计算出的用户意图(也称为规范形式)。

示例

{
  "type": "UserIntent",
  "intent": "express greeting"
}

BotIntent#

计算出的 Bot 应说的意图。

示例

{
  "type": "BotIntent",
  "intent": "express greeting"
}

StartUtteranceBotAction#

Bot 的最终消息。

示例

{
  "type": "StartUtteranceBotAction",
  "script": "Hello!"
}

StartInternalSystemAction#

需要启动一个 action。

示例

{
  "type": "StartInternalSystemAction",
  "action_name": "generate_user_intent",
  "action_params": {},
  "action_result_key": null,
  "is_system_action": true
}

InternalSystemActionFinished#

一个 action 已完成。

示例

{
  "type": "InternalSystemActionFinished",
  "action_name": "generate_user_intent",
  "action_params": {},
  "action_result_key": null,
  "status": "success",
  "return_value": null,
  "events": [
    {
      "type": "UserIntent",
      "intent": "express greeting"
    }
  ],
  "is_system_action": true
}

ContextUpdate#

对话上下文已更新。

示例

{
  "type": "ContextUpdate",
  "data": {
    "user_name": "John"
  }
}

listen#

Bot 已完成事件处理,正在等待新的输入。

示例

{
  "type": "Listen"
}

自定义事件#

您也可以使用自定义事件

{
  "type": "some_other_type",
  ...
}

注意:您需要确保 Guardrails 逻辑能够处理自定义事件。您可以通过更新您的 Flows 来处理所需的新事件。否则,自定义事件将被忽略。

典型用法#

通常,您需要

  1. 将特定用户的事件历史记录持久化到数据库中。

  2. 每当有新消息或其他事件时,您就获取历史记录并追加新事件。

  3. 使用 Guardrails API 生成后续事件。

  4. 过滤 StartUtteranceBotAction 事件并将其返回给用户。

  5. 将事件历史记录重新持久化到数据库中。