Skip to content

How to write streamable Native Function? #5944

Answered by dmytrostruk
LefanTan asked this question in Q&A
Discussion options

You must be logged in to vote

@LefanTan Yes, you can use IAsyncEnumerable as the return type in your native function, and you can use InvokeStreamingAsync to call this function. Here is test example with this functionality:

[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 

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@dmytrostruk
Comment options

@LefanTan
Comment options

Answer selected by sophialagerkranspandey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants