Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -736,6 +736,114 @@ async def chat(request: ChatRequest):
|
|
| 736 |
)
|
| 737 |
|
| 738 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
@app.post("/documents", response_model=AddDocumentResponse)
|
| 740 |
async def add_document(request: AddDocumentRequest):
|
| 741 |
"""
|
|
|
|
| 736 |
)
|
| 737 |
|
| 738 |
|
| 739 |
+
@app.get("/chat/history/{session_id}")
|
| 740 |
+
async def get_conversation_history(session_id: str, include_metadata: bool = False):
|
| 741 |
+
"""
|
| 742 |
+
Get conversation history for a session
|
| 743 |
+
|
| 744 |
+
Args:
|
| 745 |
+
session_id: Session identifier
|
| 746 |
+
include_metadata: Include metadata (rag_stats, tool_calls) in response
|
| 747 |
+
|
| 748 |
+
Returns:
|
| 749 |
+
List of messages with role and content
|
| 750 |
+
|
| 751 |
+
Example:
|
| 752 |
+
```
|
| 753 |
+
GET /chat/history/abc-123?include_metadata=true
|
| 754 |
+
```
|
| 755 |
+
"""
|
| 756 |
+
if not conversation_service.session_exists(session_id):
|
| 757 |
+
raise HTTPException(
|
| 758 |
+
status_code=404,
|
| 759 |
+
detail=f"Session {session_id} not found or has expired"
|
| 760 |
+
)
|
| 761 |
+
|
| 762 |
+
history = conversation_service.get_conversation_history(
|
| 763 |
+
session_id,
|
| 764 |
+
include_metadata=include_metadata
|
| 765 |
+
)
|
| 766 |
+
|
| 767 |
+
session_info = conversation_service.get_session_info(session_id)
|
| 768 |
+
|
| 769 |
+
return {
|
| 770 |
+
"session_id": session_id,
|
| 771 |
+
"message_count": len(history),
|
| 772 |
+
"messages": history,
|
| 773 |
+
"created_at": session_info.get("created_at") if session_info else None,
|
| 774 |
+
"updated_at": session_info.get("updated_at") if session_info else None
|
| 775 |
+
}
|
| 776 |
+
|
| 777 |
+
|
| 778 |
+
@app.post("/chat/clear-session")
|
| 779 |
+
async def clear_chat_session(session_id: str):
|
| 780 |
+
"""
|
| 781 |
+
Clear conversation history for a session
|
| 782 |
+
|
| 783 |
+
Args:
|
| 784 |
+
session_id: Session identifier to clear
|
| 785 |
+
|
| 786 |
+
Returns:
|
| 787 |
+
Success message
|
| 788 |
+
|
| 789 |
+
Example:
|
| 790 |
+
```
|
| 791 |
+
POST /chat/clear-session?session_id=abc-123
|
| 792 |
+
```
|
| 793 |
+
"""
|
| 794 |
+
success = conversation_service.clear_session(session_id)
|
| 795 |
+
|
| 796 |
+
if success:
|
| 797 |
+
return {
|
| 798 |
+
"success": True,
|
| 799 |
+
"message": f"Session {session_id} cleared successfully"
|
| 800 |
+
}
|
| 801 |
+
else:
|
| 802 |
+
raise HTTPException(
|
| 803 |
+
status_code=404,
|
| 804 |
+
detail=f"Session {session_id} not found or already cleared"
|
| 805 |
+
)
|
| 806 |
+
|
| 807 |
+
|
| 808 |
+
@app.get("/chat/session/{session_id}")
|
| 809 |
+
async def get_session_info(session_id: str):
|
| 810 |
+
"""
|
| 811 |
+
Get metadata about a conversation session
|
| 812 |
+
|
| 813 |
+
Args:
|
| 814 |
+
session_id: Session identifier
|
| 815 |
+
|
| 816 |
+
Returns:
|
| 817 |
+
Session info including creation time and message count
|
| 818 |
+
|
| 819 |
+
Example:
|
| 820 |
+
```
|
| 821 |
+
GET /chat/session/abc-123
|
| 822 |
+
```
|
| 823 |
+
"""
|
| 824 |
+
session = conversation_service.get_session_info(session_id)
|
| 825 |
+
|
| 826 |
+
if not session:
|
| 827 |
+
raise HTTPException(
|
| 828 |
+
status_code=404,
|
| 829 |
+
detail=f"Session {session_id} not found"
|
| 830 |
+
)
|
| 831 |
+
|
| 832 |
+
# Get message count
|
| 833 |
+
history = conversation_service.get_conversation_history(
|
| 834 |
+
session_id,
|
| 835 |
+
include_metadata=True
|
| 836 |
+
)
|
| 837 |
+
|
| 838 |
+
return {
|
| 839 |
+
"session_id": session["session_id"],
|
| 840 |
+
"created_at": session["created_at"],
|
| 841 |
+
"updated_at": session["updated_at"],
|
| 842 |
+
"message_count": len(history),
|
| 843 |
+
"metadata": session.get("metadata", {})
|
| 844 |
+
}
|
| 845 |
+
|
| 846 |
+
|
| 847 |
@app.post("/documents", response_model=AddDocumentResponse)
|
| 848 |
async def add_document(request: AddDocumentRequest):
|
| 849 |
"""
|