Kevin Hu commited on
Commit
42a04f2
·
1 Parent(s): d2ed30e

Make `Categorize` see more chat hisotry. (#4538)

Browse files

### What problem does this PR solve?

#4521

### Type of change
- [x] Performance Improvement

agent/component/base.py CHANGED
@@ -482,11 +482,12 @@ class ComponentBase(ABC):
482
  continue
483
 
484
  if q["component_id"].lower().find("answer") == 0:
485
- for r, c in self._canvas.history[::-1]:
486
- if r == "user":
487
- self._param.inputs.append({"content": c, "component_id": q["component_id"]})
488
- outs.append(pd.DataFrame([{"content": c}]))
489
- break
 
490
  continue
491
 
492
  outs.append(self._canvas.get_component(q["component_id"])["obj"].output(allow_partial=False)[1])
 
482
  continue
483
 
484
  if q["component_id"].lower().find("answer") == 0:
485
+ txt = []
486
+ for r, c in self._canvas.history[::-1][:self._param.message_history_window_size]:
487
+ txt.append(f"{r.upper()}: {c}")
488
+ txt = "\n".join(txt)
489
+ self._param.inputs.append({"content": txt, "component_id": q["component_id"]})
490
+ outs.append(pd.DataFrame([{"content": txt}]))
491
  continue
492
 
493
  outs.append(self._canvas.get_component(q["component_id"])["obj"].output(allow_partial=False)[1])
agent/component/categorize.py CHANGED
@@ -39,13 +39,13 @@ class CategorizeParam(GenerateParam):
39
  if not v.get("to"):
40
  raise ValueError(f"[Categorize] 'To' of category {k} can not be empty!")
41
 
42
- def get_prompt(self):
43
  cate_lines = []
44
  for c, desc in self.category_description.items():
45
  for line in desc.get("examples", "").split("\n"):
46
  if not line:
47
  continue
48
- cate_lines.append("Question: {}\tCategory: {}".format(line, c))
49
  descriptions = []
50
  for c, desc in self.category_description.items():
51
  if desc.get("description"):
@@ -62,11 +62,15 @@ class CategorizeParam(GenerateParam):
62
  {}
63
  You could learn from the above examples.
64
  Just mention the category names, no need for any additional words.
 
 
 
65
  """.format(
66
  len(self.category_description.keys()),
67
  "/".join(list(self.category_description.keys())),
68
  "\n".join(descriptions),
69
- "- ".join(cate_lines)
 
70
  )
71
  return self.prompt
72
 
@@ -76,9 +80,8 @@ class Categorize(Generate, ABC):
76
 
77
  def _run(self, history, **kwargs):
78
  input = self.get_input()
79
- input = "Question: " + (list(input["content"])[-1] if "content" in input else "") + "\tCategory: "
80
  chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id)
81
- ans = chat_mdl.chat(self._param.get_prompt(), [{"role": "user", "content": input}],
82
  self._param.gen_conf())
83
  logging.debug(f"input: {input}, answer: {str(ans)}")
84
  for c in self._param.category_description.keys():
 
39
  if not v.get("to"):
40
  raise ValueError(f"[Categorize] 'To' of category {k} can not be empty!")
41
 
42
+ def get_prompt(self, chat_hist):
43
  cate_lines = []
44
  for c, desc in self.category_description.items():
45
  for line in desc.get("examples", "").split("\n"):
46
  if not line:
47
  continue
48
+ cate_lines.append("USER: {}\nCategory: {}".format(line, c))
49
  descriptions = []
50
  for c, desc in self.category_description.items():
51
  if desc.get("description"):
 
62
  {}
63
  You could learn from the above examples.
64
  Just mention the category names, no need for any additional words.
65
+
66
+ ---- Real Data ----
67
+ {}
68
  """.format(
69
  len(self.category_description.keys()),
70
  "/".join(list(self.category_description.keys())),
71
  "\n".join(descriptions),
72
+ "- ".join(cate_lines),
73
+ chat_hist
74
  )
75
  return self.prompt
76
 
 
80
 
81
  def _run(self, history, **kwargs):
82
  input = self.get_input()
 
83
  chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id)
84
+ ans = chat_mdl.chat(self._param.get_prompt(input), [{"role": "user", "content": "\nCategory: "}],
85
  self._param.gen_conf())
86
  logging.debug(f"input: {input}, answer: {str(ans)}")
87
  for c in self._param.category_description.keys():