Skip to content

Commit 583db49

Browse files
.Net: Update sample to demonstrate how hybrid AI orchestration can be used with Kernel (microsoft#10684)
1 parent d91c96d commit 583db49

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

dotnet/samples/Concepts/ChatCompletion/HybridCompletion_Fallback.cs

+33-18
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
using System.ClientModel;
44
using System.ClientModel.Primitives;
5-
using System.ComponentModel;
65
using System.Net;
76
using System.Runtime.CompilerServices;
87
using Azure.AI.OpenAI;
98
using Azure.Identity;
109
using Microsoft.Extensions.AI;
10+
using Microsoft.Extensions.DependencyInjection;
1111
using Microsoft.SemanticKernel;
12+
using Microsoft.SemanticKernel.ChatCompletion;
13+
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
1214

1315
namespace ChatCompletion;
1416

@@ -30,23 +32,33 @@ public class HybridCompletion_Fallback(ITestOutputHelper output) : BaseTest(outp
3032
[Fact]
3133
public async Task FallbackToAvailableModelAsync()
3234
{
33-
// Create an unavailable chat client that fails with 503 Service Unavailable HTTP status code
34-
IChatClient unavailableChatClient = CreateUnavailableOpenAIChatClient();
35+
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
3536

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());
3839

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>();
4147

42-
ChatOptions chatOptions = new() { Tools = [AIFunctionFactory.Create(GetWeather)] };
48+
return new FallbackChatClient(chatClients.ToList()).AsChatCompletionService();
49+
});
4350

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")]);
4553

46-
Output.WriteLine(result);
54+
AzureOpenAIPromptExecutionSettings settings = new()
55+
{
56+
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
57+
};
4758

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);
5062
}
5163

5264
/// <summary>
@@ -62,19 +74,22 @@ public async Task FallbackToAvailableModelStreamingAsync()
6274
IChatClient availableChatClient = CreateAzureOpenAIChatClient();
6375

6476
// 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();
6678

67-
ChatOptions chatOptions = new() { Tools = [AIFunctionFactory.Create(GetWeather)] };
79+
Kernel kernel = new();
80+
kernel.ImportPluginFromFunctions("Weather", [KernelFunctionFactory.CreateFromMethod(() => "It's sunny", "GetWeather")]);
6881

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);
7088

7189
await foreach (var update in result)
7290
{
7391
Output.WriteLine(update);
7492
}
75-
76-
[Description("Gets the weather")]
77-
string GetWeather() => "It's sunny";
7893
}
7994

8095
private static IChatClient CreateUnavailableOpenAIChatClient()

0 commit comments

Comments
 (0)