Setup guide
This guide shows how to integrate Pinecone and the Haystack library for question answering. It uses OpenAI models to create embeddings and generate answers.Install Haystack
Install the latest version of Haystack, the Pinecone integration for Haystack, and Hugging Face Datasets.
Shell
Set your API keys
The
PineconeDocumentStore reads your Pinecone API key from the PINECONE_API_KEY environment variable, and Haystack’s OpenAI components read your OpenAI API key from OPENAI_API_KEY. Create an account to get your free Pinecone API key.Python
Initialize the PineconeDocumentStore
Initialize a
PineconeDocumentStore. If the index doesn’t exist, the document store creates a serverless index with the dimension, metric, and spec you provide. The dimension matches the text-embedding-3-small embedding model used later in this guide.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.Next, remove duplicates and unnecessary columns.This
Python
Python
Then convert these records into the Document format.
Python
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.Embed and upsert documents
Build an indexing pipeline that creates an embedding for each document with OpenAI’s
text-embedding-3-small model and writes the documents and their embeddings to Pinecone.Python
Inspect documents and embeddings
You can get documents by their metadata with the From here, you can view document content with
PineconeDocumentStore.filter_documents method.Python
d.content and the document embedding with d.embedding.Initialize a question-answering pipeline
A retrieval-augmented question-answering pipeline contains four components:
- a text embedder that creates an embedding for the question
- a retriever (
PineconeEmbeddingRetriever) that finds the most relevant documents in Pinecone - a prompt builder that adds the retrieved documents and the question to a prompt
- a generator that answers the question with an LLM
gpt-4o-mini model as the generator.Python
Ask questions
Define a helper function that runs the pipeline and prints the answer along with the title and score of each retrieved document. The Use your QA pipeline to ask a few questions:You can pass more context to the LLM by setting the
top_k parameter sets how many documents the retriever passes to the LLM.Python
Python
Response
top_k parameter.Python
Response