mirror of
https://github.com/mudler/LocalAI.git
synced 2026-06-19 06:09:07 -04:00
feat(pii): NER tier engine — privacy-filter.cpp backend + NER-centric PII filter (#10360)
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>
This commit is contained in:
committed by
GitHub
parent
c133ca39dc
commit
3fa7b2955c
@@ -1,8 +1,9 @@
|
||||
// Package cloudproxy stitches the cloud-proxy gRPC backend to the
|
||||
// HTTP edge: model rewrite, body shaping, and SSE-aware PII filtering
|
||||
// on the response. The outbound HTTP request itself lives inside the
|
||||
// cloud-proxy backend binary (backend/go/cloud-proxy), not here — this
|
||||
// package is the core-side glue.
|
||||
// HTTP edge: model rewrite and body shaping. The outbound HTTP request
|
||||
// itself lives inside the cloud-proxy backend binary
|
||||
// (backend/go/cloud-proxy), not here — this package is the core-side
|
||||
// glue. PII redaction runs request-side (the NER middleware + MITM
|
||||
// input path); response/output is forwarded unmodified.
|
||||
package cloudproxy
|
||||
|
||||
import (
|
||||
@@ -10,11 +11,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mudler/LocalAI/core/services/cloudproxy/ssewire"
|
||||
"github.com/mudler/LocalAI/core/services/routing/pii"
|
||||
"github.com/mudler/xlog"
|
||||
)
|
||||
|
||||
@@ -61,65 +59,30 @@ func forwardBuffered(c echo.Context, statusCode int, contentType string, body io
|
||||
return err
|
||||
}
|
||||
|
||||
// forwardStream applies SSE-aware PII rewriting as the response flows
|
||||
// to the client. provider selects the dialect (openai vs anthropic);
|
||||
// it comes from cfg.Proxy.Provider on the cloud-proxy backend.
|
||||
func forwardStream(c echo.Context, body io.Reader, provider string, filter *pii.StreamFilter) error {
|
||||
// forwardStream relays the upstream SSE response to the client,
|
||||
// flushing per read so events arrive in real time. Response/output PII
|
||||
// redaction is out of scope for now, so the stream is forwarded
|
||||
// unmodified.
|
||||
func forwardStream(c echo.Context, body io.Reader) error {
|
||||
c.Response().Header().Set("Content-Type", "text/event-stream")
|
||||
c.Response().Header().Set("Cache-Control", "no-cache")
|
||||
c.Response().Header().Set("Connection", "keep-alive")
|
||||
c.Response().WriteHeader(http.StatusOK)
|
||||
|
||||
emit := func(line string) error {
|
||||
_, err := fmt.Fprint(c.Response().Writer, line)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Response().Flush()
|
||||
return nil
|
||||
}
|
||||
|
||||
flushResidual := func() {
|
||||
if filter == nil {
|
||||
return
|
||||
}
|
||||
residual := filter.Drain()
|
||||
if residual == "" {
|
||||
return
|
||||
}
|
||||
if line := ssewire.SynthResidualEvent(ssewire.Provider(provider), residual); line != "" {
|
||||
_ = emit(line)
|
||||
}
|
||||
}
|
||||
|
||||
prov := ssewire.Provider(provider)
|
||||
scanner := ssewire.NewScanner(body)
|
||||
for scanner.Scan() {
|
||||
ev := scanner.Event()
|
||||
if ssewire.IsTerminalMarker(ev.DataLine, prov) {
|
||||
flushResidual()
|
||||
_ = emit(ev.Raw)
|
||||
continue
|
||||
}
|
||||
out := ev.Raw
|
||||
if filter != nil && ev.DataLine != "" {
|
||||
rewritten, drop := ssewire.RewritePayload(ev.DataLine, prov, filter)
|
||||
if drop {
|
||||
continue
|
||||
}
|
||||
if rewritten != ev.DataLine {
|
||||
// strings.Replace with n=1 touches only the data line,
|
||||
// preserving any "event:"/"id:" preamble.
|
||||
out = strings.Replace(ev.Raw, ev.DataLine, rewritten, 1)
|
||||
buf := make([]byte, 32*1024)
|
||||
for {
|
||||
n, rErr := body.Read(buf)
|
||||
if n > 0 {
|
||||
if _, wErr := c.Response().Writer.Write(buf[:n]); wErr != nil {
|
||||
return nil
|
||||
}
|
||||
c.Response().Flush()
|
||||
}
|
||||
if err := emit(out); err != nil {
|
||||
if rErr != nil {
|
||||
if rErr != io.EOF {
|
||||
xlog.Debug("cloudproxy: stream read error", "error", rErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil && err != io.EOF {
|
||||
xlog.Debug("cloudproxy: stream read error", "error", err)
|
||||
}
|
||||
flushResidual()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user