基于事件的 API#
您可以通过基于事件的 API 使用 guardrails 配置,使用 LLMRails.generate_events_async
和 `LLMRails.generate_events。
使用示例
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
#
为 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 来处理需要的新事件来做到这一点。否则,自定义事件将被忽略。
典型用法#
通常,您需要
将特定用户的事件历史记录持久化到数据库中。
每当有新消息或其他事件时,您获取历史记录并附加新事件。
使用 guardrails API 生成下一个事件。
过滤
StartUtteranceBotAction
事件并将它们返回给用户。将事件历史记录持久化回数据库中。