Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JXVi2GMQ7bR5EmbUFdDj7N
30 lines
818 B
Java
30 lines
818 B
Java
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);
|
|
}
|
|
}
|