2020-05-18 05:47:41 -07:00
|
|
|
from haystack import Finder
|
|
|
|
|
|
|
|
|
2020-06-22 12:07:12 +02:00
|
|
|
def test_faq_retriever_in_memory_store():
|
2020-05-18 05:47:41 -07:00
|
|
|
|
|
|
|
from haystack.database.memory import InMemoryDocumentStore
|
2020-06-30 19:05:45 +02:00
|
|
|
from haystack.retriever.dense import EmbeddingRetriever
|
2020-05-18 05:47:41 -07:00
|
|
|
|
2020-06-22 12:07:12 +02:00
|
|
|
document_store = InMemoryDocumentStore(embedding_field="embedding")
|
2020-05-18 05:47:41 -07:00
|
|
|
|
|
|
|
documents = [
|
2020-07-14 09:53:31 +02:00
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'How to test this library?', 'question': 'How to test this library?'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
|
|
|
{'text': 'By running tox in the command line!', 'meta': {'name': 'blah blah blah', 'question': 'blah blah blah'}},
|
2020-05-18 05:47:41 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
retriever = EmbeddingRetriever(document_store=document_store, embedding_model="deepset/sentence_bert", gpu=False)
|
|
|
|
|
|
|
|
embedded = []
|
|
|
|
for doc in documents:
|
2020-06-30 19:05:45 +02:00
|
|
|
doc['embedding'] = retriever.embed([doc['meta']['question']])[0]
|
2020-05-18 05:47:41 -07:00
|
|
|
embedded.append(doc)
|
|
|
|
|
|
|
|
document_store.write_documents(embedded)
|
|
|
|
|
|
|
|
finder = Finder(reader=None, retriever=retriever)
|
|
|
|
prediction = finder.get_answers_via_similar_questions(question="How to test this?", top_k_retriever=1)
|
|
|
|
|
|
|
|
assert len(prediction.get('answers', [])) == 1
|