Add PII redaction POC for secure LLM prompting.
Implements detect-redact-sanitize pipeline with regex, domain rules, and ONNX NER before the LLM boundary, plus NUnit tests and Xenovex push documentation. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using FluentAssertions;
|
||||
using PiiRedaction.Core.Abstractions;
|
||||
using PiiRedaction.Core.Models;
|
||||
using PiiRedaction.Core.Tests.TestSupport;
|
||||
using PiiRedaction.Infrastructure.Llm;
|
||||
|
||||
namespace PiiRedaction.Core.Tests.Integration;
|
||||
|
||||
[TestFixture]
|
||||
public sealed class LlmBoundaryTests
|
||||
{
|
||||
[Test]
|
||||
public async Task SendPromptAsync_DoesNotTransmitOriginalPii()
|
||||
{
|
||||
var capturingClient = new CapturingChatClient();
|
||||
ILlmPromptService service = new MockLlmPromptService(capturingClient);
|
||||
|
||||
const string sanitized = "Customer <PERSON_1> with email <EMAIL_1>.";
|
||||
await service.SendPromptAsync(sanitized);
|
||||
|
||||
capturingClient.LastUserMessage.Should().Be(sanitized);
|
||||
capturingClient.LastUserMessage.Should().NotContain("ravi.kumar@gmail.com");
|
||||
capturingClient.LastUserMessage.Should().NotContain("Ravi Kumar");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task SendPromptAsync_AfterSanitization_OnlyPlaceholdersReachLlm()
|
||||
{
|
||||
var capturingClient = new CapturingChatClient();
|
||||
var sanitizer = ProductionPipelineFactory.Create().Sanitizer;
|
||||
ILlmPromptService service = new MockLlmPromptService(capturingClient);
|
||||
|
||||
const string prompt = "Email ravi.kumar@gmail.com please.";
|
||||
var result = sanitizer.Sanitize(new SanitizationRequest(prompt));
|
||||
|
||||
result.SanitizedPrompt.Should().Contain("<EMAIL_1>");
|
||||
await service.SendPromptAsync(result.SanitizedPrompt);
|
||||
|
||||
capturingClient.LastUserMessage.Should().Contain("<EMAIL_1>");
|
||||
capturingClient.LastUserMessage.Should().NotContain("ravi.kumar@gmail.com");
|
||||
}
|
||||
|
||||
[TestCase(null)]
|
||||
[TestCase("")]
|
||||
[TestCase(" ")]
|
||||
public void SendPromptAsync_InvalidSanitizedPrompt_ThrowsArgumentException(string? prompt)
|
||||
{
|
||||
ILlmPromptService service = new MockLlmPromptService(new MockChatClient());
|
||||
var action = async () => await service.SendPromptAsync(prompt!);
|
||||
action.Should().ThrowAsync<ArgumentException>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user