Skip to content

Commit 9c9f439

Browse files
rogerbarretoBR
authored andcommitted
.Net: Enable InvokeAsync() and InvokeAsync<T> To Return the Connector Type (microsoft#4085)
### Motivation and Context Similar to InvokeStreamingAsync<T> where you can specify the T that the connector is returning, this will allow the same for the non-streaming API This change also allows `IChatCompletionService`s to be recognized by the `Prompt Functions` and return the underlying ChatMessages directly to the InvokeAsync calls. Resolves microsoft#4079
1 parent c56c4d7 commit 9c9f439

4 files changed

Lines changed: 229 additions & 12 deletions

File tree

‎dotnet/src/SemanticKernel.Abstractions/Functions/FunctionResult.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ public FunctionResult(KernelFunction function, object? value = null, CultureInfo
6464
return typedResult;
6565
}
6666

67+
if (this.Value is ContentBase content)
68+
{
69+
if (typeof(T) == typeof(string))
70+
{
71+
return (T)(object)content.ToString();
72+
}
73+
74+
if (content.InnerContent is T innerContent)
75+
{
76+
return innerContent;
77+
}
78+
}
79+
6780
throw new InvalidCastException($"Cannot cast {this.Value.GetType()} to {typeof(T)}");
6881
}
6982

‎dotnet/src/SemanticKernel.Core/Functions/KernelFunctionFromPrompt.cs‎

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
using System.Threading.Tasks;
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.Extensions.Logging.Abstractions;
12+
using Microsoft.SemanticKernel.ChatCompletion;
1213
using Microsoft.SemanticKernel.Events;
14+
using Microsoft.SemanticKernel.Services;
1315
using Microsoft.SemanticKernel.TextGeneration;
1416

