|
|
@@ -4,8 +4,11 @@ import uuid
|
|
|
from fastapi import Depends, APIRouter
|
|
|
from sqlalchemy.orm import Session
|
|
|
from starlette.responses import StreamingResponse, Response
|
|
|
+
|
|
|
+from app.api import get_current_user
|
|
|
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 import UserModel
|
|
|
from app.models.base_model import get_db
|
|
|
from app.models.v2.session_model import ChatData
|
|
|
from app.service.v2.chat import service_chat_dialog, get_chat_info, service_chat_basic, \
|
|
|
@@ -13,43 +16,78 @@ from app.service.v2.chat import service_chat_dialog, get_chat_info, service_chat
|
|
|
|
|
|
chat_router_v2 = APIRouter()
|
|
|
|
|
|
-
|
|
|
-@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_router_v2.post("/{chatId}/dialog")
|
|
|
+async def api_chat_dialog(chatId:str, dialog: ChatData, current_user: UserModel = Depends(get_current_user),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": "\n**ERROR**: parameter exception", "status": http_400})
|
|
|
+ error_msg = json.dumps(
|
|
|
+ {"message": smart_message_error, "error": "\n**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:
|
|
|
+ if not dialog.query:
|
|
|
+ error_msg = json.dumps(
|
|
|
+ {"message": smart_message_error, "error": "\n**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": "\n**ERROR**: question cannot be empty.", "status": http_400})
|
|
|
+ {"message": smart_message_error, "error": "\n**ERROR**: chat agent error", "status": http_500})
|
|
|
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": "\n**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),
|
|
|
+ session_id = session.get("data", {}).get("id")
|
|
|
+ return StreamingResponse(service_chat_dialog(db, chatId, dialog.query, session_id, current_user.id, chat_info.mode),
|
|
|
+ media_type="text/event-stream")
|
|
|
+
|
|
|
+@chat_router_v2.post("/{chatId}/agent")
|
|
|
+async def api_chat_dialog(chatId:str, dialog: ChatData, current_user: UserModel = Depends(get_current_user),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": "\n**ERROR**: parameter exception", "status": http_400})
|
|
|
+ return StreamingResponse(f"data: {error_msg}\n\n",
|
|
|
media_type="text/event-stream")
|
|
|
- elif chat_info.mode == basic_chat:
|
|
|
- return StreamingResponse(service_chat_basic(db, chatId, dialog.question, dialog_chat.sessionId, 1),
|
|
|
+ session_id = dialog.sessionId
|
|
|
+ if not session_id:
|
|
|
+ session_id = str(uuid.uuid4()).replace("-", "")
|
|
|
+ return StreamingResponse(service_chat_workflow(db, chatId, dialog, session_id, current_user.id, chat_info.mode),
|
|
|
+ media_type="text/event-stream")
|
|
|
+
|
|
|
+
|
|
|
+@chat_router_v2.post("/{chatId}/workflow")
|
|
|
+async def api_chat_dialog(chatId:str, dialog: ChatData, current_user: UserModel = Depends(get_current_user), 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": "\n**ERROR**: parameter exception", "status": http_400})
|
|
|
+ return StreamingResponse(f"data: {error_msg}\n\n",
|
|
|
media_type="text/event-stream")
|
|
|
- else:
|
|
|
- error_msg = json.dumps({"message": smart_message_error, "error": "\n**ERROR**: chat agent error", "status": http_500})
|
|
|
+ session_id = dialog.sessionId
|
|
|
+ if not session_id:
|
|
|
+ session_id = str(uuid.uuid4()).replace("-", "")
|
|
|
+ return StreamingResponse(service_chat_workflow(db, chatId, dialog, session_id, current_user.id, chat_info.mode),
|
|
|
+ media_type="text/event-stream")
|
|
|
+
|
|
|
+
|
|
|
+@chat_router_v2.post("/{chatId}/basic")
|
|
|
+async def api_chat_dialog(chatId:str, dialog: ChatData, current_user: UserModel = Depends(get_current_user), 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": "\n**ERROR**: parameter exception", "status": http_400})
|
|
|
return StreamingResponse(f"data: {error_msg}\n\n",
|
|
|
media_type="text/event-stream")
|
|
|
+ session_id = dialog.sessionId
|
|
|
+ if not session_id:
|
|
|
+ session_id = str(uuid.uuid4()).replace("-", "")
|
|
|
+
|
|
|
+
|
|
|
+ return StreamingResponse(service_chat_basic(db, chatId, dialog, session_id, current_user.id, chat_info.mode),
|
|
|
+ media_type="text/event-stream")
|
|
|
+
|
|
|
|
|
|
|
|
|
@chat_router_v2.get("/{chatId}/parameters")
|