基于事件的 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 支持多种事件类型。有些是供内部使用的(例如,UserIntent
、BotIntent
),而另一些则代表“公共”接口(例如,UtteranceUserActionFinished
、StartUtteranceBotAction
)。
UtteranceUserActionFinished
来自用户的原始消息。
示例
{
"type": "UtteranceUserActionFinished",
"final_transcript": "Hello!"
}
UserIntent
为用户所说内容计算出的意图(又名规范形式)。
示例
{
"type": "UserIntent",
"intent": "express greeting"
}
BotIntent
为机器人应该说什么计算出的意图。
示例
{
"type": "BotIntent",
"intent": "express greeting"
}
StartUtteranceBotAction
来自机器人的最终消息。
示例
{
"type": "StartUtteranceBotAction",
"script": "Hello!"
}
StartInternalSystemAction
需要启动一个操作。
示例
{
"type": "StartInternalSystemAction",
"action_name": "generate_user_intent",
"action_params": {},
"action_result_key": null,
"is_system_action": true
}
InternalSystemActionFinished
一个操作已完成。
示例
{
"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
机器人已完成事件处理,正在等待新输入。
示例
{
"type": "Listen"
}
自定义事件
您还可以使用自定义事件
{
"type": "some_other_type",
...
}
注意:您需要确保 guardrails 逻辑可以处理自定义事件。您可以通过更新您的流程来处理需要的新事件来做到这一点。否则,自定义事件将被忽略。
典型用法
通常,您需要
将特定用户的事件历史记录持久保存在数据库中。
每当有新消息或其他事件时,您都会获取历史记录并附加新事件。
使用 guardrails API 生成下一个事件。
过滤
StartUtteranceBotAction
事件并将它们返回给用户。将事件历史记录持久保存回数据库中。