Skip to content

Commit ed84c35

Browse files
stephentoubSteveSandersonMS
authored andcommitted
Add RenderAsync overload
1 parent 0e31b0f commit ed84c35

File tree

6 files changed

+47
-21
lines changed

6 files changed

+47
-21
lines changed

src/Libraries/Microsoft.Extensions.AI.Evaluation.Quality/ChatConversationEvaluator.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
7+
using System.Text;
68
using System.Threading;
79
using System.Threading.Tasks;
810
using Microsoft.Shared.Diagnostics;
@@ -250,26 +252,50 @@ await PerformEvaluationAsync(
250252
}
251253

252254
/// <summary>
253-
/// Renders the supplied <paramref name="messages"/> to a string that can be included as part of the evaluation
255+
/// Renders the supplied <paramref name="response"/> to a string that can be included as part of the evaluation
254256
/// prompt that this <see cref="IEvaluator"/> uses.
255257
/// </summary>
256-
/// <param name="messages">
257-
/// Messages that are part of the conversation history for the response being evaluated and that are to be rendered
258-
/// as part of the evaluation prompt.
258+
/// <param name="response">
259+
/// Chat response being evaluated and that is to be rendered as part of the evaluation prompt.
259260
/// </param>
260261
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can cancel the operation.</param>
261262
/// <returns>
262-
/// A string representation of the supplied <paramref name="messages"/> that can be included as part of the
263+
/// A string representation of the supplied <paramref name="response"/> that can be included as part of the
263264
/// evaluation prompt.
264265
/// </returns>
265266
/// <remarks>
266-
/// The default implementation considers only the last message of <paramref name="messages"/>.
267+
/// The default implementation uses <see cref="RenderAsync(ChatMessage, CancellationToken)"/> to render
268+
/// each message in the response.
267269
/// </remarks>
268-
protected virtual ValueTask<string> RenderAsync(IEnumerable<ChatMessage> messages, CancellationToken cancellationToken)
270+
protected virtual async ValueTask<string> RenderAsync(ChatResponse response, CancellationToken cancellationToken)
269271
{
270-
_ = Throw.IfNullOrEmpty(messages);
272+
_ = Throw.IfNull(response);
271273

272-
ChatMessage message = messages.Last();
274+
StringBuilder sb = new();
275+
foreach (ChatMessage message in response.Messages)
276+
{
277+
_ = sb.Append(await RenderAsync(message, cancellationToken).ConfigureAwait(false));
278+
}
279+
280+
return sb.ToString();
281+
}
282+
283+
/// <summary>
284+
/// Renders the supplied <paramref name="message"/> to a string that can be included as part of the evaluation
285+
/// prompt that this <see cref="IEvaluator"/> uses.
286+
/// </summary>
287+
/// <param name="message">
288+
/// Message that is part of the conversation history for the response being evaluated and that is to be rendered
289+
/// as part of the evaluation prompt.
290+
/// </param>
291+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can cancel the operation.</param>
292+
/// <returns>
293+
/// A string representation of the supplied <paramref name="message"/> that can be included as part of the
294+
/// evaluation prompt.
295+
/// </returns>
296+
protected virtual ValueTask<string> RenderAsync(ChatMessage message, CancellationToken cancellationToken)
297+
{
298+
_ = Throw.IfNull(message);
273299

274300
string? author = message.AuthorName;
275301
string role = message.Role.Value;

src/Libraries/Microsoft.Extensions.AI.Evaluation.Quality/CoherenceEvaluator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ protected override async ValueTask<string> RenderEvaluationPromptAsync(
3939
{
4040
_ = Throw.IfNull(modelResponse);
4141

42-
string renderedModelResponse = await RenderAsync(modelResponse.Messages, cancellationToken).ConfigureAwait(false);
42+
string renderedModelResponse = await RenderAsync(modelResponse, cancellationToken).ConfigureAwait(false);
4343

4444
string renderedUserRequest =
4545
userRequest is not null
46-
? await RenderAsync([userRequest], cancellationToken).ConfigureAwait(false)
46+
? await RenderAsync(userRequest, cancellationToken).ConfigureAwait(false)
4747
: string.Empty;
4848

4949
string prompt =

src/Libraries/Microsoft.Extensions.AI.Evaluation.Quality/EquivalenceEvaluator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ protected override async ValueTask<string> RenderEvaluationPromptAsync(
4343
{
4444
_ = Throw.IfNull(modelResponse);
4545

46-
string renderedModelResponse = await RenderAsync(modelResponse.Messages, cancellationToken).ConfigureAwait(false);
46+
string renderedModelResponse = await RenderAsync(modelResponse, cancellationToken).ConfigureAwait(false);
4747

4848
string renderedUserRequest =
4949
userRequest is not null
50-
? await RenderAsync([userRequest], cancellationToken).ConfigureAwait(false)
50+
? await RenderAsync(userRequest, cancellationToken).ConfigureAwait(false)
5151
: string.Empty;
5252

5353
string groundTruth;

src/Libraries/Microsoft.Extensions.AI.Evaluation.Quality/FluencyEvaluator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ protected override async ValueTask<string> RenderEvaluationPromptAsync(
3939
{
4040
_ = Throw.IfNull(modelResponse);
4141

42-
string renderedModelResponse = await RenderAsync(modelResponse.Messages, cancellationToken).ConfigureAwait(false);
42+
string renderedModelResponse = await RenderAsync(modelResponse, cancellationToken).ConfigureAwait(false);
4343

4444
string renderedUserRequest =
4545
userRequest is not null
46-
? await RenderAsync([userRequest], cancellationToken).ConfigureAwait(false)
46+
? await RenderAsync(userRequest, cancellationToken).ConfigureAwait(false)
4747
: string.Empty;
4848

4949
string prompt =

src/Libraries/Microsoft.Extensions.AI.Evaluation.Quality/GroundednessEvaluator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ protected override async ValueTask<string> RenderEvaluationPromptAsync(
4343
{
4444
_ = Throw.IfNull(modelResponse);
4545

46-
string renderedModelResponse = await RenderAsync(modelResponse.Messages, cancellationToken).ConfigureAwait(false);
46+
string renderedModelResponse = await RenderAsync(modelResponse, cancellationToken).ConfigureAwait(false);
4747

4848
string renderedUserRequest =
4949
userRequest is not null
50-
? await RenderAsync([userRequest], cancellationToken).ConfigureAwait(false)
50+
? await RenderAsync(userRequest, cancellationToken).ConfigureAwait(false)
5151
: string.Empty;
5252

5353
var builder = new StringBuilder();
@@ -64,7 +64,7 @@ userRequest is not null
6464
{
6565
foreach (ChatMessage message in includedHistory)
6666
{
67-
_ = builder.Append(await RenderAsync([message], cancellationToken).ConfigureAwait(false));
67+
_ = builder.Append(await RenderAsync(message, cancellationToken).ConfigureAwait(false));
6868
}
6969
}
7070

src/Libraries/Microsoft.Extensions.AI.Evaluation.Quality/RelevanceTruthAndCompletenessEvaluator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,19 @@ protected override async ValueTask<string> RenderEvaluationPromptAsync(
8383
{
8484
_ = Throw.IfNull(modelResponse);
8585

86-
string renderedModelResponse = await RenderAsync(modelResponse.Messages, cancellationToken).ConfigureAwait(false);
86+
string renderedModelResponse = await RenderAsync(modelResponse, cancellationToken).ConfigureAwait(false);
8787

8888
string renderedUserRequest =
8989
userRequest is not null
90-
? await RenderAsync([userRequest], cancellationToken).ConfigureAwait(false)
90+
? await RenderAsync(userRequest, cancellationToken).ConfigureAwait(false)
9191
: string.Empty;
9292

9393
var builder = new StringBuilder();
9494
if (includedHistory is not null)
9595
{
9696
foreach (ChatMessage message in includedHistory)
9797
{
98-
_ = builder.Append(await RenderAsync([message], cancellationToken).ConfigureAwait(false));
98+
_ = builder.Append(await RenderAsync(message, cancellationToken).ConfigureAwait(false));
9999
}
100100
}
101101

0 commit comments

Comments
 (0)