How to build production-level RAG 10X faster




💡
OneNode DB is currently Beta

Any questions đŸ‘‰đŸ»Â hello@onenode.ai (We will respond as much as possible)


Why OneNode DB?

While there are plenty of tutorials on building RAG (Retrieval-Augmented Generation) applications, many focus on vector-store-centric designs that often fall short in real-world production scenarios.

For instance, relying solely on a single vector database to store all metadata is rarely practical or scalable for production environments.

You may have tried open-source RAG frameworks, but these are often overly generalized, attempting to accommodate a wide range of use cases. This can make it unnecessarily challenging for developers to locate and implement the specific tools they need.

The founder of OneNode DB simply wanted a straightforward yet powerful AI-native database that offers both flexibility and ease of use.

TL;DR

If you are experienced engineer, you might not have to read the whole content to understand what OneNode DB does because it’s so simple. All the workload needed for prodcution RAG is simplified into one database operation.

Complete code for embedding, saving, and indexing data
Javascript
const COLLECTION_URL = "your_collection_url";
const ONENODE_API_KEY = "your_api_key";

// Insert Document URL
const url = `${COLLECTION_URL}/document`;

// Documents to insert (MongoDB Extended JSON with Object ID and EmbJSON)
const dataToSave = {
  documents: [
    {
      city: "Paris, France",
      bio: {
        "@embText": {
          text: "Known as the 'City of Light,' Paris is celebrated for its romantic ambiance, iconic landmarks like the Eiffel Tower and Notre-Dame Cathedral, and world-renowned art museums such as the Louvre. The city is a hub of fashion, cuisine, and culture, with charming cafes and picturesque streets that captivate visitors.",
        },
      },
    },
    {
      city: "Tokyo, Japan",
      description: {
        "@embText": {
          text: "A dazzling fusion of tradition and modernity, Tokyo offers skyscrapers, neon-lit streets, and centuries-old temples. The city is famous for its cutting-edge technology, vibrant pop culture, and culinary delights, including sushi and ramen. Tokyo is also a gateway to traditional Japanese customs, such as tea ceremonies and cherry blossom festivals.",
        },
      },
    },
    {
      city: "New York City, USA",
      description: {
        "@embText": {
          text: "Known as 'The Big Apple,' New York City is a global center for finance, entertainment, and art. Iconic landmarks include Times Square, the Statue of Liberty, and Central Park. The city's diverse neighborhoods, from Manhattan to Brooklyn, are home to a melting pot of cultures and cuisines.",
        },
      },
    },
    {
      city: "Rio de Janeiro, Brazil",
      description: {
        "@embText": {
          text: "Set against a backdrop of lush mountains and stunning beaches, Rio is famous for its vibrant Carnaval celebrations, samba music, and the Christ the Redeemer statue. The city offers breathtaking views from Sugarloaf Mountain and is a paradise for beach lovers at Copacabana and Ipanema.",
        },
      },
    },
  ],
};

// Headers with API Key
const headers = {
  Authorization: `Bearer ${ONENODE_API_KEY}`,
  "Content-Type": "application/json",
};

// Sending the request
fetch(url, {
  method: "POST",
  headers: headers,
  body: JSON.stringify(dataToSave),
})
  .then((response) => response.json())
  .then((data) => {
    console.log("Insert Response:", data);
  })
  .catch((error) => {
    console.error("Error inserting documents:", error);
  });
Complete code for querying
Javascript
// Hardcoded API Key and Collection URL (example usage only, not for production)
const COLLECTION_URL = "your_collection_url";
const ONENODE_API_KEY = "your_api_key";

// Query URL
const url = `${COLLECTION_URL}/document/query`;

// Query parameters
const query =
  "a city famous for its skyscrapers, diverse cultures, and Times Square.";
const topK = 3;

// Request body
const data = {
  query: query,
  top_k: topK,
};

// Headers with API Key
const headers = {
  Authorization: `Bearer ${ONENODE_API_KEY}`,
  "Content-Type": "application/json",
};

