Kevin Hu commited on
Commit
b471fdf
·
1 Parent(s): d78b07c

add assistant to canvas chat history (#3201)

Browse files

### What problem does this PR solve?

#3185

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

agent/canvas.py CHANGED
@@ -13,15 +13,11 @@
13
  # See the License for the specific language governing permissions and
14
  # limitations under the License.
15
  #
16
- import importlib
17
  import json
18
  import traceback
19
  from abc import ABC
20
  from copy import deepcopy
21
  from functools import partial
22
-
23
- import pandas as pd
24
-
25
  from agent.component import component_class
26
  from agent.component.base import ComponentBase
27
  from agent.settings import flow_logger, DEBUG
@@ -260,8 +256,10 @@ class Canvas(ABC):
260
  def get_history(self, window_size):
261
  convs = []
262
  for role, obj in self.history[window_size * -1:]:
263
- convs.append({"role": role, "content": (obj if role == "user" else
264
- '\n'.join([str(s) for s in pd.DataFrame(obj)['content']]))})
 
 
265
  return convs
266
 
267
  def add_user_input(self, question):
 
13
  # See the License for the specific language governing permissions and
14
  # limitations under the License.
15
  #
 
16
  import json
17
  import traceback
18
  from abc import ABC
19
  from copy import deepcopy
20
  from functools import partial
 
 
 
21
  from agent.component import component_class
22
  from agent.component.base import ComponentBase
23
  from agent.settings import flow_logger, DEBUG
 
256
  def get_history(self, window_size):
257
  convs = []
258
  for role, obj in self.history[window_size * -1:]:
259
+ if isinstance(obj, list) and obj and all([isinstance(o, dict) for o in obj]):
260
+ convs.append({"role": role, "content": '\n'.join([str(s.get("content", "")) for s in obj])})
261
+ else:
262
+ convs.append({"role": role, "content": str(obj)})
263
  return convs
264
 
265
  def add_user_input(self, question):
api/apps/api_app.py CHANGED
@@ -261,6 +261,7 @@ def completion():
261
  ensure_ascii=False) + "\n\n"
262
 
263
  canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
 
264
  if final_ans.get("reference"):
265
  canvas.reference.append(final_ans["reference"])
266
  cvs.dsl = json.loads(str(canvas))
 
261
  ensure_ascii=False) + "\n\n"
262
 
263
  canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
264
+ canvas.history.append(("assistant", final_ans["content"]))
265
  if final_ans.get("reference"):
266
  canvas.reference.append(final_ans["reference"])
267
  cvs.dsl = json.loads(str(canvas))
api/apps/canvas_app.py CHANGED
@@ -133,6 +133,7 @@ def run():
133
  yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans}, ensure_ascii=False) + "\n\n"
134
 
135
  canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
 
136
  if final_ans.get("reference"):
137
  canvas.reference.append(final_ans["reference"])
138
  cvs.dsl = json.loads(str(canvas))
 
133
  yield "data:" + json.dumps({"retcode": 0, "retmsg": "", "data": ans}, ensure_ascii=False) + "\n\n"
134
 
135
  canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
136
+ canvas.history.append(("assistant", final_ans["content"]))
137
  if final_ans.get("reference"):
138
  canvas.reference.append(final_ans["reference"])
139
  cvs.dsl = json.loads(str(canvas))
api/db/services/dialog_service.py CHANGED
@@ -164,7 +164,7 @@ def chat(dialog, messages, stream=True, **kwargs):
164
  embd_mdl = LLMBundle(dialog.tenant_id, LLMType.EMBEDDING, embd_nms[0])
165
  if not embd_mdl:
166
  raise LookupError("Embedding model(%s) not found" % embd_nms[0])
167
-
168
  if llm_id2llm_type(dialog.llm_id) == "image2text":
169
  chat_mdl = LLMBundle(dialog.tenant_id, LLMType.IMAGE2TEXT, dialog.llm_id)
170
  else:
 
164
  embd_mdl = LLMBundle(dialog.tenant_id, LLMType.EMBEDDING, embd_nms[0])
165
  if not embd_mdl:
166
  raise LookupError("Embedding model(%s) not found" % embd_nms[0])
167
+
168
  if llm_id2llm_type(dialog.llm_id) == "image2text":
169
  chat_mdl = LLMBundle(dialog.tenant_id, LLMType.IMAGE2TEXT, dialog.llm_id)
170
  else: