tnp
zhaoqingang
2025-01-07 51433cba2f35b9a2571023236006ebc69d1d4d2d
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
import json
 
from app.service.v2.app_driver.chat_dialog import ChatDialog
 
 
async def service_chat_dialog(chat_id:str, question: str, session_id: str):
    token = "ragflow-YzMzE1NDRjYzMyZjExZWY5ZjkxMDI0Mm"
    url = f"/api/v1/chats/{chat_id}/completions"
    chat = ChatDialog(token)
    data = {
        "question": question,
        "stream": True,
        "session_id": session_id
    }
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f"Bearer {token}"
    }
    try:
        for ans in chat.chat_completions(url, data, headers):
 
            yield "data:" + json.dumps({"code": 0, "message": "", "data": ans}, ensure_ascii=False) + "\n\n"
        ChatSessionModel.update_by_id(conv.id, conv.to_dict())
    except Exception as e:
        yield "data:" + json.dumps({"code": 500, "message": str(e),
                                    "data": {"answer": "**ERROR**: " + str(e), "reference": []}},
                                   ensure_ascii=False) + "\n\n"
    yield "data:" + json.dumps({"code": 0, "message": "", "data": True}, ensure_ascii=False) + "\n\n"