-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathAggregatorAgentTests.cs
184 lines (158 loc) · 6.98 KB
/
AggregatorAgentTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.Chat;
using Microsoft.SemanticKernel.ChatCompletion;
using SemanticKernel.IntegrationTests.TestSettings;
using xRetry;
using Xunit;
namespace SemanticKernel.IntegrationTests.Agents;
#pragma warning disable xUnit1004 // Contains test methods used in manual verification. Disable warning for this file only.
public sealed class AggregatorAgentTests()
{
private readonly IKernelBuilder _kernelBuilder = Kernel.CreateBuilder();
private readonly IConfigurationRoot _configuration = new ConfigurationBuilder()
.AddJsonFile(path: "testsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile(path: "testsettings.development.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.AddUserSecrets<OpenAIAssistantAgentTests>()
.Build();
/// <summary>
/// Integration test for <see cref="AggregatorAgent"/> non-streamed nested response.
/// </summary>
[RetryFact(typeof(HttpOperationException))]
public async Task AggregatorAgentFlatResponseAsync()
{
// Arrange
AggregatorAgent aggregatorAgent = new(() => this.CreateChatProvider())
{
Mode = AggregatorMode.Flat,
};
AgentGroupChat chat = new();
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "1"));
// Act
ChatMessageContent[] responses = await chat.InvokeAsync(aggregatorAgent).ToArrayAsync();
// Assert
ChatMessageContent[] innerHistory = await chat.GetChatMessagesAsync(aggregatorAgent).ToArrayAsync();
Assert.Equal(6, innerHistory.Length);
Assert.Equal(5, responses.Length);
Assert.NotNull(responses[4].Content);
AssertResponseContent(responses[4]);
}
/// <summary>
/// Integration test for <see cref="AggregatorAgent"/> non-streamed nested response.
/// </summary>
[RetryFact(typeof(HttpOperationException))]
public async Task AggregatorAgentNestedResponseAsync()
{
// Arrange
AggregatorAgent aggregatorAgent = new(() => this.CreateChatProvider())
{
Mode = AggregatorMode.Nested,
};
AgentGroupChat chat = new();
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "1"));
// Act
ChatMessageContent[] responses = await chat.InvokeAsync(aggregatorAgent).ToArrayAsync();
// Assert
ChatMessageContent[] innerHistory = await chat.GetChatMessagesAsync(aggregatorAgent).ToArrayAsync();
Assert.Equal(6, innerHistory.Length);
Assert.Single(responses);
Assert.NotNull(responses[0].Content);
AssertResponseContent(responses[0]);
}
/// <summary>
/// Integration test for <see cref="AggregatorAgent"/> non-streamed response.
/// </summary>
[RetryFact(typeof(HttpOperationException))]
public async Task AggregatorAgentFlatStreamAsync()
{
// Arrange
AggregatorAgent aggregatorAgent = new(() => this.CreateChatProvider())
{
Mode = AggregatorMode.Flat,
};
AgentGroupChat chat = new();
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "1"));
// Act
StreamingChatMessageContent[] streamedResponse = await chat.InvokeStreamingAsync(aggregatorAgent).ToArrayAsync();
// Assert
ChatMessageContent[] fullResponses = await chat.GetChatMessagesAsync().ToArrayAsync();
ChatMessageContent[] innerHistory = await chat.GetChatMessagesAsync(aggregatorAgent).ToArrayAsync();
Assert.NotEmpty(streamedResponse);
Assert.Equal(6, innerHistory.Length);
Assert.Equal(6, fullResponses.Length);
Assert.NotNull(fullResponses[0].Content);
AssertResponseContent(fullResponses[0]);
}
/// <summary>
/// Integration test for <see cref="AggregatorAgent"/> non-streamed response.
/// </summary>
[RetryFact(typeof(HttpOperationException))]
public async Task AggregatorAgentNestedStreamAsync()
{
// Arrange
AggregatorAgent aggregatorAgent = new(() => this.CreateChatProvider())
{
Mode = AggregatorMode.Nested,
};
AgentGroupChat chat = new();
chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, "1"));
// Act
StreamingChatMessageContent[] streamedResponse = await chat.InvokeStreamingAsync(aggregatorAgent).ToArrayAsync();
// Assert
ChatMessageContent[] fullResponses = await chat.GetChatMessagesAsync().ToArrayAsync();
ChatMessageContent[] innerHistory = await chat.GetChatMessagesAsync(aggregatorAgent).ToArrayAsync();
Assert.NotEmpty(streamedResponse);
Assert.Equal(6, innerHistory.Length);
Assert.Equal(2, fullResponses.Length);
Assert.NotNull(fullResponses[0].Content);
AssertResponseContent(fullResponses[0]);
}
private static void AssertResponseContent(ChatMessageContent response)
{
// Counting is hard
Assert.True(
response.Content!.Contains("five", StringComparison.OrdinalIgnoreCase) ||
response.Content!.Contains("six", StringComparison.OrdinalIgnoreCase) ||
response.Content!.Contains("seven", StringComparison.OrdinalIgnoreCase) ||
response.Content!.Contains("eight", StringComparison.OrdinalIgnoreCase),
$"Content: {response}");
}
private AgentGroupChat CreateChatProvider()
{
// Arrange
AzureOpenAIConfiguration configuration = this._configuration.GetSection("AzureOpenAI").Get<AzureOpenAIConfiguration>()!;
this._kernelBuilder.AddAzureOpenAIChatCompletion(
configuration.ChatDeploymentName!,
configuration.Endpoint,
new AzureCliCredential());
Kernel kernel = this._kernelBuilder.Build();
ChatCompletionAgent agent =
new()
{
Kernel = kernel,
Instructions = "Your job is to count. Always add one to the previous number and respond using the english word for that number, without explanation.",
};
return new AgentGroupChat(agent)
{
ExecutionSettings = new()
{
TerminationStrategy = new CountTerminationStrategy(5)
}
};
}
private sealed class CountTerminationStrategy(int maximumResponseCount) : TerminationStrategy
{
// Terminate when the assistant has responded N times.
protected override Task<bool> ShouldAgentTerminateAsync(Agent agent, IReadOnlyList<ChatMessageContent> history, CancellationToken cancellationToken)
=> Task.FromResult(history.Count(message => message.Role == AuthorRole.Assistant) >= maximumResponseCount);
}
}