db620d33df
dotnet-build-and-test / dotnet-test-functions (push) Has been cancelled
dotnet-build-and-test / paths-filter (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Debug, windows-latest, net9.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, ubuntu-latest, net8.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-build (Release, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, ubuntu-latest, net10.0) (push) Has been cancelled
dotnet-build-and-test / dotnet-test (Release, integration, true, windows-latest, net472) (push) Has been cancelled
dotnet-build-and-test / dotnet-foundry-hosted-it (push) Has been cancelled
dotnet-build-and-test / dotnet-build-and-test-check (push) Has been cancelled
dotnet-build-and-test / Integration Test Report (push) Has been cancelled
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
55 lines
2.3 KiB
C#
55 lines
2.3 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// This sample demonstrates evaluating agent responses against expected outputs.
|
|
|
|
using Azure.AI.Projects;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
|
|
string endpoint = Environment.GetEnvironmentVariable("FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("FOUNDRY_PROJECT_ENDPOINT is not set.");
|
|
string deploymentName = Environment.GetEnvironmentVariable("FOUNDRY_MODEL") ?? "gpt-4o-mini";
|
|
|
|
// Create a math tutor agent.
|
|
// WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production.
|
|
// In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid
|
|
// latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
|
|
AIAgent agent = new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential())
|
|
.AsAIAgent(
|
|
model: deploymentName,
|
|
instructions: "You are a math tutor. Answer concisely with the numeric result.",
|
|
name: "MathTutor");
|
|
|
|
// Combine built-in checks.
|
|
LocalEvaluator localEvaluator = new(
|
|
EvalChecks.ContainsExpected(), // response must contain the expected answer
|
|
EvalChecks.NonEmpty()); // response must not be empty
|
|
|
|
// Queries and expected outputs.
|
|
string[] queries = ["What is 2 + 2?", "What is the square root of 144?"];
|
|
string[] expectedOutputs = ["4", "12"];
|
|
|
|
// Run the agent and evaluate with expected outputs.
|
|
AgentEvaluationResults results = await agent.EvaluateAsync(
|
|
queries,
|
|
localEvaluator,
|
|
expectedOutput: expectedOutputs);
|
|
|
|
// Print results.
|
|
Console.WriteLine($"Evaluation: {results.ProviderName}");
|
|
Console.WriteLine($" Passed: {results.Passed}/{results.Total}");
|
|
Console.WriteLine($" All passed: {results.AllPassed}");
|
|
Console.WriteLine();
|
|
|
|
for (int i = 0; i < results.Items.Count; i++)
|
|
{
|
|
Console.WriteLine($"Query: {queries[i]} | Expected: {expectedOutputs[i]}");
|
|
Console.WriteLine($"Response: {(results.InputItems?[i].Response is { } resp ? resp.Substring(0, Math.Min(50, resp.Length)) : "N/A")}");
|
|
foreach (var metric in results.Items[i].Metrics)
|
|
{
|
|
string status = metric.Value.Interpretation?.Failed == true ? "FAIL" : "PASS";
|
|
Console.WriteLine($" [{status}] {metric.Key}: {metric.Value.Interpretation?.Reason}");
|
|
}
|
|
|
|
Console.WriteLine();
|
|
}
|