ONNX 模型库
返回模型

说明文档

BDC 领域护栏 - ModernBERT

用于汽车服务领域相关性的二分类器。判断用户消息是否与 BDC(业务发展中心)对话相关。

标签

  • BLOCK (0):消息偏离主题,与汽车服务无关
  • ALLOW (1):消息与汽车服务、预约或对话相关

训练详情

  • 基础模型:answerdotai/ModernBERT-base
  • 方法:LoRA 微调(r=32, alpha=64),采用护栏优化损失函数
  • 训练数据:约 14,600 个样本,使用置信度加权的软标签
  • 优化:对误拦截采用非对称惩罚(1.5 倍权重)
  • 类别权重:无(自然分布)

性能指标(阈值:0.35)

指标 数值
准确率 91.9%
宏平均 F1 0.907
ALLOW 精确率 89.1%
ALLOW 召回率 98.6%
误拦截率 1.4% (20/1397)
BLOCK 精确率 0.839
BLOCK 召回率 0.910

推荐阈值:0.35(在保持高 ALLOW 召回率的同时优化最小误拦截)

使用方法

ONNX(推荐用于生产环境)

import onnxruntime as ort
import numpy as np
from transformers import AutoTokenizer
from huggingface_hub import hf_hub_download

# 下载未量化的 ONNX 模型
onnx_path = hf_hub_download("karanchugh/bdc-domain-guardrail-modernbert", "onnx/model.onnx")
tokenizer = AutoTokenizer.from_pretrained("karanchugh/bdc-domain-guardrail-modernbert")

# 创建会话
session = ort.InferenceSession(onnx_path, providers=['CPUExecutionProvider'])

# 使用最优阈值进行推理
text = "Can I schedule an oil change?"
inputs = tokenizer(text, return_tensors="np", truncation=True, max_length=256, padding="max_length")
outputs = session.run(None, {
    "input_ids": inputs["input_ids"].astype(np.int64),
    "attention_mask": inputs["attention_mask"].astype(np.int64),
})

logits = outputs[0][0]
probs = np.exp(logits) / np.exp(logits).sum()
allow_prob = probs[1]

# 使用 threshold=0.35 以获得最优护栏性能
prediction = "ALLOW" if allow_prob > 0.35 else "BLOCK"
print(f"{prediction}: {allow_prob:.3f}")

PyTorch

from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

model = AutoModelForSequenceClassification.from_pretrained("karanchugh/bdc-domain-guardrail-modernbert")
tokenizer = AutoTokenizer.from_pretrained("karanchugh/bdc-domain-guardrail-modernbert")

text = "Can I schedule an oil change?"
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=256)

with torch.no_grad():
    outputs = model(**inputs)
    probs = torch.softmax(outputs.logits, dim=-1)
    allow_prob = probs[0][1].item()
    
# 使用 threshold=0.35 以获得最优护栏性能
prediction = "ALLOW" if allow_prob > 0.35 else "BLOCK"
print(f"{prediction}: {allow_prob:.3f}")

文件

  • model.safetensors:完整合并的 PyTorch 模型
  • onnx/model.onnx未量化的 ONNX 模型(571 MB)- 推荐用于生产环境
  • tokenizer.json:快速分词器

重要:决策阈值

请勿使用默认阈值 0.5!

该模型针对 threshold=0.35 进行了优化,可提供:

  • 98.6% ALLOW 召回率(捕获 98.6% 的有效客户消息)
  • 仅 1.4% 误拦截率(对客户体验影响最小)
  • 89.1% ALLOW 精确率(可接受的误放行率)

使用 threshold=0.5 将导致 7.7% 的误拦截率(108 条有效消息被拦截)。

部署配置

GUARDRAIL_USE_DOMAIN_MODEL=true
GUARDRAIL_RELEVANCE_THRESHOLD=0.35  # 关键:使用 0.35,而非 0.5!

模型大小与延迟

  • 模型大小:571 MB(未量化以获得最佳准确率)
  • CPU 推理:每个样本约 100-130ms
  • 建议:使用批处理以提高吞吐量

注意:量化版本(254 MB)准确率下降 6%,不推荐使用。

训练改进

该模型的训练特点:

  • ✅ 无逆频率加权(防止过度拦截)
  • ✅ 非对称损失惩罚(误拦截 1.5 倍权重)
  • ✅ 阈值优化(0.35 对比默认 0.5)

结果:与基线相比,误拦截率降低 81%(从 7.7% 降至 1.4%)。

karanchugh/bdc-domain-guardrail-modernbert

作者 karanchugh

text-classification
↓ 1 ♥ 0

创建时间: 2026-01-22 09:11:34+00:00

更新时间: 2026-01-22 09:11:44+00:00

在 Hugging Face 上查看

文件 (12)

.gitattributes
README.md
config.json
model.safetensors
onnx/config.json
onnx/model.onnx ONNX
onnx/special_tokens_map.json
onnx/tokenizer.json
onnx/tokenizer_config.json
special_tokens_map.json
tokenizer.json
tokenizer_config.json