Download app/api/v1/conversations.py from isold/meloon: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/spaces/isold/meloon/resolve/main/app/api/v1/conversations.py
- Command line
-
hf download hf://spaces/isold/meloon/app/api/v1/conversations.py
-
curl -L -o conversations.py https://huggingface.co/spaces/isold/meloon/resolve/main/app/api/v1/conversations.py
3.43 kB
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.orm import Session | |
| from typing import List | |
| from app.api import deps | |
| from app.models.models import Conversation, Message, User, MessageRole | |
| from app.schemas import schemas | |
| from app.services.chat_service import chat_service | |
| router = APIRouter() | |
| def create_conversation( | |
| conv_in: schemas.ConversationCreate, | |
| db: Session = Depends(deps.get_db), | |
| current_user: User = Depends(deps.get_current_user), | |
| ): | |
| conv = Conversation( | |
| user_id=current_user.id, | |
| data_source_id=conv_in.data_source_id, | |
| title=conv_in.title | |
| ) | |
| db.add(conv) | |
| db.commit() | |
| db.refresh(conv) | |
| return conv | |
| def get_conversations( | |
| db: Session = Depends(deps.get_db), | |
| current_user: User = Depends(deps.get_current_user), | |
| skip: int = 0, | |
| limit: int = 100, | |
| ): | |
| conversations = db.query(Conversation).filter( | |
| Conversation.user_id == current_user.id, | |
| Conversation.is_deleted == False | |
| ).offset(skip).limit(limit).all() | |
| return conversations | |
| def delete_conversation( | |
| conversation_id: int, | |
| db: Session = Depends(deps.get_db), | |
| current_user: User = Depends(deps.get_current_user), | |
| ): | |
| conv = db.query(Conversation).filter(Conversation.id == conversation_id, Conversation.user_id == current_user.id).first() | |
| if not conv: | |
| raise HTTPException(status_code=404, detail="Conversation not found") | |
| conv.is_deleted = True | |
| db.commit() | |
| return None | |
| def create_message( | |
| conversation_id: int, | |
| msg_in: schemas.MessageCreate, | |
| db: Session = Depends(deps.get_db), | |
| current_user: User = Depends(deps.get_current_user), | |
| ): | |
| conv = db.query(Conversation).filter(Conversation.id == conversation_id, Conversation.user_id == current_user.id).first() | |
| if not conv: | |
| raise HTTPException(status_code=404, detail="Conversation not found") | |
| # Save User Message | |
| user_msg = Message( | |
| conversation_id=conv.id, | |
| role=MessageRole.user, | |
| content=msg_in.content | |
| ) | |
| db.add(user_msg) | |
| db.commit() | |
| # Auto-update conversation title if it's default | |
| if conv.title in ["New Chat", "新建对话"]: | |
| new_title = msg_in.content[:20] | |
| if len(msg_in.content) > 20: | |
| new_title += "..." | |
| conv.title = new_title | |
| db.add(conv) | |
| db.commit() | |
| db.refresh(conv) | |
| # Process and Generate Assistant Response | |
| assistant_msg = chat_service.process_message(db, conv, msg_in.content) | |
| return assistant_msg | |
| def read_messages( | |
| conversation_id: int, | |
| db: Session = Depends(deps.get_db), | |
| current_user: User = Depends(deps.get_current_user), | |
| ): | |
| conv = db.query(Conversation).filter(Conversation.id == conversation_id, Conversation.user_id == current_user.id).first() | |
| if not conv: | |
| raise HTTPException(status_code=404, detail="Conversation not found") | |
| messages = db.query(Message).filter(Message.conversation_id == conversation_id).all() | |
| return messages | |