npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

rag-api-kit

v1.0.0

Published

Backend toolkit for building secure, scalable Retrieval-Augmented Generation (RAG) APIs

Readme

rag-api-kit

Production-grade backend toolkit for building secure, scalable Retrieval-Augmented Generation (RAG) APIs.

  • TypeScript — Written in TypeScript with full typings.
  • Modular — Embedding, vector store, cache, and security are pluggable.
  • DI-friendly — Inject your own providers via interfaces.
  • Production-oriented — Error handling, metrics, no demo shortcuts.

Install

npm install rag-api-kit

Peer / runtime: openai and ioredis (only if using Redis cache). Install them in your app:

npm install openai ioredis

Quick start

import { createRagKit } from "rag-api-kit";

const rag = createRagKit({
  embedding: { provider: "openai", apiKey: process.env.OPENAI_API_KEY! },
  vectorStore: { type: "memory" },
  retrieval: { topK: 5, minScore: 0.7 },
  chunk: { size: 100, overlap: 20 },
  security: { strict: true },
});

await rag.ingest([
  "Your document text here.",
  { text: "Another doc.", metadata: { source: "api" } },
]);

const result = await rag.ask("Your question?");
console.log(result.answer);      // Assembled context
console.log(result.context);     // Ranked chunks
console.log(result.metrics);     // retrievalTimeMs, cacheHits, etc.

Configuration

| Key | Required | Description | |-----|----------|-------------| | embedding | Yes* | { provider: "openai", apiKey, model? } or supply embeddingProvider | | vectorStore | Yes | { type: "memory" } or inject a VectorStore | | retrieval | Yes | { topK, minScore?, maxContextTokens? } | | chunk | Yes | { size, overlap } (token-based chunking) | | cache | No | { redisUrl, ttlSeconds?, similarityThreshold? } for Redis semantic cache | | security | No | { strict: true } enables prompt-injection filter |

* Either embedding (with provider: "openai") or embeddingProvider must be provided.

Features

Auto chunking

  • Token-based chunking with configurable size and overlap.
  • Metadata is preserved on each chunk.
import { createChunker } from "rag-api-kit";

const chunker = createChunker({ size: 100, overlap: 20 });
const chunks = chunker.chunk(["Long document...", { text: "Doc with meta.", metadata: { id: 1 } }]);

Embedding wrapper

  • Interface: EmbeddingProvider (embed, embedBatch, dimensions).
  • Built-in: OpenAIEmbeddingProvider (OpenAI embeddings).
  • You can implement the interface for other providers.

Vector store

  • Interface: VectorStore (add, search, clear, size).
  • Built-in: in-memory store with cosine similarity, topK, minScore, and metadata filters.

Redis semantic cache (optional)

  • Cache by embedding similarity: similar queries return cached response.
  • Configurable TTL and similarity threshold.
  • Enable by passing cache: { redisUrl, ttlSeconds?, similarityThreshold? }.

Prompt injection filter

  • Detects patterns such as “ignore previous instructions”, “reveal system prompt”, “act as system”, and prompt override attempts.
  • Configurable strict mode; returns structured SecurityError with reason and pattern.
import { createPromptInjectionFilter } from "rag-api-kit";

const filter = createPromptInjectionFilter({ strict: true });
const result = filter.check(userInput);
if (!result.safe) throw new SecurityError(result.reason!, result.pattern, result.reason);

Retriever pipeline

  • Embeds query → searches vector store → assembles context → enforces maxContextTokens when set.
  • Returns ranked chunks (scores from cosine similarity).

Metrics

  • result.metrics: retrievalTimeMs, embeddingTimeMs, totalTimeMs, cacheHits, cacheMisses.
  • rag.stats(): documentCount, chunkCount, cacheHits, cacheMisses.

API summary

  • new RagKit(config) / createRagKit(config) — Build the RAG pipeline.
  • ingest(documents) — Chunk, embed, and store. documents: string[] or { text, metadata? }[].
  • ask(query, options?) — Security check → optional cache → retrieve → return { answer, context, fromCache, metrics }. options: { skipCache?, skipSecurityCheck? }.
  • stats(){ documentCount, chunkCount, cacheHits, cacheMisses }.
  • clearCache() — Clears semantic cache and resets metrics.

Dependency injection

You can inject your own implementations:

createRagKit({
  embedding: { provider: "openai", apiKey: "" },
  embeddingProvider: myEmbeddingProvider,
  vectorStore: myVectorStore,
  retrieval: { topK: 5 },
  chunk: { size: 50, overlap: 10 },
  security: { strict: true },
});

Implement:

  • EmbeddingProviderembed(text), embedBatch(texts), dimensions().
  • VectorStoreadd(docs), search(vector, options), clear(), size().
  • SecurityFiltercheck(input){ safe, reason?, pattern? }.
  • CacheLayer / SemanticCache — for key-based or similarity-based caching.

Errors

  • RagError — Base (code: RagError).
  • SecurityError — Prompt injection or security check failed (SECURITY_VIOLATION).
  • EmbeddingError — Embedding call failed.
  • VectorStoreError — Vector store operation failed.
  • CacheError — Cache operation failed.
  • ConfigurationError — Invalid or missing config.

Project structure

src/
  core/         # Chunker, Retriever, errors
  providers/    # OpenAI embedding
  adapters/     # In-memory vector store
  security/     # Prompt injection filter
  cache/        # In-memory + Redis semantic cache
  types/        # Interfaces and config types
  utils/        # Cosine similarity, metrics, token utils
  rag-kit.ts    # Main RagKit class
  index.ts      # Public API

License

MIT