人工智能大模型AI 安全治理模型安全内容安全提示词注入防护RAG【免费下载链接】GuardrailsNeMo Guardrails is an open-source toolkit for easily adding programmable guardrails to LLM-based conversational systems.项目地址https://gitcode.com/gh_mirrors/ne/Guardrails点击查看免费下载本教程基于仓库中的 interaction_loop 示例讲解如何在 NeMo GuardrailsColang 2.x中构建一个可持续对话的交互主循环interaction loop当用户输入无法匹配任何已定义意图时如何由 LLM 兜底生成响应当用户超过 12 秒沉默时机器人如何主动提醒。读完本文你将掌握flow main事件循环的写法、unhandled user intent兜底机制以及user was silent超时监听器的用法并能直接运行示例验证效果。示例目标与运行效果interaction_loop示例模拟了一个名为 Emma 的交互式虚拟助手bot。它演示了三个核心行为未知输入兜底无法匹配到已有用户意图的输入会被主循环中的unhandled user intent分支捕获并由 LLM 生成合适的回复超时主动提醒用户 12 秒内没有发言机器人会主动提醒你可以问我任何问题常见意图应答问候greeting与告别goodbye由规则直接响应不经过 LLM。原文档给出了完整示例会话可直接运行复现$ nemoguardrails chat --configexamples/v2_x/tutorial/interaction_loop/ Starting the chat (Press Ctrl C twice to quit) ... hi Hi there! pause for 12 seconds You can ask me anything! how are you? I am doing well, thank you for asking! How can I assist you today?可以看到输入hi命中了问候意图并返回 Hi there!沉默 12 秒后机器人主动提示而how are you?这类未定义意图则走 LLM 兜底路径生成了自然回复。运行该命令需要先安装nemoguardrails并配置好models中指定的 LLM 凭据见下文config.yml。配置文件解析config.yml示例目录下包含两个文件config.yml 与 main.co。先看配置colang_version: 2.x models: - type: main engine: openai model: gpt-3.5-turbo-instruct instructions: - type: general content: | Below is a conversation between Emma, a helpful interactive avatar assistant (bot), and a user. ... The bot must not repeat itself if the user was silent. sample_conversation: | user action: user said Hello there! user intent: user expressed greeting bot intent: bot express greeting bot action: bot express Hello! How can I assist you today? and bot gesture Smile ...各字段作用如下colang_version: 2.x声明使用 Colang 2.x 语法import core、flow main等与仓库中 colang/v2_x 运行时 对应models指定主模型示例使用 OpenAI 的gpt-3.5-turbo-instruct。运行前需确保环境已配置对应的 API Key可通过环境变量或nemoguardrails configure设置instructionsgeneral指令定义了 bot 的人格与行为准则简洁作答、多用bot gesture动作、用户沉默时不重复自己sample_conversation给 LLM 的少量示例对话覆盖问候、模糊输入、知识问答、感谢、用户沉默 15 秒、指令执行等场景。其中 user was silent 15 → bot ask Are you still there? 的示例直接为运行时沉默检测→主动询问提供了 few-shot 示范。主循环定义main.comain.co 是本次交互逻辑的核心import core import avatars import timing import llm flow main activate automating intent detection activate generating user intent for unhandled user utterance while True when unhandled user intent $response ...Response to what user said. bot say $response or when user was silent 12.0 bot inform about service or when user expressed greeting bot say Hi there! or when user expressed goodbye bot inform That was fun. Goodbye逐行解读import core / avatars / timing / llm引入基础事件、角色动作、定时器与 LLM 交互所需的库。timing提供了user was silent等超时监听流程见后文activate automating intent detection开启自动意图检测/记录来自 llm.co 中flow automating intent detection它会标记并记录用户与 bot 的意图流activate generating user intent for unhandled user utterance激活未知输入的意图生成流程这是unhandled user intent能被触发的关键前置详见下节while True事件循环主循环内以when ... or when ...监听四类事件按顺序匹配优先级依次为未知用户意图LLM 兜底、用户沉默 12 秒、问候、告别。示例还用两个子流程定义了问候/告别意图的触发词以及沉默提醒的具体话术flow user expressed greeting user said hi or user said hello flow user expressed goodbye user said goodbye or user said I am done or user said I have to go flow bot inform about service bot say You can ask me anything! or bot say Just ask me something!注意user was silent 12.0中 12.0 是浮点秒数即原文档所述的12 秒提醒阈值可任意调整如user was silent 30.0。当主循环命中该分支后调用bot inform about service机器人会在 You can ask me anything! 与 Just ask me something! 两条话术中随机选择输出。源码级原理未知输入如何变成unhandled user intent原文档只描述了行为其底层链路可从 llm.co 中还原第 1 步意图自动生成。flow generating user intent for unhandled user utterance会等待_user_said_something_unexpected事件若 bot 正处于说话状态则中止避免打断否则构造user said ...动作并调用GenerateUserIntentAction让 LLM 从最多 20 个示例流中推断用户意图最后通过send FinishFlow(flow_id$intent)把推断出的意图送回运行时。第 2 步意图匹配。该FinishFlow事件到达后运行时先尝试匹配已定义的user expressed greeting等意图流匹配失败时llm.co 中定义的系统级流程flow unhandled user intent - $intent会拦截所有flow_id以user开头的未处理事件flow unhandled user intent - $intent match UnhandledEvent(eventFinishFlow, flow_idregex(^user ), loop_ids{$self.loop_id}) as $event $intent $event.flow_id第 3 步主循环兜底响应。用户在main.co中定义的when unhandled user intent分支随即被触发通过$response ...Response to what user said.调用 LLM 生成回复并bot say输出——这正是示例中how are you?得到自然回答的原因。从源码结构还可以推断该机制与continuation on unhandled user intent、continuation on undefined flow见 llm.co以及 passthrough 模式见 passthrough.co共同构成 Colang 2.x 的交互延续体系前者生成继续对话的新流程后者在纯透传模式下直接调用 LLM 生成回复。源码级原理user was silent 定时器如何工作user was silent并非魔法关键字而是 timing.co 中定义的系统流程loop(user_was_silent) meta(user_actionTrue) flow user was silent $time_s Wait for when user was silent for $time_s seconds. while True start wait $time_s as $timer_ref when $timer_ref.Finished() break or when UtteranceUserAction.Started() or UtteranceUserAction.TranscriptUpdated() send $timer_ref.Stop() match UtteranceUserAction.Finished() or when UtteranceUserAction.Finished() send $timer_ref.Stop()其工作方式是一个重置式倒计时循环启动$time_s秒的计时器底层为TimerBotAction若计时器自然结束$timer_ref.Finished()说明用户持续沉默流程结束、unhandled分支中的when user was silent 12.0得以触发若期间检测到UtteranceUserAction.Started()、TranscriptUpdated()语音场景下转写更新或Finished()立即停止计时器并等待本轮发言结束随后回到while True重新开始 12 秒倒计时。因此12 秒提醒实际是自用户上次发言结束起的 12 秒用户只要持续输入就不会被误打断。同一文件中还提供了user didnt respondbot 沉默期间用户未回应与bot was silent两个衍生监听器可作进一步扩展。常见问题与调优提示模型与 API 配置示例默认走 OpenAIgpt-3.5-turbo-instruct若替换为其他厂商模型需同步调整config.yml的engine/model字段并配置对应凭据否则nemoguardrails chat启动会失败$response ......的省略号语法这是 Colang 2.x 中由 LLM 生成该字符串的标记等价于调用一次生成动作实际回复内容由模型基于instructions与sample_conversation产生分支顺序即优先级主循环中when分支自上而下匹配若想优先处理告别或问候可调整其在while True中的书写顺序扩展沉默策略可参考timing.co中的user didnt respond $time_s流程实现bot 说话期间用户未回应的差异化提醒。总结interaction_loop教程展示了 Colang 2.x 主循环的完整用法以flow mainwhile True事件监听组织对话状态机用activate generating user intent for unhandled user utterance开启 LLM 意图兜底用unhandled user intent分支生成未知输入响应再用user was silent 12.0实现超时主动提醒。配合 config.yml 中的指令与示例对话以及 llm.co、timing.co 的系统级流程即可快速搭建一个既能处理已知意图、又能优雅应对未知输入与冷场的交互式对话助手。赞分享人工智能大模型AI 安全治理模型安全内容安全提示词注入防护RAG【免费下载链接】GuardrailsNeMo Guardrails is an open-source toolkit for easily adding programmable guardrails to LLM-based conversational systems.项目地址https://gitcode.com/gh_mirrors/ne/Guardrails点击查看免费下载相关推荐NeMo Guardrails Colang 2.x Flow 运行时深度解析事件驱动的推理循环与状态机实现NeMo Guardrails Colang 2.x Flow 运行时深度解析事件驱动的推理循环与状态机实现 本指南聚焦 NeMo Guardrails 中人工智能大模型AI 安全治理模型安全内容安全提示词注入防护RAGNeMo Guardrails迁移指南从Colang 1.0到Colang 2.0NeMo Guardrails迁移指南从Colang 1.0到Colang 2.0 概述 Colang 2.0是NeMo Guardrails对话编程语言的重人工智能大模型AI 安全治理模型安全内容安全提示词注入防护RAG在 NeMo Guardrails 中集成 CrowdStrike AIDR基于 Colang 2.x 的输入/输出护栏实战指南在 NeMo Guardrails 中集成 CrowdStrike AIDR基于 Colang 2.x 的输入/输出护栏实战指南 本指南以仓库内 crowds人工智能大模型AI 安全治理模型安全内容安全提示词注入防护RAG上一篇rknn_model_zoo 实战YOLOv5 实例分割模型在 RKNN 平台的转换与部署全流程下一篇Rich 进度条完全指南使用 rich.progress 构建多任务、可定制、文件级进度显示创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考