BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI reportLearn more >>
MongoDB Developer
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right

Building RAG Pipelines With Haystack and MongoDB Atlas

Pavel Duchovny4 min read • Published Jun 13, 2024 • Updated Jun 13, 2024
Facebook Icontwitter iconlinkedin icon
Rate this quickstart
Integrating Haystack with MongoDB Atlas allows you to build powerful retrieval-augmented generation (RAG) pipelines. This introductory article will guide you through the process of setting up a Haystack-based RAG pipeline using MongoDB Atlas for vector search. Our code will use a grocery product dataset and the RAG pipeline can fetch relevant products for a user cooking request. Relevant groceries are passed to the LLM for a detailed generated guide.
All code presented in this tutorial is available in the GitHub repository.

Step 1: Install dependencies

First, install the necessary dependencies:

Step 2: Set up MongoDB Atlas connection and OpenAI API key

If you have not created an Atlas cluster, follow our guide. Set the MongoDB connection string and OpenAI API key by following the guide on Open AI website.

Step 3: Create a Vector Search index on collection

Create a vector index on your database and collection in MongoDB Atlas. For more information and guidance, visit our Atlas Vector Search index docs. In this tutorial, the database is “ai_shop,”, and the collection name is “test_collection.”. Ensure that the index name is vector_index and specify the following syntax:

Step 4: Set up vector store and load documents

Load documents into MongoDB Atlas using the Haystack framework:

Step 5: Build a RAG pipeline

Create a pipeline that will retrieve, augment, and generate a response to user questions:

Step 6: Test the pipeline

Test the pipeline with a sample query:
Expected output:


In this article, you learned how to integrate Haystack with MongoDB Atlas to build a RAG pipeline. This powerful combination allows you to leverage vector search and retrieval-augmented generation to create sophisticated and responsive applications.
To explore more topics on RAG, have a look at the following tutorials:
If you have questions or want to connect with other developers, join us in the MongoDB Developer Community. Thanks for reading.

Facebook Icontwitter iconlinkedin icon
Rate this quickstart

Coronavirus Map and Live Data Tracker with MongoDB Charts

Nov 15, 2023 | 3 min read

A Free REST API for Johns Hopkins University COVID-19 dataset

Nov 16, 2023 | 5 min read

Build Your Own Function Retry Mechanism with Realm

Jul 19, 2022 | 4 min read

Serverless Development with AWS Lambda and MongoDB Atlas Using Java

Jul 20, 2023 | 6 min read
Table of Contents