Streaming

Stream responses in real-time using Server-Sent Events (SSE) for better user experience.

Stream Chat Completions

{
  "model": "mesh/llama-3.1-8b-q4",
  "messages": [{"role": "user", "content": "Tell me a story"}],
  "stream": true
}

When stream: true, responses are sent as Server-Sent Events (SSE).

Python Streaming Example

import requests
import json

response = requests.post(
    "https://api.resonatia.io/v1/mesh/chat",
    headers={
        "Authorization": "Bearer YOUR_API_KEY",
        "Content-Type": "application/json"
    },
    json={
        "model": "mesh/llama-3.1-8b-q4",
        "messages": [{"role": "user", "content": "Tell me a story"}],
        "stream": True
    },
    stream=True
)

for line in response.iter_lines():
    if line:
        data = line.decode('utf-8')
        if data.startswith('data: '):
            chunk = json.loads(data[6:])
            if 'choices' in chunk:
                content = chunk['choices'][0].get('delta', {}).get('content', '')
                if content:
                    print(content, end='', flush=True)