This repository was archived by the owner on Jul 17, 2026. It is now read-only.
forked from deepset-ai/haystack
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add optional BM25 retrieval confidence metadata #1
Closed
SeCuReDmE-main-dev
wants to merge
3
commits into
feature/haystack-evaluator-uncertainty-phase1
from
feature/haystack-retrieval-confidence-phase2
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -44,6 +44,7 @@ def __init__( | |||||||||||||||
| filters: dict[str, Any] | None = None, | ||||||||||||||||
| top_k: int = 10, | ||||||||||||||||
| scale_score: bool = False, | ||||||||||||||||
| include_confidence: bool = False, | ||||||||||||||||
| filter_policy: FilterPolicy = FilterPolicy.REPLACE, | ||||||||||||||||
| ) -> None: | ||||||||||||||||
| """ | ||||||||||||||||
|
|
@@ -58,6 +59,10 @@ def __init__( | |||||||||||||||
| :param scale_score: | ||||||||||||||||
| When `True`, scales the score of retrieved documents to a range of 0 to 1, where 1 means extremely relevant. | ||||||||||||||||
| When `False`, uses raw similarity scores. | ||||||||||||||||
| :param include_confidence: | ||||||||||||||||
| When `True`, adds optional retrieval confidence metadata to returned documents when `scale_score` is also | ||||||||||||||||
| `True`. The metadata is exposed via `Document.meta["retrieval_confidence"]` and | ||||||||||||||||
| `Document.meta["retrieval_confidence_source"]`. | ||||||||||||||||
| :param filter_policy: The filter policy to apply during retrieval. | ||||||||||||||||
| Filter policy determines how filters are applied when retrieving documents. You can choose: | ||||||||||||||||
| - `REPLACE` (default): Overrides the initialization filters with the filters specified at runtime. | ||||||||||||||||
|
|
@@ -78,6 +83,7 @@ def __init__( | |||||||||||||||
| self.filters = filters | ||||||||||||||||
| self.top_k = top_k | ||||||||||||||||
| self.scale_score = scale_score | ||||||||||||||||
| self.include_confidence = include_confidence | ||||||||||||||||
| self.filter_policy = filter_policy | ||||||||||||||||
|
|
||||||||||||||||
| def _get_telemetry_data(self) -> dict[str, Any]: | ||||||||||||||||
|
|
@@ -99,6 +105,7 @@ def to_dict(self) -> dict[str, Any]: | |||||||||||||||
| filters=self.filters, | ||||||||||||||||
| top_k=self.top_k, | ||||||||||||||||
| scale_score=self.scale_score, | ||||||||||||||||
| include_confidence=self.include_confidence, | ||||||||||||||||
| filter_policy=self.filter_policy.value, | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -124,6 +131,7 @@ def run( | |||||||||||||||
| filters: dict[str, Any] | None = None, | ||||||||||||||||
| top_k: int | None = None, | ||||||||||||||||
| scale_score: bool | None = None, | ||||||||||||||||
| include_confidence: bool | None = None, | ||||||||||||||||
| ) -> dict[str, list[Document]]: | ||||||||||||||||
| """ | ||||||||||||||||
| Run the InMemoryBM25Retriever on the given input data. | ||||||||||||||||
|
|
@@ -137,6 +145,9 @@ def run( | |||||||||||||||
| :param scale_score: | ||||||||||||||||
| When `True`, scales the score of retrieved documents to a range of 0 to 1, where 1 means extremely relevant. | ||||||||||||||||
| When `False`, uses raw similarity scores. | ||||||||||||||||
| :param include_confidence: | ||||||||||||||||
| When `True`, adds optional retrieval confidence metadata to returned documents when `scale_score` is also | ||||||||||||||||
| `True`. When `False`, no retrieval confidence metadata is added. | ||||||||||||||||
| :returns: | ||||||||||||||||
| The retrieved documents. | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -151,8 +162,12 @@ def run( | |||||||||||||||
| top_k = self.top_k | ||||||||||||||||
| if scale_score is None: | ||||||||||||||||
| scale_score = self.scale_score | ||||||||||||||||
| if include_confidence is None: | ||||||||||||||||
| include_confidence = self.include_confidence | ||||||||||||||||
|
|
||||||||||||||||
| docs = self.document_store.bm25_retrieval(query=query, filters=filters, top_k=top_k, scale_score=scale_score) | ||||||||||||||||
| if include_confidence and scale_score: | ||||||||||||||||
| self._add_confidence_metadata(docs) | ||||||||||||||||
| return {"documents": docs} | ||||||||||||||||
|
|
||||||||||||||||
| @component.output_types(documents=list[Document]) | ||||||||||||||||
|
|
@@ -162,6 +177,7 @@ async def run_async( | |||||||||||||||
| filters: dict[str, Any] | None = None, | ||||||||||||||||
| top_k: int | None = None, | ||||||||||||||||
| scale_score: bool | None = None, | ||||||||||||||||
| include_confidence: bool | None = None, | ||||||||||||||||
| ) -> dict[str, list[Document]]: | ||||||||||||||||
| """ | ||||||||||||||||
| Run the InMemoryBM25Retriever on the given input data. | ||||||||||||||||
|
|
@@ -175,6 +191,9 @@ async def run_async( | |||||||||||||||
| :param scale_score: | ||||||||||||||||
| When `True`, scales the score of retrieved documents to a range of 0 to 1, where 1 means extremely relevant. | ||||||||||||||||
| When `False`, uses raw similarity scores. | ||||||||||||||||
| :param include_confidence: | ||||||||||||||||
| When `True`, adds optional retrieval confidence metadata to returned documents when `scale_score` is also | ||||||||||||||||
| `True`. When `False`, no retrieval confidence metadata is added. | ||||||||||||||||
|
Comment on lines
+194
to
+196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring for
Suggested change
|
||||||||||||||||
| :returns: | ||||||||||||||||
| The retrieved documents. | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -189,8 +208,20 @@ async def run_async( | |||||||||||||||
| top_k = self.top_k | ||||||||||||||||
| if scale_score is None: | ||||||||||||||||
| scale_score = self.scale_score | ||||||||||||||||
| if include_confidence is None: | ||||||||||||||||
| include_confidence = self.include_confidence | ||||||||||||||||
|
|
||||||||||||||||
| docs = await self.document_store.bm25_retrieval_async( | ||||||||||||||||
| query=query, filters=filters, top_k=top_k, scale_score=scale_score | ||||||||||||||||
| ) | ||||||||||||||||
| if include_confidence and scale_score: | ||||||||||||||||
| self._add_confidence_metadata(docs) | ||||||||||||||||
| return {"documents": docs} | ||||||||||||||||
|
|
||||||||||||||||
| @staticmethod | ||||||||||||||||
| def _add_confidence_metadata(documents: list[Document]) -> None: | ||||||||||||||||
| for document in documents: | ||||||||||||||||
| if document.score is None: | ||||||||||||||||
| continue | ||||||||||||||||
| document.meta["retrieval_confidence"] = document.score | ||||||||||||||||
| document.meta["retrieval_confidence_source"] = "bm25_scaled_score" | ||||||||||||||||
8 changes: 8 additions & 0 deletions
8
releasenotes/notes/add-bm25-retrieval-confidence-metadata-phase2.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| enhancements: | ||
| - | | ||
| Adds an optional `include_confidence` parameter to `InMemoryBM25Retriever`. | ||
| When enabled together with `scale_score=True`, returned documents now expose | ||
| BM25 retrieval confidence metadata via `Document.meta["retrieval_confidence"]` | ||
| and `Document.meta["retrieval_confidence_source"]` without changing | ||
| `Document.score` semantics. |
4 changes: 4 additions & 0 deletions
4
releasenotes/notes/add-llm-evaluator-row-statuses-78166807b18bc138.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| enhancements: | ||
| - | | ||
| Adds an ``evaluation_statuses`` output to ``LLMEvaluator`` results so each evaluated row can report whether it was successfully evaluated or failed during generation/parsing when ``raise_on_failure=False`` is used. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring for
include_confidencein therunmethod should be consistent with the one in__init__by explicitly mentioning the metadata keys. This improves clarity for users interacting with the component'srunmethod.