// Sending the request using fetch
async function sendRequest() {
    try {
      const response = await fetch(url, {
        method: "POST",
        headers: headers,
        body: JSON.stringify(data),
      });
  
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
  
      const responseData = await response.json();
      console.log(responseData);
    } catch (error) {
      console.error("Error querying documents:", error);
    }
  }
  
  // Usage
  sendRequest();

Before we start



  • Language: In this tutorial, we use Javascript, but you can use any programming languages because OneNode DB uses REST API.
  • Cost: we are going to use OneNode DB free tier. It doesn’t cost anything at all.

1. Sign up to OneNode DB (Free)

If you haven’t yet, sign up to OneNode DB at https://db.onenode.ai. It is free, no credit card is required.

After successful signup, you’ll be redirected to the console.

💡
Currently, we only support Gmail account for new signups.

2. Create a new collection

On the side bar, you can fild “Collection” section and “Plus Button” next to it. Push the plus button to create a new collection under a new database.

You can find the “collection” section on the dashboard's sidebar.
You can find the “collection” section on the dashboard's sidebar.



💡
Your collection will be created under a database.

Hierarchical structure of your database: your database > your collection

3. Go to your collection page to copy the collection URL

After navigating to your collection page, click on the “Connect” tab to see your collection URL.

Copy your collection URL and store it securely.


Javascript
const COLLECTION_URL = "your_collection_url"

4. Create an API key


Navigate to API Keys section from the side bar, and create a new API Key and note the newly created API key to use it later.

You can find the “API Keys” section on the dashboard's sidebar.
You can find the “API Keys” section on the dashboard's sidebar.


Store your API key securely.

Javascript
const ONENODE_API_KEY="your_api_key_here"

5. Prepare your data to save

EmbJSON

We use EmbJSON data type for embedding fields. By using EmbJSON, you can automate chunking, embedding, and indexing processes asynchronously!

Javascript
{
  "@embText": {
    text: "your text data to embed",
    emb_model: "embedding_model_to_use", // Optional: default text-embedding-3-small
  }, // No custom field is allowed inside @embText
}
💡
By default OneNode uses text-embedding-3-small for embeddings.


Example data set

You can either use your own data or use this example data set below.

Here we use a simple data set of famous cities, Paris, Tokyo, NYC, Rio de Janeiro.

You can add as many metadata as you want.

Javascript
const dataToSave = {
  documents: [
    {
      city: "Paris, France",
      bio: {
        "@embText": {
          text: "Known as the 'City of Light,' Paris is celebrated for its romantic ambiance, iconic landmarks like the Eiffel Tower and Notre-Dame Cathedral, and world-renowned art museums such as the Louvre. The city is a hub of fashion, cuisine, and culture, with charming cafes and picturesque streets that captivate visitors.",
        },
      },
    },
    {
      city: "Tokyo, Japan",
      description: {
        "@embText": {
          text: "A dazzling fusion of tradition and modernity, Tokyo offers skyscrapers, neon-lit streets, and centuries-old temples. The city is famous for its cutting-edge technology, vibrant pop culture, and culinary delights, including sushi and ramen. Tokyo is also a gateway to traditional Japanese customs, such as tea ceremonies and cherry blossom festivals.",
        },
      },
    },
    {
      city: "New York City, USA",
      description: {
        "@embText": {
          text: "Known as 'The Big Apple,' New York City is a global center for finance, entertainment, and art. Iconic landmarks include Times Square, the Statue of Liberty, and Central Park. The city's diverse neighborhoods, from Manhattan to Brooklyn, are home to a melting pot of cultures and cuisines.",
        },
      },
    },
    {
      city: "Rio de Janeiro, Brazil",
      description: {
        "@embText": {
          text: "Set against a backdrop of lush mountains and stunning beaches, Rio is famous for its vibrant Carnaval celebrations, samba music, and the Christ the Redeemer statue. The city offers breathtaking views from Sugarloaf Mountain and is a paradise for beach lovers at Copacabana and Ipanema.",
        },
      },
    },
  ],
};


6. Save your data

