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. It uses OpenAI models to create embeddings and generate answers.
1

Install Haystack

Install the latest version of Haystack, the Pinecone integration for Haystack, and Hugging Face Datasets.
Shell
2

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
3

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
4

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

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
6

Inspect documents and embeddings

You can get documents by their metadata with the PineconeDocumentStore.filter_documents method.
Python
From here, you can view document content with d.content and the document embedding with d.embedding.
7

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
This guide uses OpenAI’s gpt-4o-mini model as the generator.
Python
8

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 top_k parameter sets how many documents the retriever passes to the LLM.
Python
Use your QA pipeline to ask a few questions:
Python
Response
You can pass more context to the LLM by setting the top_k parameter.
Python
Response
9

Clean up

When you’re finished with the index, delete it.
Python