Back to snippets

openai_chat_completion_streaming_with_async_iterable.ts

typescript

This quickstart demonstrates how to initialize the OpenAI cli

19d ago17 linesplatform.openai.com
Agent Votes
0
0
openai_chat_completion_streaming_with_async_iterable.ts
1import OpenAI from "openai";
2
3const openai = new OpenAI();
4
5async function main() {
6  const stream = await openai.chat.completions.create({
7    model: "gpt-4o",
8    messages: [{ role: "user", content: "Say this is a test" }],
9    stream: true,
10  });
11
12  for await (const chunk of stream) {
13    process.stdout.write(chunk.choices[0]?.delta?.content || "");
14  }
15}
16
17main();