You can save your data by simply calling POST API calls against the following URL

Javascript
const url = `${COLLECTION_URL}/document`;


Call API agains the URL with the API key you just generated in the previous step.

Javascript
// Headers with API Key
const headers = {
  Authorization: `Bearer ${ONENODE_API_KEY}`,
  "Content-Type": "application/json",
};

// Sending the request
// Async function to send the request
const insertDocuments = async () => {
  try {
    const response = await fetch(url, {
      method: "POST",
      headers: headers,
      body: JSON.stringify(dataToSave),
    });

    const data = await response.json();
    console.log("Insert Response:", data);
  } catch (error) {
    console.error("Error inserting documents:", error);
  }
};

// Call the function
insertDocuments();


Here, we omitted error handling logics for the simplicity such as try and catch blocks, but you can add those in your production code.


Complete code for saving data

Javascript
const COLLECTION_URL = "your_collection_url";
const ONENODE_API_KEY = "your_api_key";

// Insert Document URL
const url = `${COLLECTION_URL}/document`;

// Documents to insert (MongoDB Extended JSON with Object ID and EmbJSON)
const dataToSave = {
  documents: [
    {
      city: "Paris, France",
      bio: {
        "@embText": {
          text: "Known as the 'City of Light,' Paris is celebrated for its romantic ambiance, iconic landmarks like the Eiffel Tower and Notre-Dame Cathedral, and world-renowned art museums such as the Louvre. The city is a hub of fashion, cuisine, and culture, with charming cafes and picturesque streets that captivate visitors.",
        },
      },
    },
    {
      city: "Tokyo, Japan",
      description: {
        "@embText": {
          text: "A dazzling fusion of tradition and modernity, Tokyo offers skyscrapers, neon-lit streets, and centuries-old temples. The city is famous for its cutting-edge technology, vibrant pop culture, and culinary delights, including sushi and ramen. Tokyo is also a gateway to traditional Japanese customs, such as tea ceremonies and cherry blossom festivals.",
        },
      },
    },
    {
      city: "New York City, USA",
      description: {
        "@embText": {
          text: "Known as 'The Big Apple,' New York City is a global center for finance, entertainment, and art. Iconic landmarks include Times Square, the Statue of Liberty, and Central Park. The city's diverse neighborhoods, from Manhattan to Brooklyn, are home to a melting pot of cultures and cuisines.",
        },
      },
    },
    {
      city: "Rio de Janeiro, Brazil",
      description: {
        "@embText": {
          text: "Set against a backdrop of lush mountains and stunning beaches, Rio is famous for its vibrant Carnaval celebrations, samba music, and the Christ the Redeemer statue. The city offers breathtaking views from Sugarloaf Mountain and is a paradise for beach lovers at Copacabana and Ipanema.",
        },
      },
    },
  ],
};

// Headers with API Key
const headers = {
  Authorization: `Bearer ${ONENODE_API_KEY}`,
  "Content-Type": "application/json",
};

// Sending the request
// Async function to send the request
const insertDocuments = async () => {
  try {
    const response = await fetch(url, {
      method: "POST",
      headers: headers,
      body: JSON.stringify(dataToSave),
    });

    const data = await response.json();
    console.log("Insert Response:", data);
  } catch (error) {
    console.error("Error inserting documents:", error);
  }
};

// Call the function
insertDocuments();


Execute the javascript file

File name can be anything. We use “save.js” just as an example.

Shell
node save.js

Expected response

After successful save, you should get a response that looks like this:

Shell
Insert Response: {
  inserted_ids: [
    { '$oid': '673e14296713e5fdfd801bf0' },
    { '$oid': '673e14296713e5fdfd801bf1' },
    { '$oid': '673e14296713e5fdfd801bf2' },
    { '$oid': '673e14296713e5fdfd801bf3' }
  ],
  task_id: '71a85f27-315e-4a23-94ab-bdbe8cde4035'
}
💡
‘$oid’ represents an Object ID, a specific type of BSON often used in MongoDB. If you're not familiar with MongoDB, you can simply think of it as an ID value and disregard its technical details, as it is not the primary focus of this tutorial.”

