Skip to content

Chat Completion Streaming

Stream a chat completion response token by token.

This example assumes using tryAGI.OpenAI; is in scope and apiKey contains your tryAGI.OpenAI API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
using var client = new OpenAiClient(apiKey);

var enumerable = client.Chat.CreateChatCompletionAsStreamAsync(
    new CreateChatCompletionRequest
    {
        Value2 = new CreateChatCompletionRequestVariant2
        {
            Messages = ["Generate five random words."],
            Model = "gpt-4o-mini",
        }
    });

await foreach (string response in enumerable)
{
    Console.Write(response);
}