How to write streamable Native Function? #5944
-
Hi. I'm looking to use I currently have a native function like this
Ideally, I can use IAsyncEnumerable as the return type? |
Beta Was this translation helpful? Give feedback.
Answered by
dmytrostruk
Apr 22, 2024
Replies: 1 comment 2 replies
-
@LefanTan Yes, you can use [Fact]
public async Task StreamingWithNativeFunction()
{
// Arrange
async IAsyncEnumerable<int> TestMethod()
{
yield return 1;
yield return 2;
yield return 3;
}
var function = KernelFunctionFactory.CreateFromMethod(TestMethod);
var kernel = Kernel.CreateBuilder().Build();
// Act
var result = new List<int>();
await foreach (var item in kernel.InvokeStreamingAsync<int>(function))
{
result.Add(item);
}
// Assert
Assert.Equal([1, 2, 3], result);
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
sophialagerkranspandey
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@LefanTan Yes, you can use
IAsyncEnumerable
as the return type in your native function, and you can useInvokeStreamingAsync
to call this function. Here is test example with this functionality: