Skip to content

Commit

Permalink
Use client options with Azure AI Content Safety
Browse files Browse the repository at this point in the history
  • Loading branch information
dluc committed Dec 17, 2024
1 parent 7e730b0 commit bd4d8f3
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,19 @@ public AzureAIContentSafetyModeration(
this._globalSafetyThreshold = config.GlobalSafetyThreshold;
this._ignoredWords = config.IgnoredWords;

var clientOptions = GetClientOptions(config);
switch (config.Auth)
{
case AzureAIContentSafetyConfig.AuthTypes.AzureIdentity:
this._client = new ContentSafetyClient(new Uri(config.Endpoint), new DefaultAzureCredential());
this._client = new ContentSafetyClient(new Uri(config.Endpoint), new DefaultAzureCredential(), clientOptions);
break;

case AzureAIContentSafetyConfig.AuthTypes.APIKey:
this._client = new ContentSafetyClient(new Uri(config.Endpoint), new AzureKeyCredential(config.APIKey));
this._client = new ContentSafetyClient(new Uri(config.Endpoint), new AzureKeyCredential(config.APIKey), clientOptions);
break;

case AzureAIContentSafetyConfig.AuthTypes.ManualTokenCredential:
this._client = new ContentSafetyClient(new Uri(config.Endpoint), config.GetTokenCredential());
this._client = new ContentSafetyClient(new Uri(config.Endpoint), config.GetTokenCredential(), clientOptions);
break;

default:
Expand Down Expand Up @@ -83,6 +84,23 @@ public async Task<bool> IsSafeAsync(string? text, double threshold, Cancellation
return isSafe;
}

/// <summary>
/// Options used by the Azure AI client
/// </summary>
private static ContentSafetyClientOptions GetClientOptions(AzureAIContentSafetyConfig config)
{
var options = new ContentSafetyClientOptions
{
Diagnostics =
{
IsTelemetryEnabled = Telemetry.IsTelemetryEnabled,
ApplicationId = Telemetry.HttpUserAgent,
}
};

return options;
}

private string RemoveIgnoredWords(string text)
{
foreach (var word in this._ignoredWords)
Expand Down

0 comments on commit bd4d8f3

Please sign in to comment.