zhaoqingang
2025-03-06 e26a7859a8900b152e10961d91fa6ad19a8deb9c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import json
import uuid
 
from typing import List
from fastapi import Depends, APIRouter, File, UploadFile
from sqlalchemy.orm import Session
from starlette.responses import StreamingResponse, Response
from app.api import get_current_user, get_api_key
from app.config.const import smart_message_error, http_400, http_500, http_200, complex_dialog_chat
from app.models import UserModel
from app.models.base_model import get_db
from app.models.v2.chat import RetrievalRequest, ChatDataRequest, ComplexChatDao
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, service_chat_upload, \
    service_chat_sessions_list, service_chat_session_log, service_chunk_retrieval, service_complex_chat, \
    service_complex_upload
 
chat_router_v2 = APIRouter()
 
# 对话
@chat_router_v2.post("/chat/{chatId}/completions")
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 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)
        # print(session)
        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, current_user.id, chat_info.mode),
                             media_type="text/event-stream")
 
@chat_router_v2.post("/agent/{chatId}/completions")
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_workflow(db, chatId, dialog, session_id, current_user.id, chat_info.mode),
                             media_type="text/event-stream")
 
 
@chat_router_v2.post("/workflow/{chatId}/completions")
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_workflow(db, chatId, dialog, session_id, current_user.id, chat_info.mode),
                             media_type="text/event-stream")
 
 
@chat_router_v2.post("/develop/{chatId}/completions")
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("/chat/{chatId}/parameters")
async def api_chat_parameters(chatId:str, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    status_code = http_200
    data = await service_chat_parameters(db, chatId, current_user.id)
    if not data:
        status_code = http_400
        data = json.dumps({"code": http_400})
    return Response(data, media_type="application/json", status_code=status_code)
 
 
@chat_router_v2.post("/{chatId}/upload")
async def api_chat_upload(chatId:str, file: List[UploadFile] = File(...), current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    status_code = http_200
    data = await service_chat_upload(db, chatId, file, current_user.id)
    if not data:
        status_code = http_400
        data = "{}"
    return Response(data, media_type="application/json", status_code=status_code)
 
 
 
@chat_router_v2.get("/chat/sessions")
async def api_chat_sessions(chatId:str, current:int=1, pageSize:int=100, keyword:str="", current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    data = await service_chat_sessions_list(db, chatId, current, pageSize, current_user.id, keyword)
    return Response(data, media_type="application/json", status_code=http_200)
 
 
@chat_router_v2.get("/chat/session_log")
async def api_chat_sessions(sessionId:str, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    data = await service_chat_session_log(db, sessionId)
    return Response(data, media_type="application/json", status_code=http_200)
 
 
@chat_router_v2.post("/retrieval")
async def retrieve_chunks(request_data: RetrievalRequest, api_key: str = Depends(get_api_key)):
    records = await service_chunk_retrieval(request_data.query, request_data.knowledge_id, request_data.retrieval_setting.top_k, request_data.retrieval_setting.score_threshold, api_key)
    return {"records": records}
 
 
@chat_router_v2.post("/complex/chat/completions")
async def api_complex_chat_completions(chat: ChatDataRequest, current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    complex_chat = await ComplexChatDao(db).get_complex_chat_by_mode(chat.chatMode)
    if complex_chat:
        if not chat.sessionId:
            chat.sessionId = str(uuid.uuid4()).replace("-", "")
        return StreamingResponse(service_complex_chat(db, complex_chat.id, complex_chat.mode, current_user.id, chat),
                                 media_type="text/event-stream")
    else:
        error_msg = json.dumps(
            {"message": smart_message_error, "error": "\n**ERROR**: 网络异常,无法生成对话结果!", "status": http_500})
        return StreamingResponse(f"data: {error_msg}\n\n",
                                 media_type="text/event-stream")
 
 
@chat_router_v2.post("/complex/upload/{chatMode}")
async def api_complex_upload(chatMode:int, file: List[UploadFile] = File(...), current_user: UserModel = Depends(get_current_user), db: Session = Depends(get_db)): #  current_user: UserModel = Depends(get_current_user)
    status_code = http_200
    complex_chat = await ComplexChatDao(db).get_complex_chat_by_mode(chatMode)
    if complex_chat:
        data = await service_complex_upload(db, complex_chat.id, file, current_user.id)
        if not data:
            status_code = http_400
            data = "{}"
    else:
        status_code = http_500
        data = "{}"
    return Response(data, media_type="application/json", status_code=status_code)