Haystack
In this guide we will see how to integrate Pinecone and the popular Haystack library for Question-Answering.
Install Haystack
We start by installing the latest version of Haystack with all dependencies required for the PineconeDocumentStore
.
Initialize the PineconeDocumentStore
We initialize a PineconeDocumentStore
by providing an API key and environment name. Create an account to get your free API key.
Prepare data
Before adding data to the document store, we must download and convert data into the Document format that Haystack uses.
We will use the SQuAD dataset available from Hugging Face Datasets.
Next, we remove duplicates and unecessary columns.
title | context | |
---|---|---|
0 | University_of_Notre_Dame | Architecturally, the school has a Catholic cha… |
5 | University_of_Notre_Dame | As at most other universities, Notre Dame’s st… |
10 | University_of_Notre_Dame | The university is the major seat of the Congre… |
15 | University_of_Notre_Dame | The College of Engineering was established in … |
20 | University_of_Notre_Dame | All of Notre Dame’s undergraduate students are… |
Then convert these records into the Document format.
This Document
format contains two fields; ‘content’ for the text content or paragraphs, and ‘meta’ where we can place any additional information that can later be used to apply metadata filtering in our search.
Now we upsert the documents to Pinecone.
Initialize retriever
The next step is to create embeddings from these documents. We will use Haystacks EmbeddingRetriever
with a SentenceTransformer model (multi-qa-MiniLM-L6-cos-v1
) which has been designed for question-answering.
Then we run the PineconeDocumentStore.update_embeddings
method with the retriever
provided as an argument. GPU acceleration can greatly reduce the time required for this step.
Inspect documents and embeddings
We can get documents by their ID with the PineconeDocumentStore.get_documents_by_id
method.
From here we return can view document content with d.content
and the document embedding with d.embedding
.
Initialize an extractive QA pipeline
An ExtractiveQAPipeline
contains three key components by default:
- a document store (
PineconeDocumentStore
) - a retriever model
- a reader model
We use the deepset/electra-base-squad2
model from the HuggingFace model hub as our reader model.
We are now ready to initialize the ExtractiveQAPipeline
.
Ask Questions
Using our QA pipeline we can begin querying with pipe.run
.
We can return multiple answers by setting the top_k
parameter.
Was this page helpful?