|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#pragma warning disable S3604 |
| 5 | +// S3604: Member initializer values should not be redundant. |
| 6 | +// We disable this warning because it is a false positive arising from the analyzer's lack of support for C#'s primary |
| 7 | +// constructor syntax. |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Net.Http; |
| 12 | +using System.Threading; |
| 13 | +using System.Threading.Tasks; |
| 14 | + |
| 15 | +namespace Microsoft.Extensions.AI.Evaluation.Safety; |
| 16 | + |
| 17 | +/// <summary> |
| 18 | +/// An <see langword="abstract"/> base class that can be used to implement <see cref="IEvaluator"/>s that utilize the |
| 19 | +/// Azure AI Content Safety service to evaluate responses produced by an AI model for the presence of a variety of |
| 20 | +/// unsafe content such as protected material, ungrounded answers, harmful content etc. |
| 21 | +/// </summary> |
| 22 | +/// <param name="evaluatorName">The name of the derived <see cref="ContentSafetyEvaluator"/>.</param> |
| 23 | +/// <param name="metricName"> |
| 24 | +/// The name of the <see cref="EvaluationMetric"/> produced by the derived <see cref="ContentSafetyEvaluator"/>. |
| 25 | +/// </param> |
| 26 | +/// <param name="contentSafetyServiceConfiguration"> |
| 27 | +/// Specifies the Azure AI project that should be used and credentials that should be used when this |
| 28 | +/// <see cref="ContentSafetyEvaluator"/> communicates with the Azure AI Content Safety service to perform evaluations. |
| 29 | +/// </param> |
| 30 | +/// <param name="contentSafetyServiceAnnotationTask"> |
| 31 | +/// The name of the annotation task that should be used when this <see cref="ContentSafetyEvaluator"/> communicates |
| 32 | +/// with the Azure AI Content Safety service to perform evaluations. |
| 33 | +/// </param> |
| 34 | +/// <param name="contentSafetyServiceMetricName"> |
| 35 | +/// The name of the metric that should be used when this <see cref="ContentSafetyEvaluator"/> communicates with the |
| 36 | +/// Azure AI Content Safety service to perform evaluations. |
| 37 | +/// </param> |
| 38 | +/// <param name="contentSafetyServicePayloadFormat"> |
| 39 | +/// An identifier that specifies the format of the payload that should be used when this |
| 40 | +/// <see cref="ContentSafetyEvaluator"/> communicates with the Azure AI Content Safety service to perform evaluations. |
| 41 | +/// </param> |
| 42 | +/// <param name="httpClient"> |
| 43 | +/// The <see cref="System.Net.Http.HttpClient"/> that should be used when communicating with the Azure AI Content |
| 44 | +/// Safety service. While the parameter is optional, it is recommended to supply an |
| 45 | +/// <see cref="System.Net.Http.HttpClient"/> that is configured with robust resilience and retry policies. |
| 46 | +/// </param> |
| 47 | +public abstract class ContentSafetyEvaluator( |
| 48 | + string evaluatorName, |
| 49 | + string metricName, |
| 50 | + ContentSafetyServiceConfiguration contentSafetyServiceConfiguration, |
| 51 | + string contentSafetyServiceAnnotationTask, |
| 52 | + string contentSafetyServiceMetricName, |
| 53 | + string contentSafetyServicePayloadFormat, |
| 54 | + HttpClient? httpClient = null) : IEvaluator |
| 55 | +{ |
| 56 | + private readonly ContentSafetyService _service = |
| 57 | + new ContentSafetyService( |
| 58 | + contentSafetyServiceConfiguration, |
| 59 | + evaluatorName, |
| 60 | + contentSafetyServiceAnnotationTask, |
| 61 | + contentSafetyServiceMetricName, |
| 62 | +#if NET |
| 63 | + Enum.Parse<ContentSafetyServicePayloadFormat>(contentSafetyServicePayloadFormat), |
| 64 | +#else |
| 65 | + (ContentSafetyServicePayloadFormat)Enum.Parse(typeof(ContentSafetyServicePayloadFormat), contentSafetyServicePayloadFormat), |
| 66 | +#endif |
| 67 | + metricName, |
| 68 | + httpClient); |
| 69 | + |
| 70 | + /// <inheritdoc/> |
| 71 | + public IReadOnlyCollection<string> EvaluationMetricNames => [metricName]; |
| 72 | + |
| 73 | + /// <inheritdoc/> |
| 74 | + public abstract ValueTask<EvaluationResult> EvaluateAsync( |
| 75 | + IEnumerable<ChatMessage> messages, |
| 76 | + ChatResponse modelResponse, |
| 77 | + ChatConfiguration? chatConfiguration = null, |
| 78 | + IEnumerable<EvaluationContext>? additionalContext = null, |
| 79 | + CancellationToken cancellationToken = default); |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Evaluates the supplied <paramref name="modelResponse"/> using the Azure AI Content Safety Service and returns |
| 83 | + /// an <see cref="EvaluationResult"/> containing one or more <see cref="EvaluationMetric"/>s. |
| 84 | + /// </summary> |
| 85 | + /// <param name="messages"> |
| 86 | + /// The conversation history including the request that produced the supplied <paramref name="modelResponse"/>. |
| 87 | + /// </param> |
| 88 | + /// <param name="modelResponse">The response that is to be evaluated.</param> |
| 89 | + /// <param name="additionalContext"> |
| 90 | + /// Additional contextual information (beyond that which is available in <paramref name="messages"/>) that the |
| 91 | + /// <see cref="IEvaluator"/> may need to accurately evaluate the supplied <paramref name="modelResponse"/>. |
| 92 | + /// </param> |
| 93 | + /// <param name="cancellationToken"> |
| 94 | + /// A <see cref="CancellationToken"/> that can cancel the evaluation operation. |
| 95 | + /// </param> |
| 96 | + /// <returns>An <see cref="EvaluationResult"/> containing one or more <see cref="EvaluationMetric"/>s.</returns> |
| 97 | + protected ValueTask<EvaluationResult> EvaluateContentSafetyAsync( |
| 98 | + IEnumerable<ChatMessage> messages, |
| 99 | + ChatResponse modelResponse, |
| 100 | + IEnumerable<string?>? additionalContext = null, |
| 101 | + CancellationToken cancellationToken = default) |
| 102 | + => _service.EvaluateAsync(messages, modelResponse, additionalContext, cancellationToken); |
| 103 | +} |
0 commit comments