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
 
 
 
 
 
58ec31b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46ec487
58ec31b
46ec487
 
58ec31b
 
 
 
 
 
 
 
 
 
 
46ec487
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58ec31b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46ec487
58ec31b
 
 
 
46ec487
 
 
 
 
 
 
58ec31b
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# API Reference

Technical reference for Rox AI API endpoints.

## Base URL

```
https://Rox-Turbo-API.hf.space
```

---

## Endpoints

### POST /chat - Rox Core
### POST /turbo - Rox 2.1 Turbo
### POST /coder - Rox 3.5 Coder
### POST /turbo45 - Rox 4.5 Turbo
### POST /ultra - Rox 5 Ultra
### POST /dyno - Rox 6 Dyno
### POST /coder7 - Rox 7 Coder
### POST /vision - Rox Vision Max
### POST /hf/generate - HuggingFace Compatible

All endpoints use the same request/response format.

#### Request

**URL**: `/chat`  
**Method**: `POST`  
**Content-Type**: `application/json`

**Body Parameters**:

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `messages` | Array<Message> | Yes | - | Array of conversation messages |
| `temperature` | Float | No | 0.7 | Controls randomness (0.0 - 2.0) |
| `top_p` | Float | No | 0.95 | Nucleus sampling parameter (0.0 - 1.0) |
| `max_tokens` | Integer | No | 8192 | Maximum tokens in response |
| `stream` | Boolean | No | false | Enable streaming responses |

**Message Object**:

```typescript
{
  role: "user" | "assistant",
  content: string
}
```

**Example Request**:

```json
{
  "messages": [
    {
      "role": "user",
      "content": "What is artificial intelligence?"
    }
  ],
  "temperature": 0.7,
  "top_p": 0.95,
  "max_tokens": 8192,
  "stream": false
}
```

#### Response

**Standard Response** (200 OK):

```json
{
  "content": "Artificial intelligence (AI) refers to..."
}
```

**Streaming Response** (200 OK):

When `stream: true` is set, the response is sent as Server-Sent Events:

```
data: {"content": "Artificial"}
data: {"content": " intelligence"}
data: {"content": " (AI)"}
data: {"content": " refers"}
data: {"content": " to"}
data: {"content": "..."}
data: [DONE]
```

Each line starts with `data: ` followed by a JSON object containing a `content` field with the next token. The stream ends with `data: [DONE]`.

**Response Fields**:

| Field | Type | Description |
|-------|------|-------------|
| `content` | String | The generated response from Rox Core |

**Error Responses**:

**500 Internal Server Error**:
```json
{
  "detail": "Internal server error while calling Rox Core."
}
```

**502 Bad Gateway**:
```json
{
  "detail": "Bad response from upstream model provider."
}
```

**422 Unprocessable Entity**:
```json
{
  "detail": [
    {
      "loc": ["body", "messages"],
      "msg": "field required",
      "type": "value_error.missing"
    }
  ]
}
```

#### Example Usage

**Standard Request (cURL)**:
```bash
curl -X POST https://Rox-Turbo-API.hf.space/chat \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "Hello!"}
    ],
    "temperature": 0.7,
    "max_tokens": 8192
  }'
```

**Streaming Request (cURL)**:
```bash
curl -X POST https://Rox-Turbo-API.hf.space/chat \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "Hello!"}
    ],
    "stream": true
  }'
```

**Standard Request (JavaScript)**:
```javascript
const response = await fetch('https://Rox-Turbo-API.hf.space/chat', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    messages: [{ role: 'user', content: 'Hello!' }],
    temperature: 0.7,
    max_tokens: 8192
  })
});

const data = await response.json();
console.log(data.content);
```

**Streaming Request (JavaScript)**:
```javascript
const response = await fetch('https://Rox-Turbo-API.hf.space/chat', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    messages: [{ role: 'user', content: 'Hello!' }],
    stream: true
  })
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
  const { done, value } = await reader.read();
  if (done) break;

  const chunk = decoder.decode(value, { stream: true });
  const lines = chunk.split('\n');

  for (const line of lines) {
    if (line.startsWith('data: ')) {
      const data = line.slice(6).trim();
      if (data === '[DONE]') break;

      try {
        const parsed = JSON.parse(data);
        if (parsed.content) {
          process.stdout.write(parsed.content);
        }
      } catch (e) {}
    }
  }
}
```

