DEV Community

Cover image for Build RAG 10X Faster
TomoKanazawa
TomoKanazawa

Posted on

Build RAG 10X Faster

(4 min read)

1. Install CapybaraDB

pip

pip install capybaradb
Enter fullscreen mode Exit fullscreen mode

npm

npm install capybaradb
Enter fullscreen mode Exit fullscreen mode

2. Set your secrets

CAPYBARA_API_KEY="your_api_key"
CAPYBARA_PROJECT_ID="your_project_id"
Enter fullscreen mode Exit fullscreen mode

Those secrets can be found at CapybaraDB

3. Initialize the client

Javascript

import { CapybaraDB} from "capybaradb";
import dotenv from "dotenv";

dotenv.config();

const client = new CapybaraDB();
const db = client.db("test");
const collection = db.collection("test");
Enter fullscreen mode Exit fullscreen mode

Python

from capybaradb import CapybaraDB
from dotenv import load_dotenv

load_dotenv()

capybara = CapybaraDB()
db = capybara.db("test")
collection = db.collection("test")
Enter fullscreen mode Exit fullscreen mode

4. Prepare example data to save

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.

import {EmbText} from "capybaradb";
const docs = [
  {
    city: "Paris",
    bio: new EmbText(
      "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",
    description: new EmbText(
      "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",
    description: new EmbText(
      "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",
    description: new EmbText(
      "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."
    ),
  },
];
Enter fullscreen mode Exit fullscreen mode

Python

from capybaradb import EmbText
docs = [
  {
    city: "Paris",
    bio: EmbText(
      "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",
    description: EmbText(
      "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",
    description: EmbText(
      "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",
    description: EmbText(
      "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."
    ),
  },
];
Enter fullscreen mode Exit fullscreen mode

5. Save your data (No embedding needed!)

Javascript

const response = await collection.insert(docs);
Enter fullscreen mode Exit fullscreen mode

Python

response = collection.insert(docs)
Enter fullscreen mode Exit fullscreen mode

6. Search doc semantically (No embedding needed!)

Javascript

const query = "Global city iconic landmarks, cultural diversity, finance, entertainment, art"

const queryResult = await collection.query(query)
Enter fullscreen mode Exit fullscreen mode

Python

query = "Global city iconic landmarks, cultural diversity, finance, entertainment, art"

query_result = rescollection.query(query)
Enter fullscreen mode Exit fullscreen mode

Expected response

{
  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: 'bio',
      chunk_n: 0,
      score: 0.637317419,
      document: {ObjectId("6764051ace073a82cc8ab6b2")}
    },
    {
      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: 'bio',
      chunk_n: 0,
      score: 0.562242568,
      document: {ObjectId("6864051ace073a82cc8ab6de")}
    },
    {
      chunk: "The city's diverse neighborhoods, from Manhattan to Brooklyn, are home to a melting pot of cultures and cuisines.",
      path: 'bio',
      chunk_n: 1,
      score: 0.508277535,
      document: {ObjectId("7764051ace073a82cc8ab6js")}
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

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.

Also, check out CapybaraDB Docs for more details about this library.

Happy building!!

Top comments (0)