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

feat: add support for BM25Retriever in InMemoryDocumentStore #3561

Merged
merged 31 commits into from
Nov 22, 2022
Merged

feat: add support for BM25Retriever in InMemoryDocumentStore #3561

merged 31 commits into from
Nov 22, 2022

Conversation

anakin87
Copy link
Member

@anakin87 anakin87 commented Nov 12, 2022

Related Issues

Only a first draft...

Checklist

@anakin87
Copy link
Member Author

anakin87 commented Nov 13, 2022

Proposed Changes:

As discussed in #3447, I made the InMemoryDocumentStore optionally store a BM25 sparse representation for each index; this representation is based on the simple library rank_bm25.

To make the InMemoryDocumentStore accept queries from the BM25Retriever, I changed the DS: now it is a subclass of KeywordDocumentStore (instead of BaseDocumentStore) and implement the methods query and query_batch.

How did you test it?

As you can see from this notebook, this implementation works fine for Haystack Tutorial 1.

For more proper tests, I need help:

  • probably it is possible to test this new behavior of the DS with some slight changes in test_retriever.py, but I'm struggling to understand how
  • any ideas on unit tests to add?

@ZanSara feel free to jump in! 🙂

@anakin87 anakin87 marked this pull request as ready for review November 13, 2022 18:56
@anakin87 anakin87 requested a review from a team as a code owner November 13, 2022 18:56
@anakin87 anakin87 requested review from bogdankostic and removed request for a team November 13, 2022 18:56
@anakin87 anakin87 marked this pull request as draft November 13, 2022 19:05
@anakin87 anakin87 marked this pull request as ready for review November 13, 2022 19:05
Copy link
Contributor

@ZanSara ZanSara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! Thank you for the great work! 🚀

Regarding tests, I believe they should come from two angles:

  • First we need unit tests as part of the document store tests. I'm sure there are no specific InMemoryDocumentStore tests now, but please add a few in the main docstore tests test_document_store.py, and parametrize them to run on memory only. @masci will take care of them shortly after we merge this PR

  • Then we can test them from BM25Retriever side. I think it's sufficient to add (elasticsearch, memory) to this fixture: https://github.com/deepset-ai/haystack/blob/main/test/nodes/test_retriever.py#L33-L53

    • Incidentally this would be a good time to fix the parametrization of these tests and replace elasticsearch with bm25 to represent BM25Retriever in tests. The switch needs to be done:

haystack/document_stores/memory.py Outdated Show resolved Hide resolved
haystack/document_stores/memory.py Show resolved Hide resolved
haystack/document_stores/memory.py Outdated Show resolved Hide resolved
haystack/document_stores/memory.py Show resolved Hide resolved
haystack/document_stores/memory.py Outdated Show resolved Hide resolved
haystack/document_stores/memory.py Outdated Show resolved Hide resolved
@anakin87
Copy link
Member Author

anakin87 commented Nov 14, 2022

  • First we need unit tests as part of the document store tests. I'm sure there are no specific InMemoryDocumentStore tests now, but please add a few in the main docstore tests test_document_store.py, and parametrize them to run on memory only. @masci will take care of them shortly after we merge this PR

I added some document store tests. As usual, there is room for improvement... @ZanSara 😃

  • I tried to replace elasticsearch with bm25 for BM25Retriever. Please check if everything is OK
  • I added (bm25, memory_bm25) to the fixture (and in conftest) in order to test separately sparse and dense retrieval for InMemoryDocumentStore. It seems to work...

Copy link
Contributor

@ZanSara ZanSara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! Thank you for taking care of the fixture renaming. There's one last thing that we should probably fix and then it can be merged 😊

test/nodes/test_retriever.py Outdated Show resolved Hide resolved
test/conftest.py Outdated Show resolved Hide resolved
test/document_stores/test_document_store.py Outdated Show resolved Hide resolved
@anakin87 anakin87 marked this pull request as draft November 17, 2022 16:23
@anakin87 anakin87 marked this pull request as ready for review November 17, 2022 16:24
@anakin87
Copy link
Member Author

@ZanSara the CI is failing in a strange way:
AttributeError: module 'faiss' has no attribute 'swigfaiss'.

Any ideas?

@julian-risch
Copy link
Member

@ZanSara the CI is failing in a strange way: AttributeError: module 'faiss' has no attribute 'swigfaiss'.

Any ideas?

Hi @anakin87 we will pin faiss-cpu with the following PR: #3603
There was a new faiss-cpu release that seems to be causing the problems.

@anakin87 anakin87 requested a review from ZanSara November 18, 2022 14:28
Copy link
Contributor

@ZanSara ZanSara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to hold this one again but indeed you found a super valid corner case by setting use_bm25=True as default in tests. Good to see the test suite being really useful for once 😄

test/nodes/test_retriever.py Show resolved Hide resolved
@anakin87 anakin87 requested a review from ZanSara November 21, 2022 19:23
Copy link
Contributor

