Skip to content

[Question] How to interrupt the streaming output? #567

Answered by kayhantolga
MayDay-wpf asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, you can pass a cancellation token and cancel the token if you want to stop streaming. Here is a sample from the playground (the sample is for legacy completion, but chat completion should work the same way).

public static async Task RunSimpleCompletionStreamTestWithCancellationToken(IOpenAIService sdk)
{
    ConsoleExtensions.WriteLine("Completion Stream Testing is starting:", ConsoleColor.Cyan);
    var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(2)).Token;
    cancellationToken.Register(() => Console.WriteLine("Cancellation Token has been cancelled."));

    try
    {
        ConsoleExtensions.WriteLine("Completion Stream Test:", ConsoleColor.DarkCyan);

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by MayDay-wpf
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
Converted from issue

This discussion was converted from issue #566 on May 21, 2024 11:15.