Add guardrails module: prompt injection defences (document filter, tool policy, output validation) measured against an always-obeying stub model
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JXVi2GMQ7bR5EmbUFdDj7N
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.ankurm.guardrails;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Drops retrieved chunks that match {@link InjectionHeuristics} before they reach the prompt. */
|
||||
public final class DocumentGuard {
|
||||
|
||||
public record Result(List<Doc> kept, List<String> dropped) {
|
||||
}
|
||||
|
||||
private DocumentGuard() {
|
||||
}
|
||||
|
||||
public static Result filter(List<Doc> docs) {
|
||||
List<Doc> kept = new ArrayList<>();
|
||||
List<String> dropped = new ArrayList<>();
|
||||
for (Doc d : docs) {
|
||||
List<String> hits = InjectionHeuristics.matches(d.text());
|
||||
if (hits.isEmpty()) {
|
||||
kept.add(d);
|
||||
}
|
||||
else {
|
||||
dropped.add(d.id() + " (" + String.join(", ", hits) + ")");
|
||||
}
|
||||
}
|
||||
return new Result(kept, dropped);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user