| | | 1 | | using Microsoft.Extensions.AI; |
| | | 2 | | using NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.AgentFramework.Evaluation; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Extensions that convert <see cref="IAgentRunDiagnostics"/> into the input shape expected |
| | | 8 | | /// by <c>Microsoft.Extensions.AI.Evaluation</c> evaluators. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class AgentRunDiagnosticsEvaluationExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Projects a captured agent run into <see cref="EvaluationInputs"/> that can be handed |
| | | 14 | | /// directly to any <c>IEvaluator.EvaluateAsync</c> overload without re-invoking the model. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="diagnostics">Diagnostics captured for a single agent run.</param> |
| | | 17 | | /// <returns> |
| | | 18 | | /// An <see cref="EvaluationInputs"/> whose <see cref="EvaluationInputs.Messages"/> is the |
| | | 19 | | /// captured input, and whose <see cref="EvaluationInputs.ModelResponse"/> is built from |
| | | 20 | | /// the captured <c>AgentResponse</c>. When the run produced no output response, the |
| | | 21 | | /// returned response contains a single empty assistant message so evaluators always |
| | | 22 | | /// receive a non-null <see cref="ChatResponse"/>. |
| | | 23 | | /// </returns> |
| | | 24 | | /// <exception cref="ArgumentNullException"><paramref name="diagnostics"/> is <see langword="null"/>.</exception> |
| | | 25 | | public static EvaluationInputs ToEvaluationInputs(this IAgentRunDiagnostics diagnostics) |
| | | 26 | | { |
| | 5 | 27 | | ArgumentNullException.ThrowIfNull(diagnostics); |
| | | 28 | | |
| | 4 | 29 | | var outputMessages = diagnostics.OutputResponse?.Messages; |
| | 4 | 30 | | var chatResponse = outputMessages is { Count: > 0 } |
| | 4 | 31 | | ? new ChatResponse(outputMessages) |
| | 4 | 32 | | : new ChatResponse(new ChatMessage(ChatRole.Assistant, string.Empty)); |
| | | 33 | | |
| | 4 | 34 | | return new EvaluationInputs(diagnostics.InputMessages, chatResponse); |
| | | 35 | | } |
| | | 36 | | } |