Skip to content

.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
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace ChatCompletion;

public class Connectors_WithMultipleLLMs(ITestOutputHelper output) : BaseTest(output)
{
private const string ChatPrompt = "Hello AI, what can you do for me?";
/// <summary>
/// Show how to run a prompt function and specify a specific service to use.
/// </summary>
Expand All @@ -26,57 +27,92 @@ public async Task RunAsync()
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);
// Preconfigured function settings
await PreconfiguredFunctionSettingsByFirstModelIdAsync(kernel, ["gpt-4-1106-preview", TestConfiguration.AzureOpenAI.ChatModelId, TestConfiguration.OpenAI.ChatModelId]);
await PreconfiguredFunctionSettingsByFirstServiceIdAsync(kernel, ["NotFound", "AzureOpenAIChat", "OpenAIChat"]);
await PreconfiguredFunctionSettingsByModelIdAsync(kernel, TestConfiguration.OpenAI.ChatModelId);
await PreconfiguredFunctionSettingsByServiceIdAsync(kernel, "AzureOpenAIChat");

// Per invocation settings
await InvocationSettingsByServiceIdAsync(kernel, "AzureOpenAIChat");
await InvocationSettingsByFirstServiceIdAsync(kernel, ["NotFound", "AzureOpenAIChat", "OpenAIChat"]);
await InvocationSettingsByModelIdAsync(kernel, TestConfiguration.OpenAI.ChatModelId);
await InvocationSettingsByFirstModelIdAsync(kernel, ["gpt-4-1106-preview", TestConfiguration.AzureOpenAI.ChatModelId, TestConfiguration.OpenAI.ChatModelId]);
}

private async Task RunByServiceIdAsync(Kernel kernel, string serviceId)
private async Task InvocationSettingsByServiceIdAsync(Kernel kernel, string serviceId)
{
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)
private async Task InvocationSettingsByFirstServiceIdAsync(Kernel kernel, string[] serviceIds)
{
Console.WriteLine($"======== Model Id: {modelId} ========");
Console.WriteLine($"======== Service Ids: {string.Join(", ", serviceIds)} ========");

var prompt = "Hello AI, what can you do for me?";
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)
private async Task InvocationSettingsByFirstModelIdAsync(Kernel kernel, string[] modelIds)
{
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>());
}

private async Task InvocationSettingsByModelIdAsync(Kernel kernel, string modelId)
{
Console.WriteLine($"======== Model Id: {modelId} ========");

var result = await kernel.InvokePromptAsync(ChatPrompt, new(new PromptExecutionSettings() { ModelId = modelId }));

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 };
Console.WriteLine(result.GetValue<string>());
}

var function = kernel.CreateFunctionFromPrompt(promptConfig);
private async Task PreconfiguredFunctionSettingsByFirstServiceIdAsync(Kernel kernel, string[] serviceIds)
{
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>());
}

private async Task PreconfiguredFunctionSettingsByFirstModelIdAsync(Kernel kernel, string[] modelIds)
{
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>());
}

private async Task PreconfiguredFunctionSettingsByModelIdAsync(Kernel kernel, string modelId)
{
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>());
}

private async Task PreconfiguredFunctionSettingsByServiceIdAsync(Kernel kernel, string serviceId)
{
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>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ public async Task<FunctionResult> ExecuteFlowAsync(
}

var executor = new FlowExecutor(this._kernelBuilder, this._flowStatusProvider, this._globalPluginCollection, this._config);
return await executor.ExecuteFlowAsync(flow, sessionId, input, kernelArguments ?? new KernelArguments(null)).ConfigureAwait(false);
return await executor.ExecuteFlowAsync(flow, sessionId, input, kernelArguments ?? new KernelArguments()).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ public class PromptExecutionSettings
/// </remarks>
public static string DefaultServiceId => "default";

/// <summary>
/// Service identifier.
/// This identifies the service these settings are configured for e.g., azure_openai_eastus, openai, ollama, huggingface, etc.
/// </summary>
[JsonPropertyName("service_id")]
public string? ServiceId
{
get => this._serviceId;

set
{
this.ThrowIfFrozen();
this._serviceId = value;
}
}

/// <summary>
/// Model identifier.
/// This identifies the AI model these settings are configured for e.g., gpt-4, gpt-3.5-turbo
Expand Down Expand Up @@ -93,6 +109,7 @@ public virtual PromptExecutionSettings Clone()
return new()
{
ModelId = this.ModelId,
ServiceId = this.ServiceId,
ExtensionData = this.ExtensionData is not null ? new Dictionary<string, object>(this.ExtensionData) : null
};
}
Expand All @@ -113,6 +130,7 @@ protected void ThrowIfFrozen()

