feat: add conversation api with seperated python files
Browse files- app/api/conversation_api.py +49 -0
- app/api/management_api.py +2 -2
app/api/conversation_api.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter
|
| 2 |
+
from fastapi import Request, Depends, HTTPException
|
| 3 |
+
from loguru import logger
|
| 4 |
+
|
| 5 |
+
from app.schema.conversation_schema import ConversationResponse, ConversationItemResponse
|
| 6 |
+
from app.service.chat_service import ChatService
|
| 7 |
+
from app.security.auth_service import AuthService
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
router = APIRouter(prefix="/v1", tags=["conversation"])
|
| 11 |
+
chat_service = ChatService()
|
| 12 |
+
auth_service = AuthService()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
################
|
| 16 |
+
# conversation api
|
| 17 |
+
# It means User chat history
|
| 18 |
+
################
|
| 19 |
+
# GET https://chatgpt.com/platform-api/conversations
|
| 20 |
+
# GET https://chatgpt.com/platform-api/conversations/{completion_id}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# get all conversations for current user
|
| 24 |
+
@router.get("/conversations", response_model=ConversationResponse, response_model_exclude_none=True)
|
| 25 |
+
async def list_conversations(username: str = Depends(auth_service.verify_credentials)) -> ConversationResponse:
|
| 26 |
+
"""
|
| 27 |
+
Get all conversations by current user
|
| 28 |
+
"""
|
| 29 |
+
logger.debug(f"Listing conversations for username: {username}")
|
| 30 |
+
try:
|
| 31 |
+
return await chat_service.find_all_conversations(username)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
logger.error(f"Error in list_conversations: {str(e)}")
|
| 34 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# get a conversation by id for current user
|
| 38 |
+
@router.get("/conversations/{completion_id}", response_model=ConversationItemResponse, response_model_exclude_none=True)
|
| 39 |
+
async def retrieve_conversation(
|
| 40 |
+
completion_id: str, request: Request, username: str = Depends(auth_service.verify_credentials)
|
| 41 |
+
) -> ConversationItemResponse:
|
| 42 |
+
"""
|
| 43 |
+
Get a conversation by id for current user
|
| 44 |
+
"""
|
| 45 |
+
logger.debug(f"Retrieving conversation with completion_id: {completion_id}")
|
| 46 |
+
try:
|
| 47 |
+
return await chat_service.find_conversation_by_id(completion_id)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
raise HTTPException(status_code=500, detail=str(e))
|
app/api/management_api.py
CHANGED
|
@@ -39,8 +39,8 @@ def _load_version():
|
|
| 39 |
__version__ = config["project"]["version"]
|
| 40 |
logger.info(f"API Version: {__version__}")
|
| 41 |
return __version__
|
| 42 |
-
|
| 43 |
-
__version__ = "unknown"
|
| 44 |
logger.warning(f"App Version is not found in pyproject.toml path: {pyproject_path}")
|
| 45 |
return __version__
|
| 46 |
|
|
|
|
| 39 |
__version__ = config["project"]["version"]
|
| 40 |
logger.info(f"API Version: {__version__}")
|
| 41 |
return __version__
|
| 42 |
+
|
| 43 |
+
__version__ = "unknown"
|
| 44 |
logger.warning(f"App Version is not found in pyproject.toml path: {pyproject_path}")
|
| 45 |
return __version__
|
| 46 |
|