EventGet 50% off your ticket to MongoDB.local London on October 2. Use code WEB50Learn more >>
MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Building RAG Pipelines With Haystack and MongoDB Atlas

Pavel Duchovny4 min read • Published Sep 18, 2024 • Updated Sep 18, 2024
AIVector SearchPythonAtlas
SNIPPET
Facebook Icontwitter iconlinkedin icon
Rate this quickstart
star-empty
star-empty
star-empty
star-empty
star-empty
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:

Conclusion

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
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

Implementing Robust RAG Pipelines: Integrating Google's Gemma 2 (2B) Open Model, MongoDB, and LLM Evaluation Techniques


Sep 12, 2024 | 20 min read
Tutorial

Sentiment Chef Agent App with Google Cloud and MongoDB Atlas


Jun 24, 2024 | 16 min read
Article

AI Shop: The Power of LangChain, OpenAI, and MongoDB Atlas Working Together


Sep 18, 2024 | 7 min read
Tutorial

Seamless Media Storage: Integrating Azure Blob Storage and MongoDB With Spring Boot


Aug 01, 2024 | 9 min read
Table of Contents