import OpenAI from "openai";
const openai = new OpenAI();
async function main() {
const stream = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "user",
content: `your question`,
},
],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
}
main();
直接執行:
bun test.ts
程式碼會自動從.env
讀取OPENAI_API_KEY
、OPENAI_BASE_URL
等值,執行之前請設定好。