Make Categorize see more chat hisotry. (#4538)

### What problem does this PR solve?

#4521

### Type of change
- [x] Performance Improvement
This commit is contained in:
Kevin Hu 2025-01-20 11:57:56 +08:00 committed by GitHub
parent 2962284c79
commit 367babda2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 10 deletions

View File

@ -482,11 +482,12 @@ class ComponentBase(ABC):
continue continue
if q["component_id"].lower().find("answer") == 0: if q["component_id"].lower().find("answer") == 0:
for r, c in self._canvas.history[::-1]: txt = []
if r == "user": for r, c in self._canvas.history[::-1][:self._param.message_history_window_size]:
self._param.inputs.append({"content": c, "component_id": q["component_id"]}) txt.append(f"{r.upper()}: {c}")
outs.append(pd.DataFrame([{"content": c}])) txt = "\n".join(txt)
break self._param.inputs.append({"content": txt, "component_id": q["component_id"]})
outs.append(pd.DataFrame([{"content": txt}]))
continue continue
outs.append(self._canvas.get_component(q["component_id"])["obj"].output(allow_partial=False)[1]) outs.append(self._canvas.get_component(q["component_id"])["obj"].output(allow_partial=False)[1])

View File

@ -39,13 +39,13 @@ class CategorizeParam(GenerateParam):
if not v.get("to"): if not v.get("to"):
raise ValueError(f"[Categorize] 'To' of category {k} can not be empty!") raise ValueError(f"[Categorize] 'To' of category {k} can not be empty!")
def get_prompt(self): def get_prompt(self, chat_hist):
cate_lines = [] cate_lines = []
for c, desc in self.category_description.items(): for c, desc in self.category_description.items():
for line in desc.get("examples", "").split("\n"): for line in desc.get("examples", "").split("\n"):
if not line: if not line:
continue continue
cate_lines.append("Question: {}\tCategory: {}".format(line, c)) cate_lines.append("USER: {}\nCategory: {}".format(line, c))
descriptions = [] descriptions = []
for c, desc in self.category_description.items(): for c, desc in self.category_description.items():
if desc.get("description"): if desc.get("description"):
@ -62,11 +62,15 @@ class CategorizeParam(GenerateParam):
{} {}
You could learn from the above examples. You could learn from the above examples.
Just mention the category names, no need for any additional words. Just mention the category names, no need for any additional words.
---- Real Data ----
{}
""".format( """.format(
len(self.category_description.keys()), len(self.category_description.keys()),
"/".join(list(self.category_description.keys())), "/".join(list(self.category_description.keys())),
"\n".join(descriptions), "\n".join(descriptions),
"- ".join(cate_lines) "- ".join(cate_lines),
chat_hist
) )
return self.prompt return self.prompt
@ -76,9 +80,8 @@ class Categorize(Generate, ABC):
def _run(self, history, **kwargs): def _run(self, history, **kwargs):
input = self.get_input() input = self.get_input()
input = "Question: " + (list(input["content"])[-1] if "content" in input else "") + "\tCategory: "
chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id) chat_mdl = LLMBundle(self._canvas.get_tenant_id(), LLMType.CHAT, self._param.llm_id)
ans = chat_mdl.chat(self._param.get_prompt(), [{"role": "user", "content": input}], ans = chat_mdl.chat(self._param.get_prompt(input), [{"role": "user", "content": "\nCategory: "}],
self._param.gen_conf()) self._param.gen_conf())
logging.debug(f"input: {input}, answer: {str(ans)}") logging.debug(f"input: {input}, answer: {str(ans)}")
for c in self._param.category_description.keys(): for c in self._param.category_description.keys():