1517
namespace Microsoft.SemanticKernel;
@@ -121,15 +123,26 @@ protected override async ValueTask<FunctionResult> InvokeCoreAsync(
121123
{
122124
this.AddDefaultValues(arguments);
123125

124-
(var textGeneration, var renderedPrompt, var renderedEventArgs) = await this.RenderPromptAsync(kernel, arguments, cancellationToken).ConfigureAwait(false);
126+
(var aiService, var renderedPrompt, var renderedEventArgs) = await this.RenderPromptAsync(kernel, arguments, cancellationToken).ConfigureAwait(false);
125127
if (renderedEventArgs?.Cancel is true)
126128
{
127129
throw new OperationCanceledException($"A {nameof(Kernel)}.{nameof(Kernel.PromptRendered)} event handler requested cancellation before function invocation.");
128130
}
129131

130-
var textContent = await textGeneration.GetTextContentWithDefaultParserAsync(renderedPrompt, arguments.ExecutionSettings, kernel, cancellationToken).ConfigureAwait(false);
132+
if (aiService is IChatCompletionService chatCompletion)
133+
{
134+
var chatContent = await chatCompletion.GetChatMessageContentAsync(renderedPrompt, arguments.ExecutionSettings, kernel, cancellationToken).ConfigureAwait(false);
135+
return new FunctionResult(this, chatContent, kernel.Culture, chatContent.Metadata);
136+
}
137+
138+
if (aiService is ITextGenerationService textGeneration)
139+
{
140+
var textContent = await textGeneration.GetTextContentWithDefaultParserAsync(renderedPrompt, arguments.ExecutionSettings, kernel, cancellationToken).ConfigureAwait(false);
141+
return new FunctionResult(this, textContent, kernel.Culture, textContent.Metadata);
142+
}
131143

132-
return new FunctionResult(this, textContent.Text, kernel.Culture, textContent.Metadata);
144+
// This should never happen as the AI service is selected by the service selector
145+
throw new NotSupportedException($"The AI service {aiService.GetType()} is not supported. Supported services are {typeof(IChatCompletionService)} and {typeof(ITextGenerationService)}");
133146
}
134147

135148
protected override async IAsyncEnumerable<TResult> InvokeStreamingCoreAsync<TResult>(
@@ -139,13 +152,28 @@ protected override async IAsyncEnumerable<TResult> InvokeStreamingCoreAsync<TRes
139152
{
140153
this.AddDefaultValues(arguments);
141154

142-
(var textGeneration, var renderedPrompt, var renderedEventArgs) = await this.RenderPromptAsync(kernel, arguments, cancellationToken).ConfigureAwait(false);
155+
(var aiService, var renderedPrompt, var renderedEventArgs) = await this.RenderPromptAsync(kernel, arguments, cancellationToken).ConfigureAwait(false);
143156
if (renderedEventArgs?.Cancel ?? false)
144157
{
145158
yield break;
146159
}
147160

148-
await foreach (var content in textGeneration.GetStreamingTextContentsWithDefaultParserAsync(renderedPrompt, arguments.ExecutionSettings, kernel, cancellationToken))
161+
IAsyncEnumerable<StreamingContentBase>? asyncReference = null;
162+
if (aiService is IChatCompletionService chatCompletion)
163+
{
164+
asyncReference = chatCompletion.GetStreamingChatMessageContentsAsync(renderedPrompt, arguments.ExecutionSettings, kernel, cancellationToken);
165+
}
166+
else if (aiService is ITextGenerationService textGeneration)
167+
{
168+
asyncReference = textGeneration.GetStreamingTextContentsWithDefaultParserAsync(renderedPrompt, arguments.ExecutionSettings, kernel, cancellationToken);
169+
}
170+
else
171+
{
172+
// This should never happen as the AI service is selected by the service selector
173+
throw new NotSupportedException($"The AI service {aiService.GetType()} is not supported. Supported services are {typeof(IChatCompletionService)} and {typeof(ITextGenerationService)}");
174+
}
175+
176+
await foreach (var content in asyncReference)
149177
{
150178
cancellationToken.ThrowIfCancellationRequested();
151179

@@ -155,10 +183,10 @@ _ when typeof(TResult) == typeof(string)
155183
=> (TResult)(object)content.ToString(),
156184

157185
_ when content is TResult contentAsT
158-
=> (TResult)contentAsT,
186+
=> contentAsT,
159187

160188
_ when content.InnerContent is TResult innerContentAsT
161-
=> (TResult)innerContentAsT,
189+
=> innerContentAsT,
162190

163191
_ when typeof(TResult) == typeof(byte[])
164192
=> (TResult)(object)content.ToByteArray(),
@@ -212,11 +240,27 @@ private void AddDefaultValues(KernelArguments arguments)
212240
}
213241
}
214242

215-
private async Task<(ITextGenerationService, string, PromptRenderedEventArgs?)> RenderPromptAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken)
243+
private async Task<(IAIService, string, PromptRenderedEventArgs?)> RenderPromptAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken)
216244
{
217245
var serviceSelector = kernel.ServiceSelector;
218-
(var textGeneration, var defaultExecutionSettings) = serviceSelector.SelectAIService<ITextGenerationService>(kernel, this, arguments);
219-
Verify.NotNull(textGeneration);
246+
IAIService? aiService;
247+
PromptExecutionSettings? defaultExecutionSettings;
248+
249+
//TODO: Revisit if SelectAIService implementation should return null when no service is found
250+
251+
#pragma warning disable CA1031 // Do not catch general exception types
252+
try
253+
{
254+
(aiService, defaultExecutionSettings) = serviceSelector.SelectAIService<IChatCompletionService>(kernel, this, arguments);
255+
}
256+
catch
257+
{
258+
// Fallback to text generation service, if the selector don't finds it, will throw.
259+
(aiService, defaultExecutionSettings) = serviceSelector.SelectAIService<ITextGenerationService>(kernel, this, arguments);
260+
}
261+
#pragma warning restore CA1031 // Do not catch general exception types
262+
263+
Verify.NotNull(aiService);
220264

221265
arguments.ExecutionSettings ??= defaultExecutionSettings;
222266

@@ -226,7 +270,7 @@ private void AddDefaultValues(KernelArguments arguments)
226270

227271
var renderedEventArgs = kernel.OnPromptRendered(this, arguments, renderedPrompt);
228272

229-
return (textGeneration, renderedPrompt, renderedEventArgs);
273+
return (aiService, renderedPrompt, renderedEventArgs);
230274
}
231275

232276
/// <summary>Create a random, valid function name.</summary>

‎dotnet/src/SemanticKernel.UnitTests/Functions/FunctionResultTests.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Globalization;
66
using Microsoft.SemanticKernel;
7+
using Microsoft.SemanticKernel.TextGeneration;
78
using Xunit;
89

910
namespace SemanticKernel.UnitTests.Functions;
@@ -109,4 +110,29 @@ public void ToStringWorksCorrectly()
109110
// Act and Assert
110111
Assert.Equal(value, target.ToString());
111112
}
113+
114+
[Fact]
115+
public void GetValueWhenValueIsContentBaseGenericStringShouldReturnContentBaseToString()
116+
{
117+
// Arrange
118+
string expectedValue = Guid.NewGuid().ToString();
119+
FunctionResult target = new(s_nopFunction, new TextContent(expectedValue));
120+
121+
// Act and Assert
122+
Assert.Equal(expectedValue, target.GetValue<string>());
123+
}
124+
125+
[Fact]
126+
public void GetValueWhenValueIsContentBaseGenericTypeMatchShouldReturn()
127+
{
128+
// Arrange
129+
string expectedValue = Guid.NewGuid().ToString();
130+
var valueType = new TextContent(expectedValue);
131+
FunctionResult target = new(s_nopFunction, valueType);
132+
133+
// Act and Assert
134+
135+
Assert.Equal(valueType, target.GetValue<TextContent>());
136+
Assert.Equal(valueType, target.GetValue<ContentBase>());
137+
}
112138
}

