Skip to content

Code Examples

Python (Streaming)

import requests
import json

url = "https://aiservices.novasuite.one/api/v1/modela-9"
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer nvai_sk_YOUR_API_KEY"
}
data = {
    "message": "What is machine learning?",
    "context": "You are a technical educator."
}

response = requests.post(url, json=data, headers=headers, stream=True)

for line in response.iter_lines():
    if line:
        line = line.decode('utf-8')
        if line.startswith('data: '):
            json_str = line[6:]
            event = json.loads(json_str)
            if event.get('type') == 'content_block_delta':
                print(event.get('text', ''), end='', flush=True)

Python (With Images)

import base64
# ... imports and headers ...

with open("image.jpg", "rb") as image_file:
    image_data = base64.b64encode(image_file.read()).decode('utf-8')

data = {
    "message": [
        { "type": "image", "media_type": "image/jpeg", "data": image_data },
        { "type": "text", "text": "Analyze this image" }
    ]
}
# ... request ...

JavaScript (Node.js)

const fetch = require('node-fetch');

// ... setup ...

fetch(url, { method: 'POST', headers, body })
  .then(async response => {
    const reader = response.body;
    for await (const chunk of reader) {
      // ... parse SSE stream ...
    }
  });