mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.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>
This commit is contained in:
committed by
GitHub
Unverified
parent
105dc82c39
commit
aaa91954c5
+107
@@ -0,0 +1,107 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Moq;
|
||||
|
||||
namespace Microsoft.Agents.AI.Declarative.UnitTests.ChatClient;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for <see cref="ChatClientAgentFactory"/>.
|
||||
/// </summary>
|
||||
public sealed class ChatClientAgentFactoryTests
|
||||
{
|
||||
private readonly Mock<IChatClient> _mockChatClient;
|
||||
|
||||
public ChatClientAgentFactoryTests()
|
||||
{
|
||||
this._mockChatClient = new();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TryCreateAsync_WithChatClientInConstructor_CreatesAgentAsync()
|
||||
{
|
||||
// Arrange
|
||||
var promptAgent = PromptAgents.CreateTestPromptAgent();
|
||||
ChatClientAgentFactory factory = new(this._mockChatClient.Object);
|
||||
|
||||
// Act
|
||||
AIAgent? agent = await factory.TryCreateAsync(promptAgent);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(agent);
|
||||
Assert.IsType<ChatClientAgent>(agent);
|
||||
Assert.Equal("Test Agent", agent.Name);
|
||||
Assert.Equal("Test Description", agent.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TryCreateAsync_Creates_ChatClientAgentAsync()
|
||||
{
|
||||
// Arrange
|
||||
var promptAgent = PromptAgents.CreateTestPromptAgent();
|
||||
ChatClientAgentFactory factory = new(this._mockChatClient.Object);
|
||||
|
||||
// Act
|
||||
AIAgent? agent = await factory.TryCreateAsync(promptAgent);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(agent);
|
||||
Assert.IsType<ChatClientAgent>(agent);
|
||||
var chatClientAgent = agent as ChatClientAgent;
|
||||
Assert.NotNull(chatClientAgent);
|
||||
Assert.Equal("You are a helpful assistant.", chatClientAgent.Instructions);
|
||||
Assert.NotNull(chatClientAgent.ChatClient);
|
||||
Assert.NotNull(chatClientAgent.ChatOptions);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TryCreateAsync_Creates_ChatOptionsAsync()
|
||||
{
|
||||
// Arrange
|
||||
var promptAgent = PromptAgents.CreateTestPromptAgent();
|
||||
ChatClientAgentFactory factory = new(this._mockChatClient.Object);
|
||||
|
||||
// Act
|
||||
AIAgent? agent = await factory.TryCreateAsync(promptAgent);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(agent);
|
||||
Assert.IsType<ChatClientAgent>(agent);
|
||||
var chatClientAgent = agent as ChatClientAgent;
|
||||
Assert.NotNull(chatClientAgent?.ChatOptions);
|
||||
Assert.Equal("Provide detailed and accurate responses.", chatClientAgent?.ChatOptions?.Instructions);
|
||||
Assert.Equal(0.7F, chatClientAgent?.ChatOptions?.Temperature);
|
||||
Assert.Equal(0.7F, chatClientAgent?.ChatOptions?.FrequencyPenalty);
|
||||
Assert.Equal(1024, chatClientAgent?.ChatOptions?.MaxOutputTokens);
|
||||
Assert.Equal(0.9F, chatClientAgent?.ChatOptions?.TopP);
|
||||
Assert.Equal(50, chatClientAgent?.ChatOptions?.TopK);
|
||||
Assert.Equal(0.7F, chatClientAgent?.ChatOptions?.PresencePenalty);
|
||||
Assert.Equal(42L, chatClientAgent?.ChatOptions?.Seed);
|
||||
Assert.NotNull(chatClientAgent?.ChatOptions?.ResponseFormat);
|
||||
Assert.Equal("gpt-4o", chatClientAgent?.ChatOptions?.ModelId);
|
||||
Assert.Equal(["###", "END", "STOP"], chatClientAgent?.ChatOptions?.StopSequences);
|
||||
Assert.True(chatClientAgent?.ChatOptions?.AllowMultipleToolCalls);
|
||||
Assert.Equal(ChatToolMode.Auto, chatClientAgent?.ChatOptions?.ToolMode);
|
||||
Assert.Equal("customValue", chatClientAgent?.ChatOptions?.AdditionalProperties?["customProperty"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TryCreateAsync_Creates_ToolsAsync()
|
||||
{
|
||||
// Arrange
|
||||
var promptAgent = PromptAgents.CreateTestPromptAgent();
|
||||
ChatClientAgentFactory factory = new(this._mockChatClient.Object);
|
||||
|
||||
// Act
|
||||
AIAgent? agent = await factory.TryCreateAsync(promptAgent);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(agent);
|
||||
Assert.IsType<ChatClientAgent>(agent);
|
||||
var chatClientAgent = agent as ChatClientAgent;
|
||||
Assert.NotNull(chatClientAgent?.ChatOptions?.Tools);
|
||||
var tools = chatClientAgent?.ChatOptions?.Tools;
|
||||
Assert.Equal(5, tools?.Count);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user