-
Notifications
You must be signed in to change notification settings - Fork 4k
.Net Simplify configuration by ServiceId on Multi Model Scenarios. #6416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
markwallace-microsoft
merged 22 commits into
microsoft:main
from
RogerBarreto:features/service-id-settings
Jun 19, 2024
Merged
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1022444
Service Id on Execution Settings
RogerBarreto 8f4fdd1
Unit Tests Added, Sample Added
RogerBarreto b900aac
Adjust UT
RogerBarreto 33cba29
Fix typos
RogerBarreto 4f3edee
Merge branch 'main' into features/service-id-settings
markwallace-microsoft f702bb7
Address PR Feedback
RogerBarreto 14f768b
Added Invoke and PreConfigured samples, simplified code
RogerBarreto 7845415
Fix warnings
RogerBarreto 145f49b
Merge branch 'main' into features/service-id-settings
RogerBarreto 285782a
Address PR Comments
RogerBarreto f707927
is not, inverted
RogerBarreto 1b8cf01
Adding experimental tag
RogerBarreto ed28399
Merge branch 'main' into features/service-id-settings
RogerBarreto b025663
Update serviceId to be optional and not be auto-settable
RogerBarreto 2488dff
Fix warnings
RogerBarreto dbdb4af
Address PR comments
RogerBarreto 4f708df
Merge branch 'main' into features/service-id-settings
RogerBarreto 8104100
Merge branch 'main' into features/service-id-settings
RogerBarreto 37007cc
Merge branch 'main' into features/service-id-settings
RogerBarreto 423d24e
Address PR Comments
RogerBarreto 182b9bb
Typo fix
RogerBarreto 1088c4f
Merge branch 'main' into features/service-id-settings
RogerBarreto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
168 changes: 121 additions & 47 deletions
168
dotnet/samples/Concepts/ChatCompletion/Connectors_WithMultipleLLMs.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,156 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.SemanticKernel; | ||
using xRetry; | ||
|
||
namespace ChatCompletion; | ||
|
||
public class Connectors_WithMultipleLLMs(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
/// <summary> | ||
/// Show how to run a prompt function and specify a specific service to use. | ||
/// </summary> | ||
[RetryFact(typeof(HttpOperationException))] | ||
public async Task RunAsync() | ||
private const string ChatPrompt = "Hello AI, what can you do for me?"; | ||
|
||
private static Kernel BuildKernel() | ||
{ | ||
Kernel kernel = 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") | ||
.Build(); | ||
|
||
await RunByServiceIdAsync(kernel, "AzureOpenAIChat"); | ||
await RunByModelIdAsync(kernel, TestConfiguration.OpenAI.ChatModelId); | ||
await RunByFirstModelIdAsync(kernel, "gpt-4-1106-preview", TestConfiguration.AzureOpenAI.ChatModelId, TestConfiguration.OpenAI.ChatModelId); | ||
return 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") | ||
.Build(); | ||
} | ||
|
||
private async Task RunByServiceIdAsync(Kernel kernel, string serviceId) | ||
/// <summary> | ||
/// Invoke the prompt function to run for a specific service id. | ||
RogerBarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
/// <param name="serviceId">Service Id</param> | ||
[Theory] | ||
[InlineData("AzureOpenAIChat")] | ||
public async Task InvokePromptByServiceIdAsync(string serviceId) | ||
{ | ||
var kernel = BuildKernel(); | ||
Console.WriteLine($"======== Service Id: {serviceId} ========"); | ||
|
||
var prompt = "Hello AI, what can you do for me?"; | ||
var result = await kernel.InvokePromptAsync(ChatPrompt, new(new PromptExecutionSettings { ServiceId = serviceId })); | ||
|
||
KernelArguments arguments = []; | ||
arguments.ExecutionSettings = new Dictionary<string, PromptExecutionSettings>() | ||
{ | ||
{ serviceId, new PromptExecutionSettings() } | ||
}; | ||
var result = await kernel.InvokePromptAsync(prompt, arguments); | ||
Console.WriteLine(result.GetValue<string>()); | ||
} | ||
|
||
private async Task RunByModelIdAsync(Kernel kernel, string modelId) | ||
/// <summary> | ||
/// Invoke the prompt function to run for a specific model id. | ||
RogerBarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
[Fact] | ||
private async Task InvokePromptByModelIdAsync() | ||
{ | ||
var modelId = TestConfiguration.OpenAI.ChatModelId; | ||
var kernel = BuildKernel(); | ||
Console.WriteLine($"======== Model Id: {modelId} ========"); | ||
|
||
var prompt = "Hello AI, what can you do for me?"; | ||
var result = await kernel.InvokePromptAsync(ChatPrompt, new(new PromptExecutionSettings() { ModelId = modelId })); | ||
|
||
Console.WriteLine(result.GetValue<string>()); | ||
} | ||
|
||
/// <summary> | ||
/// Invoke the prompt function to preferably run for a list specific service ids where the | ||
/// first service id that is found respecting the order of the options provided will be used. | ||
RogerBarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
[Fact] | ||
public async Task InvokePromptFunctionWithFirstMatchingServiceIdAsync() | ||
{ | ||
string[] serviceIds = ["NotFound", "AzureOpenAIChat", "OpenAIChat"]; | ||
var kernel = BuildKernel(); | ||
Console.WriteLine($"======== Service Ids: {string.Join(", ", serviceIds)} ========"); | ||
|
||
var result = await kernel.InvokePromptAsync(ChatPrompt, new(serviceIds.Select(serviceId => new PromptExecutionSettings { ServiceId = serviceId }))); | ||
|
||
var result = await kernel.InvokePromptAsync( | ||
prompt, | ||
new(new PromptExecutionSettings() | ||
{ | ||
ModelId = modelId | ||
})); | ||
Console.WriteLine(result.GetValue<string>()); | ||
} | ||
|
||
private async Task RunByFirstModelIdAsync(Kernel kernel, params string[] modelIds) | ||
/// <summary> | ||
/// Invoke the prompt function to preferably run for a list of specific model ids where the | ||
/// first model id that is found respecting the order of the options provided will be used. | ||
RogerBarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
[Fact] | ||
public async Task InvokePromptFunctionWithFirstMatchingModelIdAsync() | ||
{ | ||
string[] modelIds = ["gpt-4-1106-preview", TestConfiguration.AzureOpenAI.ChatModelId, TestConfiguration.OpenAI.ChatModelId]; | ||
var kernel = BuildKernel(); | ||
Console.WriteLine($"======== Model Ids: {string.Join(", ", modelIds)} ========"); | ||
|
||
var prompt = "Hello AI, what can you do for me?"; | ||
var result = await kernel.InvokePromptAsync(ChatPrompt, new(modelIds.Select((modelId, index) => new PromptExecutionSettings { ServiceId = $"service-{index}", ModelId = modelId }))); | ||
|
||
Console.WriteLine(result.GetValue<string>()); | ||
} | ||
|
||
/// <summary> | ||
/// Create a function with a predefined configuration and invoke at later moment. | ||
RogerBarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
[Fact] | ||
public async Task InvokePreconfiguredFunctionWithFirstMatchingServiceIdAsync() | ||
{ | ||
string[] serviceIds = ["NotFound", "AzureOpenAIChat", "OpenAIChat"]; | ||
var kernel = BuildKernel(); | ||
Console.WriteLine($"======== Service Ids: {string.Join(", ", serviceIds)} ========"); | ||
|
||
var function = kernel.CreateFunctionFromPrompt(ChatPrompt, serviceIds.Select(serviceId => new PromptExecutionSettings { ServiceId = serviceId })); | ||
var result = await kernel.InvokeAsync(function); | ||
|
||
Console.WriteLine(result.GetValue<string>()); | ||
} | ||
|
||
var modelSettings = new Dictionary<string, PromptExecutionSettings>(); | ||
foreach (var modelId in modelIds) | ||
{ | ||
modelSettings.Add(modelId, new PromptExecutionSettings() { ModelId = modelId }); | ||
} | ||
var promptConfig = new PromptTemplateConfig(prompt) { Name = "HelloAI", ExecutionSettings = modelSettings }; | ||
/// <summary> | ||
/// Create a function with a predefined configuration to preferably run for a list specific model ids where the | ||
/// first model id that is found respecting the order of the options provided will be used. | ||
RogerBarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
[Fact] | ||
public async Task InvokePreconfiguredFunctionWithFirstMatchingModelIdAsync() | ||
{ | ||
string[] modelIds = ["gpt-4-1106-preview", TestConfiguration.AzureOpenAI.ChatModelId, TestConfiguration.OpenAI.ChatModelId]; | ||
var kernel = BuildKernel(); | ||
|
||
var function = kernel.CreateFunctionFromPrompt(promptConfig); | ||
Console.WriteLine($"======== Model Ids: {string.Join(", ", modelIds)} ========"); | ||
|
||
var function = kernel.CreateFunctionFromPrompt(ChatPrompt, modelIds.Select((modelId, index) => new PromptExecutionSettings { ServiceId = $"service-{index}", ModelId = modelId })); | ||
var result = await kernel.InvokeAsync(function); | ||
|
||
Console.WriteLine(result.GetValue<string>()); | ||
} | ||
|
||
/// <summary> | ||
/// Create a function with a predefined configuration to run for a specific model id. | ||
RogerBarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
[Fact] | ||
public async Task InvokePreconfiguredFunctionByModelIdAsync() | ||
{ | ||
var modelId = TestConfiguration.OpenAI.ChatModelId; | ||
var kernel = BuildKernel(); | ||
Console.WriteLine($"======== Model Id: {modelId} ========"); | ||
|
||
var function = kernel.CreateFunctionFromPrompt(ChatPrompt); | ||
var result = await kernel.InvokeAsync(function, new(new PromptExecutionSettings { ModelId = modelId })); | ||
|
||
Console.WriteLine(result.GetValue<string>()); | ||
} | ||
|
||
/// <summary> | ||
/// Create a function with a predefined configuration to run for a specific service id. | ||
RogerBarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// </summary> | ||
/// <param name="serviceId">Service Id</param> | ||
[Theory] | ||
[InlineData("AzureOpenAIChat")] | ||
public async Task InvokePreconfiguredFunctionByServiceIdAsync(string serviceId) | ||
{ | ||
var kernel = BuildKernel(); | ||
Console.WriteLine($"======== Service Id: {serviceId} ========"); | ||
|
||
var function = kernel.CreateFunctionFromPrompt(ChatPrompt); | ||
var result = await kernel.InvokeAsync(function, new(new PromptExecutionSettings { ServiceId = serviceId })); | ||
|
||
Console.WriteLine(result.GetValue<string>()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.