diff --git a/local_data/.gitignore b/local_data/.gitignore index a3a0c8b5..fec6204e 100644 --- a/local_data/.gitignore +++ b/local_data/.gitignore @@ -1,2 +1,3 @@ * +/private_gpt/ !.gitignore \ No newline at end of file diff --git a/private_gpt/server/completions/completions_router.py b/private_gpt/server/completions/completions_router.py index f604f886..5408bbef 100644 --- a/private_gpt/server/completions/completions_router.py +++ b/private_gpt/server/completions/completions_router.py @@ -118,9 +118,10 @@ def create_chat_item(db, sender, content, conversation_id): content=content, conversation_id=conversation_id ) + chat_history = crud.chat.get_conversation(db, conversation_id=conversation_id) + chat_history.generate_title() return crud.chat_item.create(db, obj_in=chat_item_create) - @completions_router.post( "/chat", response_model=None, diff --git a/private_gpt/users/api/v1/routers/audits.py b/private_gpt/users/api/v1/routers/audits.py index 17adcaba..18d17528 100644 --- a/private_gpt/users/api/v1/routers/audits.py +++ b/private_gpt/users/api/v1/routers/audits.py @@ -44,10 +44,10 @@ def list_auditlog( def filter_auditlog( db: Session = Depends(deps.get_db), filter_in= Depends(schemas.AuditFilter), - current_user: models.User = Security( - deps.get_current_user, - scopes=[Role.SUPER_ADMIN["name"]], - ), + # current_user: models.User = Security( + # deps.get_current_user, + # scopes=[Role.SUPER_ADMIN["name"]], + # ), ) -> List[schemas.Audit]: logs = crud.audit.filter(db, obj_in=filter_in) return convert_audit_logs(db, logs) @@ -56,10 +56,10 @@ def filter_auditlog( def download_auditlog( db: Session = Depends(deps.get_db), filter_in= Depends(schemas.ExcelFilter), - current_user: models.User = Security( - deps.get_current_user, - scopes=[Role.SUPER_ADMIN["name"]], - ), + # current_user: models.User = Security( + # deps.get_current_user, + # scopes=[Role.SUPER_ADMIN["name"]], + # ), ): logs = crud.audit.excel_filter(db, obj_in=filter_in) username = filter_in.username if filter_in.username else None diff --git a/private_gpt/users/api/v1/routers/chat_history.py b/private_gpt/users/api/v1/routers/chat_history.py index b0c5c6ce..6b61d545 100644 --- a/private_gpt/users/api/v1/routers/chat_history.py +++ b/private_gpt/users/api/v1/routers/chat_history.py @@ -1,126 +1,126 @@ -import logging -import traceback -import uuid -from sqlalchemy.orm import Session -from fastapi.responses import JSONResponse -from fastapi import APIRouter, Depends, HTTPException, status, Security -from fastapi_pagination import Page, paginate - -from private_gpt.users.api import deps -from private_gpt.users import crud, models, schemas - -logger = logging.getLogger(__name__) -router = APIRouter(prefix="/c", tags=["Chat Histories"]) - - -@router.get("", response_model=Page[schemas.Chat]) -def list_chat_histories( - db: Session = Depends(deps.get_db), - current_user: models.User = Security( - deps.get_current_user, - ), -) -> Page[schemas.Chat]: - """ - Retrieve a list of chat histories with pagination support. - """ - try: - chat_histories = crud.chat.get_chat_history( - db, user_id=current_user.id) - return paginate(chat_histories) - except Exception as e: - print(traceback.format_exc()) - logger.error(f"Error listing chat histories: {str(e)}") - raise HTTPException( - status_code=500, - detail="Internal Server Error", - ) - - -@router.post("/create", response_model=schemas.ChatHistory) -def create_chat_history( - db: Session = Depends(deps.get_db), - current_user: models.User = Security( - deps.get_current_user, - ), -) -> schemas.ChatHistory: - """ - Create a new chat history - """ - try: - chat_history_in = schemas.CreateChatHistory( - user_id= current_user.id - ) - chat_history = crud.chat.create( - db=db, obj_in=chat_history_in) - return chat_history - except Exception as e: - print(traceback.format_exc()) - logger.error(f"Error creating chat history: {str(e)}") - raise HTTPException( - status_code=500, - detail="Internal Server Error", - ) - - -@router.get("/{conversation_id}", response_model=schemas.ChatHistory) -def read_chat_history( - conversation_id: uuid.UUID, - skip: int = 0, - limit: int = 20, - db: Session = Depends(deps.get_db), - current_user: models.User = Security( - deps.get_current_user, - ), -) -> schemas.ChatHistory: - """ - Read a chat history by ID - """ - try: - chat_history = crud.chat.get_by_id(db, id=conversation_id, skip=skip, limit=limit) - if chat_history is None or chat_history.user_id != current_user.id: - raise HTTPException( - status_code=404, detail="Chat history not found") - return chat_history - except Exception as e: - print(traceback.format_exc()) - logger.error(f"Error reading chat history: {str(e)}") - raise HTTPException( - status_code=500, - detail="Internal Server Error", - ) - - -@router.post("/delete") -def delete_chat_history( - chat_history_in: schemas.ChatDelete, - db: Session = Depends(deps.get_db), - current_user: models.User = Security( - deps.get_current_user, - ), -): - """ - Delete a chat history by ID - """ - try: - chat_history_id = chat_history_in.conversation_id - chat_history = crud.chat.get_by_id(db, id=chat_history_id) - if chat_history is None or chat_history.user_id != current_user.id: - raise HTTPException( - status_code=404, detail="Chat history not found") - - crud.chat.remove(db=db, id=chat_history_id) - return JSONResponse( - status_code=status.HTTP_200_OK, - content={ - "message": "Chat history deleted successfully", - }, - ) - except Exception as e: - print(traceback.format_exc()) - logger.error(f"Error deleting chat history: {str(e)}") - raise HTTPException( - status_code=500, - detail="Internal Server Error", - ) - - +import logging +import traceback +import uuid +from sqlalchemy.orm import Session +from fastapi.responses import JSONResponse +from fastapi import APIRouter, Depends, HTTPException, status, Security +from fastapi_pagination import Page, paginate + +from private_gpt.users.api import deps +from private_gpt.users import crud, models, schemas + +logger = logging.getLogger(__name__) +router = APIRouter(prefix="/c", tags=["Chat Histories"]) + + +@router.get("", response_model=Page[schemas.Chat]) +def list_chat_histories( + db: Session = Depends(deps.get_db), + current_user: models.User = Security( + deps.get_current_user, + ), +) -> Page[schemas.Chat]: + """ + Retrieve a list of chat histories with pagination support. + """ + try: + chat_histories = crud.chat.get_chat_history( + db, user_id=current_user.id) + return paginate(chat_histories) + except Exception as e: + print(traceback.format_exc()) + logger.error(f"Error listing chat histories: {str(e)}") + raise HTTPException( + status_code=500, + detail="Internal Server Error", + ) + + +@router.post("/create", response_model=schemas.ChatHistory) +def create_chat_history( + db: Session = Depends(deps.get_db), + current_user: models.User = Security( + deps.get_current_user, + ), +) -> schemas.ChatHistory: + """ + Create a new chat history + """ + try: + chat_history_in = schemas.CreateChatHistory( + user_id= current_user.id + ) + chat_history = crud.chat.create( + db=db, obj_in=chat_history_in) + return chat_history + except Exception as e: + print(traceback.format_exc()) + logger.error(f"Error creating chat history: {str(e)}") + raise HTTPException( + status_code=500, + detail="Internal Server Error", + ) + + +@router.get("/{conversation_id}", response_model=schemas.ChatHistory) +def read_chat_history( + conversation_id: uuid.UUID, + skip: int = 0, + limit: int = 20, + db: Session = Depends(deps.get_db), + current_user: models.User = Security( + deps.get_current_user, + ), +) -> schemas.ChatHistory: + """ + Read a chat history by ID + """ + try: + chat_history = crud.chat.get_by_id(db, id=conversation_id, skip=skip, limit=limit) + if chat_history is None or chat_history.user_id != current_user.id: + raise HTTPException( + status_code=404, detail="Chat history not found") + return chat_history + except Exception as e: + print(traceback.format_exc()) + logger.error(f"Error reading chat history: {str(e)}") + raise HTTPException( + status_code=500, + detail="Internal Server Error", + ) + + +@router.post("/delete") +def delete_chat_history( + chat_history_in: schemas.ChatDelete, + db: Session = Depends(deps.get_db), + current_user: models.User = Security( + deps.get_current_user, + ), +): + """ + Delete a chat history by ID + """ + try: + chat_history_id = chat_history_in.conversation_id + chat_history = crud.chat.get_by_id(db, id=chat_history_id) + if chat_history is None or chat_history.user_id != current_user.id: + raise HTTPException( + status_code=404, detail="Chat history not found") + + crud.chat.remove(db=db, id=chat_history_id) + return JSONResponse( + status_code=status.HTTP_200_OK, + content={ + "message": "Chat history deleted successfully", + }, + ) + except Exception as e: + print(traceback.format_exc()) + logger.error(f"Error deleting chat history: {str(e)}") + raise HTTPException( + status_code=500, + detail="Internal Server Error", + ) + + diff --git a/private_gpt/users/crud/chat_crud.py b/private_gpt/users/crud/chat_crud.py index 61112658..c7d95c48 100644 --- a/private_gpt/users/crud/chat_crud.py +++ b/private_gpt/users/crud/chat_crud.py @@ -28,7 +28,14 @@ class CRUDChat(CRUDBase[ChatHistory, ChatHistoryCreate, ChatHistoryCreate]): .all() ) return chat_history - + + def get_conversation(self, db: Session, conversation_id: uuid.UUID) -> Optional[ChatHistory]: + return ( + db.query(self.model) + .filter(ChatHistory.conversation_id == conversation_id) + .first() + ) + def get_chat_history( self, db: Session, *,user_id:int ) -> List[ChatHistory]: diff --git a/private_gpt/users/models/chat.py b/private_gpt/users/models/chat.py index c2a1a7b1..528e1f30 100644 --- a/private_gpt/users/models/chat.py +++ b/private_gpt/users/models/chat.py @@ -33,7 +33,8 @@ class ChatHistory(Base): item for item in self.chat_items if item.sender == "user"] if user_chat_items: first_user_chat_item = user_chat_items[0] - self.title = first_user_chat_item.content[:30] + print("Chat items: ", first_user_chat_item.content['text']) + self.title = first_user_chat_item.content['text'][:30] else: self.title = str(self.conversation_id) diff --git a/private_gpt/users/schemas/chat.py b/private_gpt/users/schemas/chat.py index 80660462..a1efe4e2 100644 --- a/private_gpt/users/schemas/chat.py +++ b/private_gpt/users/schemas/chat.py @@ -41,6 +41,7 @@ class ChatHistoryUpdate(ChatHistoryBase): class Chat(BaseModel): conversation_id: uuid.UUID + title: Optional[str] class ChatHistory(ChatHistoryBase): conversation_id: uuid.UUID