mirror of
https://github.com/mudler/LocalAI.git
synced 2026-07-09 07:48:22 -04:00
Squashed feat/pii-ner-tier-engine rebased onto master (was 45 commits; see backup/pii-ner-tier-engine-prerebase). Net change: - privacy-filter.cpp: standalone GGML engine for the openai-privacy-filter PII/NER token classifier, wired as a LocalAI gRPC backend (CPU/CUDA/Vulkan). TokenClassify moves off the patched llama.cpp path onto this backend. - PII filter reworked to be NER-centric (encoder/NER detection tier scanning whole conversations as one document), with a recreated bounded restricted- regex secret-matching pattern detector tier alongside it (per-model pii_detection.builtins / .patterns + core/services/routing/piipattern). - Detection labelled by source (ner vs pattern); backend trace / confidence / debug observability; analyze/redact exposed as a synchronous API. - Instance-wide default detector policy + per-usecase default-on; request filtering extended to completions, embeddings, edits & Ollama. - React UI: NER-centric PII editor, detector-models table, pattern/builtins editor, middleware default-policy UI. - Gallery: privacy-filter-multilingual token-classify model + NER install filter; token_classify known_usecase; batch sized to context for NER models. privacy-filter backend registered in the backend gallery (cpu/vulkan/cuda-13 meta + image entries with a capabilities map) matching its CI matrix jobs, and an /import-model auto-detect importer (PrivacyFilterImporter, narrow privacy-filter GGUF detection) replacing the prior pref-only registration. Reconciled against master's independent evolution: - Dropped master's PIIPatternOverrides feature (global-pattern runtime overrides + /api/pii/patterns API + runtime_settings.json persistence). The per-model NER + pattern-detector design supersedes it; it was built on the global redactor pattern set this branch replaced. - Reverted the llama.cpp Score carry-patch (0006-server-task-type-score): removed the patch and restored master's grpc-server.cpp Score RPC (direct llama_decode, slot-loop bypass) and LLAMA_VERSION pin, plus master's model_config validation forbidding score + chat/completion/embeddings on llama-cpp. token_classify is unaffected (it runs on the privacy-filter backend, not llama-cpp). Assisted-by: Claude:claude-opus-4-8 [Claude Code] Signed-off-by: Richard Palethorpe <io@richiejp.com>
101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
package piipattern
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
const (
|
|
// MaxPatternsPerMatcher bounds how many patterns one detector may hold.
|
|
MaxPatternsPerMatcher = 128
|
|
// MaxMatchesPerPattern bounds matches emitted per pattern per call, so a
|
|
// pathological input can't produce an unbounded result set.
|
|
MaxMatchesPerPattern = 1000
|
|
)
|
|
|
|
// Pattern is one compiled-ready rule: matches are reported under Group, and a
|
|
// match shorter than MinLen bytes is dropped (0 = no floor).
|
|
type Pattern struct {
|
|
Group string
|
|
Pattern string
|
|
MinLen int
|
|
}
|
|
|
|
// Match is one detected span: a half-open byte range [Start,End) into the
|
|
// scanned text, the matched text, and the reporting Group.
|
|
type Match struct {
|
|
Group string
|
|
Start int
|
|
End int
|
|
Text string
|
|
}
|
|
|
|
type compiled struct {
|
|
group string
|
|
re *regexp.Regexp
|
|
minLen int
|
|
}
|
|
|
|
// Matcher holds a set of compiled patterns and scans text for all of them.
|
|
type Matcher struct {
|
|
pats []compiled
|
|
}
|
|
|
|
// NewMatcher compiles the named built-ins plus the custom patterns into a
|
|
// Matcher. Unknown built-in names and patterns that fail the restricted grammar
|
|
// are reported as errors (the caller fails closed). Built-in and custom counts
|
|
// together may not exceed MaxPatternsPerMatcher.
|
|
func NewMatcher(builtinNames []string, custom []Pattern) (*Matcher, error) {
|
|
if len(builtinNames)+len(custom) > MaxPatternsPerMatcher {
|
|
return nil, fmt.Errorf("too many patterns (%d; max %d)", len(builtinNames)+len(custom), MaxPatternsPerMatcher)
|
|
}
|
|
m := &Matcher{}
|
|
for _, name := range builtinNames {
|
|
b, ok := LookupBuiltin(name)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unknown built-in pattern %q", name)
|
|
}
|
|
re, err := Compile(b.Pattern)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("built-in %q: %w", name, err)
|
|
}
|
|
m.pats = append(m.pats, compiled{group: b.Group, re: re})
|
|
}
|
|
for _, p := range custom {
|
|
if p.Group == "" {
|
|
return nil, fmt.Errorf("custom pattern is missing a name/group")
|
|
}
|
|
re, err := Compile(p.Pattern)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("pattern %q: %w", p.Group, err)
|
|
}
|
|
m.pats = append(m.pats, compiled{group: p.Group, re: re, minLen: p.MinLen})
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// Find returns every match of every pattern over text. Spans from different
|
|
// patterns may overlap; the caller (the redactor) unions and resolves them.
|
|
func (m *Matcher) Find(text string) []Match {
|
|
if m == nil || text == "" {
|
|
return nil
|
|
}
|
|
var out []Match
|
|
for _, p := range m.pats {
|
|
locs := p.re.FindAllStringIndex(text, MaxMatchesPerPattern)
|
|
for _, loc := range locs {
|
|
start, end := loc[0], loc[1]
|
|
if end-start < p.minLen {
|
|
continue
|
|
}
|
|
out = append(out, Match{
|
|
Group: p.group,
|
|
Start: start,
|
|
End: end,
|
|
Text: text[start:end],
|
|
})
|
|
}
|
|
}
|
|
return out
|
|
}
|