Files
agent-framework/dotnet/tests/Microsoft.Agents.AI.Declarative.IntegrationTests/AzureOpenAIDeclarativeAgentTests.cs
Mark Wallace aaa91954c5 .NET: Declarative Agents (#1301)
* AgentFactory abstractions and ChatClient implementation

* Add a getitng started sample

* Update to latest M.B.OM

* Add some additional samples

* Work in progress

* Merge latest from main

* Start to add support for using different kinds of connections

* Remove IsSupported

* Remove IsSupported

* Refactor code to create clients to support DI

* Add some unit tests

* Update based on the latest code review feedback

* Add support for OOB tools when using persistent agent sdk

* Fix sample naming

* Fix error based on latest MEAI

* Update M.B.OM package to latest

* Update to the latest M.B.OM release

* Remove some obsolete helper methods

* Update to the latest M.B.OM version

* Fix broken unit test

* Update MCP sample

* Bump to latest M.B.OM release

* Update to latest M.B.OM release

* Update to latest M.B.OM release

* Switch to using ExternalModel

* Update to latest M.B.OM

* Resolve merge conflicts

* All tests pass

* All tests pass

* Start to clean up the code

* Start to clean up the code

* More clean up

* More clean up

* More clean up

* Fix apiType checks

* Run dotnet format

* Fix typo

* Address code review feedback

* Add all properties for MCP tool

* Address code review feedback

* Address code review feedback

* Fix merge

* Undo warnings

* Undo test change

* More copilot feedback

* Make class sealed

* Address additional core review feedback

---------

Co-authored-by: Mark Wallace <markwallace@microsoft.com>
2025-11-11 11:39:20 +00:00

96 lines
4.1 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using Azure.Identity;
using Microsoft.Extensions.AI;
using Xunit.Abstractions;
namespace Microsoft.Agents.AI.Declarative.IntegrationTests;
/// <summary>
/// Tests for declarative agents created using <see cref="AggregatorAgentFactory"/>.
/// </summary>
public sealed class AzureOpenAIDeclarativeAgentTests(ITestOutputHelper output) : BaseIntegrationTest(output)
{
[Fact]
public async Task CanCreateAndRunChatAgentAsync()
{
// Example function tool that can be used by the agent.
[Description("Get the weather for a given location.")]
static string GetWeather(
[Description("The city and state, e.g. San Francisco, CA")] string location,
[Description("The unit of temperature. Possible values are 'celsius' and 'fahrenheit'.")] string unit)
=> $"The weather in {location} is cloudy with a high of {(unit.Equals("celsius", StringComparison.Ordinal) ? "15°C" : "59°F")}.";
// Arrange
var endpointUri = new Uri(this.FoundryConfiguration.Endpoint);
var tokenCredential = new AzureCliCredential();
var agentFactory = new AggregatorAgentFactory(
[
new OpenAIChatAgentFactory(endpointUri, tokenCredential),
new OpenAIResponseAgentFactory(endpointUri, tokenCredential),
new OpenAIAssistantAgentFactory(endpointUri, tokenCredential)
]);
var agentYaml = File.ReadAllText("../../../../../../agent-samples/azure/AzureOpenAIChat.yaml");
agentYaml = agentYaml.Replace("=Env.AZURE_OPENAI_DEPLOYMENT_NAME", this.FoundryConfiguration.DeploymentName);
// Create agent run options
var options = new ChatClientAgentRunOptions(new()
{
Tools = [AIFunctionFactory.Create(GetWeather, name: nameof(GetWeather))]
});
// Act
var agent = await agentFactory.CreateFromYamlAsync(agentYaml);
var response = await agent!.RunAsync("What is the weather in Cambridge, MA in °C?", options: options);
this.Output.WriteLine($"Agent Response: {response.Text}");
// Assert
Assert.NotNull(agent);
Assert.NotNull(response);
Assert.NotEmpty(response.Text);
}
[Fact]
public async Task CanCreateAndRunResponsesAgentAsync()
{
// Example function tool that can be used by the agent.
[Description("Get the weather for a given location.")]
static string GetWeather(
[Description("The city and state, e.g. San Francisco, CA")] string location,
[Description("The unit of temperature. Possible values are 'celsius' and 'fahrenheit'.")] string unit)
=> $"The weather in {location} is cloudy with a high of {(unit.Equals("celsius", StringComparison.Ordinal) ? "15°C" : "59°F")}.";
// Arrange
var endpointUri = new Uri(this.FoundryConfiguration.Endpoint);
var tokenCredential = new AzureCliCredential();
var agentFactory = new AggregatorAgentFactory(
[
new OpenAIChatAgentFactory(endpointUri, tokenCredential),
new OpenAIResponseAgentFactory(endpointUri, tokenCredential),
new OpenAIAssistantAgentFactory(endpointUri, tokenCredential)
]);
var agentYaml = File.ReadAllText("../../../../../../agent-samples/azure/AzureOpenAIResponses.yaml");
agentYaml = agentYaml.Replace("=Env.AZURE_OPENAI_DEPLOYMENT_NAME", this.FoundryConfiguration.DeploymentName);
// Create agent run options
var options = new ChatClientAgentRunOptions(new()
{
Tools = [AIFunctionFactory.Create(GetWeather, name: nameof(GetWeather))]
});
// Act
var agent = await agentFactory.CreateFromYamlAsync(agentYaml);
var response = await agent!.RunAsync("What is the weather in Cambridge, MA in °C?", options: options);
this.Output.WriteLine($"Agent Response: {response.Text}");
// Assert
Assert.NotNull(agent);
Assert.NotNull(response);
Assert.NotEmpty(response.Text);
}
}