Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: BM25 Retriever - Corpus uses default MetadataMode while reading content from nodes instead of MetadataMode.EMBED or user provided option #13361

Open
gopalgk opened this issue May 8, 2024 · 1 comment
Labels
bug Something isn't working P1

Comments

@gopalgk
Copy link

gopalgk commented May 8, 2024

Bug Description

In BM25 retriever the corpus is built from nodes using the default argument to get_content() as shown below.

self._corpus = [self._tokenizer(node.get_content()) for node in self._nodes]

But, if the nodes have a specific metadata to include/exclude while searching then we cannot enable it. When we build hybrid retriever (using vector index and bm25 retrievers) the content that gets passed to vector retriever will be different than bm25 retriever. Since it is expected for retrieval we should use MetadataMode.EMBED then the same has to be passed for bm25 corpus generation as well (below).

self._corpus = [self._tokenizer(node.get_content(metadata_mode=MetadataMode.EMBED)) for node in self._nodes]

Version

v0.10.35

Steps to Reproduce

Use this notebook and add metadata fields to the nodes. Set excluded_embed_metadata_keys in the nodes to []. Embedded search will use different content than bm25 search.

https://colab.research.google.com/github/jerryjliu/llama_index/blob/main/docs/docs/examples/retrievers/bm25_retriever.ipynb

Relevant Logs/Tracbacks

No response

@gopalgk gopalgk added bug Something isn't working triage Issue needs to be triaged/prioritized labels May 8, 2024
@gopalgk gopalgk changed the title [Bug]: Corpus uses default MetadataMode while reading content from nodes instead of MetadataMode.EMBED or user provided option [Bug]: BM25 Retriever - Corpus uses default MetadataMode while reading content from nodes instead of MetadataMode.EMBED or user provided option May 8, 2024
Copy link

dosubot bot commented May 8, 2024

To address the issue where the BM25 Retriever does not utilize MetadataMode.EMBED or a user-provided option for metadata handling, you can extend the BM25Retriever class to support metadata. This involves modifying the retriever to include an additional parameter for metadata handling and ensuring that both the BM25 and vector index retrievers handle metadata consistently.

Here's a concise approach to modify the BM25Retriever:

  1. Extend BM25Retriever: Add a use_metadata boolean flag to indicate whether to use metadata.
  2. Uniform Metadata Handling: Ensure the vector index retriever also supports metadata in a similar manner.
  3. Metadata Preprocessing: Implement a step to combine text with metadata before indexing and retrieval.
  4. Query Expansion: Incorporate metadata into queries for consistent retrieval.
  5. Custom Retrieval Logic: Adjust the retrieval methods to consider metadata in scoring.
  6. Hybrid Model Integration: Ensure metadata handling is consistent across both retrievers.

Example modification for BM25Retriever:

class BM25Retriever(BaseRetriever):
    def __init__(
        self,
        use_metadata: bool = False,  # New parameter
        # other parameters
    ) -> None:
        self._use_metadata = use_metadata
        # Initialization code
        self._corpus = [self._build_corpus_entry(node) for node in self._nodes]
        # Additional initialization

    def _build_corpus_entry(self, node: BaseNode) -> List[str]:
        content = self._tokenizer(node.get_content())
        if self._use_metadata and node.metadata:
            metadata_content = ' '.join([f"{k}:{v}" for k, v in node.metadata.items()])
            content += self._tokenizer(metadata_content)
        return content

This approach ensures that metadata is consistently handled across both the BM25 and vector index retrievers, improving the accuracy and relevance of search results in a hybrid retrieval model.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

@logan-markewich logan-markewich added P1 and removed triage Issue needs to be triaged/prioritized labels May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P1
Projects
None yet
Development

No branches or pull requests

2 participants