@ZanSara ZanSara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thank you so much! 🚀

@ZanSara
Copy link
Contributor

ZanSara commented Nov 22, 2022

@deepset-ai/documentation Let's update the docs to reflect this change

@ZanSara ZanSara requested a review from a team November 22, 2022 08:27
@anakin87 anakin87 deleted the imds_support_for_bm25 branch November 22, 2022 08:34
ZanSara added a commit that referenced this pull request Nov 28, 2022
* Fix docstrings for DocumentStores

* Fix docstrings for AnswerGenerator

* Fix docstrings for Connector

* Fix docstrings for DocumentClassifier

* Fix docstrings for LabelGenerator

* Fix docstrings for QueryClassifier

* Fix docstrings for Ranker

* Fix docstrings for Retriever and Summarizer

* Fix docstrings for Translator

* Fix docstrings for Pipelines

* Fix docstrings for Primitives

* Fix Python code block spacing

* Add line break before code block

* Fix code blocks

* fix: discard metadata fields if not set in Weaviate (#3578)

* fix weaviate bug in returning embeddings and setting empty meta fields

* review comment

* Update unstable version and openapi schema (#3584)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* fix: Flatten `DocumentClassifier` output in `SQLDocumentStore`; remove `_sql_session_rollback` hack in tests (#3273)

* first draft

* fix

* fix

* move test to test_sql

* test: add test to check id_hash_keys is not ignored (#3577)

* refactor: Generate JSON schema when missing (#3533)

* removed unused script

* print info logs when generating openapi schema

* create json schema only when needed

* fix tests

* Remove leftover

Co-authored-by: ZanSara <sarazanzo94@gmail.com>

* move milvus tests to their own module (#3596)

* feat: store metadata using JSON in SQLDocumentStore (#3547)

* add warnings

* make the field cachable

* review comment

* Pin faiss-cpu as 1.7.3 seems to have problems (#3603)

* Update Haystack imports (#3599)

* Update Python version (#3602)

* fix: `ParsrConverter` fails on pages without text (#3605)

* try to fix bug

* remove print

* leftover

* refactor: update Squad data  (#3513)

* refractor the to_squad data class

* fix the validation label

* refractor the to_squad data class

* fix the validation label

* add the test for the to_label object function

* fix the tests for to_label_objects

* move all the test related to squad data to one file

* remove unused imports

* revert tiny_augmented.json

Co-authored-by: ZanSara <sarazanzo94@gmail.com>

* Url fixes (#3592)

* add 2 example scripts

* fixing faq script

* fixing some urls

* removing example scripts

* black reformatting

* add labeler to the repo (#3609)

* convert eval metrics to python float (#3612)

* feat: add support for `BM25Retriever` in `InMemoryDocumentStore` (#3561)

* very first draft

* implement query and query_batch

* add more bm25 parameters

* add rank_bm25 dependency

* fix mypy

* remove tokenizer callable parameter

* remove unused import

* only json serializable attributes

* try to fix: pylint too-many-public-methods / R0904

* bm25 attribute always present

* convert errors into warnings to make the tutorial 1 work

* add docstrings; tests

* try to make tests run

* better docstrings; revert not running tests

* some suggestions from review

* rename elasticsearch retriever as bm25 in tests; try to test memory_bm25

* exclude tests with filters

* change elasticsearch to bm25 retriever in test_summarizer

* add tests

* try to improve tests

* better type hint

* adapt test_table_text_retriever_embedding

* handle non-textual docs

* query only textual documents

* Incorporate Reviewer feedback

* refactor: replace `torch.no_grad` with `torch.inference_mode` (where possible) (#3601)

* try to replace torch.no_grad

* revert erroneous change

* revert other module breaking

* revert training/base

* Fix docstrings for DocumentStores

* Fix docstrings for AnswerGenerator

* Fix docstrings for Connector

* Fix docstrings for DocumentClassifier

* Fix docstrings for LabelGenerator

* Fix docstrings for QueryClassifier

* Fix docstrings for Ranker

* Fix docstrings for Retriever and Summarizer

* Fix docstrings for Translator

* Fix docstrings for Pipelines

* Fix docstrings for Primitives

* Fix Python code block spacing

* Add line break before code block

* Fix code blocks

* Incorporate Reviewer feedback

Co-authored-by: Massimiliano Pippi <mpippi@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Stefano Fiorucci <44616784+anakin87@users.noreply.github.com>
Co-authored-by: Julian Risch <julian.risch@deepset.ai>
Co-authored-by: ZanSara <sarazanzo94@gmail.com>
Co-authored-by: Espoir Murhabazi <espoir.mur@gmail.com>
Co-authored-by: Tuana Celik <tuana.celik@deepset.ai>
Co-authored-by: tstadel <60758086+tstadel@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for BM25Retriever in InMemoryDocumentStore
3 participants