Skip to content

Commit

Permalink
Make KernelFunctionNoop internal
Browse files Browse the repository at this point in the history
  • Loading branch information
markwallace-microsoft committed Feb 18, 2025
1 parent 8e70cdd commit 5dbacd7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 1 addition & 3 deletions dotnet/src/Agents/Core/ChatCompletionAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ protected override Task<AgentChannel> RestoreChannelAsync(string channelState, C

internal static (IChatCompletionService service, PromptExecutionSettings? executionSettings) GetChatCompletionService(Kernel kernel, KernelArguments? arguments)
{
// Need to provide a KernelFunction to the service selector as a container for the execution-settings.
KernelFunction nullPrompt = new KernelFunctionNoop(arguments?.ExecutionSettings);
(IChatCompletionService chatCompletionService, PromptExecutionSettings? executionSettings) =
kernel.ServiceSelector.SelectAIService<IChatCompletionService>(
kernel,
nullPrompt,
arguments?.ExecutionSettings,
arguments ?? []);

return (chatCompletionService, executionSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace Microsoft.SemanticKernel;
/// </summary>
[RequiresUnreferencedCode("Uses reflection to handle various aspects of the function creation and invocation, making it incompatible with AOT scenarios.")]
[RequiresDynamicCode("Uses reflection to handle various aspects of the function creation and invocation, making it incompatible with AOT scenarios.")]
public sealed class KernelFunctionNoop : KernelFunction
internal sealed class KernelFunctionNoop : KernelFunction
{
/// <summary>
/// Creates a new instance of the <see cref="KernelFunctionNoop"/> class.
/// </summary>
/// <param name="executionSettings">Option: Prompt execution settings.</param>
public KernelFunctionNoop(IReadOnlyDictionary<string, PromptExecutionSettings>? executionSettings) :
internal KernelFunctionNoop(IReadOnlyDictionary<string, PromptExecutionSettings>? executionSettings) :
base($"Function_{Guid.NewGuid():N}", string.Empty, [], null, executionSettings?.ToDictionary(static kv => kv.Key, static kv => kv.Value))
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#pragma warning disable CA1716 // Identifiers should not match keywords

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -109,4 +111,32 @@ public static (T, PromptExecutionSettings?) SelectAIService<T>(

throw new KernelException(message.ToString());
}

/// <summary>
/// Resolves an <see cref="IAIService"/> and associated <see cref="PromptExecutionSettings"/> from the specified
/// <see cref="Kernel"/> based on a <see cref="KernelFunction"/> and associated <see cref="KernelArguments"/>.
/// </summary>
/// <typeparam name="T">
/// Specifies the type of the <see cref="IAIService"/> required. This must be the same type
/// with which the service was registered in the <see cref="IServiceCollection"/> orvia
/// the <see cref="IKernelBuilder"/>.
/// </typeparam>
/// <param name="selector">The <see cref="IAIServiceSelector"/> to use to select a service from the <see cref="Kernel"/>.</param>
/// <param name="kernel">The <see cref="Kernel"/> containing services, plugins, and other state for use throughout the operation.</param>
/// <param name="executionSettings">The dictionary of <see cref="PromptExecutionSettings"/> to use to select a service from the <see cref="Kernel"/>.</param>
/// <param name="arguments">The function arguments.</param>
/// <returns>A tuple of the selected service and the settings associated with the service (the settings may be null).</returns>
/// <exception cref="KernelException">An appropriate service could not be found.</exception>
[RequiresUnreferencedCode("Uses reflection to handle various aspects of the function creation and invocation, making it incompatible with AOT scenarios.")]
[RequiresDynamicCode("Uses reflection to handle various aspects of the function creation and invocation, making it incompatible with AOT scenarios.")]
public static (T, PromptExecutionSettings?) SelectAIService<T>(
this IAIServiceSelector selector,
Kernel kernel,
IReadOnlyDictionary<string, PromptExecutionSettings>? executionSettings,
KernelArguments arguments) where T : class, IAIService
{
// Need to provide a KernelFunction to the service selector as a container for the execution-settings.
KernelFunction nullPrompt = new KernelFunctionNoop(executionSettings);
return selector.SelectAIService<T>(kernel, nullPrompt, arguments);
}
}

0 comments on commit 5dbacd7

Please sign in to comment.