Skip to content

Commit

Permalink
Allow strings to be passed for int args in TextSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
lossyrob committed Jan 9, 2025
1 parent 12b6b82 commit 2bf82e6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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

0 comments on commit 2bf82e6

Please sign in to comment.