Skip to content

Commit

Permalink
add message id to conversions (infiniflow#2090)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

infiniflow#2088 
### Type of change

- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
KevinHuSh authored Aug 26, 2024
1 parent f972719 commit 0272185
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
16 changes: 9 additions & 7 deletions api/apps/api_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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": ""}
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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():
Expand Down
7 changes: 4 additions & 3 deletions api/apps/canvas_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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))
Expand Down
33 changes: 27 additions & 6 deletions api/apps/conversation_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 0272185

Please sign in to comment.