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 try out the latest models in natural language processing (NLP), and it’s flexible to work with. Its community of users and builders has helped shape Haystack into a complete framework for building NLP apps for production. You can use the Haystack and Pinecone integration to keep your NLP-driven apps up to date, with Haystack’s indexing pipelines to help you prepare and maintain your data.

Setup guide

This guide shows how to integrate Pinecone and the Haystack library for question answering.

Install Haystack

Install the latest version of Haystack with all dependencies required for the PineconeDocumentStore.
Python

Initialize the PineconeDocumentStore

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

Prepare data

Before you add data to the document store, you must download the data and convert it into the Document format that Haystack uses. This guide uses the SQuAD dataset available from Hugging Face Datasets.
Python
Next, remove duplicates and unnecessary 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 for any additional information you can later use to apply metadata filtering in your search. Upsert the documents to Pinecone.
Python

Initialize retriever

The next step is to create embeddings from these documents. This guide uses Haystack’s EmbeddingRetriever with a SentenceTransformer model (multi-qa-MiniLM-L6-cos-v1), which is designed for question answering.
Python
Then 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

You can get documents by their ID with the PineconeDocumentStore.get_documents_by_id method.
Python
From here, you 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
This guide uses the deepset/electra-base-squad2 model from the Hugging Face model hub as the reader model.
Python
Now initialize the ExtractiveQAPipeline.
Python

Ask questions

Use your QA pipeline to start querying with pipe.run.
Python
Python
Python
You can return multiple answers by setting the top_k parameter.
Python