diff --git a/api/apps/api_app.py b/api/apps/api_app.py index a5243aea1c0..d9375941f03 100644 --- a/api/apps/api_app.py +++ b/api/apps/api_app.py @@ -199,15 +199,17 @@ def completion(): continue if m["role"] == "assistant" and not msg: continue - msg.append({"role": m["role"], "content": m["content"]}) + msg.append(m) + if not msg[-1].get("id"): msg[-1]["id"] = get_uuid() + message_id = msg[-1]["id"] def fillin_conv(ans): - nonlocal conv + nonlocal conv, message_id if not conv.reference: conv.reference.append(ans["reference"]) else: conv.reference[-1] = ans["reference"] - conv.message[-1] = {"role": "assistant", "content": ans["answer"]} + conv.message[-1] = {"role": "assistant", "content": ans["answer"], "id": message_id} def rename_field(ans): reference = ans['reference'] @@ -233,7 +235,7 @@ def rename_field(ans): if not conv.reference: conv.reference = [] - conv.message.append({"role": "assistant", "content": ""}) + conv.message.append({"role": "assistant", "content": "", "id": message_id}) conv.reference.append({"chunks": [], "doc_aggs": []}) final_ans = {"reference": [], "content": ""} @@ -260,7 +262,7 @@ def sse(): yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans}, ensure_ascii=False) + "\n\n" - canvas.messages.append({"role": "assistant", "content": final_ans["content"]}) + canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id}) if final_ans.get("reference"): canvas.reference.append(final_ans["reference"]) cvs.dsl = json.loads(str(canvas)) @@ -279,7 +281,7 @@ def sse(): return resp final_ans["content"] = "\n".join(answer["content"]) if "content" in answer else "" - canvas.messages.append({"role": "assistant", "content": final_ans["content"]}) + canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id}) if final_ans.get("reference"): canvas.reference.append(final_ans["reference"]) cvs.dsl = json.loads(str(canvas)) @@ -300,7 +302,7 @@ def sse(): if not conv.reference: conv.reference = [] - conv.message.append({"role": "assistant", "content": ""}) + conv.message.append({"role": "assistant", "content": "", "id": message_id}) conv.reference.append({"chunks": [], "doc_aggs": []}) def stream(): diff --git a/api/apps/canvas_app.py b/api/apps/canvas_app.py index 7f049e4fcf0..56c6010ccc8 100644 --- a/api/apps/canvas_app.py +++ b/api/apps/canvas_app.py @@ -91,10 +91,11 @@ def run(): cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False) final_ans = {"reference": [], "content": ""} + message_id = get_uuid() try: canvas = Canvas(cvs.dsl, current_user.id) if "message" in req: - canvas.messages.append({"role": "user", "content": req["message"]}) + canvas.messages.append({"role": "user", "content": req["message"], "id": message_id}) canvas.add_user_input(req["message"]) answer = canvas.run(stream=stream) print(canvas) @@ -115,7 +116,7 @@ def sse(): ans = {"answer": ans["content"], "reference": ans.get("reference", [])} yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans}, ensure_ascii=False) + "\n\n" - canvas.messages.append({"role": "assistant", "content": final_ans["content"]}) + canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id}) if final_ans.get("reference"): canvas.reference.append(final_ans["reference"]) cvs.dsl = json.loads(str(canvas)) @@ -134,7 +135,7 @@ def sse(): return resp final_ans["content"] = "\n".join(answer["content"]) if "content" in answer else "" - canvas.messages.append({"role": "assistant", "content": final_ans["content"]}) + canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id}) if final_ans.get("reference"): canvas.reference.append(final_ans["reference"]) cvs.dsl = json.loads(str(canvas)) diff --git a/api/apps/conversation_app.py b/api/apps/conversation_app.py index 9d6d623914f..b22937adaf0 100644 --- a/api/apps/conversation_app.py +++ b/api/apps/conversation_app.py @@ -117,9 +117,8 @@ def completion(): continue if m["role"] == "assistant" and not msg: continue - msg.append({"role": m["role"], "content": m["content"]}) - if "doc_ids" in m: - msg[-1]["doc_ids"] = m["doc_ids"] + msg.append(m) + message_id = msg[-1].get("id") try: e, conv = ConversationService.get_by_id(req["conversation_id"]) if not e: @@ -133,15 +132,15 @@ def completion(): if not conv.reference: conv.reference = [] - conv.message.append({"role": "assistant", "content": ""}) + conv.message.append({"role": "assistant", "content": "", "id": message_id}) conv.reference.append({"chunks": [], "doc_aggs": []}) def fillin_conv(ans): - nonlocal conv + nonlocal conv, message_id if not conv.reference: conv.reference.append(ans["reference"]) else: conv.reference[-1] = ans["reference"] - conv.message[-1] = {"role": "assistant", "content": ans["answer"]} + conv.message[-1] = {"role": "assistant", "content": ans["answer"], "id": message_id} def stream(): nonlocal dia, msg, req, conv @@ -175,3 +174,25 @@ def stream(): except Exception as e: return server_error_response(e) + +@manager.route('/delete_msg', methods=['POST']) +@login_required +@validate_request("conversation_id", "message_id") +def completion(): + req = request.json + e, conv = ConversationService.get_by_id(req["conversation_id"]) + if not e: + return get_data_error_result(retmsg="Conversation not found!") + + conv = conv.to_dict() + for i, msg in enumerate(conv["message"]): + if req["message_id"] != msg.get("id", ""): + continue + assert conv["message"][i+1]["id"] == req["message_id"] + conv["message"].pop(i) + conv["message"].pop(i) + conv["reference"].pop(i) + break + + ConversationService.update_by_id(conv["id"], conv) + return get_json_result(data=conv)