Upload 4 files
Browse files- docs/API_REFERENCE.md +206 -20
- docs/DEVELOPER_GUIDE.md +765 -213
docs/API_REFERENCE.md
CHANGED
|
@@ -35,9 +35,10 @@ All endpoints use the same request/response format.
|
|
| 35 |
| Parameter | Type | Required | Default | Description |
|
| 36 |
|-----------|------|----------|---------|-------------|
|
| 37 |
| `messages` | Array<Message> | Yes | - | Array of conversation messages |
|
| 38 |
-
| `temperature` | Float | No |
|
| 39 |
-
| `top_p` | Float | No |
|
| 40 |
-
| `max_tokens` | Integer | No |
|
|
|
|
| 41 |
|
| 42 |
**Message Object**:
|
| 43 |
|
|
@@ -58,15 +59,16 @@ All endpoints use the same request/response format.
|
|
| 58 |
"content": "What is artificial intelligence?"
|
| 59 |
}
|
| 60 |
],
|
| 61 |
-
"temperature":
|
| 62 |
"top_p": 0.95,
|
| 63 |
-
"max_tokens":
|
|
|
|
| 64 |
}
|
| 65 |
```
|
| 66 |
|
| 67 |
#### Response
|
| 68 |
|
| 69 |
-
**
|
| 70 |
|
| 71 |
```json
|
| 72 |
{
|
|
@@ -74,6 +76,22 @@ All endpoints use the same request/response format.
|
|
| 74 |
}
|
| 75 |
```
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
**Response Fields**:
|
| 78 |
|
| 79 |
| Field | Type | Description |
|
|
@@ -111,7 +129,7 @@ All endpoints use the same request/response format.
|
|
| 111 |
|
| 112 |
#### Example Usage
|
| 113 |
|
| 114 |
-
**cURL**:
|
| 115 |
```bash
|
| 116 |
curl -X POST https://Rox-Turbo-API.hf.space/chat \
|
| 117 |
-H "Content-Type: application/json" \
|
|
@@ -119,20 +137,32 @@ curl -X POST https://Rox-Turbo-API.hf.space/chat \
|
|
| 119 |
"messages": [
|
| 120 |
{"role": "user", "content": "Hello!"}
|
| 121 |
],
|
| 122 |
-
"temperature":
|
| 123 |
-
"max_tokens":
|
| 124 |
}'
|
| 125 |
```
|
| 126 |
|
| 127 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
```javascript
|
| 129 |
const response = await fetch('https://Rox-Turbo-API.hf.space/chat', {
|
| 130 |
method: 'POST',
|
| 131 |
headers: { 'Content-Type': 'application/json' },
|
| 132 |
body: JSON.stringify({
|
| 133 |
messages: [{ role: 'user', content: 'Hello!' }],
|
| 134 |
-
temperature:
|
| 135 |
-
max_tokens:
|
| 136 |
})
|
| 137 |
});
|
| 138 |
|
|
@@ -140,19 +170,85 @@ const data = await response.json();
|
|
| 140 |
console.log(data.content);
|
| 141 |
```
|
| 142 |
|
| 143 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
```python
|
| 145 |
import requests
|
| 146 |
|
| 147 |
response = requests.post('https://Rox-Turbo-API.hf.space/chat', json={
|
| 148 |
'messages': [{'role': 'user', 'content': 'Hello!'}],
|
| 149 |
-
'temperature':
|
| 150 |
-
'max_tokens':
|
| 151 |
})
|
| 152 |
|
| 153 |
print(response.json()['content'])
|
| 154 |
```
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
---
|
| 157 |
|
| 158 |
### POST /hf/generate
|
|
@@ -313,8 +409,8 @@ Example:
|
|
| 313 |
|
| 314 |
Maximum tokens in response.
|
| 315 |
|
| 316 |
-
- **Range**: 1 to
|
| 317 |
-
- **Default**:
|
| 318 |
|
| 319 |
Token estimation:
|
| 320 |
- ~1 token ≈ 4 characters
|
|
@@ -329,6 +425,41 @@ Example:
|
|
| 329 |
}
|
| 330 |
```
|
| 331 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
---
|
| 333 |
|
| 334 |
## Error Handling
|
|
@@ -424,9 +555,10 @@ class RoxAI {
|
|
| 424 |
headers: { 'Content-Type': 'application/json' },
|
| 425 |
body: JSON.stringify({
|
| 426 |
messages,
|
| 427 |
-
temperature: options.temperature ||
|
| 428 |
top_p: options.top_p || 0.95,
|
| 429 |
-
max_tokens: options.max_tokens ||
|
|
|
|
| 430 |
})
|
| 431 |
});
|
| 432 |
|
|
@@ -438,6 +570,53 @@ class RoxAI {
|
|
| 438 |
return data.content;
|
| 439 |
}
|
| 440 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
async generate(text, options = {}) {
|
| 442 |
const response = await fetch(`${this.baseURL}/hf/generate`, {
|
| 443 |
method: 'POST',
|
|
@@ -457,11 +636,18 @@ class RoxAI {
|
|
| 457 |
}
|
| 458 |
}
|
| 459 |
|
| 460 |
-
//
|
| 461 |
const rox = new RoxAI();
|
| 462 |
const response = await rox.chat([
|
| 463 |
{ role: 'user', content: 'Hello!' }
|
| 464 |
]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
```
|
| 466 |
|
| 467 |
---
|
|
|
|
| 35 |
| Parameter | Type | Required | Default | Description |
|
| 36 |
|-----------|------|----------|---------|-------------|
|
| 37 |
| `messages` | Array<Message> | Yes | - | Array of conversation messages |
|
| 38 |
+
| `temperature` | Float | No | 0.7 | Controls randomness (0.0 - 2.0) |
|
| 39 |
+
| `top_p` | Float | No | 0.95 | Nucleus sampling parameter (0.0 - 1.0) |
|
| 40 |
+
| `max_tokens` | Integer | No | 8192 | Maximum tokens in response |
|
| 41 |
+
| `stream` | Boolean | No | false | Enable streaming responses |
|
| 42 |
|
| 43 |
**Message Object**:
|
| 44 |
|
|
|
|
| 59 |
"content": "What is artificial intelligence?"
|
| 60 |
}
|
| 61 |
],
|
| 62 |
+
"temperature": 0.7,
|
| 63 |
"top_p": 0.95,
|
| 64 |
+
"max_tokens": 8192,
|
| 65 |
+
"stream": false
|
| 66 |
}
|
| 67 |
```
|
| 68 |
|
| 69 |
#### Response
|
| 70 |
|
| 71 |
+
**Standard Response** (200 OK):
|
| 72 |
|
| 73 |
```json
|
| 74 |
{
|
|
|
|
| 76 |
}
|
| 77 |
```
|
| 78 |
|
| 79 |
+
**Streaming Response** (200 OK):
|
| 80 |
+
|
| 81 |
+
When `stream: true` is set, the response is sent as Server-Sent Events:
|
| 82 |
+
|
| 83 |
+
```
|
| 84 |
+
data: {"content": "Artificial"}
|
| 85 |
+
data: {"content": " intelligence"}
|
| 86 |
+
data: {"content": " (AI)"}
|
| 87 |
+
data: {"content": " refers"}
|
| 88 |
+
data: {"content": " to"}
|
| 89 |
+
data: {"content": "..."}
|
| 90 |
+
data: [DONE]
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
Each line starts with `data: ` followed by a JSON object containing a `content` field with the next token. The stream ends with `data: [DONE]`.
|
| 94 |
+
|
| 95 |
**Response Fields**:
|
| 96 |
|
| 97 |
| Field | Type | Description |
|
|
|
|
| 129 |
|
| 130 |
#### Example Usage
|
| 131 |
|
| 132 |
+
**Standard Request (cURL)**:
|
| 133 |
```bash
|
| 134 |
curl -X POST https://Rox-Turbo-API.hf.space/chat \
|
| 135 |
-H "Content-Type: application/json" \
|
|
|
|
| 137 |
"messages": [
|
| 138 |
{"role": "user", "content": "Hello!"}
|
| 139 |
],
|
| 140 |
+
"temperature": 0.7,
|
| 141 |
+
"max_tokens": 8192
|
| 142 |
}'
|
| 143 |
```
|
| 144 |
|
| 145 |
+
**Streaming Request (cURL)**:
|
| 146 |
+
```bash
|
| 147 |
+
curl -X POST https://Rox-Turbo-API.hf.space/chat \
|
| 148 |
+
-H "Content-Type: application/json" \
|
| 149 |
+
-d '{
|
| 150 |
+
"messages": [
|
| 151 |
+
{"role": "user", "content": "Hello!"}
|
| 152 |
+
],
|
| 153 |
+
"stream": true
|
| 154 |
+
}'
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
**Standard Request (JavaScript)**:
|
| 158 |
```javascript
|
| 159 |
const response = await fetch('https://Rox-Turbo-API.hf.space/chat', {
|
| 160 |
method: 'POST',
|
| 161 |
headers: { 'Content-Type': 'application/json' },
|
| 162 |
body: JSON.stringify({
|
| 163 |
messages: [{ role: 'user', content: 'Hello!' }],
|
| 164 |
+
temperature: 0.7,
|
| 165 |
+
max_tokens: 8192
|
| 166 |
})
|
| 167 |
});
|
| 168 |
|
|
|
|
| 170 |
console.log(data.content);
|
| 171 |
```
|
| 172 |
|
| 173 |
+
**Streaming Request (JavaScript)**:
|
| 174 |
+
```javascript
|
| 175 |
+
const response = await fetch('https://Rox-Turbo-API.hf.space/chat', {
|
| 176 |
+
method: 'POST',
|
| 177 |
+
headers: { 'Content-Type': 'application/json' },
|
| 178 |
+
body: JSON.stringify({
|
| 179 |
+
messages: [{ role: 'user', content: 'Hello!' }],
|
| 180 |
+
stream: true
|
| 181 |
+
})
|
| 182 |
+
});
|
| 183 |
+
|
| 184 |
+
const reader = response.body.getReader();
|
| 185 |
+
const decoder = new TextDecoder();
|
| 186 |
+
|
| 187 |
+
while (true) {
|
| 188 |
+
const { done, value } = await reader.read();
|
| 189 |
+
if (done) break;
|
| 190 |
+
|
| 191 |
+
const chunk = decoder.decode(value, { stream: true });
|
| 192 |
+
const lines = chunk.split('\n');
|
| 193 |
+
|
| 194 |
+
for (const line of lines) {
|
| 195 |
+
if (line.startsWith('data: ')) {
|
| 196 |
+
const data = line.slice(6).trim();
|
| 197 |
+
if (data === '[DONE]') break;
|
| 198 |
+
|
| 199 |
+
try {
|
| 200 |
+
const parsed = JSON.parse(data);
|
| 201 |
+
if (parsed.content) {
|
| 202 |
+
process.stdout.write(parsed.content);
|
| 203 |
+
}
|
| 204 |
+
} catch (e) {}
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
**Standard Request (Python)**:
|
| 211 |
```python
|
| 212 |
import requests
|
| 213 |
|
| 214 |
response = requests.post('https://Rox-Turbo-API.hf.space/chat', json={
|
| 215 |
'messages': [{'role': 'user', 'content': 'Hello!'}],
|
| 216 |
+
'temperature': 0.7,
|
| 217 |
+
'max_tokens': 8192
|
| 218 |
})
|
| 219 |
|
| 220 |
print(response.json()['content'])
|
| 221 |
```
|
| 222 |
|
| 223 |
+
**Streaming Request (Python)**:
|
| 224 |
+
```python
|
| 225 |
+
import requests
|
| 226 |
+
import json
|
| 227 |
+
|
| 228 |
+
response = requests.post(
|
| 229 |
+
'https://Rox-Turbo-API.hf.space/chat',
|
| 230 |
+
json={
|
| 231 |
+
'messages': [{'role': 'user', 'content': 'Hello!'}],
|
| 232 |
+
'stream': True
|
| 233 |
+
},
|
| 234 |
+
stream=True
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
for line in response.iter_lines():
|
| 238 |
+
if line:
|
| 239 |
+
line = line.decode('utf-8')
|
| 240 |
+
if line.startswith('data: '):
|
| 241 |
+
data = line[6:]
|
| 242 |
+
if data == '[DONE]':
|
| 243 |
+
break
|
| 244 |
+
try:
|
| 245 |
+
parsed = json.loads(data)
|
| 246 |
+
if 'content' in parsed:
|
| 247 |
+
print(parsed['content'], end='', flush=True)
|
| 248 |
+
except json.JSONDecodeError:
|
| 249 |
+
pass
|
| 250 |
+
```
|
| 251 |
+
|
| 252 |
---
|
| 253 |
|
| 254 |
### POST /hf/generate
|
|
|
|
| 409 |
|
| 410 |
Maximum tokens in response.
|
| 411 |
|
| 412 |
+
- **Range**: 1 to 32768
|
| 413 |
+
- **Default**: 8192
|
| 414 |
|
| 415 |
Token estimation:
|
| 416 |
- ~1 token ≈ 4 characters
|
|
|
|
| 425 |
}
|
| 426 |
```
|
| 427 |
|
| 428 |
+
### stream
|
| 429 |
+
|
| 430 |
+
Enable streaming responses for real-time token delivery.
|
| 431 |
+
|
| 432 |
+
- **Type**: Boolean
|
| 433 |
+
- **Default**: false
|
| 434 |
+
- **Values**: true or false
|
| 435 |
+
|
| 436 |
+
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.
|
| 437 |
+
|
| 438 |
+
Benefits of streaming:
|
| 439 |
+
- Lower perceived latency
|
| 440 |
+
- Better user experience for long responses
|
| 441 |
+
- Ability to cancel generation early
|
| 442 |
+
- Real-time feedback
|
| 443 |
+
|
| 444 |
+
Example:
|
| 445 |
+
|
| 446 |
+
```json
|
| 447 |
+
{
|
| 448 |
+
"messages": [{"role": "user", "content": "Write a story"}],
|
| 449 |
+
"stream": true
|
| 450 |
+
}
|
| 451 |
+
```
|
| 452 |
+
|
| 453 |
+
Response format when streaming is enabled:
|
| 454 |
+
|
| 455 |
+
```
|
| 456 |
+
data: {"content": "Once"}
|
| 457 |
+
data: {"content": " upon"}
|
| 458 |
+
data: {"content": " a"}
|
| 459 |
+
data: {"content": " time"}
|
| 460 |
+
data: [DONE]
|
| 461 |
+
```
|
| 462 |
+
|
| 463 |
---
|
| 464 |
|
| 465 |
## Error Handling
|
|
|
|
| 555 |
headers: { 'Content-Type': 'application/json' },
|
| 556 |
body: JSON.stringify({
|
| 557 |
messages,
|
| 558 |
+
temperature: options.temperature || 0.7,
|
| 559 |
top_p: options.top_p || 0.95,
|
| 560 |
+
max_tokens: options.max_tokens || 8192,
|
| 561 |
+
stream: false
|
| 562 |
})
|
| 563 |
});
|
| 564 |
|
|
|
|
| 570 |
return data.content;
|
| 571 |
}
|
| 572 |
|
| 573 |
+
async chatStream(messages, onToken, options = {}) {
|
| 574 |
+
const response = await fetch(`${this.baseURL}/chat`, {
|
| 575 |
+
method: 'POST',
|
| 576 |
+
headers: { 'Content-Type': 'application/json' },
|
| 577 |
+
body: JSON.stringify({
|
| 578 |
+
messages,
|
| 579 |
+
temperature: options.temperature || 0.7,
|
| 580 |
+
top_p: options.top_p || 0.95,
|
| 581 |
+
max_tokens: options.max_tokens || 8192,
|
| 582 |
+
stream: true
|
| 583 |
+
})
|
| 584 |
+
});
|
| 585 |
+
|
| 586 |
+
if (!response.ok) {
|
| 587 |
+
throw new Error(`HTTP ${response.status}`);
|
| 588 |
+
}
|
| 589 |
+
|
| 590 |
+
const reader = response.body.getReader();
|
| 591 |
+
const decoder = new TextDecoder();
|
| 592 |
+
let fullContent = '';
|
| 593 |
+
|
| 594 |
+
while (true) {
|
| 595 |
+
const { done, value } = await reader.read();
|
| 596 |
+
if (done) break;
|
| 597 |
+
|
| 598 |
+
const chunk = decoder.decode(value, { stream: true });
|
| 599 |
+
const lines = chunk.split('\n');
|
| 600 |
+
|
| 601 |
+
for (const line of lines) {
|
| 602 |
+
if (line.startsWith('data: ')) {
|
| 603 |
+
const data = line.slice(6).trim();
|
| 604 |
+
if (data === '[DONE]') break;
|
| 605 |
+
|
| 606 |
+
try {
|
| 607 |
+
const parsed = JSON.parse(data);
|
| 608 |
+
if (parsed.content) {
|
| 609 |
+
fullContent += parsed.content;
|
| 610 |
+
onToken(parsed.content);
|
| 611 |
+
}
|
| 612 |
+
} catch (e) {}
|
| 613 |
+
}
|
| 614 |
+
}
|
| 615 |
+
}
|
| 616 |
+
|
| 617 |
+
return fullContent;
|
| 618 |
+
}
|
| 619 |
+
|
| 620 |
async generate(text, options = {}) {
|
| 621 |
const response = await fetch(`${this.baseURL}/hf/generate`, {
|
| 622 |
method: 'POST',
|
|
|
|
| 636 |
}
|
| 637 |
}
|
| 638 |
|
| 639 |
+
// Standard usage
|
| 640 |
const rox = new RoxAI();
|
| 641 |
const response = await rox.chat([
|
| 642 |
{ role: 'user', content: 'Hello!' }
|
| 643 |
]);
|
| 644 |
+
console.log(response);
|
| 645 |
+
|
| 646 |
+
// Streaming usage
|
| 647 |
+
await rox.chatStream(
|
| 648 |
+
[{ role: 'user', content: 'Tell me a story' }],
|
| 649 |
+
(token) => process.stdout.write(token)
|
| 650 |
+
);
|
| 651 |
```
|
| 652 |
|
| 653 |
---
|
docs/DEVELOPER_GUIDE.md
CHANGED
|
@@ -1,235 +1,720 @@
|
|
| 1 |
# Developer Guide
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
## Quick Start
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
```bash
|
| 12 |
curl -X POST https://Rox-Turbo-API.hf.space/chat \
|
| 13 |
-H "Content-Type: application/json" \
|
| 14 |
-
-d '{
|
| 15 |
-
"messages": [
|
| 16 |
-
{"role": "user", "content": "Hello"}
|
| 17 |
-
]
|
| 18 |
-
}'
|
| 19 |
```
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
```json
|
| 23 |
{
|
| 24 |
-
"
|
|
|
|
| 25 |
}
|
| 26 |
```
|
| 27 |
|
| 28 |
-
##
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
```python
|
| 33 |
import requests
|
|
|
|
| 34 |
|
| 35 |
-
def
|
| 36 |
response = requests.post(
|
| 37 |
f'https://Rox-Turbo-API.hf.space/{model}',
|
| 38 |
-
json={
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
```
|
| 45 |
|
| 46 |
-
### JavaScript
|
| 47 |
|
| 48 |
```javascript
|
| 49 |
-
async function
|
| 50 |
const response = await fetch(`https://Rox-Turbo-API.hf.space/${model}`, {
|
| 51 |
method: 'POST',
|
| 52 |
headers: { 'Content-Type': 'application/json' },
|
| 53 |
body: JSON.stringify({
|
| 54 |
-
messages: [{ role: 'user', content: message }]
|
|
|
|
| 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 |
```python
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
'
|
| 98 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
```
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
```python
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
```
|
| 117 |
|
| 118 |
-
###
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
```python
|
| 123 |
-
response = requests.post(
|
| 124 |
-
'
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
'max_tokens': 100
|
| 128 |
-
}
|
| 129 |
-
)
|
| 130 |
```
|
| 131 |
|
| 132 |
-
##
|
| 133 |
|
| 134 |
-
|
| 135 |
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
```python
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
-
base_url="https://Rox-Turbo-API.hf.space",
|
| 143 |
-
api_key="not-needed"
|
| 144 |
-
)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
model="chat",
|
| 148 |
-
messages=[{"role": "user", "content": "Hello"}]
|
| 149 |
-
)
|
| 150 |
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
```
|
| 153 |
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
```javascript
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
model: 'chat',
|
| 166 |
-
messages: [{ role: 'user', content: 'Hello' }]
|
| 167 |
-
});
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
```
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
```python
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
#
|
| 179 |
-
ask_rox('What is 2+2?', model='turbo')
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
#
|
| 188 |
-
ask_rox('Design a system', model='ultra')
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
|
|
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
```
|
| 199 |
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
|
| 203 |
|
| 204 |
```python
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
{'role': 'assistant', 'content': 'Nice to meet you, Alice!'},
|
| 208 |
-
{'role': 'user', 'content': 'What is my name?'}
|
| 209 |
-
]
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
json={'messages': conversation}
|
| 214 |
-
)
|
| 215 |
|
| 216 |
-
|
|
|
|
| 217 |
```
|
| 218 |
|
| 219 |
-
##
|
| 220 |
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
class RoxChatbot:
|
| 225 |
-
def __init__(self, model='chat'):
|
| 226 |
self.model = model
|
| 227 |
-
self.conversation = []
|
| 228 |
self.base_url = 'https://Rox-Turbo-API.hf.space'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
-
def chat(self, message):
|
| 231 |
self.conversation.append({'role': 'user', 'content': message})
|
| 232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
response = requests.post(
|
| 234 |
f'{self.base_url}/{self.model}',
|
| 235 |
json={'messages': self.conversation}
|
|
@@ -239,28 +724,69 @@ class RoxChatbot:
|
|
| 239 |
self.conversation.append({'role': 'assistant', 'content': reply})
|
| 240 |
return reply
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
def clear(self):
|
| 243 |
-
self.conversation
|
|
|
|
| 244 |
|
| 245 |
-
|
|
|
|
| 246 |
print(bot.chat('Hello'))
|
| 247 |
print(bot.chat('What is AI?'))
|
| 248 |
-
|
| 249 |
```
|
| 250 |
|
| 251 |
-
### JavaScript
|
| 252 |
|
| 253 |
```javascript
|
| 254 |
class RoxChatbot {
|
| 255 |
-
constructor(model = 'chat') {
|
| 256 |
this.model = model;
|
| 257 |
-
this.conversation = [];
|
| 258 |
this.baseUrl = 'https://Rox-Turbo-API.hf.space';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
}
|
| 260 |
|
| 261 |
-
async chat(message) {
|
| 262 |
this.conversation.push({ role: 'user', content: message });
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
const response = await fetch(`${this.baseUrl}/${this.model}`, {
|
| 265 |
method: 'POST',
|
| 266 |
headers: { 'Content-Type': 'application/json' },
|
|
@@ -274,108 +800,134 @@ class RoxChatbot {
|
|
| 274 |
return reply;
|
| 275 |
}
|
| 276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
clear() {
|
| 278 |
-
this.conversation =
|
|
|
|
| 279 |
}
|
| 280 |
}
|
| 281 |
|
| 282 |
-
|
|
|
|
| 283 |
console.log(await bot.chat('Hello'));
|
| 284 |
console.log(await bot.chat('What is AI?'));
|
|
|
|
| 285 |
```
|
| 286 |
|
| 287 |
-
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
response = requests.post(
|
| 293 |
-
f'https://Rox-Turbo-API.hf.space/{model}',
|
| 294 |
-
json={'messages': [{'role': 'user', 'content': message}]},
|
| 295 |
-
timeout=30
|
| 296 |
-
)
|
| 297 |
-
response.raise_for_status()
|
| 298 |
-
return response.json()['content']
|
| 299 |
-
except requests.exceptions.Timeout:
|
| 300 |
-
return "Request timed out"
|
| 301 |
-
except requests.exceptions.RequestException as e:
|
| 302 |
-
return f"Error: {str(e)}"
|
| 303 |
-
```
|
| 304 |
|
| 305 |
-
##
|
| 306 |
|
| 307 |
```python
|
| 308 |
-
import
|
| 309 |
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
self.requests = []
|
| 315 |
-
|
| 316 |
-
def can_request(self):
|
| 317 |
-
now = time.time()
|
| 318 |
-
self.requests = [r for r in self.requests if now - r < self.time_window]
|
| 319 |
-
return len(self.requests) < self.max_requests
|
| 320 |
-
|
| 321 |
-
def record_request(self):
|
| 322 |
-
self.requests.append(time.time())
|
| 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 |
-
# With parameters
|
| 358 |
-
requests.post('https://Rox-Turbo-API.hf.space/chat',
|
| 359 |
-
json={'messages': [...], 'temperature': 0.7, 'max_tokens': 500})
|
| 360 |
-
|
| 361 |
-
# With system prompt
|
| 362 |
-
requests.post('https://Rox-Turbo-API.hf.space/chat',
|
| 363 |
-
json={'messages': [
|
| 364 |
-
{'role': 'system', 'content': 'You are helpful'},
|
| 365 |
-
{'role': 'user', 'content': 'Hello'}
|
| 366 |
-
]})
|
| 367 |
-
```
|
| 368 |
-
|
| 369 |
-
## Endpoints
|
| 370 |
-
|
| 371 |
-
- `/chat` - Rox Core
|
| 372 |
-
- `/turbo` - Rox 2.1 Turbo
|
| 373 |
-
- `/coder` - Rox 3.5 Coder
|
| 374 |
-
- `/turbo45` - Rox 4.5 Turbo
|
| 375 |
-
- `/ultra` - Rox 5 Ultra
|
| 376 |
-
- `/dyno` - Rox 6 Dyno
|
| 377 |
-
- `/coder7` - Rox 7 Coder
|
| 378 |
-
- `/vision` - Rox Vision Max
|
| 379 |
|
| 380 |
---
|
| 381 |
|
|
|
|
| 1 |
# Developer Guide
|
| 2 |
|
| 3 |
+
Complete guide for integrating Rox AI into your applications.
|
| 4 |
|
| 5 |
+
**Base URL**: `https://Rox-Turbo-API.hf.space`
|
| 6 |
|
| 7 |
+
## Table of Contents
|
| 8 |
+
|
| 9 |
+
1. [Quick Start](#quick-start)
|
| 10 |
+
2. [Authentication](#authentication)
|
| 11 |
+
3. [Making Requests](#making-requests)
|
| 12 |
+
4. [Streaming Responses](#streaming-responses)
|
| 13 |
+
5. [Model Selection](#model-selection)
|
| 14 |
+
6. [Parameters](#parameters)
|
| 15 |
+
7. [Conversation Management](#conversation-management)
|
| 16 |
+
8. [Error Handling](#error-handling)
|
| 17 |
+
9. [Best Practices](#best-practices)
|
| 18 |
+
10. [Code Examples](#code-examples)
|
| 19 |
+
11. [OpenAI SDK Compatibility](#openai-sdk-compatibility)
|
| 20 |
+
|
| 21 |
+
---
|
| 22 |
|
| 23 |
## Quick Start
|
| 24 |
|
| 25 |
+
Send your first request in under 30 seconds.
|
| 26 |
+
|
| 27 |
+
### cURL
|
| 28 |
+
|
| 29 |
```bash
|
| 30 |
curl -X POST https://Rox-Turbo-API.hf.space/chat \
|
| 31 |
-H "Content-Type: application/json" \
|
| 32 |
+
-d '{"messages":[{"role":"user","content":"Hello"}]}'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
```
|
| 34 |
|
| 35 |
+
### Python
|
| 36 |
+
|
| 37 |
+
```python
|
| 38 |
+
import requests
|
| 39 |
+
|
| 40 |
+
response = requests.post(
|
| 41 |
+
'https://Rox-Turbo-API.hf.space/chat',
|
| 42 |
+
json={'messages': [{'role': 'user', 'content': 'Hello'}]}
|
| 43 |
+
)
|
| 44 |
+
print(response.json()['content'])
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
### JavaScript
|
| 48 |
+
|
| 49 |
+
```javascript
|
| 50 |
+
const response = await fetch('https://Rox-Turbo-API.hf.space/chat', {
|
| 51 |
+
method: 'POST',
|
| 52 |
+
headers: { 'Content-Type': 'application/json' },
|
| 53 |
+
body: JSON.stringify({
|
| 54 |
+
messages: [{ role: 'user', content: 'Hello' }]
|
| 55 |
+
})
|
| 56 |
+
});
|
| 57 |
+
const data = await response.json();
|
| 58 |
+
console.log(data.content);
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
---
|
| 62 |
+
|
| 63 |
+
## Authentication
|
| 64 |
+
|
| 65 |
+
No API key required. All endpoints are publicly accessible.
|
| 66 |
+
|
| 67 |
+
---
|
| 68 |
+
|
| 69 |
+
## Making Requests
|
| 70 |
+
|
| 71 |
+
### Request Format
|
| 72 |
+
|
| 73 |
+
All endpoints accept POST requests with JSON body.
|
| 74 |
+
|
| 75 |
+
**Required Fields:**
|
| 76 |
+
- `messages`: Array of message objects
|
| 77 |
+
|
| 78 |
+
**Optional Fields:**
|
| 79 |
+
- `temperature`: Float (0.0 - 2.0, default: 0.7)
|
| 80 |
+
- `top_p`: Float (0.0 - 1.0, default: 0.95)
|
| 81 |
+
- `max_tokens`: Integer (1 - 32768, default: 8192)
|
| 82 |
+
- `stream`: Boolean (default: false)
|
| 83 |
+
|
| 84 |
+
### Message Object
|
| 85 |
+
|
| 86 |
```json
|
| 87 |
{
|
| 88 |
+
"role": "user" | "assistant" | "system",
|
| 89 |
+
"content": "message text"
|
| 90 |
}
|
| 91 |
```
|
| 92 |
|
| 93 |
+
### Complete Request Example
|
| 94 |
|
| 95 |
+
```json
|
| 96 |
+
{
|
| 97 |
+
"messages": [
|
| 98 |
+
{"role": "system", "content": "You are a helpful assistant"},
|
| 99 |
+
{"role": "user", "content": "What is AI?"}
|
| 100 |
+
],
|
| 101 |
+
"temperature": 0.7,
|
| 102 |
+
"top_p": 0.95,
|
| 103 |
+
"max_tokens": 8192,
|
| 104 |
+
"stream": false
|
| 105 |
+
}
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
### Response Format
|
| 109 |
+
|
| 110 |
+
**Standard Response:**
|
| 111 |
+
```json
|
| 112 |
+
{
|
| 113 |
+
"content": "AI stands for Artificial Intelligence..."
|
| 114 |
+
}
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
**Streaming Response:**
|
| 118 |
+
```
|
| 119 |
+
data: {"content": "AI"}
|
| 120 |
+
data: {"content": " stands"}
|
| 121 |
+
data: {"content": " for"}
|
| 122 |
+
data: [DONE]
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
---
|
| 126 |
+
|
| 127 |
+
## Streaming Responses
|
| 128 |
+
|
| 129 |
+
Streaming provides real-time token-by-token responses for better user experience.
|
| 130 |
+
|
| 131 |
+
### When to Use Streaming
|
| 132 |
+
|
| 133 |
+
- Long-form content generation
|
| 134 |
+
- Interactive chat applications
|
| 135 |
+
- Real-time feedback requirements
|
| 136 |
+
- Improved perceived performance
|
| 137 |
+
|
| 138 |
+
### Python Implementation
|
| 139 |
|
| 140 |
```python
|
| 141 |
import requests
|
| 142 |
+
import json
|
| 143 |
|
| 144 |
+
def stream_chat(message, model='chat'):
|
| 145 |
response = requests.post(
|
| 146 |
f'https://Rox-Turbo-API.hf.space/{model}',
|
| 147 |
+
json={
|
| 148 |
+
'messages': [{'role': 'user', 'content': message}],
|
| 149 |
+
'stream': True
|
| 150 |
+
},
|
| 151 |
+
stream=True
|
| 152 |
)
|
| 153 |
+
|
| 154 |
+
for line in response.iter_lines():
|
| 155 |
+
if line:
|
| 156 |
+
line = line.decode('utf-8')
|
| 157 |
+
if line.startswith('data: '):
|
| 158 |
+
data = line[6:]
|
| 159 |
+
if data == '[DONE]':
|
| 160 |
+
break
|
| 161 |
+
try:
|
| 162 |
+
parsed = json.loads(data)
|
| 163 |
+
if 'content' in parsed:
|
| 164 |
+
print(parsed['content'], end='', flush=True)
|
| 165 |
+
yield parsed['content']
|
| 166 |
+
except json.JSONDecodeError:
|
| 167 |
+
pass
|
| 168 |
+
|
| 169 |
+
# Usage
|
| 170 |
+
for token in stream_chat('Tell me a story'):
|
| 171 |
+
pass # Tokens printed in real-time
|
| 172 |
```
|
| 173 |
|
| 174 |
+
### JavaScript Implementation
|
| 175 |
|
| 176 |
```javascript
|
| 177 |
+
async function streamChat(message, model = 'chat') {
|
| 178 |
const response = await fetch(`https://Rox-Turbo-API.hf.space/${model}`, {
|
| 179 |
method: 'POST',
|
| 180 |
headers: { 'Content-Type': 'application/json' },
|
| 181 |
body: JSON.stringify({
|
| 182 |
+
messages: [{ role: 'user', content: message }],
|
| 183 |
+
stream: true
|
| 184 |
})
|
| 185 |
});
|
| 186 |
+
|
| 187 |
+
const reader = response.body.getReader();
|
| 188 |
+
const decoder = new TextDecoder();
|
| 189 |
+
let fullContent = '';
|
| 190 |
+
|
| 191 |
+
while (true) {
|
| 192 |
+
const { done, value } = await reader.read();
|
| 193 |
+
if (done) break;
|
| 194 |
+
|
| 195 |
+
const chunk = decoder.decode(value, { stream: true });
|
| 196 |
+
const lines = chunk.split('\n');
|
| 197 |
+
|
| 198 |
+
for (const line of lines) {
|
| 199 |
+
if (line.startsWith('data: ')) {
|
| 200 |
+
const data = line.slice(6).trim();
|
| 201 |
+
if (data === '[DONE]') break;
|
| 202 |
+
|
| 203 |
+
try {
|
| 204 |
+
const parsed = JSON.parse(data);
|
| 205 |
+
if (parsed.content) {
|
| 206 |
+
fullContent += parsed.content;
|
| 207 |
+
console.log(parsed.content); // Process each token
|
| 208 |
+
}
|
| 209 |
+
} catch (e) {}
|
| 210 |
+
}
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
return fullContent;
|
| 215 |
}
|
| 216 |
|
| 217 |
+
// Usage
|
| 218 |
+
await streamChat('Tell me a story');
|
| 219 |
```
|
| 220 |
|
| 221 |
+
### Node.js Implementation
|
| 222 |
|
| 223 |
+
```javascript
|
| 224 |
+
const https = require('https');
|
| 225 |
|
| 226 |
+
function streamChat(message, model = 'chat') {
|
| 227 |
+
const data = JSON.stringify({
|
| 228 |
+
messages: [{ role: 'user', content: message }],
|
| 229 |
+
stream: true
|
| 230 |
+
});
|
| 231 |
+
|
| 232 |
+
const options = {
|
| 233 |
+
hostname: 'Rox-Turbo-API.hf.space',
|
| 234 |
+
path: `/${model}`,
|
| 235 |
+
method: 'POST',
|
| 236 |
+
headers: {
|
| 237 |
+
'Content-Type': 'application/json',
|
| 238 |
+
'Content-Length': data.length
|
| 239 |
+
}
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
const req = https.request(options, (res) => {
|
| 243 |
+
res.on('data', (chunk) => {
|
| 244 |
+
const lines = chunk.toString().split('\n');
|
| 245 |
+
for (const line of lines) {
|
| 246 |
+
if (line.startsWith('data: ')) {
|
| 247 |
+
const data = line.slice(6).trim();
|
| 248 |
+
if (data === '[DONE]') return;
|
| 249 |
+
|
| 250 |
+
try {
|
| 251 |
+
const parsed = JSON.parse(data);
|
| 252 |
+
if (parsed.content) {
|
| 253 |
+
process.stdout.write(parsed.content);
|
| 254 |
+
}
|
| 255 |
+
} catch (e) {}
|
| 256 |
}
|
| 257 |
+
}
|
| 258 |
+
});
|
| 259 |
+
});
|
| 260 |
|
| 261 |
+
req.write(data);
|
| 262 |
+
req.end();
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
// Usage
|
| 266 |
+
streamChat('Tell me a story');
|
| 267 |
```
|
| 268 |
|
| 269 |
+
---
|
| 270 |
+
|
| 271 |
+
## Model Selection
|
| 272 |
|
| 273 |
+
Choose the right model for your use case.
|
| 274 |
|
| 275 |
+
### Available Models
|
| 276 |
+
|
| 277 |
+
| Model | Endpoint | Best For | Speed | Quality |
|
| 278 |
+
|-------|----------|----------|-------|---------|
|
| 279 |
+
| Rox Core | `/chat` | General conversation | Medium | High |
|
| 280 |
+
| Rox 2.1 Turbo | `/turbo` | Quick responses | Fast | Good |
|
| 281 |
+
| Rox 3.5 Coder | `/coder` | Code generation | Medium | High |
|
| 282 |
+
| Rox 4.5 Turbo | `/turbo45` | Fast reasoning | Fast | High |
|
| 283 |
+
| Rox 5 Ultra | `/ultra` | Complex tasks | Slow | Highest |
|
| 284 |
+
| Rox 6 Dyno | `/dyno` | Long context | Medium | High |
|
| 285 |
+
| Rox 7 Coder | `/coder7` | Advanced coding | Medium | Highest |
|
| 286 |
+
| Rox Vision Max | `/vision` | Visual tasks | Medium | High |
|
| 287 |
+
|
| 288 |
+
### Model Selection Guide
|
| 289 |
|
| 290 |
```python
|
| 291 |
+
def select_model(task_type):
|
| 292 |
+
models = {
|
| 293 |
+
'chat': 'chat', # General conversation
|
| 294 |
+
'quick': 'turbo', # Fast responses
|
| 295 |
+
'code': 'coder', # Code generation
|
| 296 |
+
'reasoning': 'turbo45', # Complex reasoning
|
| 297 |
+
'complex': 'ultra', # Highest quality
|
| 298 |
+
'long': 'dyno', # Long documents
|
| 299 |
+
'advanced_code': 'coder7',# Advanced coding
|
| 300 |
+
'vision': 'vision' # Visual tasks
|
| 301 |
}
|
| 302 |
+
return models.get(task_type, 'chat')
|
| 303 |
+
|
| 304 |
+
# Usage
|
| 305 |
+
model = select_model('code')
|
| 306 |
+
response = ask_rox('Write a function', model=model)
|
| 307 |
```
|
| 308 |
|
| 309 |
+
---
|
| 310 |
+
|
| 311 |
+
## Parameters
|
| 312 |
+
|
| 313 |
+
### temperature
|
| 314 |
+
|
| 315 |
+
Controls randomness in responses.
|
| 316 |
|
| 317 |
+
**Range**: 0.0 to 2.0
|
| 318 |
+
**Default**: 0.7
|
| 319 |
+
|
| 320 |
+
- **0.0 - 0.3**: Deterministic, focused (math, facts, code)
|
| 321 |
+
- **0.4 - 0.8**: Balanced (general conversation)
|
| 322 |
+
- **0.9 - 2.0**: Creative, varied (stories, brainstorming)
|
| 323 |
|
| 324 |
```python
|
| 325 |
+
# Factual response
|
| 326 |
+
response = requests.post(url, json={
|
| 327 |
+
'messages': [{'role': 'user', 'content': 'What is 2+2?'}],
|
| 328 |
+
'temperature': 0.2
|
| 329 |
+
})
|
| 330 |
+
|
| 331 |
+
# Creative response
|
| 332 |
+
response = requests.post(url, json={
|
| 333 |
+
'messages': [{'role': 'user', 'content': 'Write a poem'}],
|
| 334 |
+
'temperature': 1.5
|
| 335 |
+
})
|
| 336 |
```
|
| 337 |
|
| 338 |
+
### top_p
|
| 339 |
+
|
| 340 |
+
Controls diversity via nucleus sampling.
|
| 341 |
|
| 342 |
+
**Range**: 0.0 to 1.0
|
| 343 |
+
**Default**: 0.95
|
| 344 |
+
|
| 345 |
+
- **0.1 - 0.5**: Narrow, focused
|
| 346 |
+
- **0.6 - 0.9**: Balanced
|
| 347 |
+
- **0.9 - 1.0**: Diverse
|
| 348 |
|
| 349 |
```python
|
| 350 |
+
response = requests.post(url, json={
|
| 351 |
+
'messages': [{'role': 'user', 'content': 'Tell me about AI'}],
|
| 352 |
+
'top_p': 0.9
|
| 353 |
+
})
|
|
|
|
|
|
|
|
|
|
| 354 |
```
|
| 355 |
|
| 356 |
+
### max_tokens
|
| 357 |
|
| 358 |
+
Maximum tokens in response.
|
| 359 |
|
| 360 |
+
**Range**: 1 to 32768
|
| 361 |
+
**Default**: 8192
|
| 362 |
+
|
| 363 |
+
Token estimation: ~1 token = 0.75 words
|
| 364 |
|
| 365 |
```python
|
| 366 |
+
# Short response
|
| 367 |
+
response = requests.post(url, json={
|
| 368 |
+
'messages': [{'role': 'user', 'content': 'Brief summary'}],
|
| 369 |
+
'max_tokens': 100
|
| 370 |
+
})
|
| 371 |
+
|
| 372 |
+
# Long response
|
| 373 |
+
response = requests.post(url, json={
|
| 374 |
+
'messages': [{'role': 'user', 'content': 'Detailed explanation'}],
|
| 375 |
+
'max_tokens': 4096
|
| 376 |
+
})
|
| 377 |
+
```
|
| 378 |
|
| 379 |
+
### stream
|
|
|
|
|
|
|
|
|
|
| 380 |
|
| 381 |
+
Enable streaming responses.
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
+
**Type**: Boolean
|
| 384 |
+
**Default**: false
|
| 385 |
+
|
| 386 |
+
```python
|
| 387 |
+
response = requests.post(url, json={
|
| 388 |
+
'messages': [{'role': 'user', 'content': 'Hello'}],
|
| 389 |
+
'stream': True
|
| 390 |
+
}, stream=True)
|
| 391 |
```
|
| 392 |
|
| 393 |
+
---
|
| 394 |
+
|
| 395 |
+
## Conversation Management
|
| 396 |
+
|
| 397 |
+
### Single Turn
|
| 398 |
+
|
| 399 |
+
```python
|
| 400 |
+
def ask_once(question):
|
| 401 |
+
response = requests.post(
|
| 402 |
+
'https://Rox-Turbo-API.hf.space/chat',
|
| 403 |
+
json={'messages': [{'role': 'user', 'content': question}]}
|
| 404 |
+
)
|
| 405 |
+
return response.json()['content']
|
| 406 |
+
```
|
| 407 |
+
|
| 408 |
+
### Multi-Turn Conversation
|
| 409 |
+
|
| 410 |
+
```python
|
| 411 |
+
class Conversation:
|
| 412 |
+
def __init__(self, model='chat', system_prompt=None):
|
| 413 |
+
self.model = model
|
| 414 |
+
self.messages = []
|
| 415 |
+
if system_prompt:
|
| 416 |
+
self.messages.append({'role': 'system', 'content': system_prompt})
|
| 417 |
+
|
| 418 |
+
def ask(self, message):
|
| 419 |
+
self.messages.append({'role': 'user', 'content': message})
|
| 420 |
+
|
| 421 |
+
response = requests.post(
|
| 422 |
+
f'https://Rox-Turbo-API.hf.space/{self.model}',
|
| 423 |
+
json={'messages': self.messages}
|
| 424 |
+
)
|
| 425 |
+
|
| 426 |
+
reply = response.json()['content']
|
| 427 |
+
self.messages.append({'role': 'assistant', 'content': reply})
|
| 428 |
+
return reply
|
| 429 |
+
|
| 430 |
+
def clear(self):
|
| 431 |
+
system_msg = [m for m in self.messages if m['role'] == 'system']
|
| 432 |
+
self.messages = system_msg
|
| 433 |
+
|
| 434 |
+
# Usage
|
| 435 |
+
conv = Conversation(system_prompt='You are a helpful assistant')
|
| 436 |
+
print(conv.ask('Hello'))
|
| 437 |
+
print(conv.ask('What is AI?'))
|
| 438 |
+
print(conv.ask('Tell me more'))
|
| 439 |
+
```
|
| 440 |
+
|
| 441 |
+
### JavaScript Conversation Manager
|
| 442 |
|
| 443 |
```javascript
|
| 444 |
+
class Conversation {
|
| 445 |
+
constructor(model = 'chat', systemPrompt = null) {
|
| 446 |
+
this.model = model;
|
| 447 |
+
this.messages = [];
|
| 448 |
+
if (systemPrompt) {
|
| 449 |
+
this.messages.push({ role: 'system', content: systemPrompt });
|
| 450 |
+
}
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
async ask(message) {
|
| 454 |
+
this.messages.push({ role: 'user', content: message });
|
| 455 |
+
|
| 456 |
+
const response = await fetch(`https://Rox-Turbo-API.hf.space/${this.model}`, {
|
| 457 |
+
method: 'POST',
|
| 458 |
+
headers: { 'Content-Type': 'application/json' },
|
| 459 |
+
body: JSON.stringify({ messages: this.messages })
|
| 460 |
+
});
|
| 461 |
+
|
| 462 |
+
const data = await response.json();
|
| 463 |
+
const reply = data.content;
|
| 464 |
+
|
| 465 |
+
this.messages.push({ role: 'assistant', content: reply });
|
| 466 |
+
return reply;
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
clear() {
|
| 470 |
+
const systemMsg = this.messages.filter(m => m.role === 'system');
|
| 471 |
+
this.messages = systemMsg;
|
| 472 |
+
}
|
| 473 |
+
}
|
| 474 |
|
| 475 |
+
// Usage
|
| 476 |
+
const conv = new Conversation('chat', 'You are a helpful assistant');
|
| 477 |
+
console.log(await conv.ask('Hello'));
|
| 478 |
+
console.log(await conv.ask('What is AI?'));
|
| 479 |
+
```
|
| 480 |
|
| 481 |
+
### System Prompts
|
|
|
|
|
|
|
|
|
|
| 482 |
|
| 483 |
+
System prompts define the assistant's behavior.
|
| 484 |
+
|
| 485 |
+
```python
|
| 486 |
+
def ask_with_personality(message, personality):
|
| 487 |
+
system_prompts = {
|
| 488 |
+
'professional': 'You are a professional business consultant.',
|
| 489 |
+
'casual': 'You are a friendly, casual assistant.',
|
| 490 |
+
'technical': 'You are a technical expert. Be precise and detailed.',
|
| 491 |
+
'creative': 'You are a creative writer. Be imaginative and expressive.'
|
| 492 |
+
}
|
| 493 |
+
|
| 494 |
+
messages = [
|
| 495 |
+
{'role': 'system', 'content': system_prompts.get(personality, '')},
|
| 496 |
+
{'role': 'user', 'content': message}
|
| 497 |
+
]
|
| 498 |
+
|
| 499 |
+
response = requests.post(
|
| 500 |
+
'https://Rox-Turbo-API.hf.space/chat',
|
| 501 |
+
json={'messages': messages}
|
| 502 |
+
)
|
| 503 |
+
return response.json()['content']
|
| 504 |
+
|
| 505 |
+
# Usage
|
| 506 |
+
answer = ask_with_personality('Explain AI', 'technical')
|
| 507 |
```
|
| 508 |
|
| 509 |
+
---
|
| 510 |
+
|
| 511 |
+
## Error Handling
|
| 512 |
+
|
| 513 |
+
### Basic Error Handling
|
| 514 |
|
| 515 |
```python
|
| 516 |
+
def safe_request(message, model='chat'):
|
| 517 |
+
try:
|
| 518 |
+
response = requests.post(
|
| 519 |
+
f'https://Rox-Turbo-API.hf.space/{model}',
|
| 520 |
+
json={'messages': [{'role': 'user', 'content': message}]},
|
| 521 |
+
timeout=30
|
| 522 |
+
)
|
| 523 |
+
response.raise_for_status()
|
| 524 |
+
return response.json()['content']
|
| 525 |
+
except requests.exceptions.Timeout:
|
| 526 |
+
return "Request timed out. Please try again."
|
| 527 |
+
except requests.exceptions.HTTPError as e:
|
| 528 |
+
return f"HTTP error: {e.response.status_code}"
|
| 529 |
+
except requests.exceptions.RequestException as e:
|
| 530 |
+
return f"Request failed: {str(e)}"
|
| 531 |
+
except KeyError:
|
| 532 |
+
return "Invalid response format"
|
| 533 |
+
```
|
| 534 |
|
| 535 |
+
### Advanced Error Handling with Retry
|
|
|
|
| 536 |
|
| 537 |
+
```python
|
| 538 |
+
import time
|
| 539 |
|
| 540 |
+
def request_with_retry(message, model='chat', max_retries=3):
|
| 541 |
+
for attempt in range(max_retries):
|
| 542 |
+
try:
|
| 543 |
+
response = requests.post(
|
| 544 |
+
f'https://Rox-Turbo-API.hf.space/{model}',
|
| 545 |
+
json={'messages': [{'role': 'user', 'content': message}]},
|
| 546 |
+
timeout=30
|
| 547 |
+
)
|
| 548 |
+
response.raise_for_status()
|
| 549 |
+
return response.json()['content']
|
| 550 |
+
except requests.exceptions.RequestException as e:
|
| 551 |
+
if attempt == max_retries - 1:
|
| 552 |
+
raise
|
| 553 |
+
wait_time = 2 ** attempt # Exponential backoff
|
| 554 |
+
time.sleep(wait_time)
|
| 555 |
+
```
|
| 556 |
|
| 557 |
+
### JavaScript Error Handling
|
|
|
|
| 558 |
|
| 559 |
+
```javascript
|
| 560 |
+
async function safeRequest(message, model = 'chat') {
|
| 561 |
+
try {
|
| 562 |
+
const response = await fetch(`https://Rox-Turbo-API.hf.space/${model}`, {
|
| 563 |
+
method: 'POST',
|
| 564 |
+
headers: { 'Content-Type': 'application/json' },
|
| 565 |
+
body: JSON.stringify({
|
| 566 |
+
messages: [{ role: 'user', content: message }]
|
| 567 |
+
})
|
| 568 |
+
});
|
| 569 |
|
| 570 |
+
if (!response.ok) {
|
| 571 |
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
| 572 |
+
}
|
| 573 |
|
| 574 |
+
const data = await response.json();
|
| 575 |
+
return data.content;
|
| 576 |
+
} catch (error) {
|
| 577 |
+
console.error('Request failed:', error);
|
| 578 |
+
throw error;
|
| 579 |
+
}
|
| 580 |
+
}
|
| 581 |
```
|
| 582 |
|
| 583 |
+
---
|
| 584 |
+
|
| 585 |
+
## Best Practices
|
| 586 |
+
|
| 587 |
+
### 1. Use Appropriate Models
|
| 588 |
+
|
| 589 |
+
Choose models based on your needs:
|
| 590 |
+
- Use `turbo` for simple, fast responses
|
| 591 |
+
- Use `coder` for code-related tasks
|
| 592 |
+
- Use `ultra` for complex reasoning
|
| 593 |
+
- Use `dyno` for long documents
|
| 594 |
|
| 595 |
+
### 2. Optimize Parameters
|
| 596 |
|
| 597 |
```python
|
| 598 |
+
# For factual questions
|
| 599 |
+
params = {'temperature': 0.2, 'max_tokens': 500}
|
|
|
|
|
|
|
|
|
|
| 600 |
|
| 601 |
+
# For creative tasks
|
| 602 |
+
params = {'temperature': 1.2, 'max_tokens': 2000}
|
|
|
|
|
|
|
| 603 |
|
| 604 |
+
# For code generation
|
| 605 |
+
params = {'temperature': 0.3, 'max_tokens': 4096}
|
| 606 |
```
|
| 607 |
|
| 608 |
+
### 3. Manage Context Length
|
| 609 |
|
| 610 |
+
```python
|
| 611 |
+
def trim_conversation(messages, max_messages=10):
|
| 612 |
+
"""Keep only recent messages to manage context"""
|
| 613 |
+
system_msgs = [m for m in messages if m['role'] == 'system']
|
| 614 |
+
other_msgs = [m for m in messages if m['role'] != 'system']
|
| 615 |
+
return system_msgs + other_msgs[-max_messages:]
|
| 616 |
+
```
|
| 617 |
+
|
| 618 |
+
### 4. Implement Caching
|
| 619 |
|
| 620 |
```python
|
| 621 |
+
from functools import lru_cache
|
| 622 |
+
import hashlib
|
| 623 |
+
|
| 624 |
+
@lru_cache(maxsize=100)
|
| 625 |
+
def cached_request(message_hash, model):
|
| 626 |
+
# Actual request implementation
|
| 627 |
+
pass
|
| 628 |
+
|
| 629 |
+
def ask_with_cache(message, model='chat'):
|
| 630 |
+
message_hash = hashlib.md5(message.encode()).hexdigest()
|
| 631 |
+
return cached_request(message_hash, model)
|
| 632 |
+
```
|
| 633 |
+
|
| 634 |
+
### 5. Rate Limiting
|
| 635 |
+
|
| 636 |
+
```python
|
| 637 |
+
import time
|
| 638 |
+
from collections import deque
|
| 639 |
+
|
| 640 |
+
class RateLimiter:
|
| 641 |
+
def __init__(self, max_requests=10, time_window=60):
|
| 642 |
+
self.max_requests = max_requests
|
| 643 |
+
self.time_window = time_window
|
| 644 |
+
self.requests = deque()
|
| 645 |
+
|
| 646 |
+
def wait_if_needed(self):
|
| 647 |
+
now = time.time()
|
| 648 |
+
|
| 649 |
+
# Remove old requests
|
| 650 |
+
while self.requests and now - self.requests[0] > self.time_window:
|
| 651 |
+
self.requests.popleft()
|
| 652 |
+
|
| 653 |
+
# Wait if at limit
|
| 654 |
+
if len(self.requests) >= self.max_requests:
|
| 655 |
+
sleep_time = self.time_window - (now - self.requests[0])
|
| 656 |
+
if sleep_time > 0:
|
| 657 |
+
time.sleep(sleep_time)
|
| 658 |
+
|
| 659 |
+
self.requests.append(now)
|
| 660 |
+
|
| 661 |
+
limiter = RateLimiter(10, 60)
|
| 662 |
+
|
| 663 |
+
def rate_limited_request(message):
|
| 664 |
+
limiter.wait_if_needed()
|
| 665 |
+
return ask_rox(message)
|
| 666 |
+
```
|
| 667 |
+
|
| 668 |
+
### 6. Streaming for Long Responses
|
| 669 |
+
|
| 670 |
+
Use streaming for responses over 500 tokens to improve user experience.
|
| 671 |
+
|
| 672 |
+
### 7. Error Recovery
|
| 673 |
+
|
| 674 |
+
```python
|
| 675 |
+
def robust_request(message, model='chat'):
|
| 676 |
+
fallback_models = ['chat', 'turbo', 'coder']
|
| 677 |
+
|
| 678 |
+
for fallback_model in fallback_models:
|
| 679 |
+
try:
|
| 680 |
+
return request_with_retry(message, fallback_model)
|
| 681 |
+
except Exception as e:
|
| 682 |
+
if fallback_model == fallback_models[-1]:
|
| 683 |
+
raise
|
| 684 |
+
continue
|
| 685 |
+
```
|
| 686 |
+
|
| 687 |
+
---
|
| 688 |
+
|
| 689 |
+
## Code Examples
|
| 690 |
+
|
| 691 |
+
### Complete Chatbot (Python)
|
| 692 |
+
|
| 693 |
+
```python
|
| 694 |
+
import requests
|
| 695 |
+
import json
|
| 696 |
+
|
| 697 |
class RoxChatbot:
|
| 698 |
+
def __init__(self, model='chat', system_prompt=None):
|
| 699 |
self.model = model
|
|
|
|
| 700 |
self.base_url = 'https://Rox-Turbo-API.hf.space'
|
| 701 |
+
self.conversation = []
|
| 702 |
+
|
| 703 |
+
if system_prompt:
|
| 704 |
+
self.conversation.append({
|
| 705 |
+
'role': 'system',
|
| 706 |
+
'content': system_prompt
|
| 707 |
+
})
|
| 708 |
|
| 709 |
+
def chat(self, message, stream=False):
|
| 710 |
self.conversation.append({'role': 'user', 'content': message})
|
| 711 |
|
| 712 |
+
if stream:
|
| 713 |
+
return self._stream_chat()
|
| 714 |
+
else:
|
| 715 |
+
return self._standard_chat()
|
| 716 |
+
|
| 717 |
+
def _standard_chat(self):
|
| 718 |
response = requests.post(
|
| 719 |
f'{self.base_url}/{self.model}',
|
| 720 |
json={'messages': self.conversation}
|
|
|
|
| 724 |
self.conversation.append({'role': 'assistant', 'content': reply})
|
| 725 |
return reply
|
| 726 |
|
| 727 |
+
def _stream_chat(self):
|
| 728 |
+
response = requests.post(
|
| 729 |
+
f'{self.base_url}/{self.model}',
|
| 730 |
+
json={'messages': self.conversation, 'stream': True},
|
| 731 |
+
stream=True
|
| 732 |
+
)
|
| 733 |
+
|
| 734 |
+
full_content = ''
|
| 735 |
+
for line in response.iter_lines():
|
| 736 |
+
if line:
|
| 737 |
+
line = line.decode('utf-8')
|
| 738 |
+
if line.startswith('data: '):
|
| 739 |
+
data = line[6:]
|
| 740 |
+
if data == '[DONE]':
|
| 741 |
+
break
|
| 742 |
+
try:
|
| 743 |
+
parsed = json.loads(data)
|
| 744 |
+
if 'content' in parsed:
|
| 745 |
+
full_content += parsed['content']
|
| 746 |
+
print(parsed['content'], end='', flush=True)
|
| 747 |
+
except json.JSONDecodeError:
|
| 748 |
+
pass
|
| 749 |
+
|
| 750 |
+
print() # New line after streaming
|
| 751 |
+
self.conversation.append({'role': 'assistant', 'content': full_content})
|
| 752 |
+
return full_content
|
| 753 |
+
|
| 754 |
def clear(self):
|
| 755 |
+
system_msgs = [m for m in self.conversation if m['role'] == 'system']
|
| 756 |
+
self.conversation = system_msgs
|
| 757 |
|
| 758 |
+
# Usage
|
| 759 |
+
bot = RoxChatbot(system_prompt='You are a helpful assistant')
|
| 760 |
print(bot.chat('Hello'))
|
| 761 |
print(bot.chat('What is AI?'))
|
| 762 |
+
bot.chat('Tell me a story', stream=True)
|
| 763 |
```
|
| 764 |
|
| 765 |
+
### Complete Chatbot (JavaScript)
|
| 766 |
|
| 767 |
```javascript
|
| 768 |
class RoxChatbot {
|
| 769 |
+
constructor(model = 'chat', systemPrompt = null) {
|
| 770 |
this.model = model;
|
|
|
|
| 771 |
this.baseUrl = 'https://Rox-Turbo-API.hf.space';
|
| 772 |
+
this.conversation = [];
|
| 773 |
+
|
| 774 |
+
if (systemPrompt) {
|
| 775 |
+
this.conversation.push({ role: 'system', content: systemPrompt });
|
| 776 |
+
}
|
| 777 |
}
|
| 778 |
|
| 779 |
+
async chat(message, stream = false) {
|
| 780 |
this.conversation.push({ role: 'user', content: message });
|
| 781 |
|
| 782 |
+
if (stream) {
|
| 783 |
+
return await this._streamChat();
|
| 784 |
+
} else {
|
| 785 |
+
return await this._standardChat();
|
| 786 |
+
}
|
| 787 |
+
}
|
| 788 |
+
|
| 789 |
+
async _standardChat() {
|
| 790 |
const response = await fetch(`${this.baseUrl}/${this.model}`, {
|
| 791 |
method: 'POST',
|
| 792 |
headers: { 'Content-Type': 'application/json' },
|
|
|
|
| 800 |
return reply;
|
| 801 |
}
|
| 802 |
|
| 803 |
+
async _streamChat() {
|
| 804 |
+
const response = await fetch(`${this.baseUrl}/${this.model}`, {
|
| 805 |
+
method: 'POST',
|
| 806 |
+
headers: { 'Content-Type': 'application/json' },
|
| 807 |
+
body: JSON.stringify({
|
| 808 |
+
messages: this.conversation,
|
| 809 |
+
stream: true
|
| 810 |
+
})
|
| 811 |
+
});
|
| 812 |
+
|
| 813 |
+
const reader = response.body.getReader();
|
| 814 |
+
const decoder = new TextDecoder();
|
| 815 |
+
let fullContent = '';
|
| 816 |
+
|
| 817 |
+
while (true) {
|
| 818 |
+
const { done, value } = await reader.read();
|
| 819 |
+
if (done) break;
|
| 820 |
+
|
| 821 |
+
const chunk = decoder.decode(value, { stream: true });
|
| 822 |
+
const lines = chunk.split('\n');
|
| 823 |
+
|
| 824 |
+
for (const line of lines) {
|
| 825 |
+
if (line.startsWith('data: ')) {
|
| 826 |
+
const data = line.slice(6).trim();
|
| 827 |
+
if (data === '[DONE]') break;
|
| 828 |
+
|
| 829 |
+
try {
|
| 830 |
+
const parsed = JSON.parse(data);
|
| 831 |
+
if (parsed.content) {
|
| 832 |
+
fullContent += parsed.content;
|
| 833 |
+
process.stdout.write(parsed.content);
|
| 834 |
+
}
|
| 835 |
+
} catch (e) {}
|
| 836 |
+
}
|
| 837 |
+
}
|
| 838 |
+
}
|
| 839 |
+
|
| 840 |
+
console.log();
|
| 841 |
+
this.conversation.push({ role: 'assistant', content: fullContent });
|
| 842 |
+
return fullContent;
|
| 843 |
+
}
|
| 844 |
+
|
| 845 |
clear() {
|
| 846 |
+
const systemMsgs = this.conversation.filter(m => m.role === 'system');
|
| 847 |
+
this.conversation = systemMsgs;
|
| 848 |
}
|
| 849 |
}
|
| 850 |
|
| 851 |
+
// Usage
|
| 852 |
+
const bot = new RoxChatbot('chat', 'You are a helpful assistant');
|
| 853 |
console.log(await bot.chat('Hello'));
|
| 854 |
console.log(await bot.chat('What is AI?'));
|
| 855 |
+
await bot.chat('Tell me a story', true);
|
| 856 |
```
|
| 857 |
|
| 858 |
+
---
|
| 859 |
|
| 860 |
+
## OpenAI SDK Compatibility
|
| 861 |
+
|
| 862 |
+
Rox AI is compatible with the OpenAI SDK.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 863 |
|
| 864 |
+
### Python with OpenAI SDK
|
| 865 |
|
| 866 |
```python
|
| 867 |
+
from openai import OpenAI
|
| 868 |
|
| 869 |
+
client = OpenAI(
|
| 870 |
+
base_url="https://Rox-Turbo-API.hf.space",
|
| 871 |
+
api_key="not-needed" # No API key required
|
| 872 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 873 |
|
| 874 |
+
# Standard request
|
| 875 |
+
response = client.chat.completions.create(
|
| 876 |
+
model="chat",
|
| 877 |
+
messages=[{"role": "user", "content": "Hello"}]
|
| 878 |
+
)
|
| 879 |
+
print(response.choices[0].message.content)
|
| 880 |
|
| 881 |
+
# Streaming request
|
| 882 |
+
stream = client.chat.completions.create(
|
| 883 |
+
model="chat",
|
| 884 |
+
messages=[{"role": "user", "content": "Tell me a story"}],
|
| 885 |
+
stream=True
|
| 886 |
+
)
|
| 887 |
+
|
| 888 |
+
for chunk in stream:
|
| 889 |
+
if chunk.choices[0].delta.content:
|
| 890 |
+
print(chunk.choices[0].delta.content, end='', flush=True)
|
| 891 |
```
|
| 892 |
|
| 893 |
+
### JavaScript with OpenAI SDK
|
| 894 |
|
| 895 |
+
```javascript
|
| 896 |
+
import OpenAI from 'openai';
|
| 897 |
|
| 898 |
+
const client = new OpenAI({
|
| 899 |
+
baseURL: 'https://Rox-Turbo-API.hf.space',
|
| 900 |
+
apiKey: 'not-needed'
|
| 901 |
+
});
|
| 902 |
+
|
| 903 |
+
// Standard request
|
| 904 |
+
const response = await client.chat.completions.create({
|
| 905 |
+
model: 'chat',
|
| 906 |
+
messages: [{ role: 'user', content: 'Hello' }]
|
| 907 |
+
});
|
| 908 |
+
console.log(response.choices[0].message.content);
|
| 909 |
+
|
| 910 |
+
// Streaming request
|
| 911 |
+
const stream = await client.chat.completions.create({
|
| 912 |
+
model: 'chat',
|
| 913 |
+
messages: [{ role: 'user', content: 'Tell me a story' }],
|
| 914 |
+
stream: true
|
| 915 |
+
});
|
| 916 |
|
| 917 |
+
for await (const chunk of stream) {
|
| 918 |
+
if (chunk.choices[0]?.delta?.content) {
|
| 919 |
+
process.stdout.write(chunk.choices[0].delta.content);
|
| 920 |
+
}
|
| 921 |
+
}
|
| 922 |
```
|
| 923 |
|
| 924 |
+
---
|
| 925 |
|
| 926 |
+
## Additional Resources
|
| 927 |
+
|
| 928 |
+
- [API Reference](API_REFERENCE.md) - Complete API documentation
|
| 929 |
+
- [Code Examples](CODE.md) - Ready-to-use code snippets
|
| 930 |
+
- [Model Guide](MODELS.md) - Detailed model information
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 931 |
|
| 932 |
---
|
| 933 |
|