lidp commited on
Commit
96bea9f
·
1 Parent(s): 1de5cd5

Support agent for aibot (#2609)

Browse files

### What problem does this PR solve?


### Type of change

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

Files changed (1) hide show
  1. api/apps/api_app.py +72 -9
api/apps/api_app.py CHANGED
@@ -679,8 +679,79 @@ def completion_faq():
679
 
680
  msg = []
681
  msg.append({"role": "user", "content": req["word"]})
 
 
 
 
 
 
 
 
 
 
 
682
 
683
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
684
  conv.message.append(msg[-1])
685
  e, dia = DialogService.get_by_id(conv.dialog_id)
686
  if not e:
@@ -689,17 +760,9 @@ def completion_faq():
689
 
690
  if not conv.reference:
691
  conv.reference = []
692
- conv.message.append({"role": "assistant", "content": ""})
693
  conv.reference.append({"chunks": [], "doc_aggs": []})
694
 
695
- def fillin_conv(ans):
696
- nonlocal conv
697
- if not conv.reference:
698
- conv.reference.append(ans["reference"])
699
- else:
700
- conv.reference[-1] = ans["reference"]
701
- conv.message[-1] = {"role": "assistant", "content": ans["answer"]}
702
-
703
  data_type_picture = {
704
  "type": 3,
705
  "url": "base64 content"
 
679
 
680
  msg = []
681
  msg.append({"role": "user", "content": req["word"]})
682
+ if not msg[-1].get("id"): msg[-1]["id"] = get_uuid()
683
+ message_id = msg[-1]["id"]
684
+
685
+ def fillin_conv(ans):
686
+ nonlocal conv, message_id
687
+ if not conv.reference:
688
+ conv.reference.append(ans["reference"])
689
+ else:
690
+ conv.reference[-1] = ans["reference"]
691
+ conv.message[-1] = {"role": "assistant", "content": ans["answer"], "id": message_id}
692
+ ans["id"] = message_id
693
 
694
  try:
695
+ if conv.source == "agent":
696
+ conv.message.append(msg[-1])
697
+ e, cvs = UserCanvasService.get_by_id(conv.dialog_id)
698
+ if not e:
699
+ return server_error_response("canvas not found.")
700
+
701
+ if not isinstance(cvs.dsl, str):
702
+ cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)
703
+
704
+ if not conv.reference:
705
+ conv.reference = []
706
+ conv.message.append({"role": "assistant", "content": "", "id": message_id})
707
+ conv.reference.append({"chunks": [], "doc_aggs": []})
708
+
709
+ final_ans = {"reference": [], "doc_aggs": []}
710
+ canvas = Canvas(cvs.dsl, objs[0].tenant_id)
711
+
712
+ canvas.messages.append(msg[-1])
713
+ canvas.add_user_input(msg[-1]["content"])
714
+ answer = canvas.run(stream=False)
715
+
716
+ assert answer is not None, "Nothing. Is it over?"
717
+
718
+ data_type_picture = {
719
+ "type": 3,
720
+ "url": "base64 content"
721
+ }
722
+ data = [
723
+ {
724
+ "type": 1,
725
+ "content": ""
726
+ }
727
+ ]
728
+ final_ans["content"] = "\n".join(answer["content"]) if "content" in answer else ""
729
+ canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
730
+ if final_ans.get("reference"):
731
+ canvas.reference.append(final_ans["reference"])
732
+ cvs.dsl = json.loads(str(canvas))
733
+
734
+ ans = {"answer": final_ans["content"], "reference": final_ans.get("reference", [])}
735
+ data[0]["content"] += re.sub(r'##\d\$\$', '', ans["answer"])
736
+ fillin_conv(ans)
737
+ API4ConversationService.append_message(conv.id, conv.to_dict())
738
+
739
+ chunk_idxs = [int(match[2]) for match in re.findall(r'##\d\$\$', ans["answer"])]
740
+ for chunk_idx in chunk_idxs[:1]:
741
+ if ans["reference"]["chunks"][chunk_idx]["img_id"]:
742
+ try:
743
+ bkt, nm = ans["reference"]["chunks"][chunk_idx]["img_id"].split("-")
744
+ response = STORAGE_IMPL.get(bkt, nm)
745
+ data_type_picture["url"] = base64.b64encode(response).decode('utf-8')
746
+ data.append(data_type_picture)
747
+ break
748
+ except Exception as e:
749
+ return server_error_response(e)
750
+
751
+ response = {"code": 200, "msg": "success", "data": data}
752
+ return response
753
+
754
+ # ******************For dialog******************
755
  conv.message.append(msg[-1])
756
  e, dia = DialogService.get_by_id(conv.dialog_id)
757
  if not e:
 
760
 
761
  if not conv.reference:
762
  conv.reference = []
763
+ conv.message.append({"role": "assistant", "content": "", "id": message_id})
764
  conv.reference.append({"chunks": [], "doc_aggs": []})
765
 
 
 
 
 
 
 
 
 
766
  data_type_picture = {
767
  "type": 3,
768
  "url": "base64 content"