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: Allow strings to be passed for int arguments in TextSearch KernelFunctions #10147

Closed
Closed
Show file tree
Hide file tree
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 @@ -390,9 +390,16 @@ async Task<IEnumerable<object>> GetSearchResultAsync(Kernel kernel, KernelFuncti
/// <param name="defaultValue">Default value of the argument.</param>
private static int GetArgumentValue(KernelArguments arguments, IReadOnlyList<KernelParameterMetadata> parameters, string name, int defaultValue)
{
if (arguments.TryGetValue(name, out var value) && value is int argument)
if (arguments.TryGetValue(name, out var value))
{
return argument;
if (value is int argument)
{
return argument;
}
else if (value is string argumentString && int.TryParse(argumentString, out var parsedArgument))
{
return parsedArgument;
}
}

value = parameters.FirstOrDefault(parameter => parameter.Name == name)?.DefaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ public async Task CountCanBeOverriddenInArgumentsAsync(KernelFunction function,
Assert.Equal(5, results.Count());
}

[Theory]
[MemberData(nameof(StandardFunctions))]
public async Task CountCanBeOverriddenInArgumentsWithStringAsync(KernelFunction function, string _)
{
// Act
var result = await function.InvokeAsync(new(), new() { ["query"] = "What is the Semantic Kernel?", ["count"] = "5" });

// Assert
Assert.NotNull(result);
var results = result.GetValue<IEnumerable<object>>();
Assert.NotNull(results);
Assert.NotEmpty(results);
Assert.Equal(5, results.Count());
}

#region private
/// <summary>
/// Create the default <see cref="KernelFunctionFromMethodOptions"/> for <see cref="ITextSearch.SearchAsync(string, TextSearchOptions?, CancellationToken)"/>.
Expand Down
Loading