Endpoints
स्ट्रीमिंग
stream: true सेट करके server-sent events के ज़रिए टोकन स्ट्रीम करें। स्ट्रीमिंग प्रतिक्रिया पढ़ने और रद्द करने के उदाहरण यहाँ देखें।
playground में आज़माएंChat completions, Responses या Completions पर stream: true सेट करें, ताकि tokens जनरेट होते ही आपको मिलते जाएँ। Response server-sent events (Content-Type: text/event-stream) का इस्तेमाल करता है। api14 upstream से आने वाले bytes को आते ही आप तक पहुँचा देता है और उन्हें buffer नहीं करता।
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api14.com/api/v1",
apiKey: process.env.API14_API_KEY,
});
const stream = await client.chat.completions.create({
"model": "openai/gpt-5-mini",
"messages": [
{
"role": "user",
"content": "Explain vector databases in two sentences."
}
],
"stream": true
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
Event फ़ॉर्मैट
हर event एक data: लाइन है, जिसमें एक JSON chunk होता है। Stream data: [DONE] के साथ खत्म होता है।
data: {"id":"gen-1760000000-abc","object":"chat.completion.chunk","model":"openai/gpt-5-mini","choices":[{"index":0,"delta":{"role":"assistant","content":"Hel"}}]}
data: {"id":"gen-1760000000-abc","object":"chat.completion.chunk","model":"openai/gpt-5-mini","choices":[{"index":0,"delta":{"content":"lo!"}}]}
data: {"id":"gen-1760000000-abc","object":"chat.completion.chunk","model":"openai/gpt-5-mini","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":3,"total_tokens":13,"cost":0.0000085}}
data: [DONE]
[DONE]से पहले का आखिरी chunkusageलेकर आता है, जिसमें USD मेंcostशामिल है। आपकोstream_options.include_usageकी ज़रूरत नहीं। Usage reporting हमेशा चालू रहती है।idहर chunk में एक ही रहता है। यह वही generation id है जिसे आप Generation lookup में दे सकते हैं।- कुछ प्रोवाइडर कनेक्शन को ज़िंदा रखने के लिए SSE comment लाइनें (
:से शुरू होने वाली) भेजते हैं। SSE क्लाइंट और OpenAI SDK इन्हें अनदेखा करते हैं। अगर आप stream खुद parse करते हैं, तो:से शुरू होने वाली लाइनें छोड़ दें।
Streams की बिलिंग
Gateway stream को आगे भेजते समय अंतिम usage chunk पढ़ता है और stream बंद होने पर आपके बैलेंस से राशि काटता है।
कनेक्शन तोड़ने से बिल रद्द नहीं होता। अगर आप stream पूरा होने से पहले कनेक्शन बंद कर देते हैं, तो upstream प्रोवाइडर जनरेट करना जारी रख सकता है और फिर भी शुल्क ले सकता है। ऐसे में api14 लागत generation record से पढ़ता है। लागत सीमित करने के लिए max_tokens सेट करें।
Stream के दौरान त्रुटियाँ
पहले byte से पहले की त्रुटियाँ, जैसे अमान्य key, कम बैलेंस या अज्ञात मॉडल, 4xx या 5xx status के साथ एक सामान्य JSON error लौटाती हैं। देखें त्रुटियाँ। अगर कोई प्रोवाइडर stream के बीच में विफल हो जाए, तो stream एक ऐसे chunk के साथ खत्म हो सकता है जिसमें एक error object और finish_reason: "error" हो:
{ "error": { "code": 502, "message": "Provider disconnected" }, "choices": [{ "delta": { "content": "" }, "finish_reason": "error" }] }
आखिरी chunk पर हमेशा finish_reason जाँचें।
सुझाव
- Proxies और serverless प्लेटफ़ॉर्म कभी-कभी responses को buffer कर लेते हैं। अगर आप stream को ब्राउज़र तक relay करते हैं, तो response buffering बंद करें (जैसे nginx पर
X-Accel-Buffering: no)। - Tool calls
delta.tool_callsके टुकड़ों के रूप में stream होते हैं। Parse करने से पहलेargumentsstrings कोindexके हिसाब से जोड़ें। देखें Tool calling। - Reasoning मॉडल जवाब से पहले अपनी reasoning
delta.reasoningमें stream करते हैं। देखें Reasoning।