RagLeap
Blog

How to use @ragleap/rag in Node.js: sanitize text, chunk it and rerank results (early TypeScript port)

Short answer: install @ragleap/rag with npm (Node.js 22 or later), run your text through sanitize_text, split it with TextChunker, and, if you want, reorder retrieved chunks with the optional RerankerService. It is an early TypeScript port: parity with the Python ragleap-rag has not been checked, so treat it as a toolbox to test, not a drop-in replacement.

What do you need?

  • Node.js 22 or later.
  • PostgreSQL with pgvector, only for the PostgreSQL-backed features.
  • API credentials for whichever embedding or generation providers you use.

How do you install it?

npm install @ragleap/rag

How do you sanitize and chunk text?

This is the README example, copied unchanged:

import {
  TextChunker,
  sanitize_text,
  detect_injection_risk,
} from "@ragleap/rag";

const text = sanitize_text("Example document text");
const chunker = new TextChunker();
const chunks = chunker.chunk_text(text);

console.log(chunks);
console.log(detect_injection_risk(text));

It sanitizes the text, splits it into chunks and prints the chunks and the result of the prompt-injection check. The README does not document the return values, so read the exported TypeScript declarations before you rely on them.

How do you rerank retrieved chunks?

RerankerService reorders retrieved chunks by how relevant they are to a query. The Transformers.js runtime is optional; install it only if you use the reranker:

npm install @ragleap/rag @huggingface/transformers

Example from the README, also copied unchanged:

import { RerankerService } from "@ragleap/rag";

const reranker = new RerankerService();

const candidates = [
  { text: "Paris is the capital of France." },
  { text: "Bananas contain potassium." },
  { text: "France is a country in Europe." },
];

const ranked = await reranker.rerank(
  "What is the capital of France?",
  candidates,
  2,
);

console.log(ranked);

The default model is Xenova/ms-marco-MiniLM-L-6-v2. Model loading is lazy: the first reranking call may download model files, so it needs network access and disk space. The scores are raw model scores (logits), not probabilities or percentages. Results are ordered from highest to lowest score, and top_k limits how many come back. We have not run these examples for this post.

What else is in the package?

The README lists document ingestion, embeddings, answer generation, vector search, PostgreSQL and pgvector, conversation memory, structured-output validation, caching and observability hooks. The package also exports an IngestionService, a RagLeap class, a QueryEmbeddingCache, token cost tracking and query helpers such as reciprocal_rank_fusion, multi_query_variants, hyde_document and contextual_rewrite. The README gives no further examples and points to the exported declarations and the source for the current API.

What has and has not been checked?

  • 158 test() calls across 20 test files (run with node --test), counted on the main branch. The pull request that added the source reported 158 tests passing on a fresh install and build under Node 24, and a fresh build matching the compiled files of the published 0.2.0 (source maps not compared).
  • Not checked: parity with the Python or Java ragleap-rag, missing features and behavioural differences, any comparison against recordings of the real Python code, mutation checks, and any live provider or database run.
  • 0.2.0 was published by hand without provenance. There is no tag or release for this folder yet, and no JavaScript job in CI yet.

Related

The @ragleap/rag library page, its test list, the Python ragleap-rag how-to and the npm page.

Back to the blog