2
2
3
3
using System . ClientModel ;
4
4
using System . ClientModel . Primitives ;
5
- using System . ComponentModel ;
6
5
using System . Net ;
7
6
using System . Runtime . CompilerServices ;
8
7
using Azure . AI . OpenAI ;
9
8
using Azure . Identity ;
10
9
using Microsoft . Extensions . AI ;
10
+ using Microsoft . Extensions . DependencyInjection ;
11
11
using Microsoft . SemanticKernel ;
12
+ using Microsoft . SemanticKernel . ChatCompletion ;
13
+ using Microsoft . SemanticKernel . Connectors . AzureOpenAI ;
12
14
13
15
namespace ChatCompletion ;
14
16
@@ -30,23 +32,33 @@ public class HybridCompletion_Fallback(ITestOutputHelper output) : BaseTest(outp
30
32
[ Fact ]
31
33
public async Task FallbackToAvailableModelAsync ( )
32
34
{
33
- // Create an unavailable chat client that fails with 503 Service Unavailable HTTP status code
34
- IChatClient unavailableChatClient = CreateUnavailableOpenAIChatClient ( ) ;
35
+ IKernelBuilder kernelBuilder = Kernel . CreateBuilder ( ) ;
35
36
36
- // Create a cloud available chat client
37
- IChatClient availableChatClient = CreateAzureOpenAIChatClient ( ) ;
37
+ // Create and register an unavailable chat client that fails with 503 Service Unavailable HTTP status code
38
+ kernelBuilder . Services . AddSingleton < IChatClient > ( CreateUnavailableOpenAIChatClient ( ) ) ;
38
39
39
- // Create a fallback chat client that will fallback to the available chat client when unavailable chat client fails
40
- IChatClient fallbackChatClient = new FallbackChatClient ( [ unavailableChatClient , availableChatClient ] ) ;
40
+ // Create and register a cloud available chat client
41
+ kernelBuilder . Services . AddSingleton < IChatClient > ( CreateAzureOpenAIChatClient ( ) ) ;
42
+
43
+ // Create and register fallback chat client that will fallback to the available chat client when unavailable chat client fails
44
+ kernelBuilder . Services . AddSingleton < IChatCompletionService > ( ( sp ) =>
45
+ {
46
+ IEnumerable < IChatClient > chatClients = sp . GetServices < IChatClient > ( ) ;
41
47
42
- ChatOptions chatOptions = new ( ) { Tools = [ AIFunctionFactory . Create ( GetWeather ) ] } ;
48
+ return new FallbackChatClient ( chatClients . ToList ( ) ) . AsChatCompletionService ( ) ;
49
+ } ) ;
43
50
44
- var result = await fallbackChatClient . GetResponseAsync ( "Do I need an umbrella?" , chatOptions ) ;
51
+ Kernel kernel = kernelBuilder . Build ( ) ;
52
+ kernel . ImportPluginFromFunctions ( "Weather" , [ KernelFunctionFactory . CreateFromMethod ( ( ) => "It's sunny" , "GetWeather" ) ] ) ;
45
53
46
- Output . WriteLine ( result ) ;
54
+ AzureOpenAIPromptExecutionSettings settings = new ( )
55
+ {
56
+ FunctionChoiceBehavior = FunctionChoiceBehavior . Auto ( )
57
+ } ;
47
58
48
- [ Description ( "Gets the weather" ) ]
49
- string GetWeather ( ) => "It's sunny" ;
59
+ FunctionResult result = await kernel . InvokePromptAsync ( "Do I need an umbrella?" , new ( settings ) ) ;
60
+
61
+ Output . WriteLine ( result ) ;
50
62
}
51
63
52
64
/// <summary>
@@ -62,19 +74,22 @@ public async Task FallbackToAvailableModelStreamingAsync()
62
74
IChatClient availableChatClient = CreateAzureOpenAIChatClient ( ) ;
63
75
64
76
// Create a fallback chat client that will fallback to the available chat client when unavailable chat client fails
65
- IChatClient fallbackChatClient = new FallbackChatClient ( [ unavailableChatClient , availableChatClient ] ) ;
77
+ IChatCompletionService fallbackCompletionService = new FallbackChatClient ( [ unavailableChatClient , availableChatClient ] ) . AsChatCompletionService ( ) ;
66
78
67
- ChatOptions chatOptions = new ( ) { Tools = [ AIFunctionFactory . Create ( GetWeather ) ] } ;
79
+ Kernel kernel = new ( ) ;
80
+ kernel . ImportPluginFromFunctions ( "Weather" , [ KernelFunctionFactory . CreateFromMethod ( ( ) => "It's sunny" , "GetWeather" ) ] ) ;
68
81
69
- var result = fallbackChatClient . GetStreamingResponseAsync ( "Do I need an umbrella?" , chatOptions ) ;
82
+ AzureOpenAIPromptExecutionSettings settings = new ( )
83
+ {
84
+ FunctionChoiceBehavior = FunctionChoiceBehavior . Auto ( )
85
+ } ;
86
+
87
+ IAsyncEnumerable < StreamingChatMessageContent > result = fallbackCompletionService . GetStreamingChatMessageContentsAsync ( "Do I need an umbrella?" , settings , kernel ) ;
70
88
71
89
await foreach ( var update in result )
72
90
{
73
91
Output . WriteLine ( update ) ;
74
92
}
75
-
76
- [ Description ( "Gets the weather" ) ]
77
- string GetWeather ( ) => "It's sunny" ;
78
93
}
79
94
80
95
private static IChatClient CreateUnavailableOpenAIChatClient ( )
0 commit comments