Untuk output real-time, atur stream: true. Alih-alih menunggu respons penuh, Anda menerima potongan saat dihasilkan.
const stream = await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{ role: "user", content: "Tulis haiku tentang kode" }],
stream: true,
});
for await (const chunk of stream) {
const text = chunk.choices[0]?.delta?.content ?? "";
process.stdout.write(text);
}
const stream = await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{ role: "user", content: "Ceritakan sebuah kisah" }],
stream: true,
});
let fullText = "";
for await (const chunk of stream) {
fullText += chunk.choices[0]?.delta?.content ?? "";
}
console.log(fullText);
Batalkan stream yang sedang berjalan menggunakan metode abort():
const stream = await client.chat.completions.create({
model: "openai/gpt-4o",
messages: [{ role: "user", content: "Tulis esai panjang" }],
stream: true,
});
for await (const chunk of stream) {
const text = chunk.choices[0]?.delta?.content ?? "";
process.stdout.write(text);
if (shouldStop) {
stream.abort();
break;
}
}
Tidak ada judul di halaman ini.
