MCPcopy
hub / github.com/langchain-ai/langchain / reciprocal_rank_fusion

Function reciprocal_rank_fusion

templates/rag-fusion/rag_fusion/chain.py:10–24  ·  view source on GitHub ↗
(results: list[list], k=60)

Source from the content-addressed store, hash-verified

8
9
10def reciprocal_rank_fusion(results: list[list], k=60):
11 fused_scores = {}
12 for docs in results:
13 # Assumes the docs are returned in sorted order of relevance
14 for rank, doc in enumerate(docs):
15 doc_str = dumps(doc)
16 if doc_str not in fused_scores:
17 fused_scores[doc_str] = 0
18 fused_scores[doc_str] += 1 / (rank + k)
19
20 reranked_results = [
21 (loads(doc), score)
22 for doc, score in sorted(fused_scores.items(), key=lambda x: x[1], reverse=True)
23 ]
24 return reranked_results
25
26
27prompt = hub.pull("langchain-ai/rag-fusion-query-generation")

Callers

nothing calls this directly

Calls 2

dumpsFunction · 0.90
loadsFunction · 0.90

Tested by

no test coverage detected