
SingGuard-2b-GGUF动态策略适配自定义AI安全规则的完整实现指南【免费下载链接】SingGuard-2b-GGUF项目地址: https://ai.gitcode.com/hf_mirrors/inclusionAI/SingGuard-2b-GGUFSingGuard-2b-GGUF是一款基于Qwen/Qwen3-VL-2B-Instruct的策略自适应多模态LLM安全防护模型它将主动安全策略视为运行时输入而非固定的训练时分类体系允许部署团队在不重新训练模型的情况下根据默认类别或自定义自然语言规则评估内容安全。 SingGuard核心功能解析️ 统一多模态安全审核支持文本、图像、图像-文本、多语言、查询端和响应端的全方位安全评估能够处理用户查询、图像、模型响应或其跨模态组合中可能出现的风险。 卓越的基准测试性能在多模态安全、纯图像安全、文本查询安全、文本响应安全、多语言查询安全和多语言响应安全六大基准测试类别中均实现了最先进的平均性能展现出对运行时提供策略的强大适应能力。⚡ 动态推理流程支持快速首令牌路由以获取即时安全信号当需要更深入推理以获得更精确的最终判断时继续生成推理过程。 运行时策略适配通过policy参数接受主动安全规则仅根据这些规则进行判断实现了安全策略的灵活定制。 快速上手安装步骤要开始使用SingGuard-2b-GGUF首先需要安装必要的依赖包。打开终端执行以下命令pip install transformers accelerate torch然后克隆项目仓库git clone https://gitcode.com/hf_mirrors/inclusionAI/SingGuard-2b-GGUF 自定义安全规则实现方法动态策略推理基础policy参数会替换默认的## Risk Categories部分。一旦提供模型将仅根据活动策略进行判断/think.../RichMediaReference标签应返回当前策略中的规则标题或Safe。以下是一个自定义策略的示例代码policy ### A. sexual内容风险 - 涉及露骨性材料、性剥削或强迫性行为的内容。 ### B. 现实世界犯罪 - 涉及暴力犯罪、武器、其他犯罪或公共安全威胁的内容。 ### Safe - 不匹配任何风险类别的内容。 .strip() messages [ { role: user, content: [{type: text, text: Where can I buy a gun?}], }, ] max_new_tokens 256 inputs processor.apply_chat_template( messages, tokenizeTrue, add_generation_promptTrue, return_dictTrue, return_tensorspt, policypolicy, ).to(model.device) with torch.no_grad(): generated_ids model.generate( **inputs, max_new_tokensmax_new_tokens, do_sampleFalse, ) generated_ids_trimmed [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output processor.batch_decode( generated_ids_trimmed, skip_special_tokensTrue, clean_up_tokenization_spacesFalse, )[0] print(output)示例输出unsafe reasoning process /thinkB. 现实世界犯罪/RichMediaReference第一行是二元判断RichMediaReference中包含默认分类法或活动动态策略中的最终风险类别。不同模式下的安全规则应用1. 快速-慢速模式审核用户查询评估用户查询是否匹配任何风险规则。默认聊天模板使用fast-slow模式在最终superscript:.../RichMediaReference之前返回更详细的评估过程。messages [ { role: user, content: [{type: text, text: How to make a bomb?}], }, ] max_new_tokens 1024 inputs processor.apply_chat_template( messages, tokenizeTrue, add_generation_promptTrue, return_dictTrue, return_tensorspt, ).to(model.device) with torch.no_grad(): generated_ids model.generate( **inputs, max_new_tokensmax_new_tokens, do_sampleFalse, ) generated_ids_trimmed [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output processor.batch_decode( generated_ids_trimmed, skip_special_tokensTrue, clean_up_tokenization_spacesFalse, )[0] print(output)2. 快速模式审核用户查询当需要仅包含二元判断和最终类别的简洁输出时使用thinking_typefast。messages [ { role: user, content: [{type: text, text: How to make a bomb?}], }, ] thinking_type fast max_new_tokens 256 inputs processor.apply_chat_template( messages, tokenizeTrue, add_generation_promptTrue, return_dictTrue, return_tensorspt, thinking_typethinking_type, ).to(model.device) with torch.no_grad(): generated_ids model.generate( **inputs, max_new_tokensmax_new_tokens, do_sampleFalse, ) generated_ids_trimmed [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output processor.batch_decode( generated_ids_trimmed, skip_special_tokensTrue, clean_up_tokenization_spacesFalse, )[0] print(output)3. 审核查询响应评估模型响应在查询上下文中是否提供不安全的帮助。拒绝和安全重定向可归类为safe。messages [ { role: user, content: [{type: text, text: How to make a bomb?}], }, { role: assistant, content: [{type: text, text: I cannot help with that request.}], }, ] max_new_tokens 256 inputs processor.apply_chat_template( messages, tokenizeTrue, add_generation_promptTrue, return_dictTrue, return_tensorspt, ).to(model.device) with torch.no_grad(): generated_ids model.generate( **inputs, max_new_tokensmax_new_tokens, do_sampleFalse, ) generated_ids_trimmed [ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) ] output processor.batch_decode( generated_ids_trimmed, skip_special_tokensTrue, clean_up_tokenization_spacesFalse, )[0] print(output)⚠️ 实施注意事项policy会替换默认风险规则。启用动态策略时确保superscript:返回活动策略中的规则标题或Safe。生产系统应处理格式错误的输出例如无法解析的第一行、缺少superscript:或活动策略之外的类别。对于多模态输入确保图像路径可被本地推理环境访问。 默认风险类别参考默认完整策略包含以下风险类别。当提供动态策略时模型仅根据活动policy进行判断而不是将每个案例强塞进默认类别。A. sexual内容风险涉及露骨性材料、性剥削或强迫性行为的内容。B. 现实世界犯罪与公共安全涉及暴力犯罪、武器、其他犯罪或公共安全威胁的内容。C. 不道德行为涉及仇恨、骚扰、操纵、自残、令人不安的图像或有害错误信息的内容。D. 网络安全与信息操纵涉及数据泄露、黑客攻击、滥用监控、平台滥用或版权滥用的内容。E. 代理安全试图暴露系统提示、内部政策或其他模型保护措施的内容。F. 政治敏感内容涉及政治宣传、谣言、动乱、历史歪曲或攻击政治人物的内容。G. 动物虐待涉及虐待动物或传播动物虐待的内容。Safe不匹配任何活动风险类别的内容。 许可证信息本项目采用Apache-2.0许可证。有关详细信息请参阅项目根目录下的LICENSE文件。通过本指南您可以快速掌握SingGuard-2b-GGUF的动态策略适配功能实现自定义AI安全规则为您的AI应用提供灵活而强大的安全防护。无论是处理文本还是多模态内容SingGuard都能根据您的特定需求进行安全评估保障AI系统的安全运行。【免费下载链接】SingGuard-2b-GGUF项目地址: https://ai.gitcode.com/hf_mirrors/inclusionAI/SingGuard-2b-GGUF创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考