Kevin Hu commited on
Commit
9309ea5
·
1 Parent(s): 08ede16

add message id to conversions (#2090)

Browse files

### What problem does this PR solve?

#2088
### Type of change

- [x] New Feature (non-breaking change which adds functionality)

api/apps/api_app.py CHANGED
@@ -199,15 +199,17 @@ def completion():
199
  continue
200
  if m["role"] == "assistant" and not msg:
201
  continue
202
- msg.append({"role": m["role"], "content": m["content"]})
 
 
203
 
204
  def fillin_conv(ans):
205
- nonlocal conv
206
  if not conv.reference:
207
  conv.reference.append(ans["reference"])
208
  else:
209
  conv.reference[-1] = ans["reference"]
210
- conv.message[-1] = {"role": "assistant", "content": ans["answer"]}
211
 
212
  def rename_field(ans):
213
  reference = ans['reference']
@@ -233,7 +235,7 @@ def completion():
233
 
234
  if not conv.reference:
235
  conv.reference = []
236
- conv.message.append({"role": "assistant", "content": ""})
237
  conv.reference.append({"chunks": [], "doc_aggs": []})
238
 
239
  final_ans = {"reference": [], "content": ""}
@@ -260,7 +262,7 @@ def completion():
260
  yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans},
261
  ensure_ascii=False) + "\n\n"
262
 
263
- canvas.messages.append({"role": "assistant", "content": final_ans["content"]})
264
  if final_ans.get("reference"):
265
  canvas.reference.append(final_ans["reference"])
266
  cvs.dsl = json.loads(str(canvas))
@@ -279,7 +281,7 @@ def completion():
279
  return resp
280
 
281
  final_ans["content"] = "\n".join(answer["content"]) if "content" in answer else ""
282
- canvas.messages.append({"role": "assistant", "content": final_ans["content"]})
283
  if final_ans.get("reference"):
284
  canvas.reference.append(final_ans["reference"])
285
  cvs.dsl = json.loads(str(canvas))
@@ -300,7 +302,7 @@ def completion():
300
 
301
  if not conv.reference:
302
  conv.reference = []
303
- conv.message.append({"role": "assistant", "content": ""})
304
  conv.reference.append({"chunks": [], "doc_aggs": []})
305
 
306
  def stream():
 
199
  continue
200
  if m["role"] == "assistant" and not msg:
201
  continue
202
+ msg.append(m)
203
+ if not msg[-1].get("id"): msg[-1]["id"] = get_uuid()
204
+ message_id = msg[-1]["id"]
205
 
206
  def fillin_conv(ans):
207
+ nonlocal conv, message_id
208
  if not conv.reference:
209
  conv.reference.append(ans["reference"])
210
  else:
211
  conv.reference[-1] = ans["reference"]
212
+ conv.message[-1] = {"role": "assistant", "content": ans["answer"], "id": message_id}
213
 
214
  def rename_field(ans):
215
  reference = ans['reference']
 
235
 
236
  if not conv.reference:
237
  conv.reference = []
238
+ conv.message.append({"role": "assistant", "content": "", "id": message_id})
239
  conv.reference.append({"chunks": [], "doc_aggs": []})
240
 
241
  final_ans = {"reference": [], "content": ""}
 
262
  yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans},
263
  ensure_ascii=False) + "\n\n"
264
 
265
+ canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
266
  if final_ans.get("reference"):
267
  canvas.reference.append(final_ans["reference"])
268
  cvs.dsl = json.loads(str(canvas))
 
281
  return resp
282
 
283
  final_ans["content"] = "\n".join(answer["content"]) if "content" in answer else ""
284
+ canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
285
  if final_ans.get("reference"):
286
  canvas.reference.append(final_ans["reference"])
287
  cvs.dsl = json.loads(str(canvas))
 
302
 
303
  if not conv.reference:
304
  conv.reference = []
305
+ conv.message.append({"role": "assistant", "content": "", "id": message_id})
306
  conv.reference.append({"chunks": [], "doc_aggs": []})
307
 
308
  def stream():
api/apps/canvas_app.py CHANGED
@@ -91,10 +91,11 @@ def run():
91
  cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)
92
 
93
  final_ans = {"reference": [], "content": ""}
 
94
  try:
95
  canvas = Canvas(cvs.dsl, current_user.id)
96
  if "message" in req:
97
- canvas.messages.append({"role": "user", "content": req["message"]})
98
  canvas.add_user_input(req["message"])
99
  answer = canvas.run(stream=stream)
100
  print(canvas)
@@ -115,7 +116,7 @@ def run():
115
  ans = {"answer": ans["content"], "reference": ans.get("reference", [])}
116
  yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans}, ensure_ascii=False) + "\n\n"
117
 
