Skip to content

Commit 888ce99

Browse files
committed
Revert "Update reciprocal_rank_stage"
This reverts commit fc2e381.
1 parent fd10e97 commit 888ce99

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

libs/langchain-mongodb/langchain_mongodb/pipelines.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pymongo_search_utils import (
1313
combine_pipelines, # noqa: F401
1414
final_hybrid_stage, # noqa: F401
15-
reciprocal_rank_stage, # noqa: F401
1615
)
1716

1817

@@ -95,3 +94,41 @@ def vector_search_stage(
9594
if filter:
9695
stage["filter"] = filter
9796
return {"$vectorSearch": stage}
97+
98+
99+
def reciprocal_rank_stage(
100+
score_field: str, penalty: float = 0, weight: float = 1, **kwargs: Any
101+
) -> List[Dict[str, Any]]:
102+
"""
103+
Stage adds Weighted Reciprocal Rank Fusion (WRRF) scoring.
104+
105+
First, it groups documents into an array, assigns rank by array index,
106+
and then computes a weighted RRF score.
107+
108+
Args:
109+
score_field: A unique string to identify the search being ranked.
110+
penalty: A non-negative float (e.g., 60 for RRF-60). Controls the denominator.
111+
weight: A float multiplier for this source's importance.
112+
**kwargs: Ignored; allows future extensions or passthrough args.
113+
114+
Returns:
115+
Aggregation pipeline stage for weighted RRF scoring.
116+
"""
117+
118+
return [
119+
{"$group": {"_id": None, "docs": {"$push": "$$ROOT"}}},
120+
{"$unwind": {"path": "$docs", "includeArrayIndex": "rank"}},
121+
{
122+
"$addFields": {
123+
f"docs.{score_field}": {
124+
"$multiply": [
125+
weight,
126+
{"$divide": [1.0, {"$add": ["$rank", penalty, 1]}]},
127+
]
128+
},
129+
"docs.rank": "$rank",
130+
"_id": "$docs._id",
131+
}
132+
},
133+
{"$replaceRoot": {"newRoot": "$docs"}},
134+
]

libs/langchain-mongodb/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dev = [
4242
"langchain-tests==0.3.22,<1.0",
4343
"pip>=25.0.1",
4444
"typing-extensions>=4.12.2",
45-
"pymongo-search-utils@git+https://github.com/aclark4life/pymongo-search-utils.git@INTPYTHON-752",
45+
"pymongo-search-utils@git+https://github.com/mongodb-labs/pymongo-search-utils.git",
4646
]
4747

4848
[tool.pytest.ini_options]

0 commit comments

Comments
 (0)