Show HN: A key value store based on semantic similarity rather lexical equality

https://github.com/upstash/semantic-cache

Semantic Cache

Semantic Cache is a tool for caching natural text based on semantic similarity. It's ideal for any task that involves querying or retrieving information based on meaning, such as natural language classification or caching AI responses. Two pieces of text can be similar but not identical (e.g., "great places to check out in Spain" vs. "best places to visit in Spain"). Traditional caching doesn't recognize this semantic similarity and misses opportunities for reuse.

Semantic Cache allows you to:

  • Easily classify natural text into predefined categories
  • Avoid redundant LLM work by caching AI responses
  • Reduce API latency by responding to similar queries with already cached values

Highlights

  • Uses semantic similarity: Stores cache entries by their meaning, not just the literal characters
  • Handles synonyms: Recognizes and handles synonyms
  • Multi-language support: Works across different languages (if configured with multilingual vector models)
  • Complex query support: Understands long and nested user queries
  • Easy integration: Simple API for usage in Node.js applications
  • Customizable: Set a custom proximity threshold to filter out less relevant results

Getting Started

Prerequisites

  • An Upstash Vector database (create one here)

Installation

Install the package:

npm install @upstash/semantic-cache @upstash/vector

Setup

First, create an Upstash Vector database here. You'll need the url and token credentials to connect your semantic cache. Important: Choose any pre-made embedding model when creating your database.

Note

Different embedding models are great for different use cases. For example, if low latency is a priority, choose a model with a smaller dimension size like bge-small-en-v1.5. If accuracy is important, choose a model with more dimensions.

Create a .env file in the root directory of your project and add your Upstash Vector URL and token:

UPSTASH_VECTOR_REST_URL=https://example.upstash.io
UPSTASH_VECTOR_REST_TOKEN=your_secret_token_here

Using Semantic Cache

Here’s how you can use Semantic Cache in your Node.js application:

