Spaces:
Sleeping
Sleeping
Download app/api/chat.py from vara201/simple-rag: direct link, hf CLI and curl.
- Browser
- Download file 6.65 kB
-
https://huggingface.co/spaces/vara201/simple-rag/resolve/main/app/api/chat.py
- Command line
-
hf download hf://spaces/vara201/simple-rag/app/api/chat.py
-
curl -L -o chat.py https://huggingface.co/spaces/vara201/simple-rag/resolve/main/app/api/chat.py
6.65 kB
| from fastapi import ( | |
| APIRouter, | |
| Depends, | |
| HTTPException, BackgroundTasks | |
| ) | |
| from sqlalchemy.ext.asyncio import ( | |
| AsyncSession | |
| ) | |
| from app.core.database import ( | |
| get_db | |
| ) | |
| from app.core.dependencies import ( | |
| get_current_user | |
| ) | |
| from app.services.chat_service import ( | |
| ChatService | |
| ) | |
| from app.services.chat_management_service import ( | |
| ChatManagementService | |
| ) | |
| from app.repositories.chat_repository import ( | |
| ChatRepository | |
| ) | |
| from app.repositories.message_repository import ( | |
| MessageRepository | |
| ) | |
| from app.schemas.chat import ( | |
| ChatRequest, | |
| CreateChatRequest, | |
| ChatResponse | |
| ) | |
| import json | |
| router = APIRouter( | |
| prefix="/chats", | |
| tags=["Chats"] | |
| ) | |
| async def create_chat( | |
| payload: CreateChatRequest = None, | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| title = (payload.title if payload and payload.title else "New Conversation").strip() or "New Conversation" | |
| chat = await ( | |
| ChatManagementService.create_chat( | |
| db=db, | |
| user_id=current_user.id, | |
| title=title | |
| ) | |
| ) | |
| return { | |
| "id": chat.id, | |
| "title": chat.title | |
| } | |
| async def get_chats( | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| chats = await ( | |
| ChatRepository.get_user_chats( | |
| db, | |
| current_user.id | |
| ) | |
| ) | |
| return chats | |
| async def get_chat( | |
| chat_id: str, | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| chat = await ( | |
| ChatRepository.get_by_id( | |
| db, | |
| chat_id | |
| ) | |
| ) | |
| if not chat: | |
| raise HTTPException( | |
| status_code=404, | |
| detail="Chat not found" | |
| ) | |
| if chat.user_id != current_user.id: | |
| raise HTTPException( | |
| status_code=403, | |
| detail="Forbidden" | |
| ) | |
| messages = await ( | |
| MessageRepository.get_chat_messages( | |
| db, | |
| chat_id | |
| ) | |
| ) | |
| return { | |
| "id": chat.id, | |
| "title": chat.title, | |
| "messages": [ | |
| { | |
| "id": m.id, | |
| "role": m.role, | |
| "content": m.content, | |
| "citations": json.loads(m.citations) if m.citations else [], | |
| "created_at": m.created_at.isoformat() if hasattr(m.created_at, "isoformat") else str(m.created_at), | |
| "trace_id": getattr(m, "trace_id", None), | |
| "steps": json.loads(m.steps_data) if getattr(m, "steps_data", None) else [] | |
| } | |
| for m in messages | |
| ] | |
| } | |
| async def send_message( | |
| chat_id: str, | |
| payload: ChatRequest, | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| chat = await ( | |
| ChatRepository.get_by_id( | |
| db, | |
| chat_id | |
| ) | |
| ) | |
| if not chat: | |
| raise HTTPException( | |
| status_code=404, | |
| detail="Chat not found" | |
| ) | |
| if chat.user_id != current_user.id: | |
| raise HTTPException( | |
| status_code=403, | |
| detail="Forbidden" | |
| ) | |
| return await ( | |
| ChatService.chat( | |
| query=payload.query, | |
| user_id=current_user.id, | |
| chat_id=chat_id, | |
| db=db, | |
| ) | |
| ) | |
| async def update_chat( | |
| chat_id: str, | |
| title: str, | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| """ | |
| Update chat title | |
| """ | |
| chat = await ( | |
| ChatRepository.get_by_id( | |
| db, | |
| chat_id | |
| ) | |
| ) | |
| if not chat: | |
| raise HTTPException( | |
| status_code=404, | |
| detail="Chat not found" | |
| ) | |
| if chat.user_id != current_user.id: | |
| raise HTTPException( | |
| status_code=403, | |
| detail="Forbidden" | |
| ) | |
| updated_chat = await ChatManagementService.update_chat( | |
| db=db, | |
| chat_id=chat_id, | |
| title=title | |
| ) | |
| return { | |
| "id": updated_chat.id, | |
| "title": updated_chat.title | |
| } | |
| async def delete_chat( | |
| chat_id: str, | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| """ | |
| Delete a chat and all its messages | |
| """ | |
| chat = await ( | |
| ChatRepository.get_by_id( | |
| db, | |
| chat_id | |
| ) | |
| ) | |
| if not chat: | |
| raise HTTPException( | |
| status_code=404, | |
| detail="Chat not found" | |
| ) | |
| if chat.user_id != current_user.id: | |
| raise HTTPException( | |
| status_code=403, | |
| detail="Forbidden" | |
| ) | |
| await ChatManagementService.delete_chat( | |
| db=db, | |
| chat_id=chat_id | |
| ) | |
| return { | |
| "message": f"Chat {chat_id} deleted successfully", | |
| "status": "success" | |
| } | |
| async def delete_message( | |
| chat_id: str, | |
| message_id: str, | |
| db: AsyncSession = Depends( | |
| get_db | |
| ), | |
| current_user=Depends( | |
| get_current_user | |
| ) | |
| ): | |
| """ | |
| Delete a message from a chat | |
| """ | |
| chat = await ( | |
| ChatRepository.get_by_id( | |
| db, | |
| chat_id | |
| ) | |
| ) | |
| if not chat: | |
| raise HTTPException( | |
| status_code=404, | |
| detail="Chat not found" | |
| ) | |
| if chat.user_id != current_user.id: | |
| raise HTTPException( | |
| status_code=403, | |
| detail="Forbidden" | |
| ) | |
| message = await MessageRepository.get_by_id( | |
| db, | |
| message_id | |
| ) | |
| if not message or message.chat_id != chat_id: | |
| raise HTTPException( | |
| status_code=404, | |
| detail="Message not found" | |
| ) | |
| await MessageRepository.delete(db, message_id) | |
| return { | |
| "message": f"Message {message_id} deleted successfully", | |
| "status": "success" | |
| } |