private string? _modelId;
private IDictionary<string, object>? _extensionData;
private string? _serviceId;

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;

#pragma warning disable CA1710 // Identifiers should have correct suffix
Expand All @@ -21,6 +22,7 @@ public sealed class KernelArguments : IDictionary<string, object?>, IReadOnlyDic
{
/// <summary>Dictionary of name/values for all the arguments in the instance.</summary>
private readonly Dictionary<string, object?> _arguments;
private Dictionary<string, PromptExecutionSettings>? _executionSettings;

/// <summary>
/// Initializes a new instance of the <see cref="KernelArguments"/> class with the specified AI execution settings.
Expand All @@ -36,12 +38,36 @@ public KernelArguments()
/// </summary>
/// <param name="executionSettings">The prompt execution settings.</param>
public KernelArguments(PromptExecutionSettings? executionSettings)
: this(executionSettings is null ? null : [executionSettings])
{
this._arguments = new(StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Initializes a new instance of the <see cref="KernelArguments"/> class with the specified AI execution settings.
/// </summary>
/// <param name="executionSettings">The prompt execution settings.</param>
public KernelArguments(IEnumerable<PromptExecutionSettings>? executionSettings)
{
this._arguments = new(StringComparer.OrdinalIgnoreCase);
if (executionSettings is not null)
{
this.ExecutionSettings = new Dictionary<string, PromptExecutionSettings>() { { PromptExecutionSettings.DefaultServiceId, executionSettings } };
var newExecutionSettings = new Dictionary<string, PromptExecutionSettings>();
foreach (var settings in executionSettings)
{
var targetServiceId = settings.ServiceId ?? PromptExecutionSettings.DefaultServiceId;
if (newExecutionSettings.ContainsKey(targetServiceId))
{
var exceptionMessage = (targetServiceId == PromptExecutionSettings.DefaultServiceId)
? $"Default service id '{PromptExecutionSettings.DefaultServiceId}' must not be duplicated."
: $"Service id '{settings.ServiceId}' must not be duplicated and should match the key '{targetServiceId}'.";

throw new ArgumentException(exceptionMessage, nameof(executionSettings));
}

newExecutionSettings[targetServiceId] = settings;
}

this.ExecutionSettings = newExecutionSettings;
}
}

Expand All @@ -65,7 +91,38 @@ public KernelArguments(IDictionary<string, object?> source, Dictionary<string, P
/// <summary>
/// Gets or sets the prompt execution settings.
/// </summary>
public IReadOnlyDictionary<string, PromptExecutionSettings>? ExecutionSettings { get; set; }
/// <remarks>
/// The settings dictionary is keyed by the service ID, or <see cref="PromptExecutionSettings.DefaultServiceId"/> for the default execution settings.
/// When setting, the service id of each <see cref="PromptExecutionSettings"/> must match the key in the dictionary.
/// </remarks>
public IReadOnlyDictionary<string, PromptExecutionSettings>? ExecutionSettings
{
get => this._executionSettings;
set
{
// Clone the settings to avoid reference changes.
this._executionSettings = value is IDictionary<string, PromptExecutionSettings> dictionary
? new Dictionary<string, PromptExecutionSettings>(dictionary)
: value?.ToDictionary(kv => kv.Key, kv => kv.Value);

if (this._executionSettings is not null && this._executionSettings.Count != 0)
{
foreach (var kv in this._executionSettings!)
{
// Ensures that if a service id is not specified and is not default, it is set to the current service id.
if (kv.Key != kv.Value.ServiceId)
{
if (!string.IsNullOrWhiteSpace(kv.Value.ServiceId))
{
throw new ArgumentException($"Service id '{kv.Value.ServiceId}' must match the key '{kv.Key}'.", nameof(this.ExecutionSettings));
}

kv.Value.ServiceId = kv.Key;
}
}
}
}
}

/// <summary>
/// Gets the number of arguments contained in the <see cref="KernelArguments"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public List<InputVariable> InputVariables
/// </summary>
/// <remarks>
/// The settings dictionary is keyed by the service ID, or <see cref="PromptExecutionSettings.DefaultServiceId"/> for the default execution settings.
/// When setting, the service id of each <see cref="PromptExecutionSettings"/> must match the key in the dictionary.
/// </remarks>
[JsonPropertyName("execution_settings")]
public Dictionary<string, PromptExecutionSettings> ExecutionSettings
Expand All @@ -186,7 +187,25 @@ public Dictionary<string, PromptExecutionSettings> ExecutionSettings
set
{
Verify.NotNull(value);
this._executionSettings = value;

// Clone the settings to avoid reference changes.
this._executionSettings = new(value);
if (this._executionSettings.Count != 0)
{
foreach (var kv in this._executionSettings)
{
// Ensures that if a service id is not specified and is not default, it is set to the current service id.
if (kv.Key != kv.Value.ServiceId)
{
if (!string.IsNullOrWhiteSpace(kv.Value.ServiceId))
{
throw new ArgumentException($"Service id '{kv.Value.ServiceId}' must match the key '{kv.Key}'.", nameof(this.ExecutionSettings));
}

kv.Value.ServiceId = kv.Key;
}
}
}
}
}

Expand Down Expand Up @@ -224,13 +243,19 @@ public void AddExecutionSettings(PromptExecutionSettings settings, string? servi
{
Verify.NotNull(settings);

var key = serviceId ?? PromptExecutionSettings.DefaultServiceId;
var key = serviceId ?? settings.ServiceId ?? PromptExecutionSettings.DefaultServiceId;

// To avoid any reference changes to the settings object, clone it before changing service id.
var clonedSettings = settings.Clone();

// Overwrite the service id if provided in the method.
clonedSettings.ServiceId = key;
if (this.ExecutionSettings.ContainsKey(key))
{
throw new ArgumentException($"Execution settings for service id '{key}' already exists.", nameof(serviceId));
}

this.ExecutionSettings[key] = settings;
this.ExecutionSettings[key] = clonedSettings;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -107,6 +108,37 @@ public static KernelFunction CreateFromPrompt(
string? templateFormat = null,
IPromptTemplateFactory? promptTemplateFactory = null,
ILoggerFactory? loggerFactory = null) =>
KernelFunctionFromPrompt.Create(
promptTemplate,
CreateSettingsDictionary(executionSettings is null ? null : [executionSettings]),
functionName,
description,
templateFormat,
promptTemplateFactory,
loggerFactory);

/// <summary>
/// Creates a <see cref="KernelFunction"/> instance for a prompt specified via a prompt template.
/// </summary>
/// <param name="promptTemplate">Prompt template for the function.</param>
/// <param name="executionSettings">Default execution settings to use when invoking this prompt function.</param>
/// <param name="functionName">The name to use for the function. If null, it will default to a randomly generated name.</param>
/// <param name="description">The description to use for the function.</param>
/// <param name="templateFormat">The template format of <paramref name="promptTemplate"/>. This must be provided if <paramref name="promptTemplateFactory"/> is not null.</param>
/// <param name="promptTemplateFactory">
/// The <see cref="IPromptTemplateFactory"/> to use when interpreting the <paramref name="promptTemplate"/> into a <see cref="IPromptTemplate"/>.
/// If null, a default factory will be used.
/// </param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.</param>
/// <returns>The created <see cref="KernelFunction"/> for invoking the prompt.</returns>
public static KernelFunction CreateFromPrompt(
string promptTemplate,
IEnumerable<PromptExecutionSettings>? executionSettings,
string? functionName = null,
string? description = null,
string? templateFormat = null,
IPromptTemplateFactory? promptTemplateFactory = null,
ILoggerFactory? loggerFactory = null) =>
KernelFunctionFromPrompt.Create(promptTemplate, CreateSettingsDictionary(executionSettings), functionName, description, templateFormat, promptTemplateFactory, loggerFactory);

/// <summary>
Expand Down Expand Up @@ -141,10 +173,6 @@ public static KernelFunction CreateFromPrompt(
/// Wraps the specified settings into a dictionary with the default service ID as the key.
/// </summary>
[return: NotNullIfNotNull(nameof(settings))]
private static Dictionary<string, PromptExecutionSettings>? CreateSettingsDictionary(PromptExecutionSettings? settings) =>
settings is null ? null :
new Dictionary<string, PromptExecutionSettings>(1)
{
{ PromptExecutionSettings.DefaultServiceId, settings },
};
private static Dictionary<string, PromptExecutionSettings>? CreateSettingsDictionary(IEnumerable<PromptExecutionSettings>? settings) =>
settings?.ToDictionary(s => s.ServiceId ?? PromptExecutionSettings.DefaultServiceId, s => s);
}
Loading
Loading