
人工智能大模型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 是一个用于为基于 LLM 的对话系统添加可编程护栏的开源工具包而 Colang 2.x 是其新一代流程定义语言。本文以仓库测试夹具tests/test_configs/mvp_v2_x_d/testing.md为核心剖析其中指令驱动动态流程生成Instruction-Driven Flow Generation这一实验性特性的设计意图、运行机制与可靠性边界。读完本文你将掌握Colang 2.x 中flow do $instructions、fallback与continuation on undefined flow的完整调用链MVP 测试配置config.yml/config.co/actions.py的逐项含义以及如何借助TestChat与RailsConfig.from_path复现与验证该测试场景。关联文档定位一张预期正确的测试用例表tests/test_configs/mvp_v2_x_d/testing.md全文仅围绕一个核心场景展开用户输入一段自然语言指令do something: ...系统应解析出意图并动态生成一个 Colang 流程来响应该指令。文档以注释方式给出了两条对话示例- user: do something: Ask user to choose from two offered colors: red and blue then give a answer depending on the user choice - bot: What color would you like? Red or blue? - user: I like blue - bot: I dont like blue, but its a nice color!文档标题同时点明了关键前提——Since we dont have yet an NLU matching for generated flows the correct answer is not guaranteed由于目前还没有针对生成流程的 NLU 匹配正确答案无法保证。这句话是理解整个测试夹具的钥匙该测试旨在验证动态流程生成的能力基线而非稳定性承诺它暴露了 Colang 2.x 在运行时生成流程 事件匹配链路中的已知局限。测试配置全景mvp_v2_x_d夹具的三件套该测试用例不是一个孤立文档而是tests/test_configs/mvp_v2_x_d/夹具目录的说明文件。整个夹具由三份文件构成文件角色tests/test_configs/mvp_v2_x_d/config.ymlRails 主配置声明 Colang 版本、LLM 模型与示例对话sample_conversationtests/test_configs/mvp_v2_x_d/config.coColang 2.x 源码预定义流程 动态生成流程do/fallback/continuation on undefined flowtests/test_configs/mvp_v2_x_d/actions.py自定义 ActionGenerateFlowAction的桩实现tests/test_configs/mvp_v2_x_d/testing.md本文讨论的测试说明文档此外tests/test_configs/mvp_v2_x_e/config.yml仅包含一行import_paths: - tests/test_configs/mvp_v2_x_d用于验证配置的跨目录导入能力——这说明mvp_v2_x_d被设计为可被其他配置复用导入的基础版测试底座。配置解析config.yml中的模型与示例对话# Using OpenAI colang_version: 2.x models: - type: main engine: openai model: gpt-3.5-turbo-instructcolang_version: 2.x声明使用 Colang 2.x 语法与仓库中nemoguardrails/colang/v2_x/目录对应models指定主 LLM 为 OpenAI 的gpt-3.5-turbo-instructCompletion 型模型。在testing.md所描述的动态流程生成场景中该模型承担意图识别GenerateUserIntentAction与流程生成GenerateFlowFromInstructionsAction等关键 LLM 调用sample_conversation提供了一条完整的示例对话问候、能力询问、NVIDIA 历史问答、致谢为 LLM 提供 few-shot 上下文帮助其理解对话结构。需要强调的是该配置是测试环境的基线设置测试运行时可注入llm_completions桩替换真实 LLM见下文测试驱动部分因此模型名本身不影响确定性测试的结果。Colang 2.x 核心流程拆解config.co的六类构造tests/test_configs/mvp_v2_x_d/config.co是整个夹具的灵魂它用 Colang 2.x 展示了从简单应答到动态生成的完整能力阶梯1. 简单应答Simple answerflow answer question about france Answer the following question: What is the capital of France. bot say The capital of France its Paris.通过 docstring 描述意图预定义流程直接给出应答。2. 多模态应答Multimodal answerflow tell a joke Tell a joke. bot say Why dont scientists trust atoms? Because they make up everything! and bot gesture smileand bot gesture smile演示了 Colang 2.x 中一条语句内并发执行多个 bot 动作的能力。3. 循环Loopflow count Count from 1 to 5. $count 1 while $count 5 bot say {$count} $count $count 1展示了变量赋值、while循环与字符串插值{$count}。4. 序列Sequenceflow bot ask user for age Ask the user how old she is and response with You look younger! when she answers something. bot say How old are you? user said I am 16 bot say You look younger!演示bot 提问 → 用户回答 → bot 回应的顺序事件流。5. 分支Branchingflow bot ask user to pick a color Ask user to choose from two offered colors: red and blue then give a answer depending on the user choice. bot say What color would you like? Red or blue? when user said Red bot say I like red! or when user said Blue bot say I dont like blue!这正是testing.md中第一条对话所对应的预定义静态流程testing.md期望的回复路径What color would you like? → I dont like blue...与该流程结构高度吻合说明文档描述的对话既可以由静态流程支撑也可以由动态生成的流程支撑——这正是正确答案不保证的根源两种路径都可能被触发。6. 小游戏A little gameflow number guessing game Ask the user to guess a random number between 1 and 100; ... bot say Hi, please guess the random number between 0 and 100 $random_number 66 $is_correct_number False while $is_correct_number user said number $number 13 if $number ! None if $number $random_number bot say Sorry, this number is too low! elif $number $random_number bot say Sorry, this number is too high! else bot say Congratulation! This is the correct number! $is_correct_number True综合运用循环、条件与状态变量注释也标明$number 13仅为占位实际场景应从用户消息中提取数值。公共基础流程Commonmeta(exclude_from_llmTrue) flow user said $text match UtteranceUserAction.Finished(final_transcript$text)meta(exclude_from_llmTrue)标记的流程user said、bot say、wait等是运行时基础设施不应被 LLM 当作可生成/可调用的用户流程match UtteranceUserAction.Finished(...)是监听用户话语结束事件的标准写法。动态流程生成testing.md主题背后的三条关键流程testing.md的场景用户输入 do something: ... 后系统动态响应用户指令在config.co中有三条支撑流程它们构成了 Colang 2.x 指令驱动对话的核心链路流程一custom instructions—— 指令入口meta(exclude_from_llmTrue) flow custom instructions user provided custom instructions $instructions await GetLastUserMessageAction() do $instructions前置意图user provided custom instructions定义于同文件user said do something即用户消息以 do something 开头GetLastUserMessageAction取出用户最新消息作为指令文本委托给do $instructions执行。流程二do—— 指令到流程的桥接meta(exclude_from_llmTrue) flow do $instructions $flow_info await GenerateFlowFromInstructionsAction(instructions$instructions) await AddFlowsAction(config$flow_info.body) send StartFlow(flow_id$flow_info[name]) match FlowStarted(flow_id$flow_info[name]) as $event_ref match $event_ref.flow.Finished()GenerateFlowFromInstructionsAction是动态流程生成的核心系统 Action。其源码实现位于 nemoguardrails/actions/v2_x/generation.py#L488-L559通过instruction_flows_index.search(textinstructions, max_results5, ...)检索相似的历史流程作为 few-shot 示例见_ensure_instruction_flows_index生成新的流程名dynamic_{uuid的前4位}并渲染Task.GENERATE_FLOW_FROM_INSTRUCTIONS提示词交给 LLMllm_params{temperature: self.config.lowest_temperature}使生成尽可能确定LLM 输出经过解析若首行以缩进开头则包装为flow {flow_name}\n 输出返回给AddFlowsAction动态注册若解析失败则回退到固定的兜底流程bot inform LLM issueSorry! There was an issue in the LLM result form GenerateFlowFromInstructionsAction!。此外nemoguardrails/colang/v2_x/library/llm.co#L348-L359中还存在一个未文档化的实验流程flow execute llm instruction $instructions其逻辑与do基本一致生成 →AddFlowsAction→StartFlow→ 等待FlowStarted/Finished→RemoveFlowsAction清理可作为指令执行链路的标准参考实现。流程三fallback与continuation on undefined flow—— 未命中意图的兜底meta(exclude_from_llmTrue) flow fallback match UnhandledEvent(eventUtteranceUserActionFinished) as $event start_new_flow_instance: $intent await GenerateUserIntentAction(user_utterance$event.final_transcript) $exists await CheckForActiveEventMatchAction(nameFlowFinished, flow_id$intent) if $exists send FinishFlow(flow_id$intent) else send DynamicFlowFinished(flow_id$intent) $flow_info await GenerateFlowContinuationAction() await AddFlowsAction(config$flow_info[body]) send StartFlow(flow_id$flow_info[name]) match FlowStarted(flow_id$flow_info[name]) as $event_ref match $event_ref.flow.Finished()fallback在用户话语未被任何激活流程匹配时触发先由GenerateUserIntentAction识别意图若无活动流程则调用GenerateFlowContinuationAction续写新流程并启动。continuation on undefined flow则处理StartFlow目标流程不存在的情形——通过GenerateFlowFromNameAction动态补全流程源码后注册启动。二者共同构成任意用户输入都能得到响应的完整性兜底。自定义 Action 桩actions.py的测试注入点from nemoguardrails.actions import action action(nameGenerateFlowAction) async def generate_flow(instructions: str, flow_name: str): flow_source f flow {flow_name} bot say Why dont scientists trust atoms? Because they make up everything! bot say Im smiling. return flow_source该文件用action装饰器注册名为GenerateFlowAction的 Action返回一段硬编码的 Colang 流程源码讲笑话 微笑。这是测试桩的典型用法在不依赖真实 LLM 的情况下为动态流程生成环节提供确定性输出。文件末尾注释掉的GenerateUserIntentAction桩则展示了一种更细粒度的注入思路按关键词 hello / i want 返回意图说明意图识别同样可被桩替换。测试驱动TestChat与确定性断言testing.md场景对应的自动化测试位于 tests/v2_x/test_mvp_v2_x_d.pyconfig RailsConfig.from_path(os.path.join(CONFIGS_FOLDER, mvp_v2_x_d)) chat TestChat(config, llm_completions[]) chat hi chat Hello world!RailsConfig.from_path从夹具目录加载config.yml、config.co、actions.pyTestChat是测试用对话驱动工具tests/utils.py中定义表示用户输入断言 bot 回复llm_completions[]表示不注入任何 LLM 回复桩纯走预定义流程因此 hi 得到 Hello world! 而非由模型生成文件末尾有一个被TODO: Fix this test注释掉的历史用例它原本用llm_completions桩依次模拟意图识别user provide custom instructions→ 流程生成讲原子笑话然后输入do something: tell me a joke并断言 bot 输出笑话——正是testing.md所述指令驱动场景。该测试被注释掉与文档标题正确答案不保证相互印证动态流程生成的输出与事件匹配尚不稳定官方选择暂缓将其固化为确定性断言。参考同级测试tests/v2_x/test_mvp_v2_x_c.py可以看到更成熟的确定性断言模式chat hi→Hello world!chat asdf→Im not sure how to respond.未知意图兜底多轮对话后chat hi→Hello again!。这展示了 Colang 2.x 测试的标准写法对稳定路径用llm_completions[]做纯确定性验证对不稳定路径动态生成保留注释或仅验证框架不崩溃。可靠性边界为什么正确答案不保证综合文档声明与源码证据可以归纳testing.md主题的可靠性边界来自三个层面缺少对生成流程的 NLU 匹配testing.md标题明确指出没有针对动态生成流程的 NLU 匹配。预定义流程如flow bot ask user to pick a color可以用when user said Red做事件匹配而动态生成流程的内部事件匹配依赖 LLM 生成质量无法静态保证LLM 生成的固有不确定性GenerateFlowFromInstructionsAction/GenerateFlowContinuationAction依赖 LLM 输出合法 Colang 源码。尽管实现中降低了温度temperature0/lowest_temperature、注入了相似流程作为 few-shot 示例并在解析失败时回退到bot inform LLM issue兜底流程但生成的流程语义仍可能偏离用户指令nemoguardrails/actions/v2_x/generation.py测试策略的保守性test_mvp_v2_x_d.py中与动态生成相关的用例被TODO注释挂起而稳定路径用例保持启用——这是仓库对实验性能力与确定性回归保障之间权衡的直接证据。实战指引如何基于该夹具复现与扩展复现测试在仓库根目录运行pytest tests/v2_x/test_mvp_v2_x_d.py -v可看到启用的test_1通过hi → Hello world!。注意测试使用TestChat与配置桩不发起真实 LLM 调用因此无需配置 OpenAI API Key。探索动态生成将test_mvp_v2_x_d.py中被注释的test_2恢复并传入合理的llm_completions桩意图 流程生成各一条即可在隔离真实模型的情况下验证do $instructions链路若要验证解析失败路径可让桩返回非法 Colang 源码观察是否触发bot inform LLM issue兜底。配置导入复用参考tests/test_configs/mvp_v2_x_e/config.yml通过import_paths将mvp_v2_x_d作为基础配置导入在其上叠加新流程即可围绕动态生成能力搭建自己的测试集。从源码理解链路深入阅读 nemoguardrails/actions/v2_x/generation.pyGenerateFlowFromInstructionsAction/GenerateFlowContinuationAction/GenerateUserIntentAction的系统 Action 实现与 nemoguardrails/colang/v2_x/library/llm.copolling llm request response与实验流程execute llm instruction可完整掌握指令 → 流程生成 → 注册 → 启动 → 匹配结束事件的运行时闭环。结语tests/test_configs/mvp_v2_x_d/testing.md虽只有寥寥数行却是 Colang 2.x 动态流程生成能力最凝练的说明书它既展示了用户指令驱动对话这一先进交互形态的愿景静态流程示例 动态生成链路的完整配置又坦诚标注了其当前的技术边界——缺少针对生成流程的 NLU 匹配导致正确答案无法保证。对希望在 NeMo Guardrails Colang 2.x 中实验指令驱动对话的开发者而言本夹具config.yml、config.co、actions.py是理解该特性、复现其行为、并评估其生产可用性的最佳起点。赞分享人工智能大模型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 Hello World 实战用 config.yml 与 Colang 流打造可编程对话护栏NeMo Guardrails Hello World 实战用 config.yml 与 Colang 流打造可编程对话护栏 本文以开源仓库中的 Hello人工智能大模型AI 安全治理模型安全内容安全提示词注入防护RAGNeMo Guardrails迁移指南从Colang 1.0到Colang 2.0NeMo Guardrails迁移指南从Colang 1.0到Colang 2.0 概述 Colang 2.0是NeMo Guardrails对话编程语言的重人工智能大模型AI 安全治理模型安全内容安全提示词注入防护RAG终极Colang语言入门指南如何用NeMo Guardrails定义智能对话流程终极Colang语言入门指南如何用NeMo Guardrails定义智能对话流程 想要为你的LLM对话系统添加可编程护栏吗NeMo Guardrails项目人工智能大模型AI 安全治理模型安全内容安全提示词注入防护RAG上一篇3步解锁网易云音乐隐藏功能BetterNCM Installer完全指南下一篇参与 AI-For-Beginners 课程贡献CLA 流程、开放主题清单与多语言翻译实战创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考