Skip to content
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

.Net: Function name policy to control function fqn creation and parsing #10206

Closed
Prev Previous commit
Next Next commit
update sample
SergeyMenshykh committed Jan 16, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 2f2717c2a02cf6185d0c25e0a4f2cf6e1db82904
Original file line number Diff line number Diff line change
@@ -92,31 +92,34 @@
IKernelBuilder builder = Kernel.CreateBuilder();

// Define a custom function FQN parser that can handle a hallucinated function name.
static (string? PluginName, string FunctioName) ParseFunctionFqn(ParseFunctionFqnContext context)

Check warning on line 95 in dotnet/samples/Concepts/FunctionCalling/FunctionCalling_FunctionNamePolicy.cs

GitHub Actions / Spell Check with Typos

"Functio" should be "Function".
{
// Try to use use hyphen as separator.
var parts = context.FunctionFqn.Split('-');
if (parts.Length == 2)
static (string? PluginName, string FunctioName)? Parse(ParseFunctionFqnContext context, char separator)

Check warning on line 97 in dotnet/samples/Concepts/FunctionCalling/FunctionCalling_FunctionNamePolicy.cs

GitHub Actions / Spell Check with Typos

"Functio" should be "Function".
{
// Check if the function registered in the kernel
if (context.Kernel is { } kernel && kernel.Plugins.TryGetFunction(parts[0], parts[1], out _))
var parts = context.FunctionFqn.Split(separator);
if (parts.Length == 2)
{
return (parts[0], parts[1]);
// Check if the function registered in the kernel
if (context.Kernel is { } kernel && kernel.Plugins.TryGetFunction(parts[0], parts[1], out _))
{
return (parts[0], parts[1]);
}
}

return null;
}

// If hyphen is not found, try to use underscore as separator. This approach presumes the underscore symbol is not used in function name.
parts = context.FunctionFqn.Split('_');
if (parts.Length == 2)
// Try to use use hyphen, dot, and underscore sequentially as separators.
var result = Parse(context, '-') ??
Parse(context, '.') ??
Parse(context, '_');

if (result is not null)
{
// Check if the function registered in the kernel
if (context.Kernel is { } kernel && kernel.Plugins.TryGetFunction(parts[0], parts[1], out _))
{
return (parts[0], parts[1]);
}
return result.Value;
}

// If no separator is found, return the function name as is.
// If no separator is found, return the function name as is allowing AI connector to apply default behavior.
return (null, context.FunctionFqn);
}