Spaces:
Running
Running
File size: 3,057 Bytes
91fe002 |
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 |
# 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):
@app.post("/chat", response_model=ChatResponse)
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
)
@app.post("/chat/clear-session")
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"
)
@app.get("/chat/session/{session_id}")
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", {})
}
|