‎dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromPromptTests.cs‎

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public async Task ItNotParsesStandardizedPromptWhenServiceIsOnlyTextCompletionAs
194194
}
195195

196196
[Fact]
197-
public async Task ItNotParsesStandardizedPromptWhenStreamingInServiceIsOnlyTextCompletionAsync()
197+
public async Task ItNotParsesStandardizedPromptWhenStreamingWhenServiceIsOnlyTextCompletionAsync()
198198
{
199199
var mockService = new Mock<ITextGenerationService>();
200200
var mockResult = mockService.Setup(s => s.GetStreamingTextContentsAsync(It.IsAny<string>(), It.IsAny<PromptExecutionSettings>(), It.IsAny<Kernel>(), It.IsAny<CancellationToken>()))
@@ -223,6 +223,140 @@ public async Task ItNotParsesStandardizedPromptWhenStreamingInServiceIsOnlyTextC
223223
}
224224
}
225225

226+
[Fact]
227+
public async Task InvokeAsyncReturnsTheConnectorResultWhenInServiceIsOnlyTextCompletionAsync()
228+
{
229+
var mockService = new Mock<ITextGenerationService>();
230+
var mockResult = mockService.Setup(s => s.GetTextContentsAsync(It.IsAny<string>(), It.IsAny<PromptExecutionSettings>(), It.IsAny<Kernel>(), It.IsAny<CancellationToken>()))
231+
.ReturnsAsync(new List<TextContent>() { new("something") });
232+
233+
KernelBuilder builder = new();
234+
builder.Services.AddTransient<ITextGenerationService>((sp) => mockService.Object);
235+
Kernel kernel = builder.Build();
236+
237+
KernelFunction function = KernelFunctionFactory.CreateFromPrompt("Anything");
238+
239+
var result = await kernel.InvokeAsync(function);
240+
241+
Assert.Equal("something", result.GetValue<string>());
242+
Assert.Equal("something", result.GetValue<TextContent>()!.Text);
243+
Assert.Equal("something", result.GetValue<ContentBase>()!.ToString());
244+
}
245+
246+
[Fact]
247+
public async Task InvokeAsyncReturnsTheConnectorChatResultWhenInServiceIsOnlyChatCompletionAsync()
248+
{
249+
var mockService = new Mock<IChatCompletionService>();
250+
var mockResult = mockService.Setup(s => s.GetChatMessageContentsAsync(It.IsAny<ChatHistory>(), It.IsAny<PromptExecutionSettings>(), It.IsAny<Kernel>(), It.IsAny<CancellationToken>()))
251+
.ReturnsAsync(new List<ChatMessageContent>() { new(AuthorRole.User, "something") });
252+
253+
KernelBuilder builder = new();
254+
builder.Services.AddTransient<IChatCompletionService>((sp) => mockService.Object);
255+
Kernel kernel = builder.Build();
256+
257+
KernelFunction function = KernelFunctionFactory.CreateFromPrompt("Anything");
258+
259+
var result = await kernel.InvokeAsync(function);
260+
261+
Assert.Equal("something", result.GetValue<string>());
262+
Assert.Equal("something", result.GetValue<ChatMessageContent>()!.Content);
263+
Assert.Equal(AuthorRole.User, result.GetValue<ChatMessageContent>()!.Role);
264+
Assert.Equal("something", result.GetValue<ContentBase>()!.ToString());
265+
}
266+
267+
[Fact]
268+
public async Task InvokeAsyncReturnsTheConnectorChatResultWhenInServiceIsChatAndTextCompletionAsync()
269+
{
270+
var fakeService = new FakeChatAsTextService();
271+
KernelBuilder builder = new();
272+
builder.Services.AddTransient<ITextGenerationService>((sp) => fakeService);
273+
Kernel kernel = builder.Build();
274+
275+
KernelFunction function = KernelFunctionFactory.CreateFromPrompt("Anything");
276+
277+
var result = await kernel.InvokeAsync(function);
278+
279+
Assert.Equal("Something", result.GetValue<string>());
280+
Assert.Equal("Something", result.GetValue<ChatMessageContent>()!.Content);
281+
Assert.Equal(AuthorRole.Assistant, result.GetValue<ChatMessageContent>()!.Role);
282+
Assert.Equal("Something", result.GetValue<ContentBase>()!.ToString());
283+
}
284+
285+
[Fact]
286+
public async Task InvokeAsyncOfTGivesBackTheExpectedResultTypeFromTheConnectorWhenStreamingWhenServiceIsOnlyTextCompletionAsync()
287+
{
288+
var expectedContent = new StreamingTextContent("something");
289+
var mockService = new Mock<ITextGenerationService>();
290+
var mockResult = mockService.Setup(s => s.GetStreamingTextContentsAsync(It.IsAny<string>(), It.IsAny<PromptExecutionSettings>(), It.IsAny<Kernel>(), It.IsAny<CancellationToken>()))
291+
.Returns(this.ToAsyncEnumerable<StreamingTextContent>(new List<StreamingTextContent>() { expectedContent }));
292+
293+
KernelBuilder builder = new();
294+
builder.Services.AddTransient<ITextGenerationService>((sp) => mockService.Object);
295+
Kernel kernel = builder.Build();
296+
297+
KernelFunction function = KernelFunctionFactory.CreateFromPrompt("Anything");
298+
299+
await foreach (var chunk in kernel.InvokeStreamingAsync<StreamingContentBase>(function))
300+
{
301+
Assert.Equal(expectedContent, chunk);
302+
}
303+
304+
await foreach (var chunk in kernel.InvokeStreamingAsync<StreamingTextContent>(function))
305+
{
306+
Assert.Equal(expectedContent, chunk);
307+
}
308+
}
309+
310+
[Fact]
311+
public async Task InvokeAsyncOfTGivesBackTheExpectedResultTypeFromTheConnectorWhenStreamingWhenerviceIsOnlyChatCompletionAsync()
312+
{
313+
var expectedContent = new StreamingChatMessageContent(AuthorRole.Assistant, "Something");
314+
var mockService = new Mock<IChatCompletionService>();
315+
var mockResult = mockService.Setup(s => s.GetStreamingChatMessageContentsAsync(It.IsAny<ChatHistory>(), It.IsAny<PromptExecutionSettings>(), It.IsAny<Kernel>(), It.IsAny<CancellationToken>()))
316+
.Returns(this.ToAsyncEnumerable<StreamingChatMessageContent>(new List<StreamingChatMessageContent>() { expectedContent }));
317+
318+
KernelBuilder builder = new();
319+
builder.Services.AddTransient<IChatCompletionService>((sp) => mockService.Object);
320+
Kernel kernel = builder.Build();
321+
322+
KernelFunction function = KernelFunctionFactory.CreateFromPrompt("Anything");
323+
324+
await foreach (var chunk in kernel.InvokeStreamingAsync<StreamingContentBase>(function))
325+
{
326+
Assert.Equal(expectedContent, chunk);
327+
Assert.Equal("Something", chunk.ToString());
328+
}
329+
330+
await foreach (var chunk in kernel.InvokeStreamingAsync<StreamingChatMessageContent>(function))
331+
{
332+
Assert.Equal(expectedContent, chunk);
333+
Assert.Equal("Something", chunk.Content);
334+
Assert.Equal(AuthorRole.Assistant, chunk.Role);
335+
}
336+
}
337+
338+
[Fact]
339+
public async Task InvokeAsyncOfTGivesBackTheExpectedResultTypeFromTheConnectorWhenStreamingWhenServiceIsTextAndChatCompletionAsync()
340+
{
341+
var fakeService = new FakeChatAsTextService();
342+
KernelBuilder builder = new();
343+
builder.Services.AddTransient<ITextGenerationService>((sp) => fakeService);
344+
Kernel kernel = builder.Build();
345+
346+
KernelFunction function = KernelFunctionFactory.CreateFromPrompt("Anything");
347+
348+
await foreach (var chunk in kernel.InvokeStreamingAsync<StreamingContentBase>(function))
349+
{
350+
Assert.Equal("Something", chunk.ToString());
351+
}
352+
353+
await foreach (var chunk in kernel.InvokeStreamingAsync<StreamingChatMessageContent>(function))
354+
{
355+
Assert.Equal(AuthorRole.Assistant, chunk.Role);
356+
Assert.Equal("Something", chunk.Content);
357+
}
358+
}
359+
226360
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
227361
#pragma warning disable IDE1006 // Naming Styles
228362
private async IAsyncEnumerable<T> ToAsyncEnumerable<T>(IEnumerable<T> enumeration)

0 commit comments

Comments
 (0)