Skip to main content
Haystack is the open source Python framework by Deepset for building custom apps with large language models (LLMs). It lets you quickly try out the latest models in natural language processing (NLP) while being flexible and easy to use. Their community of users and builders has helped shape Haystack into what it is today: a complete framework for building production-ready NLP apps. Haystack and Pinecone integration can be used to keep your NLP-driven apps up-to-date with Haystack’s indexing pipelines that help you prepare and maintain your data.

Setup guide

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.
Python

Initialize the PineconeDocumentStore

We initialize a PineconeDocumentStore by providing an API key and environment name. Create an account to get your free API key.
Python

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.
Python
Next, we remove duplicates and unecessary columns.
Python
Then convert these records into the Document format.
Python
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.
Python

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.
Python
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.
Python

Inspect documents and embeddings

We can get documents by their ID with the PineconeDocumentStore.get_documents_by_id method.
Python
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.
Python
We are now ready to initialize the ExtractiveQAPipeline.
Python

Ask Questions

Using our QA pipeline we can begin querying with pipe.run.
Python
Python
Python
We can return multiple answers by setting the top_k parameter.
Python