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
.
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
.
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.
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00, 3.71 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00, 3.78 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00, 3.88 Batches/s]
Query: Who was the first person to step foot on the moon?
Answers:
[ <Answer {
'answer': 'Armstrong', 'type': 'extractive', 'score': 0.9998227059841156,
'context': 'The trip to the Moon took just over three days. After achieving orbit, Armstrong and Aldrin transferred into the Lunar Module, named Eagle, and after ',
'offsets_in_document': [{'start': 71, 'end': 80}],
'offsets_in_context': [{'start': 71, 'end': 80}],
'document_id': 'f74e1bf667e68d72e45437a7895df921',
'meta': {
'context': 'The trip to the Moon took just over three days. After achieving orbit, Armstrong and Aldrin transferred into the Lunar Module, named Eagle, and after a landing gear inspection by Collins remaining in the Command/Service Module Columbia, began their descent. After overcoming several computer overload alarms caused by an antenna switch left in the wrong position, and a slight downrange error, Armstrong took over manual flight control at about 180 meters (590 ft), and guided the Lunar Module to a safe landing spot at 20:18:04 UTC, July 20, 1969 (3:17:04 pm CDT). The first humans on the Moon would wait another six hours before they ventured out of their craft. At 02:56 UTC, July 21 (9:56 pm CDT July 20), Armstrong became the first human to set foot on the Moon.', 'title': 'Space_Race'
}
}>, <Answer {
'answer': 'Frank Borman', 'type': 'extractive', 'score': 0.7770257890224457,
'context': 'On December 21, 1968, Frank Borman, James Lovell, and William Anders became the first humans to ride the Saturn V rocket into space on Apollo 8. They ',
'offsets_in_document': [{'start': 22, 'end': 34}],
'offsets_in_context': [{'start': 22, 'end': 34}],
'document_id': '2bc046ba90d94fe201ccde9d20552200',
'meta': {
'context': "On December 21, 1968, Frank Borman, James Lovell, and William Anders became the first humans to ride the Saturn V rocket into space on Apollo 8. They also became the first to leave low-Earth orbit and go to another celestial body, and entered lunar orbit on December 24. They made ten orbits in twenty hours, and transmitted one of the most watched TV broadcasts in history, with their Christmas Eve program from lunar orbit, that concluded with a reading from the biblical Book of Genesis. Two and a half hours after the broadcast, they fired their engine to perform the first trans-Earth injection to leave lunar orbit and return to the Earth. Apollo 8 safely landed in the Pacific ocean on December 27, in NASA's first dawn splashdown and recovery.", 'title': 'Space_Race'
}
}>, <Answer {
'answer': 'Aldrin', 'type': 'extractive', 'score': 0.6680101901292801,
'context': ' were, "That\'s one small step for [a] man, one giant leap for mankind." Aldrin joined him on the surface almost 20 minutes later. Altogether, they spe',
'offsets_in_document': [{'start': 240, 'end': 246}],
'offsets_in_context': [{'start': 72, 'end': 78}],
'document_id': 'ae1c366b1eaf5fc9d32a8d81f76bd795',
'meta': {
'context': 'The first step was witnessed by at least one-fifth of the population of Earth, or about 723 million people. His first words when he stepped off the LM\'s landing footpad were, "That\'s one small step for [a] man, one giant leap for mankind." Aldrin joined him on the surface almost 20 minutes later. Altogether, they spent just under two and one-quarter hours outside their craft. The next day, they performed the first launch from another celestial body, and rendezvoused back with Columbia.', 'title': 'Space_Race'
}
}>
]