2025年1月11日 星期六

Openai API: Non-streaming vs. streaming, Python vs. Javascript

Reference: https://platform.openai.com/docs/api-reference/streaming Python + Streaming 寫法: ''' from openai import OpenAI client = OpenAI() stream = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Say this is a test"}], stream=True, ) for chunk in stream: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end="") ''' Node / Typescript + Streaming 寫法: ''' import OpenAI from "openai"; const openai = new OpenAI(); async function main() { const stream = await openai.chat.completions.create({ model: "gpt-4o-mini", messages: [{ role: "user", content: "Say this is a test" }], stream: true, }); for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content || ""); } } main(); '''

沒有留言: