@ibti-tech/chatbot 0.5.6 → 0.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +179 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -276,12 +276,186 @@ npm install react react-dom lodash nookies
|
|
|
276
276
|
|
|
277
277
|
## 📡 Backend API
|
|
278
278
|
|
|
279
|
-
The chatbot expects the backend API to be configured and accessible at the provided URL. The API must implement the
|
|
279
|
+
The chatbot expects the backend API to be configured and accessible at the provided URL. The API must implement the following endpoints:
|
|
280
280
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
281
|
+
### Required Endpoints
|
|
282
|
+
|
|
283
|
+
#### 1. `GET /chat/welcome?locale={locale}`
|
|
284
|
+
|
|
285
|
+
**Purpose**: Retrieves the welcome message and checks API health/availability.
|
|
286
|
+
|
|
287
|
+
**Query Parameters**:
|
|
288
|
+
- `locale` (required): The locale code (`'pt-BR'` or `'en'`)
|
|
289
|
+
|
|
290
|
+
**Response**:
|
|
291
|
+
```json
|
|
292
|
+
{
|
|
293
|
+
"welcomeMessage": "Hello! How can I help you today?"
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
**Status Codes**:
|
|
298
|
+
- `200 OK`: API is available and welcome message is returned
|
|
299
|
+
- `4xx/5xx`: API is considered unavailable
|
|
300
|
+
|
|
301
|
+
**Notes**:
|
|
302
|
+
- This endpoint is used for health checks (via `HEAD` or `GET` requests)
|
|
303
|
+
- The chatbot uses this to determine connection status
|
|
304
|
+
- Should return a welcome message appropriate for the specified locale
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
308
|
+
#### 2. `POST /chat/completion?locale={locale}`
|
|
309
|
+
|
|
310
|
+
**Purpose**: Sends a chat message and receives a streaming response from the AI assistant.
|
|
311
|
+
|
|
312
|
+
**Query Parameters**:
|
|
313
|
+
- `locale` (required): The locale code (`'pt-BR'` or `'en'`)
|
|
314
|
+
|
|
315
|
+
**Request Body**:
|
|
316
|
+
```json
|
|
317
|
+
{
|
|
318
|
+
"context": [
|
|
319
|
+
{
|
|
320
|
+
"role": "user",
|
|
321
|
+
"content": "What is artificial intelligence?"
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
"role": "assistant",
|
|
325
|
+
"content": "Artificial intelligence (AI) is..."
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
"role": "user",
|
|
329
|
+
"content": "Tell me more about machine learning"
|
|
330
|
+
}
|
|
331
|
+
]
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
**Request Body Schema**:
|
|
336
|
+
- `context` (required, array): Array of chat messages representing the conversation history
|
|
337
|
+
- Each item must have:
|
|
338
|
+
- `role` (required, string): Either `"user"` or `"assistant"`
|
|
339
|
+
- `content` (required, string): The message content
|
|
340
|
+
- The array must not be empty
|
|
341
|
+
- The last message should typically be from the `"user"` role
|
|
342
|
+
|
|
343
|
+
**Response**:
|
|
344
|
+
- **Content-Type**: `text/event-stream`
|
|
345
|
+
- **Format**: Server-Sent Events (SSE) stream
|
|
346
|
+
- **Body**: Plain text chunks that are concatenated to form the complete response
|
|
347
|
+
|
|
348
|
+
**Response Headers**:
|
|
349
|
+
```
|
|
350
|
+
Content-Type: text/event-stream
|
|
351
|
+
Cache-Control: no-cache
|
|
352
|
+
Connection: keep-alive
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
**Status Codes**:
|
|
356
|
+
- `200 OK`: Stream started successfully
|
|
357
|
+
- `429 Too Many Requests`: Rate limit exceeded (should send error message via stream before closing)
|
|
358
|
+
- `4xx/5xx`: Error occurred
|
|
359
|
+
|
|
360
|
+
**Streaming Behavior**:
|
|
361
|
+
- The response is sent as a continuous stream of text chunks
|
|
362
|
+
- Each chunk is immediately displayed to the user as it arrives
|
|
363
|
+
- The stream ends when the complete response is sent
|
|
364
|
+
- The chatbot accumulates all chunks to form the final message
|
|
365
|
+
|
|
366
|
+
**Error Handling**:
|
|
367
|
+
- For `429` errors, the API should send a friendly error message via the stream before closing:
|
|
368
|
+
```json
|
|
369
|
+
{"error": "Sorry, too many requests were made. Please try again in a few moments. 😊"}
|
|
370
|
+
```
|
|
371
|
+
- For other errors, the stream should be closed and an appropriate HTTP status code returned
|
|
372
|
+
|
|
373
|
+
**Example Flow**:
|
|
374
|
+
1. Frontend sends POST request with conversation context
|
|
375
|
+
2. Backend starts streaming response chunks
|
|
376
|
+
3. Frontend receives chunks and displays them incrementally
|
|
377
|
+
4. Stream completes when response is finished
|
|
378
|
+
5. Frontend saves the complete message to conversation history
|
|
379
|
+
|
|
380
|
+
---
|
|
381
|
+
|
|
382
|
+
#### 3. `POST /chat/feedback`
|
|
383
|
+
|
|
384
|
+
**Purpose**: Submits user feedback about a chatbot response (rating and optional message).
|
|
385
|
+
|
|
386
|
+
**Request Body**:
|
|
387
|
+
```json
|
|
388
|
+
{
|
|
389
|
+
"rating_score": 5,
|
|
390
|
+
"chat_context": [
|
|
391
|
+
{
|
|
392
|
+
"role": "user",
|
|
393
|
+
"content": "What is AI?"
|
|
394
|
+
},
|
|
395
|
+
{
|
|
396
|
+
"role": "assistant",
|
|
397
|
+
"content": "Artificial intelligence is..."
|
|
398
|
+
}
|
|
399
|
+
],
|
|
400
|
+
"locale": "pt-BR",
|
|
401
|
+
"message": "Very helpful response!"
|
|
402
|
+
}
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
**Request Body Schema**:
|
|
406
|
+
- `rating_score` (required, number): Rating from 1 to 5 (inclusive)
|
|
407
|
+
- Valid values: `1`, `2`, `3`, `4`, `5`
|
|
408
|
+
- `chat_context` (required, array): The conversation context that was rated
|
|
409
|
+
- Same structure as in `/chat/completion` endpoint
|
|
410
|
+
- Must not be empty
|
|
411
|
+
- `locale` (required, string): The locale code (`'pt-BR'` or `'en'`)
|
|
412
|
+
- `message` (optional, string): Optional feedback message from the user
|
|
413
|
+
|
|
414
|
+
**Response**:
|
|
415
|
+
- **Status Code**: `200 OK` or `201 Created` (implementation dependent)
|
|
416
|
+
- **Body**: Typically empty or a success confirmation
|
|
417
|
+
|
|
418
|
+
**Status Codes**:
|
|
419
|
+
- `200 OK` / `201 Created`: Feedback submitted successfully
|
|
420
|
+
- `400 Bad Request`: Invalid request body (invalid rating, missing fields, etc.)
|
|
421
|
+
- `4xx/5xx`: Other errors
|
|
422
|
+
|
|
423
|
+
**Notes**:
|
|
424
|
+
- This endpoint is called when a user rates a chatbot response
|
|
425
|
+
- The feedback is typically stored for analytics and improvement purposes
|
|
426
|
+
- The endpoint should handle the request asynchronously if possible to avoid blocking the UI
|
|
427
|
+
|
|
428
|
+
---
|
|
429
|
+
|
|
430
|
+
### API Requirements Summary
|
|
431
|
+
|
|
432
|
+
| Endpoint | Method | Purpose | Streaming | Required |
|
|
433
|
+
|----------|--------|---------|-----------|----------|
|
|
434
|
+
| `/chat/welcome` | `GET` | Health check & welcome message | ❌ | ✅ |
|
|
435
|
+
| `/chat/completion` | `POST` | Send message & receive response | ✅ | ✅ |
|
|
436
|
+
| `/chat/feedback` | `POST` | Submit user feedback | ❌ | ✅ |
|
|
437
|
+
|
|
438
|
+
### Implementation Notes
|
|
439
|
+
|
|
440
|
+
1. **CORS**: The API must allow cross-origin requests from the frontend domain
|
|
441
|
+
2. **Locale Support**: All endpoints should respect the `locale` parameter and return appropriate content
|
|
442
|
+
3. **Error Handling**: Provide clear error messages in the appropriate locale
|
|
443
|
+
4. **Streaming**: The `/chat/completion` endpoint must support Server-Sent Events (SSE) for real-time response streaming
|
|
444
|
+
5. **Rate Limiting**: Consider implementing rate limiting and return `429` status when exceeded
|
|
445
|
+
6. **Content-Type**:
|
|
446
|
+
- Use `application/json` for JSON endpoints
|
|
447
|
+
- Use `text/event-stream` for streaming endpoints
|
|
448
|
+
|
|
449
|
+
### Example API Base URL
|
|
450
|
+
|
|
451
|
+
```jsx
|
|
452
|
+
<ChatbotProvider
|
|
453
|
+
apiURL="https://api.example.com" // Base URL without trailing slash
|
|
454
|
+
// ...
|
|
455
|
+
/>
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
The chatbot will automatically append the endpoint paths (e.g., `/chat/welcome`, `/chat/completion`, `/chat/feedback`) to this base URL.
|
|
285
459
|
|
|
286
460
|
## 🐛 Issues and Support
|
|
287
461
|
|