Skip to content

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

fix(embedding): preserve zero filename embedding weight (#19397)

Fei Haihao48f6cbf27516
diff --git a/rag/svr/task_executor.py b/rag/svr/task_executor.py
index 7b2009463b..fbda9b9669 100644
--- a/rag/svr/task_executor.py
+++ b/rag/svr/task_executor.py
@@ -776,7 +776,7 @@ async def embedding(docs, mdl, parser_config=None, callback=None):
callback(prog=0.7 + 0.2 * (i + 1) / len(cnts), msg="")
cnts = np.vstack(cnts_batches) if cnts_batches else np.array([])
filename_embd_weight = parser_config.get("filename_embd_weight", 0.1) # due to the db support none value
- if not filename_embd_weight:
+ if filename_embd_weight is None:
filename_embd_weight = 0.1
title_w = float(filename_embd_weight)
if tts.ndim == 2 and cnts.ndim == 2 and tts.shape == cnts.shape:
diff --git a/rag/svr/task_executor_refactor/embedding_utils.py b/rag/svr/task_executor_refactor/embedding_utils.py
index 0d9b87e60e..d5e0b63098 100644
--- a/rag/svr/task_executor_refactor/embedding_utils.py
+++ b/rag/svr/task_executor_refactor/embedding_utils.py
@@ -177,8 +177,6 @@ class EmbeddingUtils:
"""
if title_weight is None:
title_weight = cls.DEFAULT_TITLE_WEIGHT
- if not title_weight:
- title_weight = cls.DEFAULT_TITLE_WEIGHT
if title_vecs is not None and content_vecs.ndim == 2 and title_vecs.shape == content_vecs.shape:
return title_weight * title_vecs + (1 - title_weight) * content_vecs
diff --git a/test/unit_test/rag/svr/task_executor_refactor/test_embedding_service.py b/test/unit_test/rag/svr/task_executor_refactor/test_embedding_service.py
index f01be12a42..a8d0a293a0 100644
--- a/test/unit_test/rag/svr/task_executor_refactor/test_embedding_service.py
+++ b/test/unit_test/rag/svr/task_executor_refactor/test_embedding_service.py
@@ -186,7 +186,10 @@ class TestEmbeddingServiceEmbedChunks:
@patch("rag.svr.task_executor_refactor.embedding_service.thread_pool_exec", new_callable=AsyncMock)
async def test_embed_chunks_title_weight_zero(self, mock_thread_pool):
"""Test embedding with filename_embd_weight=0.0 — no title contribution."""
- mock_thread_pool.return_value = (np.array([[1.0, 2.0]]), 5)
+ mock_thread_pool.side_effect = [
+ (np.array([[10.0, 20.0]]), 5),
+ (np.array([[1.0, 2.0]]), 5),
+ ]
ctx = MagicMock()
ctx.progress_cb = None
ctx.embed_limiter = AsyncMockLimiter()
@@ -198,6 +201,7 @@ class TestEmbeddingServiceEmbedChunks:
_, vector_size = await service.embed_chunks(docs, model, parser_config={"filename_embd_weight": 0.0})
assert vector_size == 2
+ np.testing.assert_array_equal(docs[0]["q_2_vec"], [1.0, 2.0])
@pytest.mark.asyncio
@patch("rag.svr.task_executor_refactor.embedding_service.thread_pool_exec", new_callable=AsyncMock)
diff --git a/test/unit_test/rag/svr/task_executor_refactor/test_embedding_utils.py b/test/unit_test/rag/svr/task_executor_refactor/test_embedding_utils.py
index 2c7601e852..bb902e1f32 100644
--- a/test/unit_test/rag/svr/task_executor_refactor/test_embedding_utils.py
+++ b/test/unit_test/rag/svr/task_executor_refactor/test_embedding_utils.py
@@ -265,14 +265,13 @@ class TestEmbeddingUtilsCombineVectors:
np.testing.assert_array_equal(result, content_vecs)
def test_combine_vectors_with_zero_weight(self):
- """Test combining when weight is 0 uses default 0.1."""
+ """Test combining when weight is 0 excludes title vectors."""
title_vecs = np.array([[1.0, 2.0]])
content_vecs = np.array([[5.0, 6.0]])
result = EmbeddingUtils.combine_title_content_vectors(title_vecs, content_vecs, title_weight=0)
- # Should use default weight of 0.1
- expected = 0.1 * title_vecs + 0.9 * content_vecs
+ expected = content_vecs
np.testing.assert_array_almost_equal(result, expected)