Skip to content

Commit 3cd48fe

Browse files
committed
Presidio Sample working as expected
1 parent 6779d71 commit 3cd48fe

16 files changed

+135
-49
lines changed

Applying-Responsible-Secure-AI/Samples/PromptFilteringPresidio/Core/Models/AnalyzerEntityType.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

Applying-Responsible-Secure-AI/Samples/PromptFilteringPresidio/Core/Models/AnonymizerType.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Sample;
2+
3+
internal class PresidioAnalyzerConfig
4+
{
5+
public double? ScoreThreshold { get; set; }
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Sample;
2+
3+
/// <summary>
4+
/// PII entities Presidio Text Analyzer is capable of detecting. Only some of them are defined here for demonstration purposes.
5+
/// Full list can be found here: https://microsoft.github.io/presidio/api-docs/api-docs.html#tag/Analyzer/paths/~1supportedentities/get.
6+
/// </summary>
7+
internal readonly struct PresidioAnalyzerEntityType(string name)
8+
{
9+
public string Name { get; } = name;
10+
11+
public static PresidioAnalyzerEntityType Person = new("PERSON");
12+
public static PresidioAnalyzerEntityType PhoneNumber = new("PHONE_NUMBER");
13+
public static PresidioAnalyzerEntityType EmailAddress = new("EMAIL_ADDRESS");
14+
public static PresidioAnalyzerEntityType CreditCard = new("CREDIT_CARD");
15+
16+
public static implicit operator string(PresidioAnalyzerEntityType type) => type.Name;
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Sample;
2+
3+
/// <summary>
4+
/// Anonymizer action type that can be performed to update the prompt.
5+
/// More information here: https://microsoft.github.io/presidio/api-docs/api-docs.html#tag/Anonymizer/paths/~1anonymizers/get
6+
/// </summary>
7+
internal readonly struct PresidioAnonymizerType(string name)
8+
{
9+
public string Name { get; } = name;
10+
11+
public static PresidioAnonymizerType Hash = new("hash");
12+
public static PresidioAnonymizerType Mask = new("mask");
13+
public static PresidioAnonymizerType Redact = new("redact");
14+
public static PresidioAnonymizerType Replace = new("replace");
15+
public static PresidioAnonymizerType Encrypt = new("encrypt");
16+
17+
public static implicit operator string(PresidioAnonymizerType type) => type.Name;
18+
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
using Microsoft.Extensions.Logging;
2-
using Microsoft.SemanticKernel;
1+
using Microsoft.SemanticKernel;
32

43
namespace Sample;
54

65
/// <summary>
76
/// Filter which use Text Analyzer to detect PII in prompt and prevent sending it to LLM.
87
/// </summary>
98
internal sealed class PromptAnalyzerFilter(
10-
ILogger logger,
119
PresidioTextAnalyzerService analyzerService,
12-
double scoreThreshold) : IPromptRenderFilter
10+
PresidioAnalyzerConfig config) : IPromptRenderFilter
1311
{
1412
public async Task OnPromptRenderAsync(PromptRenderContext context, Func<PromptRenderContext, Task> next)
1513
{
@@ -18,7 +16,8 @@ public async Task OnPromptRenderAsync(PromptRenderContext context, Func<PromptRe
1816
// Get rendered prompt
1917
var prompt = context.RenderedPrompt!;
2018

21-
logger.LogTrace("Prompt: {Prompt}", prompt);
19+
Console.ForegroundColor = ConsoleColor.Cyan;
20+
Console.WriteLine($"Prompt: {prompt}");
2221

2322
// Call analyzer to detect PII
2423
var analyzerResults = await analyzerService.AnalyzeAsync(new PresidioTextAnalyzerRequest { Text = prompt });
@@ -28,19 +27,21 @@ public async Task OnPromptRenderAsync(PromptRenderContext context, Func<PromptRe
2827
// Check analyzer results
2928
foreach (var result in analyzerResults)
3029
{
31-
logger.LogInformation("Entity type: {EntityType}. Score: {Score}", result.EntityType, result.Score);
30+
Console.WriteLine($"Entity type: {result.EntityType}. Score: {result.Score}");
3231

33-
if (result.Score > scoreThreshold)
32+
if (result.Score > config.ScoreThreshold!)
3433
{
3534
piiDetected = true;
3635
}
3736
}
3837

38+
Console.ResetColor();
39+
3940
// If PII detected, throw an exception to prevent this prompt from being sent to LLM.
4041
// It's also possible to override 'context.Result' to return some default function result instead.
4142
if (piiDetected)
4243
{
43-
throw new KernelException("Prompt contains PII information. Operation is canceled.");
44+
throw new InvalidOperationException("Prompt contains PII information. Operation is canceled.");
4445
}
4546
}
4647
}

Applying-Responsible-Secure-AI/Samples/PromptFilteringPresidio/Filters/PresidioPromptAnonymizerFilter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
/// Filter which use Text Anonymizer to detect PII in prompt and update the prompt by following specified rules before sending it to LLM.
77
/// </summary>
88
internal sealed class PromptAnonymizerFilter(
9-
ILogger logger,
9+
ILoggerFactory loggerFactory,
1010
PresidioTextAnalyzerService analyzerService,
1111
PresidioTextAnonymizerService anonymizerService,
1212
Dictionary<string, PresidioTextAnonymizer> anonymizers) : IPromptRenderFilter
1313
{
14+
private readonly ILogger<PromptAnonymizerFilter> logger = loggerFactory.CreateLogger<PromptAnonymizerFilter>();
15+
1416
public async Task OnPromptRenderAsync(PromptRenderContext context, Func<PromptRenderContext, Task> next)
1517
{
1618
await next(context);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
POST http://localhost:5002/analyze HTTP/1.1
2+
content-type: application/json
3+
4+
{
5+
"text": "My name is Bob, I live in New York and my phone number is 212-555-5555",
6+
"language": "en"
7+
}
8+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GET http://localhost:5002/recognizers?language=en HTTP/1.1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GET http://localhost:5002/supportedentities?language=en HTTP/1.1

0 commit comments

Comments
 (0)