Skip to content

Commit 58a1389

Browse files
.Net: BugFix TryGetFunction + FunctionInvocationFilter when using IChatClient (microsoft#12405)
### Motivation and Context - Fixes microsoft#12378 Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
1 parent e6b2cf3 commit 58a1389

3 files changed

Lines changed: 500 additions & 4 deletions

File tree

dotnet/src/SemanticKernel.Abstractions/AI/ChatClient/KernelFunctionInvokingChatClient.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ await autoFunctionInvocationFilters[index].OnAutoFunctionInvocationAsync(
102102
// Note that we explicitly do not use executionSettings here; those pertain to the all-up operation and not necessarily to any
103103
// further calls made as part of this function invocation. In particular, we must not use function calling settings naively here,
104104
// as the called function could in turn telling the model about itself as a possible candidate for invocation.
105-
result = await autoContext.AIFunction.InvokeAsync(autoContext.Arguments, cancellationToken).ConfigureAwait(false);
106-
ctx.Result = new FunctionResult(ctx.Function, result);
105+
ctx.Result = await autoContext.Function.InvokeAsync(kernelChatOptions.Kernel, autoContext.Arguments, cancellationToken).ConfigureAwait(false);
107106
}).ConfigureAwait(false);
108107
result = autoContext.Result.GetValue<object>();
109108

dotnet/src/SemanticKernel.Core/Functions/DefaultKernelPlugin.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
7+
using Microsoft.Extensions.AI;
68

79
namespace Microsoft.SemanticKernel;
810

@@ -41,8 +43,27 @@ internal DefaultKernelPlugin(string name, string? description, IEnumerable<Kerne
4143
public override int FunctionCount => this._functions.Count;
4244

4345
/// <inheritdoc/>
44-
public override bool TryGetFunction(string name, [NotNullWhen(true)] out KernelFunction? function) =>
45-
this._functions.TryGetValue(name, out function);
46+
public override bool TryGetFunction(string name, [NotNullWhen(true)] out KernelFunction? function)
47+
{
48+
if (this._functions.TryGetValue(name, out function))
49+
{
50+
return true;
51+
}
52+
53+
if (this._functions.Count == 0 || name.Length <= this.Name.Length)
54+
{
55+
// The function name is too short to have the plugin name aborting the search.
56+
function = null;
57+
return false;
58+
}
59+
60+
// When a kernel function is used as an ai function by IChatClients it needs to be discoverable by the FQN.
61+
function = (KernelFunction?)this._functions.Values
62+
.Select(f => f as AIFunction)
63+
.FirstOrDefault(aiFunction => aiFunction.Name == name);
64+
65+
return function is not null;
66+
}
4667

4768
/// <inheritdoc/>
4869
public override IEnumerator<KernelFunction> GetEnumerator() => this._functions.Values.GetEnumerator();

0 commit comments

Comments
 (0)