Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Feat:add variables to the metadata filtering function of the knowledg… (#10967)

buua436ac465ba2a650
diff --git a/agent/tools/retrieval.py b/agent/tools/retrieval.py
index f0823be69a..bb44b124ad 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