**Standard Request (Python)**:
```python
import requests

response = requests.post('https://Rox-Turbo-API.hf.space/chat', json={
    'messages': [{'role': 'user', 'content': 'Hello!'}],
    'temperature': 0.7,
    'max_tokens': 8192
})

print(response.json()['content'])
```

**Streaming Request (Python)**:
```python
import requests
import json

response = requests.post(
    'https://Rox-Turbo-API.hf.space/chat',
    json={
        'messages': [{'role': 'user', 'content': 'Hello!'}],
        'stream': True
    },
    stream=True
)

for line in response.iter_lines():
    if line:
        line = line.decode('utf-8')
        if line.startswith('data: '):
            data = line[6:]
            if data == '[DONE]':
                break
            try:
                parsed = json.loads(data)
                if 'content' in parsed:
                    print(parsed['content'], end='', flush=True)
            except json.JSONDecodeError:
                pass
```

---

### POST /hf/generate

Hugging Face compatible text generation endpoint for single-turn interactions.

#### Request

**URL**: `/hf/generate`  
**Method**: `POST`  
**Content-Type**: `application/json`

**Body Parameters**:

| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| `inputs` | String | Yes | - | The input text/prompt |
| `parameters` | Object | No | {} | Generation parameters |

**Parameters Object**:

| Field | Type | Required | Default | Description |
|-------|------|----------|---------|-------------|
| `temperature` | Float | No | 1.0 | Controls randomness (0.0 - 2.0) |
| `top_p` | Float | No | 0.95 | Nucleus sampling (0.0 - 1.0) |
| `max_new_tokens` | Integer | No | 8192 | Maximum tokens to generate |

**Example Request**:

```json
{
  "inputs": "Write a haiku about technology",
  "parameters": {
    "temperature": 0.7,
    "top_p": 0.95,
    "max_new_tokens": 256
  }
}
```

#### Response

**Success Response** (200 OK):

```json
[
  {
    "generated_text": "Silicon dreams awake\nCircuits pulse with electric life\nFuture in our hands"
  }
]
```

**Response Format**:

Returns an array with a single object containing the generated text.

| Field | Type | Description |
|-------|------|-------------|
| `generated_text` | String | The generated response |

**Error Responses**:

Same as `/chat` endpoint (500, 502, 422).

#### Example Usage

**cURL**:
```bash
curl -X POST https://Rox-Turbo-API.hf.space/hf/generate \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": "Explain quantum computing",
    "parameters": {
      "temperature": 0.7,
      "max_new_tokens": 256
    }
  }'
```

**JavaScript**:
```javascript
const response = await fetch('https://Rox-Turbo-API.hf.space/hf/generate', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    inputs: 'Explain quantum computing',
    parameters: {
      temperature: 0.7,
      max_new_tokens: 256
    }
  })
});

const data = await response.json();
console.log(data[0].generated_text);
```

**Python**:
```python
import requests

response = requests.post('https://Rox-Turbo-API.hf.space/hf/generate', json={
    'inputs': 'Explain quantum computing',
    'parameters': {
        'temperature': 0.7,
        'max_new_tokens': 256
    }
})

print(response.json()[0]['generated_text'])
```

---

## Parameters

### temperature

Controls output randomness.

- **Range**: 0.0 to 2.0
- **Default**: 1.0
- **Lower** (0.1-0.5): Focused, deterministic
- **Medium** (0.6-1.0): Balanced
- **Higher** (1.1-2.0): Creative, varied

Examples:
- `0.3`: Math problems, factual questions, code generation
- `0.7`: General conversation, explanations
- `1.2`: Creative writing, brainstorming, storytelling

**Example**:
```json
{
  "messages": [{"role": "user", "content": "What is 2+2?"}],
  "temperature": 0.2
}
```

### top_p

Nucleus sampling parameter.

- **Range**: 0.0 to 1.0
- **Default**: 0.95 (/hf/generate), 1.0 (/chat)
- **Lower**: More focused
- **Higher**: More diverse

Example:
```json
{
  "messages": [{"role": "user", "content": "Tell me a story"}],
  "top_p": 0.9
}
```

### max_tokens / max_new_tokens

Maximum tokens in response.

- **Range**: 1 to 32768
- **Default**: 8192