7. Retrieve relevant data

Here we’re going to see how to do semantic search to retrieve relevant data from your database.


Set query parameters

query: query text for semantic search

topK: maximum numbers of returned chunks

Javascript
// Query parameters
const query = "a city famous for its skyscrapers, diverse cultures, and Times Square.";
const topK = 3; // Optional: default 10

// Request body
const data = {
  query: query,
  top_k: topK,
};


Call API

Call POST request against query URL.

Javascript
// Query URL
const url = `${COLLECTION_URL}/document/query`;

// Headers with API Keyconst headers = {  Authorization: `Bearer ${ONENODE_API_KEY}`,  "Content-Type": "application/json",};// Sending the request using fetchasync function sendRequest() {    try {      const response = await fetch(url, {        method: "POST",        headers: headers,        body: JSON.stringify(data),      });        if (!response.ok) {        throw new Error(`HTTP error! status: ${response.status}`);      }        const responseData = await response.json();      console.log(responseData);    } catch (error) {      console.error("Error querying documents:", error);    }  }    // Usage  sendRequest();// Headers with API Key
const headers = {
  Authorization: `Bearer ${ONENODE_API_KEY}`,
  "Content-Type": "application/json",
};

// Sending the request using fetch
async function sendRequest() {
    try {
      const response = await fetch(url, {
        method: "POST",
        headers: headers,
        body: JSON.stringify(data),
      });
  
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
  
      const responseData = await response.json();
      console.log(responseData);
    } catch (error) {
      console.error("Error querying documents:", error);
    }
  }
  
  // Usage
  sendRequest();


Complete code for query

Javascript
// Hardcoded API Key and Collection URL (example usage only, not for production)
const COLLECTION_URL = "your_collection_url";
const ONENODE_API_KEY = "your_api_key";

// Query URL
const url = `${COLLECTION_URL}/document/query`;

// Query parameters
const query =
  "a city famous for its skyscrapers, diverse cultures, and Times Square.";
const topK = 3;

// Request body
const data = {
  query: query,
  top_k: topK,
};

// Headers with API Key
const headers = {
  Authorization: `Bearer ${ONENODE_API_KEY}`,
  "Content-Type": "application/json",
};

// Sending the request using fetch
async function sendRequest() {
    try {
      const response = await fetch(url, {
        method: "POST",
        headers: headers,
        body: JSON.stringify(data),
      });
  
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
  
      const responseData = await response.json();
      console.log(responseData);
    } catch (error) {
      console.error("Error querying documents:", error);
    }
  }
  
  // Usage
  sendRequest();


Example return value

You’ll see something like this as a query response.

matches is a list of matched chunks.

Shell
{
  matches: [
    {
      chunk: "Known as 'The Big Apple,' New York City is a global center for finance, entertainment, and art. Iconic landmarks include Times Square, the Statue of Liberty, and Central Park. The city's diverse",
      path: 'description',
      chunk_n: 0,
      score: 0.637317419,
      document: [Object]
    },
    {
      chunk: 'A dazzling fusion of tradition and modernity, Tokyo offers skyscrapers, neon-lit streets, and centuries-old temples. The city is famous for its cutting-edge technology, vibrant pop culture, and',
      path: 'description',
      chunk_n: 0,
      score: 0.562242568,
      document: [Object]
    },
    {
      chunk: "The city's diverse neighborhoods, from Manhattan to Brooklyn, are home to a melting pot of cultures and cuisines.",
      path: 'description',
      chunk_n: 1,
      score: 0.508277535,
      document: [Object]
    }
  ]
}


Return value fields


8. More


Now you know how to retrieve relevant chunks of your document using a simple query.

To implement the RAG feature, you can simply include these chunks in your prompt when calling LLM APIs.

While this guide doesn’t cover the specifics of using RAG prompts with LLM APIs, it’s worth noting that this step is much simpler compared to the process of implementing RAG itself. This is because retrieving relevant data accounts for 95% of the complexity in RAG.