WritingFireworks AIFireworks AIpublished Aug 11, 2026seen Jun 26

Optimize Rag With Mongodb Atlas And Fireworks

Open original ↗

Captured source

source ↗
published Aug 11, 2026seen Jun 26captured Jun 27http 200method plain

Optimizing Retrieval Augmented Generation (RAG) with MongoDB Atlas and Fireworks AI

GLM 5.2 is live! Opus-level intelligence at open-source rates. Pay per token on serverless. Try it today.

Blog

Optimize RAG With Mongodb Atlas And Fireworks Optimizing Retrieval Augmented Generation (RAG) with MongoDB Atlas and Fireworks AI

PUBLISHED 3/21/2024

Table of Contents What is RAG? Why RAG? RAG Architecture Optimizing RAG architecture Prerequisites Configuring your environment Gathering Credentials

MongoDB Atlas

Fireworks Initializing Fireworks and MongoDB Clients Using Fireworks with OSS Embedding Models Generating embeddings Creating a Index on MongoDB Collection Generating personalized recommendations with Fireworks

Querying the Recommender System

Generating Recommendations What’s next?

Table of Contents

RAG is all the rage now! Haven’t heard about it? In this blog, we precisely help you kickstart your Generative AI Application development Journey and how to build a Retrieval Augmented Generation (RAG) App using MongoDB Atlas and Fireworks AI. Further we’ll discuss how to optimize the architecture to achieve better cost and performance. In a post-ChatGPT world, hearing about a new AI advancement or a Large Language Model (LLM) as a developer is as common as a new Javascript framework. Playing around with LLMs is fun, but creating AI-enabled experiences is the real deal in skill building for any developer. Building apps or experiences on pre-trained LLMs has limitations. GPT, Claude, Llama and Mixtral have their knowledge/learning cutoff at a specific date. Methods to add custom knowledge, like fine-tuning, also have restrictions, like cost and knowledge limits that are restricted to training data. MongoDB and Fireworks have partnered together to help enterprises build the next generation of scalable, secure cost-effective RAG applications grounded in their operational data. What is RAG?

Retrieval Augmented Generation (RAG) combines best of both worlds by leveraging a retrieval component to fetch relevant information from a database (or a vector store) and a generative component (LLM) to synthesize and generate a coherent response to the user query. Through a RAG Architecture, LLMs get a second brain and the ability to fetch relevant and up-to-date info and turn the LLM into a real-time response generation engine that is grounded in your own data. Why RAG?

Some more reasons that make a RAG App special are: Data Efficiency: RAG is data-efficient because it dynamically pulls in relevant data or information that may not have been seen during training. This saves time, effort and money compared to more data-hungry solutions like fine-tuning, which also often demand specialist, hard to find skills. Flexibility: RAG enables dynamic updating of underlying knowledge bases or documents, making it easier to maintain the model without regular retraining. It is beneficial when the domain information is changing frequently, like with stock prices, weather etc. RAG Architecture

A RAG Architecture consists of a Large Language Model to synthesize and submit the query to a data store (also can be called a vector store). The vector store then returns the relevant vector chunks as response to the LLM’s initial request. The LLM absorbs the response into its context and generates a response relevant to the user’s query.

As mentioned in the beginning of the blog, we’ll create a RAG-based app which recommends movies based on the user’s query. We are going to build a model to index and retrieve movie recommendations. The example will be built on top of MongoDB and Fireworks AI and involves: • MongoDB Atlas Database that indexes movies using embeddings. ( Vector Store ) • A system for document embedding generation. We'll use the Fireworks embedding API to create embeddings from text data. ( Vectorisation ) • MongoDB Atlas Vector Search that responds to user queries by converting the query to an embedding, fetching the corresponding movies. ( Retrieval Engine ) • The Mixtral model using the Fireworks inference API to generate the recommendations. You can also use Llama, Gemma, and other great OSS models if you like. ( LLM) • Loading MongoDB Atlas Sample Mflix Dataset to generate embeddings ( Dataset )

💡Note: You can further learn more about optimizing RAG architecture. We have some helpful tips to reduce costs, improve throughput, add batching, and introduce function calling. These options help customize and scale your RAG architecture to suit your specific needs. Optimizing RAG architecture

While this tutorial focuses on building a basic RAG Pipeline, we have guides to build optimized RAG architectures that can be further customized and scaled to suit various needs. For example: • Reduce the cost: Fireworks provides a range of embedding models with advanced capabilities. You can reduce the size of the embeddings, without a significant drop in retrieval performance, leading to reduced downstream costs associated with storing and retrieving embeddings. Improve the throughput: We are only documenting 400 movies in this example, which is not a lot. This is because we wanted to keep this tutorial simple and not batching the embedding lookups, and just have a for loop that goes through all the documents and embed them manually. This method does not scale. First, we will cover basic batching in the following guide . • Tap into the rich AI ecosystem: MongoDB and Fireworks work great with the various tools and frameworks you may be already using. Food for thought! There are a lot of great frameworks that offer batching out of the box, and please check out our guides here for LlamaIndex and LangChain .

Prerequisites

• MongoDB Atlas Account • Fireworks AI Account

Note: You can follow the tutorial using the Notebook Configuring your environment

Before we dive into the code, make sure to set up your environment. This involves installing necessary packages like pymongo , fireworks-ai and openai . 1 2 !pip install -q pymongo fireworks-ai tqdm openai

Note: We use the OpenAI Python SDK because it’s compatible with the Fireworks SDK Gathering Credentials

To interact with Fireworks AI and MongoDB Atlas Cluster, we need to initialize their respective clients. Replace "FIREWORKS_API_KEY" and "MONGODB_URI" with your actual credentials. MongoDB Atlas

You can create and pick up the MongoDB URI from the MongoDB Atlas Cluster following the steps below.

Fireworks

After creating your account at...

Excerpt shown — open the source for the full document.

Notability

notability 5.0/10

Substantive technical blog post on RAG integration, not a model release.