@@ -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