Browse Source

Fix: incorrect total chunks count in retrieval function after similarity filtering (#6741) (#6932)

### Related Issue:
https://github.com/infiniflow/ragflow/issues/6741

### Environment:
Using nightly version
Commit version:
[[6051abb](6051abb4a3)]

### Bug Description:
The retrieval function in rag/nlp/search.py returns the original total
chunks number
even after chunks are filtered by similarity_threshold. This creates
inconsistency
between the actual returned chunks and the reported total.

### Changes Made:
Added code to count how many search results actually meet or exceed the
configured similarity threshold
Positioned the calculation after the doc_ids conditional logic to ensure
special cases are handled correctly
Updated the ranks["total"] value to store this filtered count instead of
using the raw search result count
Using NumPy leverages optimized C-level batch operations to optimize
speed
tags/v0.18.0
alulala 6 months ago
parent
commit
d9266ed65a
No account linked to committer's email address
1 changed files with 3 additions and 1 deletions
  1. 3
    1
      rag/nlp/search.py

+ 3
- 1
rag/nlp/search.py View File

@@ -363,7 +363,6 @@ class Dealer:

sres = self.search(req, [index_name(tid) for tid in tenant_ids],
kb_ids, embd_mdl, highlight, rank_feature=rank_feature)
ranks["total"] = sres.total

if rerank_mdl and sres.total > 0:
sim, tsim, vsim = self.rerank_by_model(rerank_mdl,
@@ -383,6 +382,9 @@ class Dealer:
if doc_ids:
similarity_threshold = 0
page_size = 30
sim_np = np.array(sim)
filtered_count = (sim_np >= similarity_threshold).sum()
ranks["total"] = int(filtered_count) # Convert from np.int64 to Python int otherwise JSON serializable error
for i in idx:
if sim[i] < similarity_threshold:
break

Loading…
Cancel
Save