| | |
| | | import json |
| | | import uuid |
| | | |
| | | from fastapi import Depends, APIRouter |
| | | from sqlalchemy.orm import Session |
| | | from starlette.responses import StreamingResponse |
| | | from app.api import get_current_user |
| | | from app.models import UserModel |
| | | from starlette.responses import StreamingResponse, Response |
| | | from app.config.const import dialog_chat, advanced_chat, base_chat, agent_chat, workflow_chat, basic_chat, \ |
| | | smart_message_error, http_400, http_500, http_200 |
| | | from app.models.base_model import get_db |
| | | from app.models.v2.session_model import ChatDialogData |
| | | from app.service.v2.chat import service_chat_dialog |
| | | from app.models.v2.session_model import ChatData |
| | | from app.service.v2.chat import service_chat_dialog, get_chat_info, service_chat_basic, \ |
| | | service_chat_workflow, service_chat_parameters, service_chat_sessions |
| | | |
| | | chat1_router = APIRouter() |
| | | chat_router_v2 = APIRouter() |
| | | |
| | | |
| | | @chat1_router.post("/chat_dialog") |
| | | async def api_chat_dialog(dialog: ChatDialogData, db: Session = Depends(get_db)): # current_user: UserModel = Depends(get_current_user) |
| | | return StreamingResponse(service_chat_dialog(db, dialog.chatId ,dialog.question, dialog.sessionId, 1), media_type="text/event-stream") |
| | | @chat_router_v2.post("/{chatId}/run") |
| | | async def api_chat_dialog(chatId:str, dialog: ChatData, db: Session = Depends(get_db)): # current_user: UserModel = Depends(get_current_user) |
| | | |
| | | chat_info = await get_chat_info(db, chatId) |
| | | if not chat_info: |
| | | error_msg = json.dumps({"message": smart_message_error, "error": "**ERROR**: parameter exception", "status": http_400}) |
| | | return StreamingResponse(f"data: {error_msg}\n\n", |
| | | media_type="text/event-stream") |
| | | session_id = dialog.sessionId |
| | | if chat_info.mode == dialog_chat: |
| | | if not dialog.query: |
| | | error_msg = json.dumps( |
| | | {"message": smart_message_error, "error": "**ERROR**: question cannot be empty.", "status": http_400}) |
| | | return StreamingResponse(f"data: {error_msg}\n\n", |
| | | media_type="text/event-stream") |
| | | if not session_id: |
| | | session = await service_chat_sessions(db, chatId, dialog.query) |
| | | if not session or session.get("code") != 0: |
| | | error_msg = json.dumps( |
| | | {"message": smart_message_error, "error": "**ERROR**: chat agent error", "status": http_500}) |
| | | return StreamingResponse(f"data: {error_msg}\n\n", |
| | | media_type="text/event-stream") |
| | | session_id = session.get("data", {}).get("id") |
| | | return StreamingResponse(service_chat_dialog(db, chatId ,dialog.query, session_id, 1, chat_info.mode), media_type="text/event-stream") |
| | | elif chat_info.mode == agent_chat or chat_info.mode == workflow_chat or chat_info.mode == advanced_chat or chat_info.mode == base_chat: |
| | | if not session_id: |
| | | session_id = str(uuid.uuid4()).replace("-", "") |
| | | return StreamingResponse(service_chat_workflow(db, chatId, dialog, session_id, 1, chat_info.mode), |
| | | media_type="text/event-stream") |
| | | elif chat_info.mode == basic_chat: |
| | | return StreamingResponse(service_chat_basic(db, chatId, dialog.question, dialog_chat.sessionId, 1), |
| | | media_type="text/event-stream") |
| | | else: |
| | | error_msg = json.dumps({"message": smart_message_error, "error": "**ERROR**: chat agent error", "status": http_500}) |
| | | return StreamingResponse(f"data: {error_msg}\n\n", |
| | | media_type="text/event-stream") |
| | | |
| | | |
| | | @chat_router_v2.get("/{chatId}/parameters") |
| | | async def api_chat_parameters(chatId:str, db: Session = Depends(get_db)): # current_user: UserModel = Depends(get_current_user) |
| | | status_code = http_200 |
| | | data = await service_chat_parameters(db, chatId, 1) |
| | | if not data: |
| | | status_code = http_400 |
| | | return Response(json.dumps(data), media_type="application/json", status_code=status_code) |
| | | |
| | | # @chat_router_v2.get("/{chatId}/parameters") |
| | | # async def api_chat_parameters(chatId:str, db: Session = Depends(get_db)): # current_user: UserModel = Depends(get_current_user) |
| | | # status_code = http_200 |
| | | # data = await service_chat_parameters(db, chatId, 1) |
| | | # if not data: |
| | | # status_code = http_400 |
| | | # return Response(json.dumps(data), media_type="application/json", status_code=status_code) |