OpenAI WebSocket API: 75% குறைந்த செலவில் Real-Time Streaming
Smart AIPI இப்போது OpenAI-ன் WebSocket API-ஐ real-time, bidirectional streaming-க்கு ஆதரிக்கிறது. SSE-ஐ விட குறைந்த latency, persistent connections, மேலும் 75% குறைந்த விலை. எப்படி connect செய்வது என்பதை இங்கே பார்க்கலாம்.
சுருக்கமாக: Smart AIPI இப்போது OpenAI-ன் WebSocket API-ஐ ஆதரிக்கிறது. wss://api.smartaipi.com/v1/realtime க்கு connect செய்து, ஒரு response.create event-ஐ அனுப்பி, persistent connection வழியாக real time-ல் responses-ஐ stream செய்யலாம். அதே models, அதே protocol, 75% குறைந்த விலை.
AI models-உடன் தொடர்பு கொள்ள WebSocket streaming தான் மிக வேகமான வழி. பாரம்பரிய HTTP requests அல்லது Server-Sent Events (SSE) கூட அல்லாமல், WebSocket உங்கள் application மற்றும் API இடையில் persistent, bidirectional connection-ஐ பராமரிக்கிறது. ஒவ்வொரு request-க்கும் தனி connection setup இல்லை, HTTP overhead இல்லை, half-duplex வரம்புகளும் இல்லை.
Smart AIPI இப்போது இந்த protocol-ஐ wss://api.smartaipi.com/v1/realtime இல் ஆதரிக்கிறது — OpenAI-ன் WebSocket API-உடன் முழுமையாக இணக்கமானது, மேலும் 75% குறைந்த செலவில் கிடைக்கிறது.
ஏன் SSE-க்கு பதிலாக WebSocket?
AI streaming-க்கு Server-Sent Events ஒரு பொதுவான தரமாக இருந்தாலும், அதில் உள்ள trade-off-களை WebSocket நீக்குகிறது:
| அம்சம் | SSE (HTTP) | WebSocket |
|---|---|---|
| ஒவ்வொரு request-க்கும் connection | ஒவ்வொரு முறையும் புதிய connection | Persistent (மீண்டும் பயன்படுத்தப்படும்) |
| திசை | Server → Client மட்டும் | Bidirectional |
| ஒரே connection-ல் பல requests | இல்லை | ஆம் |
| முதல்-token latency | அதிகம் (புதிய TCP + TLS) | குறைவு (connection reuse) |
| சிறந்த பயன்பாடு | எளிய integrations | Agents, real-time apps, high-throughput |
தொடர்ச்சியாக பல டஜன் API calls செய்வதான agent loops-க்கு, persistent WebSocket connection மூலம் கிடைக்கும் மொத்த latency சேமிப்பு கணிசமானது.
இது எப்படி வேலை செய்கிறது
WebSocket API ஒரு event-driven protocol-ஐ பின்பற்றுகிறது. நீங்கள் server-க்கு JSON events-ஐ அனுப்புகிறீர்கள், server-இலிருந்து JSON events-ஐ திரும்பப் பெறுகிறீர்கள் — இவை அனைத்தும் ஒரே persistent connection வழியாக நடக்கிறது.
1. Connect செய்து Authenticate செய்யுங்கள்
உங்கள் API key-ஐ headers-ல் சேர்த்து ஒரு WebSocket connection-ஐத் திறக்கவும்:
wss://api.smartaipi.com/v1/realtime
Authorization: Bearer sk-proj-your-smart-aipi-key
OpenAI-Beta: realtime=v1
2. ஒரு Request அனுப்புங்கள்
உங்கள் prompt-உடன் ஒரு response.create event-ஐ அனுப்புங்கள்:
{
"type": "response.create",
"response": {
"model": "gpt-5.3-codex",
"store": false,
"instructions": "You are a helpful assistant.",
"input": [
{
"type": "message",
"role": "user",
"content": [
{ "type": "input_text", "text": "What is WebSocket?" }
]
}
]
}
}
குறிப்பு: store: false parameter என்பது Smart AIPI WebSocket connections-க்கு அவசியம்.
3. Streaming Events-ஐ பெறுங்கள்
response உருவாகும் போது server தொடர்ந்து events வரிசையை அனுப்பும்:
| Event | விளக்கம் |
|---|---|
| response.created | Response object உருவாக்கப்பட்டுள்ளது |
| response.output_item.added | புதிய output item (message) தொடங்கப்பட்டது |
| response.content_part.added | ஒரு output item-க்குள் content part தொடங்கப்பட்டது |
| response.output_text.delta | Text chunk (உண்மையான streamed content) |
| response.output_text.done | Text output முடிந்தது |
| response.completed | முழு response முடிந்தது (terminal event) |
Code Examples
Node.js
import WebSocket from "ws";
const ws = new WebSocket("wss://api.smartaipi.com/v1/realtime", {
headers: {
"Authorization": "Bearer sk-proj-your-key",
"OpenAI-Beta": "realtime=v1",
},
});
ws.on("open", () => {
ws.send(JSON.stringify({
type: "response.create",
response: {
model: "gpt-5.3-codex",
store: false,
input: [{
type: "message",
role: "user",
content: [{ type: "input_text", text: "Hello!" }],
}],
},
}));
});
ws.on("message", (data) => {
const event = JSON.parse(data);
if (event.type === "response.output_text.delta") {
process.stdout.write(event.delta);
}
if (event.type === "response.completed") {
console.log("\n\nDone. Usage:", event.response.usage);
ws.close();
}
});
Python
import asyncio
import json
import websockets
async def main():
headers = {
"Authorization": "Bearer sk-proj-your-key",
"OpenAI-Beta": "realtime=v1",
}
async with websockets.connect(
"wss://api.smartaipi.com/v1/realtime",
extra_headers=headers,
) as ws:
await ws.send(json.dumps({
"type": "response.create",
"response": {
"model": "gpt-5.3-codex",
"store": False,
"input": [{
"type": "message",
"role": "user",
"content": [{"type": "input_text", "text": "Hello!"}],
}],
},
}))
async for message in ws:
event = json.loads(message)
if event["type"] == "response.output_text.delta":
print(event["delta"], end="", flush=True)
if event["type"] == "response.completed":
print(f"\n\nUsage: {event['response']['usage']}")
break
asyncio.run(main())
cURL (விரைவு சோதனை)
ஒரே command மூலம் WebSocket handshake வெற்றிகரமாக நடைபெறுகிறதா என்பதைச் சரிபார்க்கவும்:
curl -isN --http1.1 \
-H "Connection: Upgrade" \
-H "Upgrade: websocket" \
-H "Sec-WebSocket-Version: 13" \
-H "Sec-WebSocket-Key: dGVzdA==" \
-H "Authorization: Bearer sk-proj-your-key" \
-H "OpenAI-Beta: realtime=v1" \
https://api.smartaipi.com/v1/realtime
வெற்றிகரமான connection-க்கு HTTP/1.1 101 Switching Protocols திரும்ப வரும்.
WebSocket மற்றும் SSE-ஐ எப்போது பயன்படுத்த வேண்டும்
இரண்டு protocol-களும் Smart AIPI வழியாக வேலை செய்கின்றன. உங்கள் use case அடிப்படையில் தேர்வு செய்யுங்கள்:
- SSE பயன்படுத்துங்கள் — எளிய integrations, one-off requests, மற்றும் மிகவும் எளிய implementation வேண்டும் என்றால். எந்த standard API call-லுமே
stream: trueஎன்று அமைத்தால் போதும். - WebSocket பயன்படுத்துங்கள் — agent loops, interactive applications, high-frequency request patterns, மற்றும் தொடர்ச்சியான calls இடையே குறைந்தபட்ச latency தேவைப்படும் இடங்களில்.
விலை நிர்ணயம்
WebSocket requests-க்கும் standard API requests போலவே token usage அடிப்படையில் கட்டணம் வசூலிக்கப்படுகிறது. 75% தள்ளுபடி இங்கேயும் பொருந்தும்:
| Model | OpenAI Direct | Smart AIPI | சேமிப்பு |
|---|---|---|---|
| GPT-5.3 Codex (output) | $14.00 / 1M tokens | $3.50 / 1M tokens | 75% |
| GPT-5.2 (output) | $10.00 / 1M tokens | $2.50 / 1M tokens | 75% |
| Codex Mini (output) | $0.60 / 1M tokens | $0.15 / 1M tokens | 75% |
தொடங்குவது எப்படி
- ஒரு API key பெறுங்கள் — smartaipi.com இல் பதிவு செய்யுங்கள் (இலவச credits உடன், credit card தேவையில்லை)
- Connect செய்யுங்கள் —
wss://api.smartaipi.com/v1/realtimeக்கு ஒரு WebSocket-ஐத் திறக்கவும் - Events அனுப்புங்கள் — உங்கள் model மற்றும் prompt உடன்
response.createenvelope-ஐ பயன்படுத்தவும் - Responses-ஐ stream செய்யுங்கள் —
response.output_text.deltaevents வந்தவுடன் அவற்றை process செய்யுங்கள்
நீங்கள் ஏற்கனவே OpenAI-ன் WebSocket API-ஐ பயன்படுத்தி வந்தால், மாற்ற வேண்டியது URL மட்டும். மற்ற அனைத்தும் — authentication, events, payload format — அப்படியே ஒன்றாக இருக்கும்.
அடிக்கடி கேட்கப்படும் கேள்விகள்
Smart AIPI, OpenAI WebSocket API-ஐ ஆதரிக்கிறதா?
ஆம். உங்கள் API key-ஐ Authorization header-ல் சேர்த்து wss://api.smartaipi.com/v1/realtime க்கு connect செய்யுங்கள். இந்த protocol, OpenAI-ன் WebSocket Responses API-உடன் முழுமையாக இணக்கமானது.
WebSocket, SSE-ஐ விட வேகமா?
தொடர்ச்சியான requests-க்கு, ஆம். WebSocket ஒரு persistent connection-ஐ பராமரிப்பதால், ஒவ்வொரு புதிய request-க்கும் SSE ஏற்படுத்தும் TCP மற்றும் TLS handshake overhead நீங்குகிறது. ஒரு தனிப்பட்ட one-off request-க்கு, வித்தியாசம் மிகச் சிறியது.
WebSocket வழியாக எந்த models வேலை செய்கின்றன?
Responses API வழியாக கிடைக்கும் அனைத்து models-மும்: GPT-5.3 Codex, GPT-5.2, Codex Mini மற்றும் பிறவை. response.create event-ல் model-ஐ குறிப்பிடுங்கள்.
WebSocket வழியாக function calling மற்றும் tool use வேலை செய்யுமா?
ஆம். முழு Responses API feature set-மும் கிடைக்கும் — function calling, tool use, structured outputs, மற்றும் multi-turn conversations அனைத்தும் WebSocket connection-ல் வேலை செய்கின்றன.
Connection-க்கு நேர வரம்பு உள்ளதா?
Idle connections 15 நிமிடங்களுக்கு பிறகு மூடப்படும். தேவையானபோது இடைவிடாத messages அனுப்புங்கள் அல்லது reconnect செய்யுங்கள். Data stream ஆகிக்கொண்டிருக்கும் active connections பாதிக்கப்படாது.
ஒரே connection-ல் பல requests அனுப்பலாமா?
ஆம். அதுதான் இதன் முக்கிய நன்மைகளில் ஒன்று. ஒரு response முடிந்த பிறகு, reconnect செய்யாமல் அதே connection-ல் இன்னொரு response.create event-ஐ அனுப்பலாம்.
OpenAI-compatible API gateway. Frontier AI models ஐ 75% குறைந்த செலவில் அணுகுங்கள்.
இலவசமாக தொடங்குங்கள்