Token estimation:
- ~1 token ≈ 4 characters
- ~1 token ≈ 0.75 words

Example:

```json
{
  "messages": [{"role": "user", "content": "Brief summary of AI"}],
  "max_tokens": 150
}
```

### stream

Enable streaming responses for real-time token delivery.

- **Type**: Boolean
- **Default**: false
- **Values**: true or false

When enabled, responses are sent as Server-Sent Events instead of a single JSON response. This allows you to display tokens as they are generated rather than waiting for the complete response.

Benefits of streaming:
- Lower perceived latency
- Better user experience for long responses
- Ability to cancel generation early
- Real-time feedback

Example:

```json
{
  "messages": [{"role": "user", "content": "Write a story"}],
  "stream": true
}
```

Response format when streaming is enabled:

```
data: {"content": "Once"}
data: {"content": " upon"}
data: {"content": " a"}
data: {"content": " time"}
data: [DONE]
```

---

## Error Handling

### Status Codes

| Code | Meaning | Description |
|------|---------|-------------|
| 200 | OK | Request successful |
| 422 | Unprocessable Entity | Invalid request format |
| 500 | Internal Server Error | Server-side error |
| 502 | Bad Gateway | Upstream model error |

### Error Response Format

```json
{
  "detail": "Error message here"
}
```

### Common Errors

Missing field:
```json
{
  "detail": [
    {
      "loc": ["body", "messages"],
      "msg": "field required",
      "type": "value_error.missing"
    }
  ]
}
```

Invalid type:
```json
{
  "detail": [
    {
      "loc": ["body", "temperature"],
      "msg": "value is not a valid float",
      "type": "type_error.float"
    }
  ]
}
```

Example error handler:

```javascript
async function safeRequest(endpoint, body) {
  try {
    const response = await fetch(endpoint, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(body)
    });

    if (!response.ok) {
      const error = await response.json();
      throw new Error(error.detail || `HTTP ${response.status}`);
    }

    return await response.json();
  } catch (error) {
    console.error('API Error:', error);
    throw error;
  }
}
```

---

## Rate Limiting

No enforced rate limits. Implement client-side limiting as needed.

---

## Client Wrapper Example

```javascript
class RoxAI {
  constructor(baseURL = 'https://Rox-Turbo-API.hf.space') {
    this.baseURL = baseURL;
  }

  async chat(messages, options = {}) {
    const response = await fetch(`${this.baseURL}/chat`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        messages,
        temperature: options.temperature || 0.7,
        top_p: options.top_p || 0.95,
        max_tokens: options.max_tokens || 8192,
        stream: false
      })
    });

    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`);
    }

    const data = await response.json();
    return data.content;
  }

  async chatStream(messages, onToken, options = {}) {
    const response = await fetch(`${this.baseURL}/chat`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        messages,
        temperature: options.temperature || 0.7,
        top_p: options.top_p || 0.95,
        max_tokens: options.max_tokens || 8192,
        stream: true
      })
    });

    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`);
    }

    const reader = response.body.getReader();
    const decoder = new TextDecoder();
    let fullContent = '';

    while (true) {
      const { done, value } = await reader.read();
      if (done) break;

      const chunk = decoder.decode(value, { stream: true });
      const lines = chunk.split('\n');

      for (const line of lines) {
        if (line.startsWith('data: ')) {
          const data = line.slice(6).trim();
          if (data === '[DONE]') break;

          try {
            const parsed = JSON.parse(data);
            if (parsed.content) {
              fullContent += parsed.content;
              onToken(parsed.content);
            }
          } catch (e) {}
        }
      }
    }

    return fullContent;
  }

  async generate(text, options = {}) {
    const response = await fetch(`${this.baseURL}/hf/generate`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        inputs: text,
        parameters: options
      })
    });

    if (!response.ok) {
      throw new Error(`HTTP ${response.status}`);
    }

    const data = await response.json();
    return data[0].generated_text;
  }
}

// Standard usage
const rox = new RoxAI();
const response = await rox.chat([
  { role: 'user', content: 'Hello!' }
]);
console.log(response);

// Streaming usage
await rox.chatStream(
  [{ role: 'user', content: 'Tell me a story' }],
  (token) => process.stdout.write(token)
);
```

---

---

Built by Mohammad Faiz