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

local-semantic-cache

v1.0.2

Published

Ultra-fast, zero-dependency local semantic cache for LLM/AI prompt responses with built-in TF-IDF vectorizer, vector similarity search, and file persistence.

Readme

🧠 local-semantic-cache

NPM Version Downloads Build Status License GitHub Stars

Ultra-fast, zero-dependency local semantic cache for LLM/AI prompt responses with built-in text vectorization, similarity search, and file persistence.

Traditional caching (like key-value maps or standard Redis caches) requires exact string matches. A query like "What is the capital of Nigeria?" won't hit a cache keyed with "Tell me the capital of Nigeria".

local-semantic-cache resolves this by encoding prompt query keys into a vector space and using cosine similarity to retrieve cached responses for semantically identical queries.

[Incoming Prompt] ──► Tokenize & Embed ──► Vector [0.2, 0.8, ...] 
                                                  │
                                                  ├──► Cosine Similarity (Dot Product)
                                                  │
[Cached Prompts]  ◄───────────────────────────────┴──► Hit >= 0.90 ? Serve Cached Response

Key Features

  • ⚡ Sub-millisecond Execution: Core vector search calculations are written in vanilla JS/TS, utilizing unit L2 normalized vectors where cosine similarity simplifies to a fast, raw dot-product multiplication.
  • 🔌 Pluggable Embeddings: Integrates seamlessly with cloud embedding APIs (OpenAI, Gemini, Cohere) or local TensorFlow.js/Transformers pipelines.
  • 📦 Out-of-the-Box Fallback: Includes a fast, zero-setup, built-in text vectorizer (using the Hashing Trick/Feature Hashing unigrams and bigrams) so the package functions instantly without requiring cloud keys.
  • 💾 File Persistence: Auto-saves and loads cache collections to and from local disk storage.
  • 🛡️ Type Safe: Built entirely in TypeScript with full type declarations.

Installation

Install via your preferred package manager:

npm install local-semantic-cache
# or
yarn add local-semantic-cache
# or
pnpm add local-semantic-cache
# or
bun add local-semantic-cache

Quick Start (Zero-Setup Local Mode)

By default, local-semantic-cache uses its built-in string vectorizer. No API keys are required:

import { SemanticCache } from 'local-semantic-cache';

const cache = new SemanticCache({
  minSimilarity: 0.60,             // Cosine threshold between 0.0 and 1.0 (0.60 is recommended for sparse local vectorizer)
  filePath: './data/cache-db.json' // Path to persist logs on disk
});

// Seed the cache
await cache.set('What is the capital of Nigeria?', 'Abuja');

// Query with a semantically similar prompt
const response = await cache.get('Tell me the capital of Nigeria');
console.log(response); // "Abuja" (Cache Hit!)

// Query out of context
const invalidResponse = await cache.get('What is the temperature in Abuja?');
console.log(invalidResponse); // null (Cache Miss)

Integration with LLM APIs

1. OpenAI Embeddings Example

import { SemanticCache } from 'local-semantic-cache';
import OpenAI from 'openai';

const openai = new OpenAI();

const cache = new SemanticCache({
  dimensions: 1536, // text-embedding-3-small dimension
  minSimilarity: 0.92,
  embeddingFunction: async (text) => {
    const res = await openai.embeddings.create({
      model: 'text-embedding-3-small',
      input: text
    });
    return res.data[0].embedding;
  }
});

await cache.set('Who wrote Hamlet?', 'William Shakespeare');

const result = await cache.get('Who is the author of Hamlet?');
console.log(result); // "William Shakespeare"

2. Google Gemini Embeddings Example

import { SemanticCache } from 'local-semantic-cache';
import { GoogleGenAI } from '@google/genai';

const ai = new GoogleGenAI();

const cache = new SemanticCache({
  dimensions: 768, // text-embedding-004 dimension
  minSimilarity: 0.88,
  embeddingFunction: async (text) => {
    const res = await ai.models.embedContent({
      model: 'text-embedding-004',
      contents: text
    });
    return res.embedding.values;
  }
});

API Reference

new SemanticCache(options)

Creates a cache instance.

  • options.embeddingFunction: A sync/async function taking (text: string) and returning number[].
  • options.minSimilarity: Similarity float cutoff value (default 0.90).
  • options.dimensions: Hashed vector output slots length (default 128).
  • options.filePath: Storage path to read/write JSON files on disk.

cache.set(prompt, response, metadata?)

Saves a record to the cache.

cache.get(prompt)

Returns the cached response string if similarity matches, or null.

cache.getDetailed(prompt)

Returns detailed matching payload containing { response, similarity, metadata } or null.

cache.clear()

Wipes the cache memory pool and removes local storage files.


Contributing

We welcome community contributions! Please read our Contributing Guide to get started.


License

MIT © Ezekiel Adejobi