From ac465ba2a65073b2d2a441b1212916953d636bcd Mon Sep 17 00:00:00 2001 From: buua436 <66937541+buua436@users.noreply.github.com> Date: Mon, 3 Nov 2025 19:19:09 +0800 Subject: [PATCH] =?UTF-8?q?Feat:add=20variables=20to=20the=20metadata=20fi?= =?UTF-8?q?ltering=20function=20of=20the=20knowledg=E2=80=A6=20(#10967)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …e retrieval component. ### What problem does this PR solve? issue: #10861 change: add variables to the metadata filtering function of the knowledge retrieval component ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- agent/tools/retrieval.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/agent/tools/retrieval.py b/agent/tools/retrieval.py index f0823be69..bb44b124a 100644 --- a/agent/tools/retrieval.py +++ b/agent/tools/retrieval.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +from functools import partial +import json import os import re from abc import ABC @@ -131,7 +133,35 @@ class Retrieval(ToolBase, ABC): if not doc_ids: doc_ids = None elif self._param.meta_data_filter.get("method") == "manual": - doc_ids.extend(meta_filter(metas, self._param.meta_data_filter["manual"])) + filters=self._param.meta_data_filter["manual"] + for flt in filters: + pat = re.compile(r"\{* *\{([a-zA-Z:0-9]+@[A-Za-z:0-9_.-]+|sys\.[a-z_]+)\} *\}*") + s = flt["value"] + out_parts = [] + last = 0 + + for m in pat.finditer(s): + out_parts.append(s[last:m.start()]) + key = m.group(1) + v = self._canvas.get_variable_value(key) + if v is None: + rep = "" + elif isinstance(v, partial): + buf = [] + for chunk in v(): + buf.append(chunk) + rep = "".join(buf) + elif isinstance(v, str): + rep = v + else: + rep = json.dumps(v, ensure_ascii=False) + + out_parts.append(rep) + last = m.end() + + out_parts.append(s[last:]) + flt["value"] = "".join(out_parts) + doc_ids.extend(meta_filter(metas, filters)) if not doc_ids: doc_ids = None