Skip to content

AI Streaming API

An AI/LLM API example demonstrating streaming responses with Server-Sent Events.

  • SSE streaming configuration
  • Dual response types (streaming and non-streaming)
  • Sentinel event filtering
  • Long timeout configuration
  • Streaming SDK usage patterns
openapi.yaml
openapi: 3.1.0
info:
title: AI Completions API
version: 1.0.0
servers:
- url: https://api.ai.example.com/v1
security:
- bearerAuth: []
paths:
/chat/completions:
post:
operationId: createChatCompletion
summary: Create a chat completion
description: |
Generate a chat completion. When `stream: true`, returns
Server-Sent Events stream of completion chunks.
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ChatCompletionRequest'
responses:
"200":
description: Chat completion
content:
application/json:
schema:
$ref: '#/components/schemas/ChatCompletion'
text/event-stream:
schema:
$ref: '#/components/schemas/ChatCompletionChunk'
/embeddings:
post:
operationId: createEmbedding
summary: Create embeddings
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/EmbeddingRequest'
responses:
"200":
description: Embeddings
content:
application/json:
schema:
$ref: '#/components/schemas/EmbeddingResponse'
/models:
get:
operationId: listModels
summary: List available models
responses:
"200":
description: Model list
content:
application/json:
schema:
type: object
properties:
data:
type: array
items:
$ref: '#/components/schemas/Model'
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
schemas:
ChatMessage:
type: object
required:
- role
- content
properties:
role:
type: string
enum: [system, user, assistant]
content:
type: string
name:
type: string
ChatCompletionRequest:
type: object
required:
- model
- messages
properties:
model:
type: string
example: "gpt-4"
messages:
type: array
items:
$ref: '#/components/schemas/ChatMessage'
minItems: 1
temperature:
type: number
minimum: 0
maximum: 2
default: 1
max_tokens:
type: integer
minimum: 1
stream:
type: boolean
default: false
ChatCompletion:
type: object
required:
- id
- model
- choices
properties:
id:
type: string
model:
type: string
choices:
type: array
items:
type: object
properties:
index:
type: integer
message:
$ref: '#/components/schemas/ChatMessage'
finish_reason:
type: string
enum: [stop, length, content_filter]
usage:
type: object
properties:
prompt_tokens:
type: integer
completion_tokens:
type: integer
total_tokens:
type: integer
ChatCompletionChunk:
type: object
required:
- id
- model
- choices
properties:
id:
type: string
model:
type: string
choices:
type: array
items:
type: object
properties:
index:
type: integer
delta:
type: object
properties:
role:
type: string
content:
type: string
finish_reason:
type: string
enum: [stop, length, content_filter]
nullable: true
EmbeddingRequest:
type: object
required:
- model
- input
properties:
model:
type: string
input:
oneOf:
- type: string
- type: array
items:
type: string
EmbeddingResponse:
type: object
properties:
data:
type: array
items:
type: object
properties:
index:
type: integer
embedding:
type: array
items:
type: number
usage:
type: object
properties:
prompt_tokens:
type: integer
total_tokens:
type: integer
Model:
type: object
properties:
id:
type: string
created:
type: integer
owned_by:
type: string
soxom.yaml
version: "1.0"
sdk:
name: ai-sdk
version: 1.0.0
description: SDK for AI Completions API
spec:
path: ./openapi.yaml
targets:
- typescript
- python
resources:
$client:
methods:
listModels: get /models
chat:
subresources:
completions:
models:
- ChatCompletion
- ChatCompletionChunk
- ChatMessage
methods:
create: post /chat/completions
embeddings:
methods:
create: post /embeddings
authentication:
default: bearer
env_vars:
bearer:
token: AI_API_KEY
# Streaming configuration
streaming:
sse:
enabled: true
sentinel_events:
- "[DONE]"
# Longer timeouts for AI operations
timeouts:
default_ms: 120000 # 2 minutes
operations:
chat_completions_create:
default_ms: 300000 # 5 minutes for long completions
# Retry on rate limits
retries:
enabled: true
max_attempts: 3
retry_on:
- 429
- 500
- 502
- 503
import { AIClient } from 'ai-sdk';
const client = new AIClient({
token: process.env.AI_API_KEY
});
// Non-streaming (default)
const completion = await client.chat.completions.create({
model: "gpt-4",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Explain quantum computing in simple terms." }
],
temperature: 0.7,
max_tokens: 500
});
console.log(completion.choices[0].message.content);
console.log(`Tokens used: ${completion.usage.total_tokens}`);
// Enable streaming
const stream = await client.chat.completions.create({
model: "gpt-4",
messages: [
{ role: "user", content: "Write a short story about a robot." }
],
stream: true
});
// Process chunks as they arrive
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
process.stdout.write(content);
}
// Check for completion
if (chunk.choices[0]?.finish_reason === "stop") {
console.log("\n[Complete]");
}
}
const stream = await client.chat.completions.create({
model: "gpt-4",
messages: [{ role: "user", content: "Hello!" }],
stream: true
});
// Collect all content
let fullContent = "";
for await (const chunk of stream) {
fullContent += chunk.choices[0]?.delta?.content ?? "";
}
console.log("Full response:", fullContent);
// Single text
const embedding = await client.embeddings.create({
model: "text-embedding-ada-002",
input: "Hello, world!"
});
console.log(`Dimensions: ${embedding.data[0].embedding.length}`);
// Multiple texts
const embeddings = await client.embeddings.create({
model: "text-embedding-ada-002",
input: ["Hello", "World", "AI"]
});
console.log(`Generated ${embeddings.data.length} embeddings`);
const models = await client.listModels();
for (const model of models.data) {
console.log(`${model.id} (${model.owned_by})`);
}