118
- canvas.messages.append({"role": "assistant", "content": final_ans["content"]})
119
  if final_ans.get("reference"):
120
  canvas.reference.append(final_ans["reference"])
121
  cvs.dsl = json.loads(str(canvas))
@@ -134,7 +135,7 @@ def run():
134
  return resp
135
 
136
  final_ans["content"] = "\n".join(answer["content"]) if "content" in answer else ""
137
- canvas.messages.append({"role": "assistant", "content": final_ans["content"]})
138
  if final_ans.get("reference"):
139
  canvas.reference.append(final_ans["reference"])
140
  cvs.dsl = json.loads(str(canvas))
 
91
  cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)
92
 
93
  final_ans = {"reference": [], "content": ""}
94
+ message_id = get_uuid()
95
  try:
96
  canvas = Canvas(cvs.dsl, current_user.id)
97
  if "message" in req:
98
+ canvas.messages.append({"role": "user", "content": req["message"], "id": message_id})
99
  canvas.add_user_input(req["message"])
100
  answer = canvas.run(stream=stream)
101
  print(canvas)
 
116
  ans = {"answer": ans["content"], "reference": ans.get("reference", [])}
117
  yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans}, ensure_ascii=False) + "\n\n"
118
 
119
+ canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
120
  if final_ans.get("reference"):
121
  canvas.reference.append(final_ans["reference"])
122
  cvs.dsl = json.loads(str(canvas))
 
135
  return resp
136
 
137
  final_ans["content"] = "\n".join(answer["content"]) if "content" in answer else ""
138
+ canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
139
  if final_ans.get("reference"):
140
  canvas.reference.append(final_ans["reference"])
141
  cvs.dsl = json.loads(str(canvas))
api/apps/conversation_app.py CHANGED
@@ -117,9 +117,8 @@ def completion():
117
  continue
118
  if m["role"] == "assistant" and not msg:
119
  continue
120
- msg.append({"role": m["role"], "content": m["content"]})
121
- if "doc_ids" in m:
122
- msg[-1]["doc_ids"] = m["doc_ids"]
123
  try:
124
  e, conv = ConversationService.get_by_id(req["conversation_id"])
125
  if not e:
@@ -133,15 +132,15 @@ def completion():
133
 
134
  if not conv.reference:
135
  conv.reference = []
136
- conv.message.append({"role": "assistant", "content": ""})
137
  conv.reference.append({"chunks": [], "doc_aggs": []})
138
 
139
  def fillin_conv(ans):
140
- nonlocal conv
141
  if not conv.reference:
142
  conv.reference.append(ans["reference"])
143
  else: conv.reference[-1] = ans["reference"]
144
- conv.message[-1] = {"role": "assistant", "content": ans["answer"]}
145
 
146
  def stream():
147
  nonlocal dia, msg, req, conv
@@ -175,3 +174,25 @@ def completion():
175
  except Exception as e:
176
  return server_error_response(e)
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  continue
118
  if m["role"] == "assistant" and not msg:
119
  continue
120
+ msg.append(m)
121
+ message_id = msg[-1].get("id")
 
122
  try:
123
  e, conv = ConversationService.get_by_id(req["conversation_id"])
124
  if not e:
 
132
 
133
  if not conv.reference:
134
  conv.reference = []
135
+ conv.message.append({"role": "assistant", "content": "", "id": message_id})
136
  conv.reference.append({"chunks": [], "doc_aggs": []})
137
 
138
  def fillin_conv(ans):
139
+ nonlocal conv, message_id
140
  if not conv.reference:
141
  conv.reference.append(ans["reference"])
142
  else: conv.reference[-1] = ans["reference"]
143
+ conv.message[-1] = {"role": "assistant", "content": ans["answer"], "id": message_id}
144
 
145
  def stream():
146
  nonlocal dia, msg, req, conv
 
174
  except Exception as e:
175
  return server_error_response(e)
176
 
177
+
178
+ @manager.route('/delete_msg', methods=['POST'])
179
+ @login_required
180
+ @validate_request("conversation_id", "message_id")
181
+ def completion():
182
+ req = request.json
183
+ e, conv = ConversationService.get_by_id(req["conversation_id"])
184
+ if not e:
185
+ return get_data_error_result(retmsg="Conversation not found!")
186
+
187
+ conv = conv.to_dict()
188
+ for i, msg in enumerate(conv["message"]):
189
+ if req["message_id"] != msg.get("id", ""):
190
+ continue
191
+ assert conv["message"][i+1]["id"] == req["message_id"]
192
+ conv["message"].pop(i)
193
+ conv["message"].pop(i)
194
+ conv["reference"].pop(i)
195
+ break
196
+
197
+ ConversationService.update_by_id(conv["id"], conv)
198
+ return get_json_result(data=conv)