-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathCustomAIServiceSelector.cs
75 lines (67 loc) · 3.26 KB
/
CustomAIServiceSelector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) Microsoft. All rights reserved.
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Services;
namespace KernelExamples;
public class CustomAIServiceSelector(ITestOutputHelper output) : BaseTest(output)
{
/// <summary>
/// Show how to use a custom AI service selector to select a specific model
/// </summary>
[Fact]
public async Task RunAsync()
{
Console.WriteLine($"======== {nameof(CustomAIServiceSelector)} ========");
// Build a kernel with multiple chat completion services
var builder = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(
deploymentName: TestConfiguration.AzureOpenAI.ChatDeploymentName,
endpoint: TestConfiguration.AzureOpenAI.Endpoint,
apiKey: TestConfiguration.AzureOpenAI.ApiKey,
serviceId: "AzureOpenAIChat",
modelId: TestConfiguration.AzureOpenAI.ChatModelId)
.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey,
serviceId: "OpenAIChat");
builder.Services.AddSingleton<IAIServiceSelector>(new GptAIServiceSelector(this.Output)); // Use the custom AI service selector to select the GPT model
Kernel kernel = builder.Build();
// This invocation is done with the model selected by the custom selector
var prompt = "Hello AI, what can you do for me?";
var result = await kernel.InvokePromptAsync(prompt);
Console.WriteLine(result.GetValue<string>());
}
/// <summary>
/// Custom AI service selector that selects a GPT model.
/// This selector just naively selects the first service that provides
/// a completion model whose name starts with "gpt". But this logic could
/// be as elaborate as needed to apply your own selection criteria.
/// </summary>
private sealed class GptAIServiceSelector(ITestOutputHelper output) : IAIServiceSelector
{
private readonly ITestOutputHelper _output = output;
public bool TrySelectAIService<T>(
Kernel kernel, KernelFunction function, KernelArguments arguments,
[NotNullWhen(true)] out T? service, out PromptExecutionSettings? serviceSettings) where T : class, IAIService
{
foreach (var serviceToCheck in kernel.GetAllServices<T>())
{
// Find the first service that has a model id that starts with "gpt"
var serviceModelId = serviceToCheck.GetModelId();
var endpoint = serviceToCheck.GetEndpoint();
if (!string.IsNullOrEmpty(serviceModelId) && serviceModelId.StartsWith("gpt", StringComparison.OrdinalIgnoreCase))
{
this._output.WriteLine($"Selected model: {serviceModelId} {endpoint}");
service = serviceToCheck;
serviceSettings = new OpenAIPromptExecutionSettings();
return true;
}
}
service = null;
serviceSettings = null;
return false;
}
}
}