Skip to content

Commit efe47a6

Browse files
yossiovadiaclaude
andcommitted
fix(pii): enable LoRA PII auto-detection with minimal changes
Switch PII classification from hardcoded ModernBERT to auto-detecting Candle BERT classifier. The Rust layer already has built-in auto-detection that checks for lora_config.json and routes to LoRA or Traditional models. Changes: 1. Init: Use InitCandleBertTokenClassifier (has auto-detect built-in) 2. Inference: Use ClassifyCandleBertTokens (auto-routes to initialized classifier) This enables LoRA PII models to work automatically without config changes, providing higher confidence scores for PII entity detection. Fixes vllm-project#647 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> Signed-off-by: Yossi Ovadia <[email protected]>
1 parent 0816eb8 commit efe47a6

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/semantic-router/pkg/classification/classifier.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,41 @@ func createJailbreakInference(useModernBERT bool) JailbreakInference {
139139
}
140140

141141
type PIIInitializer interface {
142-
Init(modelID string, useCPU bool) error
142+
Init(modelID string, useCPU bool, numClasses int) error
143143
}
144144

145-
type ModernBertPIIInitializer struct{}
145+
type PIIInitializerImpl struct{}
146146

147-
func (c *ModernBertPIIInitializer) Init(modelID string, useCPU bool) error {
148-
err := candle_binding.InitModernBertPIITokenClassifier(modelID, useCPU)
149-
if err != nil {
150-
return err
147+
func (c *PIIInitializerImpl) Init(modelID string, useCPU bool, numClasses int) error {
148+
// Use auto-detecting Candle BERT init - checks for lora_config.json
149+
success := candle_binding.InitCandleBertTokenClassifier(modelID, numClasses, useCPU)
150+
if !success {
151+
return fmt.Errorf("failed to initialize PII token classifier")
151152
}
152-
logging.Infof("Initialized ModernBERT PII token classifier for entity detection")
153+
logging.Infof("Initialized PII token classifier with auto-detection (LoRA or Traditional)")
153154
return nil
154155
}
155156

156-
// createPIIInitializer creates the appropriate PII initializer (currently only ModernBERT)
157-
func createPIIInitializer() PIIInitializer { return &ModernBertPIIInitializer{} }
157+
// createPIIInitializer creates the PII initializer (auto-detecting)
158+
func createPIIInitializer() PIIInitializer {
159+
return &PIIInitializerImpl{}
160+
}
158161

159162
type PIIInference interface {
160163
ClassifyTokens(text string, configPath string) (candle_binding.TokenClassificationResult, error)
161164
}
162165

163-
type ModernBertPIIInference struct{}
166+
type PIIInferenceImpl struct{}
164167

165-
func (c *ModernBertPIIInference) ClassifyTokens(text string, configPath string) (candle_binding.TokenClassificationResult, error) {
166-
return candle_binding.ClassifyModernBertPIITokens(text, configPath)
168+
func (c *PIIInferenceImpl) ClassifyTokens(text string, configPath string) (candle_binding.TokenClassificationResult, error) {
169+
// Auto-detecting inference - uses whichever classifier was initialized (LoRA or Traditional)
170+
return candle_binding.ClassifyCandleBertTokens(text)
167171
}
168172

169-
// createPIIInference creates the appropriate PII inference (currently only ModernBERT)
170-
func createPIIInference() PIIInference { return &ModernBertPIIInference{} }
173+
// createPIIInference creates the PII inference (auto-detecting)
174+
func createPIIInference() PIIInference {
175+
return &PIIInferenceImpl{}
176+
}
171177

172178
// JailbreakDetection represents the result of jailbreak analysis for a piece of content
173179
type JailbreakDetection struct {
@@ -350,7 +356,7 @@ func NewClassifier(cfg *config.RouterConfig, categoryMapping *CategoryMapping, p
350356

351357
// Add in-tree classifier if configured
352358
if cfg.CategoryModel.ModelID != "" {
353-
options = append(options, withCategory(categoryMapping, createCategoryInitializer(cfg.UseModernBERT), createCategoryInference(cfg.UseModernBERT)))
359+
options = append(options, withCategory(categoryMapping, createCategoryInitializer(cfg.CategoryModel.UseModernBERT), createCategoryInference(cfg.CategoryModel.UseModernBERT)))
354360
}
355361

356362
// Add MCP classifier if configured
@@ -511,7 +517,8 @@ func (c *Classifier) initializePIIClassifier() error {
511517
return fmt.Errorf("not enough PII types for classification, need at least 2, got %d", numPIIClasses)
512518
}
513519

514-
return c.piiInitializer.Init(c.Config.PIIModel.ModelID, c.Config.PIIModel.UseCPU)
520+
// Pass numClasses to support auto-detection
521+
return c.piiInitializer.Init(c.Config.PIIModel.ModelID, c.Config.PIIModel.UseCPU, numPIIClasses)
515522
}
516523

517524
// ClassifyCategoryWithEntropy performs category classification with entropy-based reasoning decision

0 commit comments

Comments
 (0)