|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Microsoft.SemanticKernel; |
| 7 | +using Microsoft.SemanticKernel.Agents; |
| 8 | +using Microsoft.SemanticKernel.ChatCompletion; |
| 9 | +using xRetry; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace SemanticKernel.IntegrationTests.Agents.CommonInterfaceConformance.InvokeStreamingConformance; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Base test class for testing the <see cref="Agent.InvokeStreamingAsync(ChatMessageContent, AgentThread?, AgentInvokeOptions?, System.Threading.CancellationToken)"/> method of agents. |
| 16 | +/// Each agent type should have its own derived class. |
| 17 | +/// </summary> |
| 18 | +public abstract class InvokeStreamingTests(Func<AgentFixture> createAgentFixture) : IAsyncLifetime |
| 19 | +{ |
| 20 | +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. |
| 21 | + private AgentFixture _agentFixture; |
| 22 | +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. |
| 23 | + |
| 24 | + protected AgentFixture Fixture => this._agentFixture; |
| 25 | + |
| 26 | + [RetryFact(3, 10_000)] |
| 27 | + public virtual async Task InvokeStreamingAsyncReturnsResultAsync() |
| 28 | + { |
| 29 | + // Arrange |
| 30 | + var agent = this.Fixture.Agent; |
| 31 | + |
| 32 | + // Act |
| 33 | + var asyncResults = agent.InvokeStreamingAsync(new ChatMessageContent(AuthorRole.User, "What is the capital of France."), this.Fixture.AgentThread); |
| 34 | + var results = await asyncResults.ToListAsync(); |
| 35 | + |
| 36 | + // Assert |
| 37 | + var firstResult = results.First(); |
| 38 | + var resultString = string.Join(string.Empty, results.Select(x => x.Message.Content)); |
| 39 | + |
| 40 | + Assert.Contains("Paris", resultString); |
| 41 | + Assert.NotNull(firstResult.Thread); |
| 42 | + } |
| 43 | + |
| 44 | + [RetryFact(3, 10_000)] |
| 45 | + public virtual async Task InvokeStreamingAsyncWithoutThreadCreatesThreadAsync() |
| 46 | + { |
| 47 | + // Arrange |
| 48 | + var agent = this.Fixture.Agent; |
| 49 | + |
| 50 | + // Act |
| 51 | + var asyncResults = agent.InvokeStreamingAsync(new ChatMessageContent(AuthorRole.User, "What is the capital of France.")); |
| 52 | + var results = await asyncResults.ToListAsync(); |
| 53 | + |
| 54 | + // Assert |
| 55 | + var firstResult = results.First(); |
| 56 | + var resultString = string.Join(string.Empty, results.Select(x => x.Message.Content)); |
| 57 | + |
| 58 | + Assert.Contains("Paris", resultString); |
| 59 | + Assert.NotNull(firstResult.Thread); |
| 60 | + |
| 61 | + // Cleanup |
| 62 | + await this.Fixture.DeleteThread(firstResult.Thread); |
| 63 | + } |
| 64 | + |
| 65 | + [RetryFact(3, 10_000)] |
| 66 | + public virtual async Task ConversationMaintainsHistoryAsync() |
| 67 | + { |
| 68 | + // Arrange |
| 69 | + var q1 = "What is the capital of France."; |
| 70 | + var q2 = "What is the capital of Austria."; |
| 71 | + var agent = this.Fixture.Agent; |
| 72 | + |
| 73 | + // Act |
| 74 | + var asyncResults1 = agent.InvokeStreamingAsync(new ChatMessageContent(AuthorRole.User, q1), this.Fixture.AgentThread); |
| 75 | + var results1 = await asyncResults1.ToListAsync(); |
| 76 | + var resultString1 = string.Join(string.Empty, results1.Select(x => x.Message.Content)); |
| 77 | + var result1 = results1.First(); |
| 78 | + |
| 79 | + var asyncResults2 = agent.InvokeStreamingAsync(new ChatMessageContent(AuthorRole.User, q2), result1.Thread); |
| 80 | + var results2 = await asyncResults2.ToListAsync(); |
| 81 | + var resultString2 = string.Join(string.Empty, results2.Select(x => x.Message.Content)); |
| 82 | + |
| 83 | + // Assert |
| 84 | + Assert.Contains("Paris", resultString1); |
| 85 | + Assert.Contains("Austria", resultString2); |
| 86 | + |
| 87 | + var chatHistory = await this.Fixture.GetChatHistory(); |
| 88 | + Assert.Equal(4, chatHistory.Count); |
| 89 | + Assert.Equal(2, chatHistory.Count(x => x.Role == AuthorRole.User)); |
| 90 | + Assert.Equal(2, chatHistory.Count(x => x.Role == AuthorRole.Assistant)); |
| 91 | + Assert.Equal(q1, chatHistory[0].Content); |
| 92 | + Assert.Equal(q2, chatHistory[2].Content); |
| 93 | + Assert.Contains("Paris", chatHistory[1].Content); |
| 94 | + Assert.Contains("Vienna", chatHistory[3].Content); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Verifies that the agent can invoke a plugin and respects the override |
| 99 | + /// Kernel and KernelArguments provided in the options. |
| 100 | + /// The step does multiple iterations to make sure that the agent |
| 101 | + /// also manages the chat history correctly. |
| 102 | + /// </summary> |
| 103 | + [RetryFact(3, 10_000)] |
| 104 | + public virtual async Task MultiStepInvokeStreamingAsyncWithPluginAndArgOverridesAsync() |
| 105 | + { |
| 106 | + // Arrange |
| 107 | + var questionsAndAnswers = new[] |
| 108 | + { |
| 109 | + ("Hello", string.Empty), |
| 110 | + ("What is the special soup?", "Clam Chowder"), |
| 111 | + ("What is the special drink?", "Chai Tea"), |
| 112 | + ("What is the special salad?", "Cobb Salad"), |
| 113 | + ("Thank you", string.Empty) |
| 114 | + }; |
| 115 | + |
| 116 | + var agent = this.Fixture.Agent; |
| 117 | + var kernel = agent.Kernel.Clone(); |
| 118 | + kernel.Plugins.AddFromType<MenuPlugin>(); |
| 119 | + |
| 120 | + foreach (var questionAndAnswer in questionsAndAnswers) |
| 121 | + { |
| 122 | + // Act |
| 123 | + var asyncResults = agent.InvokeStreamingAsync( |
| 124 | + new ChatMessageContent(AuthorRole.User, questionAndAnswer.Item1), |
| 125 | + this.Fixture.AgentThread, |
| 126 | + options: new() |
| 127 | + { |
| 128 | + Kernel = kernel, |
| 129 | + KernelArguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }) |
| 130 | + }); |
| 131 | + var results = await asyncResults.ToListAsync(); |
| 132 | + |
| 133 | + // Assert |
| 134 | + var resultString = string.Join(string.Empty, results.Select(x => x.Message.Content)); |
| 135 | + Assert.Contains(questionAndAnswer.Item2, resultString); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public Task InitializeAsync() |
| 140 | + { |
| 141 | + this._agentFixture = createAgentFixture(); |
| 142 | + return this._agentFixture.InitializeAsync(); |
| 143 | + } |
| 144 | + |
| 145 | + public Task DisposeAsync() |
| 146 | + { |
| 147 | + return this._agentFixture.DisposeAsync(); |
| 148 | + } |
| 149 | +} |
0 commit comments