zhaoqingang
2025-02-06 bcc63761bdc4c1604c9275a3c5cdf8a483ad5611
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import uuid
 
from fastapi import Depends, APIRouter
from sqlalchemy.orm import Session
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 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
 
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_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 chat_info.mode == dialog_chat:
        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**: 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": "\n**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
        data = "{}"
    return Response(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)