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

Enable overriding FunctionInvokingChatClient function discovery #5809

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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 @@ -551,8 +551,7 @@ private async Task<FunctionInvocationResult> ProcessFunctionCallAsync(
int iteration, int functionCallIndex, int totalFunctionCount, CancellationToken cancellationToken)
{
// Look up the AIFunction for the function call. If the requested function isn't available, send back an error.
AIFunction? function = options.Tools!.OfType<AIFunction>().FirstOrDefault(t => t.Metadata.Name == functionCallContent.Name);
if (function is null)
if (FindFunction(options, functionCallContent) is not AIFunction function)
{
return new(ContinueMode.Continue, FunctionStatus.NotFound, functionCallContent, result: null, exception: null);
}
Expand Down Expand Up @@ -630,7 +629,7 @@ FunctionResultContent CreateFunctionResultContent(FunctionInvocationResult resul
{
string message = result.Status switch
{
FunctionStatus.NotFound => "Error: Requested function not found.",
FunctionStatus.NotFound => $"Error: Requested function '{result.CallContent.Name}' not found.",
FunctionStatus.Failed => "Error: Function failed.",
_ => "Error: Unknown error.",
};
Expand All @@ -647,6 +646,22 @@ FunctionResultContent CreateFunctionResultContent(FunctionInvocationResult resul
}
}

/// <summary>Finds the <see cref="AIFunction"/> to use for the specified <see cref="FunctionCallContent"/>.</summary>
/// <param name="options">The <see cref="ChatOptions"/> used during the request. It contains the <see cref="ChatOptions.Tools"/> provided in the request.</param>
/// <param name="functionCallContent">The <see cref="FunctionCallContent"/> detailing the function requested to be called.</param>
/// <returns>The found <see cref="AIFunction"/>, or <see langword="null"/> if none could be found.</returns>
protected virtual AIFunction? FindFunction(
ChatOptions options, FunctionCallContent functionCallContent)
{
_ = Throw.IfNull(options);
_ = Throw.IfNull(functionCallContent);

return options
.Tools?
.OfType<AIFunction>()
.FirstOrDefault(t => t.Metadata.Name == functionCallContent.Name);
}

/// <summary>Invokes the function asynchronously.</summary>
/// <param name="context">
/// The function invocation context detailing the function to be invoked and its arguments along with additional request information.
Expand Down
Loading