minhvtt commited on
Commit
911cc77
·
verified ·
1 Parent(s): d6a12b4

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +203 -19
main.py CHANGED
@@ -109,34 +109,218 @@ class AddDocumentResponse(BaseModel):
109
 
110
  @app.get("/")
111
  async def root():
112
- """Health check endpoint"""
113
  return {
114
  "status": "running",
115
- "service": "Event Social Media Embeddings & ChatbotRAG API",
116
- "embedding_model": "Jina CLIP v2",
117
  "vector_db": "Qdrant",
118
- "language_support": "Vietnamese + 88 other languages",
119
  "endpoints": {
120
- "embeddings": {
121
- "POST /index": "Index data với text/image",
122
- "POST /search": "Hybrid search",
123
- "POST /search/text": "Text search",
124
- "POST /search/image": "Image search",
125
- "DELETE /delete/{doc_id}": "Delete document",
126
- "GET /document/{doc_id}": "Get document",
127
- "GET /stats": "Collection statistics"
128
- },
129
  "chatbot_rag": {
130
- "POST /chat": "Chat với RAG",
131
- "POST /documents": "Add document to knowledge base",
132
- "POST /rag/search": "Search in knowledge base",
133
- "GET /history": "Get chat history",
134
- "DELETE /documents/{doc_id}": "Delete document from knowledge base"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  }
137
  }
138
 
139
-
140
  @app.post("/index", response_model=IndexResponse)
141
  async def index_data(
142
  id: str = Form(...),
 
109
 
110
  @app.get("/")
111
  async def root():
112
+ """Health check endpoint with comprehensive API documentation"""
113
  return {
114
  "status": "running",
115
+ "service": "ChatbotRAG API",
116
+ "version": "2.0.0",
117
  "vector_db": "Qdrant",
118
+ "document_db": "MongoDB",
119
  "endpoints": {
 
 
 
 
 
 
 
 
 
120
  "chatbot_rag": {
121
+ "POST /chat": {
122
+ "description": "Chat với AI sử dụng RAG (Retrieval-Augmented Generation)",
123
+ "request": {
124
+ "method": "POST",
125
+ "content_type": "application/json",
126
+ "body": {
127
+ "message": "string (required) - User message/question",
128
+ "use_rag": "boolean (optional, default: true) - Enable RAG context retrieval",
129
+ "top_k": "integer (optional, default: 3) - Number of context documents to retrieve",
130
+ "system_message": "string (optional) - Custom system prompt",
131
+ "max_tokens": "integer (optional, default: 512) - Max response length",
132
+ "temperature": "float (optional, default: 0.7, range: 0-1) - Creativity level",
133
+ "top_p": "float (optional, default: 0.95) - Nucleus sampling",
134
+ "hf_token": "string (optional) - Hugging Face token (fallback to env)"
135
+ }
136
+ },
137
+ "response": {
138
+ "response": "string - AI generated response",
139
+ "context_used": [
140
+ {
141
+ "id": "string - Document ID",
142
+ "confidence": "float - Relevance score",
143
+ "metadata": {
144
+ "text": "string - Retrieved context"
145
+ }
146
+ }
147
+ ],
148
+ "timestamp": "string - ISO 8601 timestamp"
149
+ },
150
+ "example_request": {
151
+ "message": "Dao có nguy hiểm không?",
152
+ "use_rag": True,
153
+ "top_k": 3,
154
+ "temperature": 0.7
155
+ },
156
+ "example_response": {
157
+ "response": "Dựa trên thông tin trong database, dao được phân loại là vũ khí nguy hiểm. Dao sắc có thể gây thương tích nghiêm trọng nếu không sử dụng đúng cách. Cần tuân thủ các quy định an toàn khi sử dụng.",
158
+ "context_used": [
159
+ {
160
+ "id": "68a3fc14c853d7621e8977b5",
161
+ "confidence": 0.92,
162
+ "metadata": {
163
+ "text": "Vũ khí"
164
+ }
165
+ },
166
+ {
167
+ "id": "68a3fc4cc853d7621e8977b6",
168
+ "confidence": 0.85,
169
+ "metadata": {
170
+ "text": "Con dao sắc"
171
+ }
172
+ }
173
+ ],
174
+ "timestamp": "2025-10-13T10:30:45.123456"
175
+ },
176
+ "notes": [
177
+ "RAG retrieves relevant context from vector DB before generating response",
178
+ "LLM uses context to provide accurate, grounded answers",
179
+ "Requires HUGGINGFACE_TOKEN environment variable or hf_token in request"
180
+ ]
181
+ },
182
+ "POST /documents": {
183
+ "description": "Add document to knowledge base for RAG",
184
+ "request": {
185
+ "method": "POST",
186
+ "content_type": "application/json",
187
+ "body": {
188
+ "text": "string (required) - Document text content",
189
+ "metadata": "object (optional) - Additional metadata (source, category, etc.)"
190
+ }
191
+ },
192
+ "response": {
193
+ "success": "boolean",
194
+ "doc_id": "string - MongoDB ObjectId",
195
+ "message": "string - Status message"
196
+ },
197
+ "example_request": {
198
+ "text": "Để tạo event mới: Click nút 'Tạo Event' ở góc trên bên phải màn hình. Điền thông tin sự kiện bao gồm tên, ngày giờ, địa điểm. Click Lưu để hoàn tất.",
199
+ "metadata": {
200
+ "source": "user_guide.pdf",
201
+ "section": "create_event",
202
+ "page": 5,
203
+ "category": "tutorial"
204
+ }
205
+ },
206
+ "example_response": {
207
+ "success": True,
208
+ "doc_id": "67a9876543210fedcba98765",
209
+ "message": "Document added successfully with ID: 67a9876543210fedcba98765"
210
+ }
211
+ },
212
+ "POST /rag/search": {
213
+ "description": "Search in knowledge base (similar to /search/text but for RAG documents)",
214
+ "request": {
215
+ "method": "POST",
216
+ "content_type": "multipart/form-data",
217
+ "body": {
218
+ "query": "string (required) - Search query",
219
+ "top_k": "integer (optional, default: 5) - Number of results",
220
+ "score_threshold": "float (optional, default: 0.5) - Minimum relevance score"
221
+ }
222
+ },
223
+ "response": [
224
+ {
225
+ "id": "string",
226
+ "confidence": "float",
227
+ "metadata": {
228
+ "text": "string",
229
+ "source": "string"
230
+ }
231
+ }
232
+ ],
233
+ "example_request": {
234
+ "query": "cách tạo sự kiện mới",
235
+ "top_k": 3,
236
+ "score_threshold": 0.6
237
+ }
238
+ },
239
+ "GET /history": {
240
+ "description": "Get chat conversation history",
241
+ "request": {
242
+ "method": "GET",
243
+ "query_params": {
244
+ "limit": "integer (optional, default: 10) - Number of messages",
245
+ "skip": "integer (optional, default: 0) - Pagination offset"
246
+ }
247
+ },
248
+ "response": {
249
+ "history": [
250
+ {
251
+ "user_message": "string",
252
+ "assistant_response": "string",
253
+ "context_used": "array",
254
+ "timestamp": "string - ISO 8601"
255
+ }
256
+ ],
257
+ "total": "integer - Total messages count"
258
+ },
259
+ "example_request": "GET /history?limit=5&skip=0",
260
+ "example_response": {
261
+ "history": [
262
+ {
263
+ "user_message": "Dao có nguy hiểm không?",
264
+ "assistant_response": "Dao được phân loại là vũ khí...",
265
+ "context_used": [],
266
+ "timestamp": "2025-10-13T10:30:45.123456"
267
+ }
268
+ ],
269
+ "total": 15
270
+ }
271
+ },
272
+ "DELETE /documents/{doc_id}": {
273
+ "description": "Delete document from knowledge base",
274
+ "request": {
275
+ "method": "DELETE",
276
+ "path_params": {
277
+ "doc_id": "string - MongoDB ObjectId"
278
+ }
279
+ },
280
+ "response": {
281
+ "success": "boolean",
282
+ "message": "string"
283
+ }
284
+ }
285
  }
286
+ },
287
+ "usage_examples": {
288
+ "curl_chat": "curl -X POST 'http://localhost:8000/chat' -H 'Content-Type: application/json' -d '{\"message\": \"Dao có nguy hiểm không?\", \"use_rag\": true}'",
289
+ "python_chat": """
290
+ import requests
291
+
292
+ response = requests.post(
293
+ 'http://localhost:8000/chat',
294
+ json={
295
+ 'message': 'Nút tạo event ở đâu?',
296
+ 'use_rag': True,
297
+ 'top_k': 3
298
+ }
299
+ )
300
+ print(response.json()['response'])
301
+ """
302
+ },
303
+ "authentication": {
304
+ "embeddings_apis": "No authentication required",
305
+ "chat_api": "Requires HUGGINGFACE_TOKEN (env variable or request body)"
306
+ },
307
+ "rate_limits": {
308
+ "embeddings": "No limit",
309
+ "chat_with_llm": "Limited by Hugging Face API (free tier: ~1000 requests/hour)"
310
+ },
311
+ "error_codes": {
312
+ "400": "Bad Request - Missing required fields or invalid input",
313
+ "401": "Unauthorized - Invalid Hugging Face token",
314
+ "404": "Not Found - Document ID not found",
315
+ "500": "Internal Server Error - Server or database error"
316
+ },
317
+ "links": {
318
+ "docs": "http://localhost:8000/docs",
319
+ "redoc": "http://localhost:8000/redoc",
320
+ "openapi": "http://localhost:8000/openapi.json"
321
  }
322
  }
323
 
 
324
  @app.post("/index", response_model=IndexResponse)
325
  async def index_data(
326
  id: str = Form(...),