typescript openai gtp-4o 示例
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等值,运行之前请设置好。

浙公网安备 33010602011771号