Streaming
Gerçek zamanlı yanıt akışı
Streaming özelliği, AI modelinden gelen yanıtları gerçek zamanlı olarak almanızı sağlar. Bu sayede kullanıcılar, tam yanıt oluşturulmadan önce kısmi sonuçları görebilir.
Daha İyi UX: Streaming kullanarak kullanıcılarınıza daha hızlı geri bildirim sağlayabilirsiniz. Özellikle uzun yanıtlar için bekleme süresini önemli ölçüde azaltır.
Streaming'i Etkinleştirme
İsteğinizde stream: true parametresini ekleyin:
curl https://api.onysoft.com/v1/chat/completions \
-H "Authorization: Bearer sk-ony-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemini-3.8-flash",
"messages": [{"role": "user", "content": "Merhaba!"}],
"stream": true
}'
Stream Yanıt Formatı
Streaming yanıtlar Server-Sent Events (SSE) formatında gelir:
data: {"id":"req_a1b2c3d4e5f6a1b2c3d4e5f6","object":"chat.completion.chunk","choices":[{"delta":{"content":"Merhaba"}}]}
data: {"id":"req_a1b2c3d4e5f6a1b2c3d4e5f6","object":"chat.completion.chunk","choices":[{"delta":{"content":"!"}}]}
data: {"id":"req_a1b2c3d4e5f6a1b2c3d4e5f6","object":"chat.completion.chunk","choices":[{"delta":{"content":" Nasıl"}}]}
data: {"id":"req_a1b2c3d4e5f6a1b2c3d4e5f6","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: {"id":"req_a1b2c3d4e5f6a1b2c3d4e5f6","object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":8,"total_tokens":20},"cost":{"amount":0.000008,"currency":"USD"}}
data: [DONE]
Son parçada choices boş bir dizidir; isteğin usage ve cost bilgisi bu parçadadır. Bu parça stream_options gönderilmese de her zaman gelir. Bazı modellerde hemen öncesinde sağlayıcının ayrıntılı token kırılımlı kendi usage parçası da gelebilir; faturalanan kesin değerler cost içeren son parçadadır. Döngünüzde choices dizisinin boş olabileceğini kontrol edin (aşağıdaki örneklerde olduğu gibi).
Python ile Streaming
from openai import OpenAI
client = OpenAI(
api_key="sk-ony-your-api-key",
base_url="https://api.onysoft.com/v1"
)
stream = client.chat.completions.create(
model="google/gemini-3.8-flash",
messages=[{"role": "user", "content": "Merhaba!"}],
stream=True
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
Node.js ile Streaming
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-ony-your-api-key',
baseURL: 'https://api.onysoft.com/v1'
});
const stream = await client.chat.completions.create({
model: 'google/gemini-3.8-flash',
messages: [{ role: 'user', content: 'Merhaba!' }],
stream: true
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
Responses API Akışı
POST /v1/responses akışında response.* olayları gelir; data: [DONE] yazılmaz, akış response.completed, response.incomplete ya da response.failed ile biter. Ayrıntı: Responses API.
Anthropic Messages Akışı
POST /v1/messages akışında Anthropic SSE olayları gelir (message_start … message_stop). Akış başladıktan sonra oluşan geçici bir hata event: error olayıyla ve overloaded_error türüyle bildirilir. Ayrıntı: Anthropic Messages API.
Üç uçta da akış sırasında : ile başlayan yorum satırları gelebilir: bağlantıyı canlı tutmak içindir; SSE istemcileri ve SDK'lar bunları yok sayar.