| 12345678910111213141516171819202122232425262728 |
- 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"
|