haystack icon indicating copy to clipboard operation
haystack copied to clipboard

Load pre-computed embedding into document store object

Open jqJordan opened this issue 5 years ago • 2 comments

Question I may miss something here. Is there a way to directly load pre-computed embedding(e.g. DPR on Wikipedia dump) into the document store object? I see FAISSDocumentStore has two components, documents, and their embeddings. It seems that all tutorials first write the document with write_documents(dicts), and then call update_embeddings(retriever). But if I already have the pre-computed embedding in a pickle file for each document, how can I load the document and embedding together. Thank you for this amazing framework.

jqJordan avatar May 23 '21 08:05 jqJordan

Hey @jqJordan ,

Yep, that's absolutely possible. Just put your embedding in the dict as a numpy array with the key embedding. It will then be converted to Haystack's Document object where the embedding is one of the attributes: https://github.com/deepset-ai/haystack/blob/4ab1bc3c3ec0f7f2bb86625e4a71b231c034f5dd/haystack/schema.py#L41

tholor avatar May 23 '21 08:05 tholor

Thank you so much for the speedy response. I did a small test and it worked very well.

documents = [{"text": "abc", "meta": {"author": "a"}, 'embedding':np.array([0.1,0.2,0.3])}, {"text": "xyz", "meta": {"author": "b"}, 'embedding':np.array([1.1,1.2,1.3])}]
test = FAISSDocumentStore(vector_dim=3)
test.write_documents(documents)
test.get_all_documents(return_embedding=True)

jqJordan avatar May 23 '21 08:05 jqJordan