import { SemanticCache } from "@upstash/semantic-cache";
import { Index } from "@upstash/vector";
// πŸ‘‡ your vector database
const index = new Index();
// πŸ‘‡ your semantic cache
const semanticCache = new SemanticCache({ index, minProximity: 0.95 });
async function runDemo() {
  await semanticCache.set("Capital of Turkey", "Ankara");
  await delay(1000);
  // πŸ‘‡ outputs: "Ankara"
  const result = await semanticCache.get("What is Turkey's capital?");
  console.log(result);
}
function delay(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
runDemo();

The minProximity Parameter

The minProximity parameter ranges from 0 to 1. It lets you define the minimum relevance score to determine a cache hit. The higher this number, the more similar your user input must be to the cached content to be a hit. In practice, a score of 0.95 indicates a very high similarity, while a score of 0.75 already indicates a low similarity. For example, a value of 1.00, the highest possible, would only accept an exact match of your user query and cache content as a cache hit.

Namespace Support

You can seperate your data into partitions with namespaces.

import { SemanticCache } from "@upstash/semantic-cache";
import { Index } from "@upstash/vector";
// πŸ‘‡ your vector database
const index = new Index();
// πŸ‘‡ your semantic cache
const semanticCache = new SemanticCache({ index, minProximity: 0.95, namespace: "user1" });
await semanticCache.set("Capital of Turkey", "Ankara");

Examples

The following examples demonstrate how you can utilize Semantic Cache in various use cases:

Note

We add a 1-second delay after setting the data to allow time for the vector index to update. This delay is necessary to ensure that the data is available for retrieval.

Basic Semantic Retrieval

await semanticCache.set("Capital of France", "Paris");
await delay(1000);
// πŸ‘‡ outputs "Paris"
const result = await semanticCache.get("What's the capital of France?");

Handling Synonyms

await semanticCache.set("largest city in USA by population", "New York");
await delay(1000);
// πŸ‘‡ outputs "New York"
const result = await semanticCache.get("which is the most populated city in the USA?");

Multilingual Queries

Note: Your embedding model needs to support the languages you intend to use.

await semanticCache.set("German Chancellor", "Olaf Scholz");
await delay(1000);
// πŸ‘‡ "Who is the chancellor of Germany?" -> outputs "Olaf Scholz"
const result = await semanticCache.get("Wer ist der Bundeskanzler von Deutschland?");

Complex Queries

await semanticCache.set("year in which the Berlin wall fell", "1989");
await delay(1000);
// πŸ‘‡ outputs "1989"
const result = await semanticCache.get("what's the year the Berlin wall destroyed?");

Different Contexts

await semanticCache.set("the chemical formula for water", "H2O");
await semanticCache.set("the healthiest drink on a hot day", "water");
await delay(1000);
// πŸ‘‡ outputs "water"
const result = await semanticCache.get("what should i drink when it's hot outside?");
// πŸ‘‡ outputs "H2O"
const result = await semanticCache.get("tell me water's chemical formula");

Contributing

We appreciate your contributions! If you'd like to contribute to this project, please fork the repository, make your changes, and submit a pull request.

License

Distributed under the MIT License. See LICENSE for more information.

{
"by": "enesakar",
"descendants": 2,
"id": 40219731,
"kids": [
40249501,
40261490
],
"score": 8,
"time": 1714540372,
"title": "Show HN: A key value store based on semantic similarity rather lexical equality",
"type": "story",
"url": "https://github.com/upstash/semantic-cache"
}
{
"author": "upstash",
"date": null,
"description": "A fuzzy key value store based on semantic similarity rather lexical equality. - upstash/semantic-cache",
"image": "https://opengraph.githubassets.com/49895a34c910f2049f1fdf17c00df0e86bdcfd9cf74611953591c12b0a2fdf0e/upstash/semantic-cache",
"logo": "https://logo.clearbit.com/github.com",
"publisher": "GitHub",
"title": "GitHub - upstash/semantic-cache: A fuzzy key value store based on semantic similarity rather lexical equality.",
"url": "https://github.com/upstash/semantic-cache"
}
{
"url": "https://github.com/upstash/semantic-cache",
"title": "GitHub - upstash/semantic-cache: A fuzzy key value store based on semantic similarity rather lexical equality.",
"description": "Semantic Cache Semantic Cache is a tool for caching natural text based on semantic similarity. It's ideal for any task that involves querying or retrieving information based on meaning, such as natural...",
"links": [
"https://github.com/upstash/semantic-cache"
],
"image": "https://opengraph.githubassets.com/49895a34c910f2049f1fdf17c00df0e86bdcfd9cf74611953591c12b0a2fdf0e/upstash/semantic-cache",
"content": "<div><article><p></p><h2>Semantic Cache</h2><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#semantic-cache\"></a><p></p>\n<p>Semantic Cache is a tool for caching natural text based on semantic similarity. It's ideal for any task that involves querying or retrieving information based on meaning, such as natural language classification or caching AI responses. Two pieces of text can be similar but not identical (e.g., \"great places to check out in Spain\" vs. \"best places to visit in Spain\"). Traditional caching doesn't recognize this semantic similarity and misses opportunities for reuse.</p>\n<p>Semantic Cache allows you to:</p>\n<ul>\n<li>Easily classify natural text into predefined categories</li>\n<li>Avoid redundant LLM work by caching AI responses</li>\n<li>Reduce API latency by responding to similar queries with already cached values</li>\n</ul>\n<p><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache/blob/master/assets/how-semantic-cache-works.png\"><img src=\"https://github.com/upstash/semantic-cache/raw/master/assets/how-semantic-cache-works.png\" /></a></p>\n<p></p><h2>Highlights</h2><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#highlights\"></a><p></p>\n<ul>\n<li><strong>Uses semantic similarity</strong>: Stores cache entries by their meaning, not just the literal characters</li>\n<li><strong>Handles synonyms</strong>: Recognizes and handles synonyms</li>\n<li><strong>Multi-language support</strong>: Works across different languages (if configured with multilingual vector models)</li>\n<li><strong>Complex query support</strong>: Understands long and nested user queries</li>\n<li><strong>Easy integration</strong>: Simple API for usage in Node.js applications</li>\n<li><strong>Customizable</strong>: Set a custom proximity threshold to filter out less relevant results</li>\n</ul>\n<p></p><h2>Getting Started</h2><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#getting-started\"></a><p></p>\n<p></p><h3>Prerequisites</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#prerequisites\"></a><p></p>\n<ul>\n<li>An Upstash Vector database (create one <a target=\"_blank\" href=\"https://console.upstash.com/vector\">here</a>)</li>\n</ul>\n<p></p><h3>Installation</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#installation\"></a><p></p>\n<p>Install the package:</p>\n<div><pre>npm install @upstash/semantic-cache @upstash/vector</pre></div>\n<p></p><h3>Setup</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#setup\"></a><p></p>\n<p>First, create an Upstash Vector database <a target=\"_blank\" href=\"https://console.upstash.com/vector\">here</a>. You'll need the <code>url</code> and <code>token</code> credentials to connect your semantic cache. Important: Choose any pre-made embedding model when creating your database.</p>\n<div><p>Note</p><p>Different embedding models are great for different use cases. For example, if low latency is a priority, choose a model with a smaller dimension size like <code>bge-small-en-v1.5</code>. If accuracy is important, choose a model with more dimensions.</p>\n</div>\n<p>Create a <code>.env</code> file in the root directory of your project and add your Upstash Vector URL and token:</p>\n<div><pre><code>UPSTASH_VECTOR_REST_URL=https://example.upstash.io\nUPSTASH_VECTOR_REST_TOKEN=your_secret_token_here\n</code></pre></div>\n<p></p><h3>Using Semantic Cache</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#using-semantic-cache\"></a><p></p>\n<p>Here’s how you can use Semantic Cache in your Node.js application:</p>\n<div><pre><span>import</span> <span>{</span> <span>SemanticCache</span> <span>}</span> <span>from</span> <span>\"@upstash/semantic-cache\"</span><span>;</span>\n<span>import</span> <span>{</span> <span>Index</span> <span>}</span> <span>from</span> <span>\"@upstash/vector\"</span><span>;</span>\n<span>// πŸ‘‡ your vector database</span>\n<span>const</span> <span>index</span> <span>=</span> <span>new</span> <span>Index</span><span>(</span><span>)</span><span>;</span>\n<span>// πŸ‘‡ your semantic cache</span>\n<span>const</span> <span>semanticCache</span> <span>=</span> <span>new</span> <span>SemanticCache</span><span>(</span><span>{</span> index<span>,</span> <span>minProximity</span>: <span>0.95</span> <span>}</span><span>)</span><span>;</span>\n<span>async</span> <span>function</span> <span>runDemo</span><span>(</span><span>)</span> <span>{</span>\n <span>await</span> <span>semanticCache</span><span>.</span><span>set</span><span>(</span><span>\"Capital of Turkey\"</span><span>,</span> <span>\"Ankara\"</span><span>)</span><span>;</span>\n <span>await</span> <span>delay</span><span>(</span><span>1000</span><span>)</span><span>;</span>\n <span>// πŸ‘‡ outputs: \"Ankara\"</span>\n <span>const</span> <span>result</span> <span>=</span> <span>await</span> <span>semanticCache</span><span>.</span><span>get</span><span>(</span><span>\"What is Turkey's capital?\"</span><span>)</span><span>;</span>\n <span>console</span><span>.</span><span>log</span><span>(</span><span>result</span><span>)</span><span>;</span>\n<span>}</span>\n<span>function</span> <span>delay</span><span>(</span><span>ms</span>: <span>number</span><span>)</span> <span>{</span>\n <span>return</span> <span>new</span> <span>Promise</span><span>(</span><span>(</span><span>resolve</span><span>)</span> <span>=&gt;</span> <span>setTimeout</span><span>(</span><span>resolve</span><span>,</span> <span>ms</span><span>)</span><span>)</span><span>;</span>\n<span>}</span>\n<span>runDemo</span><span>(</span><span>)</span><span>;</span></pre></div>\n<p></p><h3>The <code>minProximity</code> Parameter</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#the-minproximity-parameter\"></a><p></p>\n<p>The <code>minProximity</code> parameter ranges from <code>0</code> to <code>1</code>. It lets you define the minimum relevance score to determine a cache hit. The higher this number, the more similar your user input must be to the cached content to be a hit. In practice, a score of 0.95 indicates a very high similarity, while a score of 0.75 already indicates a low similarity. For example, a value of 1.00, the highest possible, would only accept an <em>exact</em> match of your user query and cache content as a cache hit.</p>\n<p></p><h3>Namespace Support</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#namespace-support\"></a><p></p>\n<p>You can seperate your data into partitions with namespaces.</p>\n<div><pre><span>import</span> <span>{</span> <span>SemanticCache</span> <span>}</span> <span>from</span> <span>\"@upstash/semantic-cache\"</span><span>;</span>\n<span>import</span> <span>{</span> <span>Index</span> <span>}</span> <span>from</span> <span>\"@upstash/vector\"</span><span>;</span>\n<span>// πŸ‘‡ your vector database</span>\n<span>const</span> <span>index</span> <span>=</span> <span>new</span> <span>Index</span><span>(</span><span>)</span><span>;</span>\n<span>// πŸ‘‡ your semantic cache</span>\n<span>const</span> <span>semanticCache</span> <span>=</span> <span>new</span> <span>SemanticCache</span><span>(</span><span>{</span> index<span>,</span> <span>minProximity</span>: <span>0.95</span><span>,</span> <span>namespace</span>: <span>\"user1\"</span> <span>}</span><span>)</span><span>;</span>\n<span>await</span> <span>semanticCache</span><span>.</span><span>set</span><span>(</span><span>\"Capital of Turkey\"</span><span>,</span> <span>\"Ankara\"</span><span>)</span><span>;</span></pre></div>\n<p></p><h2>Examples</h2><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#examples\"></a><p></p>\n<p>The following examples demonstrate how you can utilize Semantic Cache in various use cases:</p>\n<div><p>Note</p><p>We add a 1-second delay after setting the data to allow time for the vector index to update. This delay is necessary to ensure that the data is available for retrieval.</p>\n</div>\n<p></p><h3>Basic Semantic Retrieval</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#basic-semantic-retrieval\"></a><p></p>\n<div><pre><span>await</span> <span>semanticCache</span><span>.</span><span>set</span><span>(</span><span>\"Capital of France\"</span><span>,</span> <span>\"Paris\"</span><span>)</span><span>;</span>\n<span>await</span> <span>delay</span><span>(</span><span>1000</span><span>)</span><span>;</span>\n<span>// πŸ‘‡ outputs \"Paris\"</span>\n<span>const</span> <span>result</span> <span>=</span> <span>await</span> <span>semanticCache</span><span>.</span><span>get</span><span>(</span><span>\"What's the capital of France?\"</span><span>)</span><span>;</span></pre></div>\n<p></p><h3>Handling Synonyms</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#handling-synonyms\"></a><p></p>\n<div><pre><span>await</span> <span>semanticCache</span><span>.</span><span>set</span><span>(</span><span>\"largest city in USA by population\"</span><span>,</span> <span>\"New York\"</span><span>)</span><span>;</span>\n<span>await</span> <span>delay</span><span>(</span><span>1000</span><span>)</span><span>;</span>\n<span>// πŸ‘‡ outputs \"New York\"</span>\n<span>const</span> <span>result</span> <span>=</span> <span>await</span> <span>semanticCache</span><span>.</span><span>get</span><span>(</span><span>\"which is the most populated city in the USA?\"</span><span>)</span><span>;</span></pre></div>\n<p></p><h3>Multilingual Queries</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#multilingual-queries\"></a><p></p>\n<p>Note: Your embedding model needs to support the languages you intend to use.</p>\n<div><pre><span>await</span> <span>semanticCache</span><span>.</span><span>set</span><span>(</span><span>\"German Chancellor\"</span><span>,</span> <span>\"Olaf Scholz\"</span><span>)</span><span>;</span>\n<span>await</span> <span>delay</span><span>(</span><span>1000</span><span>)</span><span>;</span>\n<span>// πŸ‘‡ \"Who is the chancellor of Germany?\" -&gt; outputs \"Olaf Scholz\"</span>\n<span>const</span> <span>result</span> <span>=</span> <span>await</span> <span>semanticCache</span><span>.</span><span>get</span><span>(</span><span>\"Wer ist der Bundeskanzler von Deutschland?\"</span><span>)</span><span>;</span></pre></div>\n<p></p><h3>Complex Queries</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#complex-queries\"></a><p></p>\n<div><pre><span>await</span> <span>semanticCache</span><span>.</span><span>set</span><span>(</span><span>\"year in which the Berlin wall fell\"</span><span>,</span> <span>\"1989\"</span><span>)</span><span>;</span>\n<span>await</span> <span>delay</span><span>(</span><span>1000</span><span>)</span><span>;</span>\n<span>// πŸ‘‡ outputs \"1989\"</span>\n<span>const</span> <span>result</span> <span>=</span> <span>await</span> <span>semanticCache</span><span>.</span><span>get</span><span>(</span><span>\"what's the year the Berlin wall destroyed?\"</span><span>)</span><span>;</span></pre></div>\n<p></p><h3>Different Contexts</h3><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#different-contexts\"></a><p></p>\n<div><pre><span>await</span> <span>semanticCache</span><span>.</span><span>set</span><span>(</span><span>\"the chemical formula for water\"</span><span>,</span> <span>\"H2O\"</span><span>)</span><span>;</span>\n<span>await</span> <span>semanticCache</span><span>.</span><span>set</span><span>(</span><span>\"the healthiest drink on a hot day\"</span><span>,</span> <span>\"water\"</span><span>)</span><span>;</span>\n<span>await</span> <span>delay</span><span>(</span><span>1000</span><span>)</span><span>;</span>\n<span>// πŸ‘‡ outputs \"water\"</span>\n<span>const</span> <span>result</span> <span>=</span> <span>await</span> <span>semanticCache</span><span>.</span><span>get</span><span>(</span><span>\"what should i drink when it's hot outside?\"</span><span>)</span><span>;</span>\n<span>// πŸ‘‡ outputs \"H2O\"</span>\n<span>const</span> <span>result</span> <span>=</span> <span>await</span> <span>semanticCache</span><span>.</span><span>get</span><span>(</span><span>\"tell me water's chemical formula\"</span><span>)</span><span>;</span></pre></div>\n<p></p><h2>Contributing</h2><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#contributing\"></a><p></p>\n<p>We appreciate your contributions! If you'd like to contribute to this project, please fork the repository, make your changes, and submit a pull request.</p>\n<p></p><h2>License</h2><a target=\"_blank\" href=\"https://github.com/upstash/semantic-cache#license\"></a><p></p>\n<p>Distributed under the MIT License. See <code>LICENSE</code> for more information.</p>\n</article></div>",
"author": "",
"favicon": "https://github.githubassets.com/favicons/favicon.svg",
"source": "github.com",
"published": "",
"ttr": 153,
"type": "object"
}