AI Streaming API
An AI/LLM API example demonstrating streaming responses with Server-Sent Events.
Features Demonstrated
Section titled “Features Demonstrated”- SSE streaming configuration
- Dual response types (streaming and non-streaming)
- Sentinel event filtering
- Long timeout configuration
- Streaming SDK usage patterns
OpenAPI Specification
Section titled “OpenAPI Specification”openapi: 3.1.0info: 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: stringSoxom Configuration
Section titled “Soxom Configuration”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 configurationstreaming: sse: enabled: true sentinel_events: - "[DONE]"
# Longer timeouts for AI operationstimeouts: default_ms: 120000 # 2 minutes operations: chat_completions_create: default_ms: 300000 # 5 minutes for long completions
# Retry on rate limitsretries: enabled: true max_attempts: 3 retry_on: - 429 - 500 - 502 - 503Generated SDK Usage
Section titled “Generated SDK Usage”Non-Streaming Completion
Section titled “Non-Streaming Completion”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}`);from ai_sdk import AIClient
client = AIClient(token=os.environ["AI_API_KEY"])
# Non-streaming (default)completion = 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)
print(completion.choices[0].message.content)print(f"Tokens used: {completion.usage.total_tokens}")Streaming Completion
Section titled “Streaming Completion”// Enable streamingconst 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 arrivefor 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]"); }}# Enable streamingstream = client.chat.completions.create( model="gpt-4", messages=[ {"role": "user", "content": "Write a short story about a robot."} ], stream=True)
# Process chunks as they arrivefor chunk in stream: content = chunk.choices[0].delta.content if content: print(content, end="", flush=True)
# Check for completion if chunk.choices[0].finish_reason == "stop": print("\n[Complete]")Collecting Streamed Response
Section titled “Collecting Streamed Response”const stream = await client.chat.completions.create({ model: "gpt-4", messages: [{ role: "user", content: "Hello!" }], stream: true});
// Collect all contentlet fullContent = "";for await (const chunk of stream) { fullContent += chunk.choices[0]?.delta?.content ?? "";}
console.log("Full response:", fullContent);stream = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Hello!"}], stream=True)
# Collect all contentfull_content = ""for chunk in stream: full_content += chunk.choices[0].delta.content or ""
print("Full response:", full_content)Embeddings
Section titled “Embeddings”// Single textconst embedding = await client.embeddings.create({ model: "text-embedding-ada-002", input: "Hello, world!"});console.log(`Dimensions: ${embedding.data[0].embedding.length}`);
// Multiple textsconst embeddings = await client.embeddings.create({ model: "text-embedding-ada-002", input: ["Hello", "World", "AI"]});console.log(`Generated ${embeddings.data.length} embeddings`);# Single textembedding = client.embeddings.create( model="text-embedding-ada-002", input="Hello, world!")print(f"Dimensions: {len(embedding.data[0].embedding)}")
# Multiple textsembeddings = client.embeddings.create( model="text-embedding-ada-002", input=["Hello", "World", "AI"])print(f"Generated {len(embeddings.data)} embeddings")List Models
Section titled “List Models”const models = await client.listModels();for (const model of models.data) { console.log(`${model.id} (${model.owned_by})`);}models = client.list_models()for model in models.data: print(f"{model.id} ({model.owned_by})")