Spaces:
Running
Running
| # Integration code để thêm vào main.py | |
| # ADD THIS IMPORT near line 20: | |
| from chat_endpoint import chat_endpoint | |
| # ADD THESE ROUTES before "if __name__ == '__main__':" (around line 1000): | |
| async def chat(request: ChatRequest): | |
| """ | |
| Multi-turn conversational chatbot với RAG + Function Calling | |
| Features: | |
| - Server-side session management | |
| - Conversation history tracking | |
| - RAG context retrieval | |
| - Function calling (API integration) | |
| Example: | |
| ``` | |
| # First message - creates session | |
| POST /chat | |
| { | |
| "message": "Tìm sự kiện hòa nhạc", | |
| "use_rag": true | |
| } | |
| Response: { "session_id": "abc-123", ... } | |
| # Follow-up message - uses session | |
| POST /chat | |
| { | |
| "message": "Ngày tổ chức chính xác là khi nào?", | |
| "session_id": "abc-123" | |
| } | |
| # Bot understands context và calls API nếu cần | |
| ``` | |
| """ | |
| return await chat_endpoint( | |
| request=request, | |
| conversation_service=conversation_service, | |
| tools_service=tools_service, | |
| advanced_rag=advanced_rag, | |
| embedding_service=embedding_service, | |
| qdrant_service=qdrant_service, | |
| chat_history_collection=chat_history_collection, | |
| hf_token=hf_token | |
| ) | |
| async def clear_chat_session(session_id: str): | |
| """ | |
| Clear conversation history cho một session | |
| Args: | |
| session_id: Session identifier to clear | |
| Returns: | |
| Success message | |
| Example: | |
| ``` | |
| POST /chat/clear-session?session_id=abc-123 | |
| ``` | |
| """ | |
| success = conversation_service.clear_session(session_id) | |
| if success: | |
| return { | |
| "success": True, | |
| "message": f"Session {session_id} cleared successfully" | |
| } | |
| else: | |
| raise HTTPException( | |
| status_code=404, | |
| detail=f"Session {session_id} not found or already cleared" | |
| ) | |
| async def get_session_info(session_id: str): | |
| """ | |
| Get thông tin về một conversation session | |
| Args: | |
| session_id: Session identifier | |
| Returns: | |
| Session metadata và message count | |
| Example: | |
| ``` | |
| GET /chat/session/abc-123 | |
| ``` | |
| """ | |
| session = conversation_service.get_session_info(session_id) | |
| if not session: | |
| raise HTTPException( | |
| status_code=404, | |
| detail=f"Session {session_id} not found" | |
| ) | |
| # Get message count | |
| history = conversation_service.get_conversation_history( | |
| session_id, | |
| include_metadata=True | |
| ) | |
| return { | |
| "session_id": session["session_id"], | |
| "created_at": session["created_at"], | |
| "updated_at": session["updated_at"], | |
| "message_count": len(history), | |
| "metadata": session.get("metadata", {}) | |
| } | |