File size: 3,907 Bytes
519b145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Chatbot API Endpoints

## Overview

This document describes the chatbot API endpoints available in the system.

## Base URL

- Default: `http://localhost:8000`
- Override via env when running test scripts:
  ```bash
  export API_BASE_URL=http://localhost:8090  # e.g. when runserver uses port 8090
  ```

## Endpoints

### 1. Health Check

**Endpoint**: `GET /api/chatbot/health/`

**Description**: Check the health status of the chatbot service.

**Response**:
```json
{
  "status": "healthy",
  "service": "chatbot",
  "classifier_loaded": true
}
```

**Example**:
```bash
curl http://localhost:8000/api/chatbot/health/
```

### 2. Chat

**Endpoint**: `POST /api/chat/`

**Description**: Send a message to the chatbot and get a response.

**Request Body**:
```json
{
  "message": "Làm thủ tục cư trú cần gì?"
}
```

**Response**:
```json
{
  "message": "Tôi tìm thấy 5 thủ tục liên quan đến 'Làm thủ tục cư trú cần gì?':\n\n1. Đăng ký thường trú\n   ...",
  "intent": "search_procedure",
  "confidence": 0.95,
  "results": [
    {
      "type": "procedure",
      "data": {
        "id": 1,
        "title": "Đăng ký thường trú",
        "domain": "Cư trú",
        ...
      }
    }
  ],
  "count": 5
}
```

**Example**:
```bash
curl -X POST http://localhost:8000/api/chat/ \
  -H "Content-Type: application/json" \
  -d '{"message": "Làm thủ tục cư trú cần gì?"}'
```

## Intent Types

The chatbot can classify queries into the following intents:

- `search_fine`: Search for traffic fines
- `search_procedure`: Search for administrative procedures
- `search_office`: Search for office/unit information
- `search_advisory`: Search for security advisories
- `general_query`: General queries
- `greeting`: Greetings

## Response Fields

- `message`: The response message to display to the user
- `intent`: The classified intent
- `confidence`: Confidence score (0.0 to 1.0)
- `results`: Array of search results
- `count`: Number of results found

## Error Handling

### 400 Bad Request

```json
{
  "error": "message is required"
}
```

### 500 Internal Server Error

```json
{
  "message": "Xin lỗi, có lỗi xảy ra. Vui lòng thử lại.",
  "intent": "error",
  "error": "Error details",
  "results": [],
  "count": 0
}
```

## Testing

Use the provided test script:

```bash
cd backend
API_BASE_URL=http://localhost:8090 \\
POSTGRES_HOST=localhost POSTGRES_PORT=5433 \\
python scripts/test_api_endpoint.py
```

The script automatically:
- Hits `GET /api/chatbot/health/` to confirm classifier loading.
- Sends six representative queries and reports status, intent, confidence, latency, and first result title.

## API Endpoint Testing & Fixes — 2025-11-14

- Added trailing slashes to `backend/hue_portal/chatbot/urls.py` and `backend/hue_portal/core/urls.py` so `/api/chatbot/health/` and `/api/chat/` resolve correctly.
- Hardened chatbot serialization via `_serialize_document` to avoid `TypeError: Object of type type is not JSON serializable`.
- Latest test run:
  - Command: `API_BASE_URL=http://localhost:8090 POSTGRES_HOST=localhost POSTGRES_PORT=5433 python scripts/test_api_endpoint.py`
  - Result: **6/6** successful queries, **100 % intent accuracy**, avg latency **~3.7 s** (first call includes SentenceTransformer warm-up).
- Checklist before running tests:
  1. `POSTGRES_HOST=localhost POSTGRES_PORT=5433 ../../.venv/bin/python manage.py runserver 0.0.0.0:8090`
  2. Ensure `API_BASE_URL` matches runserver port.
  3. (Optional) export `DJANGO_DEBUG=1` for verbose stack traces during local debugging.

## Notes

- The API uses RAG (Retrieval-Augmented Generation) pipeline for generating responses
- Hybrid search (BM25 + Vector similarity) is used for retrieval
- Intent classification uses ML model with keyword-based fallback
- Response latency typically ranges from 200-1000ms depending on query complexity