-
Notifications
You must be signed in to change notification settings - Fork 180
fix: use unified classifier in classification endpoints #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OneZero-Y
wants to merge
1
commit into
vllm-project:main
Choose a base branch
from
OneZero-Y:fix/api-classifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,7 +71,7 @@ func GetGlobalClassificationService() *ClassificationService { | |
|
||
// HasClassifier returns true if the service has a real classifier (not placeholder) | ||
func (s *ClassificationService) HasClassifier() bool { | ||
return s.classifier != nil | ||
return s.unifiedClassifier != nil || s.classifier != nil | ||
} | ||
|
||
// NewPlaceholderClassificationService creates a placeholder service for API-only mode | ||
|
@@ -118,7 +118,12 @@ func (s *ClassificationService) ClassifyIntent(req IntentRequest) (*IntentRespon | |
return nil, fmt.Errorf("text cannot be empty") | ||
} | ||
|
||
// Check if classifier is available | ||
// Prioritize unified classifier if available | ||
if s.unifiedClassifier != nil { | ||
return s.ClassifyIntentUnified(req) | ||
} | ||
|
||
// Check if legacy classifier is available | ||
if s.classifier == nil { | ||
// Return placeholder response | ||
processingTime := time.Since(start).Milliseconds() | ||
|
@@ -210,7 +215,12 @@ func (s *ClassificationService) DetectPII(req PIIRequest) (*PIIResponse, error) | |
return nil, fmt.Errorf("text cannot be empty") | ||
} | ||
|
||
// Check if classifier is available | ||
// Prioritize unified classifier if available | ||
if s.unifiedClassifier != nil { | ||
return s.DetectPIIUnified(req) | ||
} | ||
|
||
// Check if legacy classifier is available | ||
if s.classifier == nil { | ||
// Return placeholder response | ||
processingTime := time.Since(start).Milliseconds() | ||
|
@@ -290,7 +300,12 @@ func (s *ClassificationService) CheckSecurity(req SecurityRequest) (*SecurityRes | |
return nil, fmt.Errorf("text cannot be empty") | ||
} | ||
|
||
// Check if classifier is available | ||
// Prioritize unified classifier if available | ||
if s.unifiedClassifier != nil { | ||
return s.CheckSecurityUnified(req) | ||
} | ||
|
||
// Check if legacy classifier is available | ||
if s.classifier == nil { | ||
// Return placeholder response | ||
processingTime := time.Since(start).Milliseconds() | ||
|
@@ -454,6 +469,59 @@ func (s *ClassificationService) ClassifyPIIUnified(texts []string) ([]classifica | |
return results.PIIResults, nil | ||
} | ||
|
||
// DetectPIIUnified performs PII detection using unified classifier and returns PIIResponse format | ||
func (s *ClassificationService) DetectPIIUnified(req PIIRequest) (*PIIResponse, error) { | ||
start := time.Now() | ||
|
||
if req.Text == "" { | ||
return nil, fmt.Errorf("text cannot be empty") | ||
} | ||
|
||
// Use unified classifier for PII detection | ||
piiResults, err := s.ClassifyPIIUnified([]string{req.Text}) | ||
if err != nil { | ||
return nil, fmt.Errorf("PII detection failed: %w", err) | ||
} | ||
|
||
processingTime := time.Since(start).Milliseconds() | ||
|
||
// Convert PIIResult to PIIResponse format | ||
if len(piiResults) == 0 { | ||
return &PIIResponse{ | ||
HasPII: false, | ||
Entities: []PIIEntity{}, | ||
SecurityRecommendation: "allow", | ||
ProcessingTimeMs: processingTime, | ||
}, nil | ||
} | ||
|
||
piiResult := piiResults[0] | ||
response := &PIIResponse{ | ||
HasPII: piiResult.HasPII, | ||
Entities: []PIIEntity{}, | ||
ProcessingTimeMs: processingTime, | ||
} | ||
|
||
// Convert PII types to entities | ||
for _, piiType := range piiResult.PIITypes { | ||
entity := PIIEntity{ | ||
Type: piiType, | ||
Value: "[DETECTED]", // Placeholder - unified classifier doesn't provide exact positions yet | ||
Confidence: float64(piiResult.Confidence), | ||
} | ||
response.Entities = append(response.Entities, entity) | ||
} | ||
|
||
// Set security recommendation | ||
if response.HasPII { | ||
response.SecurityRecommendation = "block" | ||
} else { | ||
response.SecurityRecommendation = "allow" | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
// ClassifySecurityUnified performs security detection using unified classifier | ||
func (s *ClassificationService) ClassifySecurityUnified(texts []string) ([]classification.SecurityResult, error) { | ||
if s.unifiedClassifier == nil { | ||
|
@@ -468,6 +536,71 @@ func (s *ClassificationService) ClassifySecurityUnified(texts []string) ([]class | |
return results.SecurityResults, nil | ||
} | ||
|
||
// CheckSecurityUnified performs security detection using unified classifier and returns SecurityResponse format | ||
func (s *ClassificationService) CheckSecurityUnified(req SecurityRequest) (*SecurityResponse, error) { | ||
start := time.Now() | ||
|
||
if req.Text == "" { | ||
return nil, fmt.Errorf("text cannot be empty") | ||
} | ||
|
||
// Use unified classifier for security detection | ||
securityResults, err := s.ClassifySecurityUnified([]string{req.Text}) | ||
if err != nil { | ||
return nil, fmt.Errorf("security detection failed: %w", err) | ||
} | ||
|
||
processingTime := time.Since(start).Milliseconds() | ||
|
||
// Convert SecurityResult to SecurityResponse format | ||
if len(securityResults) == 0 { | ||
return &SecurityResponse{ | ||
IsJailbreak: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why using hardcoded values? no security result can have a special output but a hardcoded response like this is a bit confusing. |
||
RiskScore: 0.1, | ||
DetectionTypes: []string{}, | ||
Confidence: 0.9, | ||
Recommendation: "allow", | ||
PatternsDetected: []string{}, | ||
ProcessingTimeMs: processingTime, | ||
}, nil | ||
} | ||
|
||
securityResult := securityResults[0] | ||
response := &SecurityResponse{ | ||
IsJailbreak: securityResult.IsJailbreak, | ||
RiskScore: float64(securityResult.Confidence), | ||
Confidence: float64(securityResult.Confidence), | ||
ProcessingTimeMs: processingTime, | ||
} | ||
|
||
// Set detection types based on threat type | ||
if securityResult.ThreatType != "" { | ||
response.DetectionTypes = []string{securityResult.ThreatType} | ||
response.PatternsDetected = []string{securityResult.ThreatType} | ||
} else { | ||
response.DetectionTypes = []string{} | ||
response.PatternsDetected = []string{} | ||
} | ||
|
||
// Set recommendation based on jailbreak detection | ||
if response.IsJailbreak { | ||
response.Recommendation = "block" | ||
} else { | ||
response.Recommendation = "allow" | ||
} | ||
|
||
// Add reasoning if requested | ||
if req.Options != nil && req.Options.IncludeReasoning { | ||
if response.IsJailbreak { | ||
response.Reasoning = fmt.Sprintf("Detected %s with confidence %.2f", securityResult.ThreatType, securityResult.Confidence) | ||
} else { | ||
response.Reasoning = "No security threats detected" | ||
} | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
// HasUnifiedClassifier returns true if the service has a unified classifier | ||
func (s *ClassificationService) HasUnifiedClassifier() bool { | ||
return s.unifiedClassifier != nil && s.unifiedClassifier.IsInitialized() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The token sequence classifier should have the location index. Can you follow up in another PR? Thanks