Privasis-Cleaner-4B API集成教程:在Python、JavaScript等语言中的应用
Privasis-Cleaner-4B API集成教程在Python、JavaScript等语言中的应用【免费下载链接】Privasis-Cleaner-4B项目地址: https://ai.gitcode.com/hf_mirrors/nvidia/Privasis-Cleaner-4BPrivasis-Cleaner-4B是一款轻量级文本清理模型专为从文本中移除或抽象敏感信息而设计。本文将详细介绍如何在Python、JavaScript等主流编程语言中集成Privasis-Cleaner-4B API帮助开发者轻松实现文本数据的隐私保护。快速了解Privasis-Cleaner-4BPrivasis-Cleaner-4B基于Qwen3 4B Instruct模型构建通过37K指令-输入-输出三元组进行微调能够根据用户提供的清理指令如移除姓名、日期、位置等敏感信息类别处理原始文本并返回合规的清理结果。该模型适用于数据工程师、机器学习从业者和处理敏感文本的组织可用于PII/PHI自动脱敏、隐私保护研究的预处理、内容清理以及合规管道GDPR、HIPAA等。环境准备与安装系统要求操作系统Linux硬件支持NVIDIA H100-80GB、NVIDIA A100等GPU运行时引擎Privasis-Cleaner-4B依赖库transformers、vllm、torch等安装步骤首先克隆仓库git clone https://gitcode.com/hf_mirrors/nvidia/Privasis-Cleaner-4B cd Privasis-Cleaner-4B然后安装所需依赖pip install transformers vllm torchPython语言集成指南使用Transformers库Transformers库是集成Privasis-Cleaner-4B的常用方式以下是详细步骤导入必要的库from transformers import AutoModelForCausalLM, AutoTokenizer加载模型和分词器model_id nvidia/Privasis-Cleaner-4B tokenizer AutoTokenizer.from_pretrained(model_id) model AutoModelForCausalLM.from_pretrained(model_id, torch_dtypeauto, device_mapauto)构建清理指令和待处理文本instruction Remove all person names, exact dates, and exact locations. text On March 3, 2021, Jane Doe visited the clinic in Boston for a follow-up.构建提示词prompt ( f**Sanitization Instruction:**\n{instruction}\n Do not output any explanation or other comment than the sanitized text.\n\n f**Text to sanitize:**\n{text}\n\n **Sanitized Text:** )处理输入并生成清理结果inputs tokenizer.apply_chat_template( [{role: user, content: prompt}], add_generation_promptTrue, enable_thinkingFalse, # 直接输出清理后的文本 return_tensorspt, ).to(model.device) output model.generate(inputs, max_new_tokens4096, do_sampleFalse) response tokenizer.decode(output[0][inputs.shape[-1]:], skip_special_tokensTrue) # 移除可能的Sanitized Text:头部 if Sanitized Text: in response: response response.split(Sanitized Text:)[-1] print(response.strip())使用vLLM服务vLLM提供了OpenAI兼容的API服务使用起来更加便捷启动vLLM服务vllm serve nvidia/Privasis-Cleaner-4B --port 8000使用Python客户端调用APIfrom openai import OpenAI client OpenAI(base_urlhttp://localhost:8000/v1, api_keyEMPTY) instruction Remove all person names, exact dates, and exact locations. text On March 3, 2021, Jane Doe visited the clinic in Boston for a follow-up. prompt ( f**Sanitization Instruction:**\n{instruction}\n Do not output any explanation or other comment than the sanitized text.\n\n f**Text to sanitize:**\n{text}\n\n **Sanitized Text:** ) resp client.chat.completions.create( modelnvidia/Privasis-Cleaner-4B, messages[{role: user, content: prompt}], temperature0.0, max_tokens4096, ) print(resp.choices[0].message.content.strip())JavaScript语言集成指南虽然官方未直接提供JavaScript示例但我们可以通过调用vLLM提供的OpenAI兼容API来实现集成。以下是使用Node.js的示例安装必要的依赖npm install openai编写JavaScript代码const { OpenAI } require(openai); const client new OpenAI({ baseURL: http://localhost:8000/v1, apiKey: EMPTY, }); async function sanitizeText() { const instruction Remove all person names, exact dates, and exact locations.; const text On March 3, 2021, Jane Doe visited the clinic in Boston for a follow-up.; const prompt ( **Sanitization Instruction:**\n${instruction}\n Do not output any explanation or other comment than the sanitized text.\n\n **Text to sanitize:**\n${text}\n\n **Sanitized Text:** ); const response await client.chat.completions.create({ model: nvidia/Privasis-Cleaner-4B, messages: [{ role: user, content: prompt }], temperature: 0.0, max_tokens: 4096, }); console.log(response.choices[0].message.content.strip()); } sanitizeText();API调用最佳实践输入格式规范Privasis-Cleaner-4B的输入需要遵循特定的格式以确保模型正确理解清理指令和待处理文本**Sanitization Instruction:** {instruction} Do not output any explanation or other comment than the sanitized text. **Text to sanitize:** {text} **Sanitized Text:**其中{instruction}是用户指定的清理指令{text}是需要处理的原始文本。常见问题解决模型输出包含Sanitized Text:头部解决方案在处理响应时检查并移除该头部如Python示例中所示。长文本处理模型支持最长262,144 tokens的输入对于超长文本建议进行分段处理。性能优化使用vLLM服务可以显著提高推理速度特别是在处理大量文本时。总结Privasis-Cleaner-4B作为一款高效的文本清理模型为开发者提供了简单易用的API集成方式。通过本文介绍的方法您可以轻松在Python、JavaScript等语言中集成该模型实现敏感信息的自动移除为数据隐私保护提供有力支持。无论是数据预处理、内容审核还是合规需求Privasis-Cleaner-4B都能成为您的得力助手。如需更多详细信息请参考项目中的README.md文件或查看Privasis benchmark进行模型评估。【免费下载链接】Privasis-Cleaner-4B项目地址: https://ai.gitcode.com/hf_mirrors/nvidia/Privasis-Cleaner-4B创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考