Skip to content

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

fix: support SeekDB vector score extraction (#18874)

kic63588e80fc5d102
diff --git a/rag/utils/ob_conn.py b/rag/utils/ob_conn.py
index 24e9d6f400..45a8e05a4a 100644
--- a/rag/utils/ob_conn.py
+++ b/rag/utils/ob_conn.py
@@ -696,7 +696,9 @@ class OBConnection(OBConnectionBase):
result.total = result.total + 1
return result
- output_fields = select_fields.copy()
+ output_fields = [field for field in select_fields.copy() if field != "row_id()"]
+ if len(output_fields) != len(select_fields):
+ logger.warning("SeekDB/OceanBase does not support row_id(); removing it from output fields")
if "*" in output_fields:
if index_names[0].startswith("ragflow_doc_meta_"):
output_fields = doc_meta_column_names.copy()
@@ -1292,6 +1294,24 @@ class OBConnection(OBConnectionBase):
Helper functions for search result
"""
+ def get_scores(self, res: SearchResult) -> dict[str, float]:
+ """Return the scores attached to SeekDB search result chunks."""
+ scores = {}
+ missing_id = 0
+ missing_score = 0
+ for chunk in res.chunks:
+ chunk_id = chunk.get("id")
+ if chunk_id is None:
+ missing_id += 1
+ continue
+ score = chunk.get("_score")
+ if score is None:
+ missing_score += 1
+ scores[chunk_id] = float(score or 0.0)
+ if missing_id or missing_score:
+ logger.debug("get_scores skipped chunks: missing_id=%d missing_score=%d", missing_id, missing_score)
+ return scores
+
def get_total(self, res) -> int:
return res.total
diff --git a/test/unit_test/rag/utils/test_ob_conn.py b/test/unit_test/rag/utils/test_ob_conn.py
index af11901d27..7f89088e19 100644
--- a/test/unit_test/rag/utils/test_ob_conn.py
+++ b/test/unit_test/rag/utils/test_ob_conn.py
@@ -18,7 +18,37 @@
Unit tests for OceanBase connection utility functions.
"""
-from rag.utils.ob_conn import get_value_str, get_metadata_filter_expression
+import logging
+
+from rag.utils.ob_conn import OBConnection, SearchResult, get_metadata_filter_expression, get_value_str
+
+
+def _ob_connection_class():
+ return next(cell.cell_contents for cell in OBConnection.__closure__ if isinstance(cell.cell_contents, type))
+
+
+class TestOceanBaseVectorScoreExtraction:
+ def test_get_scores_preserves_scores_and_defaults_missing_scores(self, caplog):
+ """SeekDB vector results expose chunk ids and scores in SearchResult.chunks."""
+ connection = object.__new__(_ob_connection_class())
+ result = SearchResult(
+ total=5,
+ chunks=[
+ {"id": "chunk-1", "_score": 0.8123},
+ {"id": "chunk-2", "_score": 0.1034},
+ {"id": "chunk-3"},
+ {"id": "chunk-4", "_score": None},
+ {"_score": 0.5},
+ ],
+ )
+
+ with caplog.at_level(logging.DEBUG, logger="ragflow.ob_conn"):
+ scores = connection.get_scores(result)
+
+ assert scores == {"chunk-1": 0.8123, "chunk-2": 0.1034, "chunk-3": 0.0, "chunk-4": 0.0}
+ assert "get_scores skipped chunks" in caplog.text
+ assert "missing_id=1" in caplog.text
+ assert "missing_score=2" in caplog.text
class TestGetValueStr: