{"id":10496,"date":"2026-01-06T13:58:41","date_gmt":"2026-01-06T13:58:41","guid":{"rendered":"https:\/\/techtrendfeed.com\/?p=10496"},"modified":"2026-01-06T13:58:41","modified_gmt":"2026-01-06T13:58:41","slug":"a-sensible-information-to-semantic-caching-with-redis-langcache","status":"publish","type":"post","link":"https:\/\/techtrendfeed.com\/?p=10496","title":{"rendered":"A Sensible Information to Semantic Caching With Redis LangCache"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div>\n<p style=\"text-align: left;\"><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/dzone.com\/articles\/llm-integration-unleashed-elevating-efficiency-and\">Semantic cache<\/a> is a complicated caching mechanism that differs from conventional caching, which depends on precise key phrase matching; it shops and retrieves information based mostly on semantic similarity. <strong>Redis LangCache\u00a0<\/strong>is a totally hosted semantic caching service that helps cache LLM prompts and responses semantically, thereby decreasing LLM utilization prices.<\/p>\n<p style=\"text-align: left;\">On this tutorial, let&#8217;s learn to shortly create a easy utility and use LangCache for caching LLM queries. Additionally, see if we are able to mix fuzzy logic to enhance the responses. \u00a0<\/p>\n<h2 style=\"text-align: left;\"><strong>Step 1: Set Up Redis LangCache<\/strong><\/h2>\n<p style=\"text-align: left;\">If you happen to shouldn&#8217;t have an account but, create an account on <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/cloud.redis.io\/#\/databases\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/cloud.redis.io\/<\/a>. Upon getting logged in,\u00a0<\/p>\n<ol>\n<li style=\"text-align: left;\">Navigate to databases and create a brand new database (I used the free plan right here).<\/li>\n<li style=\"text-align: left;\">Click on on LangCache within the left menu and create an occasion of the LangCache service. I used the &#8220;Fast service creation&#8221; choice to create the LangCache service.\u00a0<\/li>\n<li style=\"text-align: left;\">Copy the API key and hold it protected.<\/li>\n<li style=\"text-align: left;\">Click on on the LangCache service you simply created. Once you click on the &#8220;Join&#8221; button, a fast join information will seem on the fitting with examples of how to hook up with your LangCache occasion.<\/li>\n<\/ol>\n<h2 style=\"text-align: left;\"><strong>Step 2: Create a Easy Python Script<\/strong><\/h2>\n<p style=\"text-align: left;\">Let&#8217;s create a easy Python script that can first verify the cache for a immediate. If a match is discovered, it returns the cached response. If not, it sends the immediate to the LLM, caches the response, and returns it.<\/p>\n<p style=\"text-align: left;\">Connect with LangCache:<\/p>\n<div data-code=\"lang_cache =  LangCache( &#10;    server_url=&quot;https:\/\/aws-us-north-1.langcache.redis.io&quot;,&#10;    cache_id=&quot;&lt;Get the cache id from LangCache Service created&quot;,&#10;    api_key=&lt;LangCache service api_key&gt;,&#10;)\" data-lang=\"text\/x-python\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"lang_cache =  LangCache( &#10;    server_url=&quot;&lt;LangCache Service URL&gt;&quot;,&#10;    cache_id=&quot;&lt;Get the cache id from LangCache Service created&quot;,&#10;    api_key=&lt;LangCache service api_key&gt;,&#10;)\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\">lang_cache =  LangCache( \n    server_url=\"<langcache service=\"\" url=\"\">\",\n    cache_id=\"<get the=\"\" cache=\"\" id=\"\" from=\"\" langcache=\"\" service=\"\" created=\"\" api_key=\"&lt;LangCache\">,\n)<\/get><\/langcache><\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<\/div>\n<p style=\"text-align: left;\">Search the cache earlier than sending it to LLM:<\/p>\n<div data-code=\"result = lang_cache.search(&#10;        prompt=query,&#10;        similarity_threshold=0.90,&#10;    )\" data-lang=\"text\/x-python\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"result = lang_cache.search(&#10;        prompt=query,&#10;        similarity_threshold=0.90,&#10;\u00a0 \u00a0 )\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\">outcome = lang_cache.search(\n        immediate=question,\n        similarity_threshold=0.90,\n\u00a0 \u00a0 )<\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<\/div>\n<p style=\"text-align: left;\">This <code>similarity_threshold<\/code> determines how intently the immediate should match. The next worth means stricter matching.<\/p>\n<p style=\"text-align: left;\">Deal with a cache hit:<\/p>\n<div data-code=\"if result :&#10;        for entry in result.data:&#10;            print(&quot;Cache Hit!&quot;)&#10;            print(&quot;Cache Response:::&quot;)&#10;            print(f&quot;Prompt: {entry.prompt}&quot;)&#10;            print(f&quot;Response: {entry.response}&quot;)&#10;            print(f&quot;Score: {entry.similarity}&quot;)&#10;            return\" data-lang=\"text\/x-python\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"if result :&#10;        for entry in result.data:&#10;            print(&quot;Cache Hit!&quot;)&#10;            print(&quot;Cache Response:::&quot;)&#10;            print(f&quot;Prompt: {entry.prompt}&quot;)&#10;            print(f&quot;Response: {entry.response}&quot;)&#10;            print(f&quot;Score: {entry.similarity}&quot;)&#10;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\">if outcome :\n        for entry in outcome.information:\n            print(\"Cache Hit!\")\n            print(\"Cache Response:::\")\n            print(f\"Immediate: {entry.immediate}\")\n            print(f\"Response: {entry.response}\")\n            print(f\"Rating: {entry.similarity}\")\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return<\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<\/div>\n<p style=\"text-align: left;\">Deal with cache miss and retailer response:<\/p>\n<div data-code=\"# Calling LLM here&#10;response = requests.post(url, json=payload, headers=headers)&#10;response_json = response.json()&#10;response_text = response_json[&quot;choices&quot;][0][&quot;message&quot;][&quot;content&quot;]&#10;&#10;# --- Storing the reponse from LLM in LangCache ---&#10;save_response = lang_cache.set(&#10;     prompt=query,&#10;     response=response_text,&#10;  )\" data-lang=\"text\/x-python\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"# Calling LLM here&#10;response = requests.post(url, json=payload, headers=headers)&#10;response_json = response.json()&#10;response_text = response_json[&quot;choices&quot;][0][&quot;message&quot;][&quot;content&quot;]&#10;&#10;# --- Storing the reponse from LLM in LangCache ---&#10;save_response = lang_cache.set(&#10;     prompt=query,&#10;     response=response_text,&#10;\u00a0 )\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\"># Calling LLM right here\nresponse = requests.put up(url, json=payload, headers=headers)\nresponse_json = response.json()\nresponse_text = response_json[\"choices\"][0][\"message\"][\"content\"]\n\n# --- Storing the reponse from LLM in LangCache ---\nsave_response = lang_cache.set(\n     immediate=question,\n     response=response_text,\n\u00a0 )<\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<\/div>\n<h2 style=\"text-align: left;\"><strong>Advantages of Semantic Caching<\/strong><\/h2>\n<ul>\n<li style=\"text-align: left;\">For related queries, responses are fetched from the cache, avoiding costly LLM calls.<\/li>\n<li style=\"text-align: left;\">Sooner response instances.<\/li>\n<\/ul>\n<h2 style=\"text-align: left;\"><strong>Issues to Watch Out For<\/strong><\/h2>\n<ul>\n<li style=\"text-align: left;\"><strong>Similarity threshold<\/strong>: Set it thoughtfully. Too excessive and you will miss helpful matches. Too low and you will obtain irrelevant outcomes.<\/li>\n<li style=\"text-align: left;\"><strong>Accuracy<\/strong>: Even with an optimum threshold, outcomes might not at all times be excellent.<\/li>\n<li style=\"text-align: left;\"><strong>Knowledge privateness<\/strong>: In multi-tenant architectures, guarantee correct information partitioning so customers don\u2019t see one another\u2019s data.<\/li>\n<li style=\"text-align: left;\"><strong>Cache eviction<\/strong>: Know when and the way to evict cache entries.<\/li>\n<\/ul>\n<h2 style=\"text-align: left;\"><strong>Let\u2019s Run the Code<\/strong><\/h2>\n<h3 style=\"text-align: left;\"><strong>First Question: (Cache Is Empty)<\/strong><\/h3>\n<div data-code=\"Query : Brief history on Capital of France&#10;Response: &#10;  \u00a0\u00a0\u00a0\u00a0Cache Miss!&#10;    Redirecting to LLM&#10;    \" data-lang=\"text\/x-markdown\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"Query : Brief history on Capital of France&#10;Response: &#10;  \u00a0\u00a0\u00a0\u00a0Cache Miss!&#10;    Redirecting to LLM\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\">Question : Temporary historical past on Capital of France\nResponse: \n  \u00a0\u00a0\u00a0\u00a0Cache Miss!\n\u00a0 \u00a0 Redirecting to LLM<\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<p style=\"text-align: left;\">The question and the response are actually saved within the cache.<\/p>\n<\/div>\n<h3 style=\"text-align: left;\"><strong>Modified Question<\/strong><\/h3>\n<div data-code=\"Query:  ----Brief history on Paris---&#10;Response: &#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0Prompt: Brief history on Capital of France&#10;\u00a0\u00a0\u00a0\u00a0Score: 0.92440444\" data-lang=\"text\/x-markdown\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"Query:  ----Brief history on Paris---&#10;Response: &#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0Prompt: Brief history on Capital of France&#10;\u00a0\u00a0\u00a0\u00a0Score: 0.92440444\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\">Question:  ----Temporary historical past on Paris---\nResponse: \n\u00a0\u00a0\u00a0\u00a0Cache Hit!\n\u00a0\u00a0\u00a0\u00a0Cache Response:::\n\u00a0\u00a0\u00a0\u00a0Immediate: Temporary historical past on Capital of France\n\u00a0 \u00a0 Rating: 0.92440444<\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<p style=\"text-align: left;\">Though the question modified, the cache returned a semantically related outcome with a similarity rating of 0.92<\/p>\n<\/div>\n<h3 style=\"text-align: left;\"><strong>One other Variation<\/strong><\/h3>\n<div data-code=\"Query: \u00a0----Brief history on France---&#10;Response: &#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0Prompt: Brief history on Capital of France&#10;\u00a0\u00a0\u00a0\u00a0Score: 0.9121176\" data-lang=\"text\/x-markdown\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"Query: \u00a0----Brief history on France---&#10;Response: &#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0Prompt: Brief history on Capital of France&#10;\u00a0\u00a0\u00a0\u00a0Score: 0.9121176\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\">Question: \u00a0----Temporary historical past on France---\nResponse: \n\u00a0\u00a0\u00a0\u00a0Cache Hit!\n\u00a0\u00a0\u00a0\u00a0Cache Response:::\n\u00a0\u00a0\u00a0\u00a0Immediate: Temporary historical past on Capital of France\n\u00a0 \u00a0 Rating: 0.9121176<\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<p style=\"text-align: left;\">Oops! We requested for the historical past of France, however we received the historical past of the capital of France. Although Paris performs a serious function in France&#8217;s historical past, the context is completely different. One is a metropolis, and the opposite is a rustic!<\/p>\n<\/div>\n<h3 style=\"text-align: left;\"><strong>Tuning the Threshold<\/strong><\/h3>\n<p style=\"text-align: left;\">Let\u2019s enhance the edge to 0.92 and clear the cache.<\/p>\n<div data-code=\"Query:  ----Brief history on Capital of France---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Miss!&#10;\u00a0\u00a0\u00a0\u00a0LLM Response: &lt;llm response&gt;&#10;#####################################&#10;&#10;Query:  ----Brief history on Paris---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Prompt: Brief history on Capital of France&#10;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Score: 0.92440444&#10;#####################################&#10;&#10;Query:  ----Brief history on France---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Miss!&#10;\u00a0\u00a0\u00a0\u00a0LLM Response: &lt;llm response&gt;&#10;\" data-lang=\"text\/x-markdown\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"Query:  ----Brief history on Capital of France---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Miss!&#10;\u00a0\u00a0\u00a0\u00a0LLM Response: &lt;llm response&gt;&#10;#####################################&#10;&#10;Query:  ----Brief history on Paris---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Prompt: Brief history on Capital of France&#10;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Score: 0.92440444&#10;#####################################&#10;&#10;Query:  ----Brief history on France---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Miss!&#10;\u00a0\u00a0\u00a0\u00a0LLM Response: &lt;llm response&gt;\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\">Question:  ----Temporary historical past on Capital of France---\nResponse:\n\u00a0\u00a0\u00a0\u00a0Cache Miss!\n\u00a0\u00a0\u00a0\u00a0LLM Response: <llm response=\"\">\n#####################################\n\nQuestion:  ----Temporary historical past on Paris---\nResponse:\n\u00a0\u00a0\u00a0\u00a0Cache Hit!\n\u00a0\u00a0\u00a0\u00a0Cache Response:::\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Immediate: Temporary historical past on Capital of France\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Rating: 0.92440444\n#####################################\n\nQuestion:  ----Temporary historical past on France---\nResponse:\n\u00a0\u00a0\u00a0\u00a0Cache Miss!\n\u00a0 \u00a0 LLM Response: <llm response=\"\"\/><\/llm><\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<\/div>\n<p style=\"text-align: left;\">It appears to be working higher!\u00a0<\/p>\n<h2 style=\"text-align: left;\"><strong>Efficiency Comparability<\/strong><\/h2>\n<p style=\"text-align: left;\">Let&#8217;s evaluate the time it takes to question from the <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/dzone.com\/articles\/front-end-cache-strategies-you-should-know\">cache<\/a> vs. querying from the LLM.<\/p>\n<h3 style=\"text-align: left;\">Semantic Cache Outcomes \u2014 Sorted by Time<\/h3>\n<div class=\"table-responsive\" style=\"border: none;\">\n<table style=\"max-width: 100%; width: auto; table-layout: fixed; display: table;\" width=\"auto\">\n<thead>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<th style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Question\n     <\/p>\n<\/th>\n<th style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Consequence\n     <\/p>\n<\/th>\n<th style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Matched Immediate\n     <\/p>\n<\/th>\n<th style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Similarity Rating\n     <\/p>\n<\/th>\n<th style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Response Supply\n     <\/p>\n<\/th>\n<th style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Time (seconds)\n     <\/p>\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on the Capital of France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Miss\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n     <\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n     <\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      LLM\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.8499\n     <\/p>\n<\/td>\n<\/tr>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on Paris\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Hit\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on the Capital of France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.9244\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.2705\n     <\/p>\n<\/td>\n<\/tr>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Miss\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n     <\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n     <\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      LLM\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      1.2543\n     <\/p>\n<\/td>\n<\/tr>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on the Capital of France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Hit\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on the Capital of France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      1.0\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      1.1139\n     <\/p>\n<\/td>\n<\/tr>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on Paris\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Hit\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.9386\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.2761\n     <\/p>\n<\/td>\n<\/tr>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Hit\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      1.0\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.2798\n     <\/p>\n<\/td>\n<\/tr>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on the \u00a0Capital of France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Hit\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on the Capital of France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      1.0\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      1.0178\n     <\/p>\n<\/td>\n<\/tr>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on Paris\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Hit\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.9386\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.2806\n     <\/p>\n<\/td>\n<\/tr>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Hit\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Temporary historical past on France\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      1.0\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.2778\n     <\/p>\n<\/td>\n<\/tr>\n<tr style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      How does Langcache work? clarify\u2026\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache Hit\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      How does Langcache work? clarify\u2026\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      1.0\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      Cache\n     <\/p>\n<\/td>\n<td style=\"overflow-wrap: break-word; width: auto;\" width=\"auto\">\n<p>\n      0.2930\n     <\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"text-align: left;\">Observations:<\/p>\n<ol>\n<li style=\"text-align: left;\">Although there are some anomalies, the response from the cache is far quicker, which is apparent.<\/li>\n<li style=\"text-align: left;\">A excessive similarity threshold is held to reuse the cached response.<\/li>\n<li style=\"text-align: left;\">For distinct solutions, a better similarity threshold is really helpful.<\/li>\n<li style=\"text-align: left;\">Primarily based on the question and enterprise necessities, at all times tune and experiment, as outcomes fluctuate with embedding fashions, similarity thresholds, and so forth.<\/li>\n<\/ol>\n<h3 style=\"text-align: left;\"><strong>One Extra Instance<\/strong><\/h3>\n<div data-code=\"Query:  ----How does Langcache work---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Miss!&#10;\u00a0\u00a0\u00a0\u00a0LLM Response: &lt; Gives a 20 lines response&gt;&#10;\u00a0\u00a0\u00a0\u00a0Time to get response from LLM API: 0.8144 seconds&#10;----#####################################---&#10;Query:  ----How does Langcache work---&#10;Response:  &#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Time for cache lookup: 0.3162 seconds&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0Prompt: How does Langcache work&#10;\u00a0\u00a0\u00a0\u00a0Response: &lt; Gives the same 20 lines response&gt;&#10;\u00a0\u00a0\u00a0\u00a0Score: 1.0&#10;\" data-lang=\"text\/x-markdown\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"Query:  ----How does Langcache work---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Miss!&#10;\u00a0\u00a0\u00a0\u00a0LLM Response: &lt; Gives a 20 lines response&gt;&#10;\u00a0\u00a0\u00a0\u00a0Time to get response from LLM API: 0.8144 seconds&#10;----#####################################---&#10;Query:  ----How does Langcache work---&#10;Response:  &#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Time for cache lookup: 0.3162 seconds&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0Prompt: How does Langcache work&#10;\u00a0\u00a0\u00a0\u00a0Response: &lt; Gives the same 20 lines response&gt;&#10;\u00a0\u00a0\u00a0\u00a0Score: 1.0\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\">Question:  ----How does Langcache work---\nResponse:\n\u00a0\u00a0\u00a0\u00a0Cache Miss!\n\u00a0\u00a0\u00a0\u00a0LLM Response: &lt; Offers a 20 strains response&gt;\n\u00a0\u00a0\u00a0\u00a0Time to get response from LLM API: 0.8144 seconds\n----#####################################---\nQuestion:  ----How does Langcache work---\nResponse:  \n\u00a0\u00a0\u00a0\u00a0Cache Hit!\n\u00a0\u00a0\u00a0\u00a0Time for cache lookup: 0.3162 seconds\n\u00a0\u00a0\u00a0\u00a0Cache Response:::\n\u00a0\u00a0\u00a0\u00a0Immediate: How does Langcache work\n\u00a0\u00a0\u00a0\u00a0Response: &lt; Offers the identical 20 strains response&gt;\n\u00a0 \u00a0 Rating: 1.0<\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<p style=\"text-align: left;\">All is properly until now. Let&#8217;s modify the question a bit!<\/p>\n<\/div>\n<div data-code=\"----#####################################---&#10;Query:  ----How does Langcache work, explain in 5 lines---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Time for cache lookup: 0.2719 seconds&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0Prompt: How does Langcache work&#10;\u00a0\u00a0\u00a0\u00a0Response: &lt; Gives the same 20 lines response&gt;&#10;\u00a0\u00a0\u00a0\u00a0Score: 0.9714471&#10;&#10;----#####################################---&#10;\" data-lang=\"text\/x-markdown\">\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"----#####################################---&#10;Query:  ----How does Langcache work, explain in 5 lines---&#10;Response:&#10;\u00a0\u00a0\u00a0\u00a0Cache Hit!&#10;\u00a0\u00a0\u00a0\u00a0Time for cache lookup: 0.2719 seconds&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;\u00a0\u00a0\u00a0\u00a0Prompt: How does Langcache work&#10;\u00a0\u00a0\u00a0\u00a0Response: &lt; Gives the same 20 lines response&gt;&#10;\u00a0\u00a0\u00a0\u00a0Score: 0.9714471&#10;&#10;----#####################################---\" data-lang=\"text\/x-python\">\n<pre style=\"text-align: left;\"><code lang=\"text\/x-python\">----#####################################---\nQuestion:  ----How does Langcache work, clarify in 5 lines---\nResponse:\n\u00a0\u00a0\u00a0\u00a0Cache Hit!\n\u00a0\u00a0\u00a0\u00a0Time for cache lookup: 0.2719 seconds\n\u00a0\u00a0\u00a0\u00a0Cache Response:::\n\u00a0\u00a0\u00a0\u00a0Immediate: How does Langcache work\n\u00a0\u00a0\u00a0\u00a0Response: &lt; Offers the identical 20 strains response&gt;\n\u00a0\u00a0\u00a0\u00a0Rating: 0.9714471\n\n----#####################################---<\/code><\/pre>\n<\/p><\/div><\/div><\/div>\n<p style=\"text-align: left;\">We requested for a 5-line response, however received the cached 20-line one. This highlights the significance of tuning and utilizing attributes to scope responses.<\/p>\n<\/div>\n<h2 style=\"text-align: left;\"><strong>Semantic Cache vs. Fuzzy Match\u00a0<\/strong><\/h2>\n<p style=\"text-align: left;\"><strong>Fuzzy matching\u00a0<\/strong>works based mostly on approximate string matching. It really works finest for dealing with typos, spelling variants, and near-duplicate strings, whereas semantic match operates on the that means and context ranges.<\/p>\n<p style=\"text-align: left;\">Let&#8217;s have a look at the distinction between them in motion. Let&#8217;s evaluate the semantic rating (LangCache) with the fuzzy rating (<a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/xlinux.nist.gov\/dads\/HTML\/ratcliffObershelp.html\" rel=\"noopener noreferrer\" target=\"_blank\">Ratcliff\u2013Obershelp algorithm<\/a>) when matching two strings.<\/p>\n<p style=\"text-align: left;\">Querying for the primary time:<\/p>\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"Query: Does Semantic cache work?&#10;Response:&#10;    Cache Miss!&#10;\u00a0\u00a0\u00a0\u00a0Cache Response:::&#10;    Prompt: How does Semantic cache work?&#10;    Response: Semantic caching stores query results along with their semantic descriptions, enabling new queries to be answered partially or fully by reusing cached data.\" data-lang=\"text\/plain\">\n<pre><code lang=\"text\/plain\">Question: Does Semantic cache work?\nResponse:\n    Cache Miss!\n\u00a0\u00a0\u00a0\u00a0Cache Response:::\n    Immediate: How does Semantic cache work?\n\u00a0 \u00a0 Response: Semantic caching shops question outcomes together with their semantic descriptions, enabling new queries to be answered partially or totally by reusing cached information.<\/code><\/pre>\n<\/p><\/div><\/div>\n<\/div>\n<p style=\"text-align: left;\">Querying the identical once more \u2014 discover that the semantic rating and fuzzy rating are fairly shut.<\/p>\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"Query: How does Semantic cache work?&#10;Response:&#10;    Cache Hit!&#10;    Cache Response:::&#10;    Prompt: How does Semantic cache work?&#10;\u00a0 \u00a0 Response: Semantic caching stores query results along with their semantic descriptions, enabling new queries to be answered partially or fully by reusing cached data.&#10;\u00a0 \u00a0 Semantic score \u00a0(Langacache) : 0.95&#10;\u00a0 \u00a0 Fuzzy match score : 0.93\" data-lang=\"text\/plain\">\n<pre><code lang=\"text\/plain\">Question: How does Semantic cache work?\nResponse:\n    Cache Hit!\n    Cache Response:::\n    Immediate: How does Semantic cache work?\n\u00a0 \u00a0 Response: Semantic caching shops question outcomes together with their semantic descriptions, enabling new queries to be answered partially or totally by reusing cached information.\n\u00a0 \u00a0 Semantic rating \u00a0(Langacache) : 0.95\n\u00a0 \u00a0 Fuzzy match rating : 0.93<\/code><\/pre>\n<\/p><\/div><\/div>\n<\/div>\n<p style=\"text-align: left;\">Let&#8217;s attempt with a couple of extra variations:<\/p>\n<div class=\"codeMirror-wrapper\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"Query: Will Semantic cache work?&#10;Response:&#10;    Cache Hit!&#10;    Cache Response:::&#10;    Prompt: How does Semantic cache work?&#10;\u00a0 \u00a0 Response: Semantic caching stores query results along with their semantic descriptions, enabling new queries to be answered partially or fully by reusing cached data.&#10;\u00a0 \u00a0 Semantic score \u00a0(Langacache) : 0.94&#10;\u00a0 \u00a0 Fuzzy match score : 0.83\" data-lang=\"text\/plain\">\n<pre><code lang=\"text\/plain\">Question: Will Semantic cache work?\nResponse:\n    Cache Hit!\n    Cache Response:::\n    Immediate: How does Semantic cache work?\n\u00a0 \u00a0 Response: Semantic caching shops question outcomes together with their semantic descriptions, enabling new queries to be answered partially or totally by reusing cached information.\n\u00a0 \u00a0 Semantic rating \u00a0(Langacache) : 0.94\n\u00a0 \u00a0 Fuzzy match rating : 0.83<\/code><\/pre>\n<\/p><\/div><\/div>\n<\/div>\n<div class=\"codeMirror-wrapper newest\" contenteditable=\"false\">\n<div contenteditable=\"false\">\n<div class=\"codeMirror-code--wrapper\" data-code=\"Query: What does\u00a0Semantic cache mean?&#10;Response:&#10;    Cache Hit!&#10;    Cache Response:::&#10;    Prompt: How does Semantic cache work?&#10;\u00a0 \u00a0 Response: Semantic caching stores query results along with their semantic descriptions, enabling new queries to be answered partially or fully by reusing cached data.&#10;\u00a0 \u00a0 Semantic score \u00a0(Langacache) : 0.94&#10;\u00a0 \u00a0 Fuzzy match score : 0.79\" data-lang=\"text\/plain\">\n<pre><code lang=\"text\/plain\">Question: What does\u00a0Semantic cache imply?\nResponse:\n    Cache Hit!\n    Cache Response:::\n    Immediate: How does Semantic cache work?\n\u00a0 \u00a0 Response: Semantic caching shops question outcomes together with their semantic descriptions, enabling new queries to be answered partially or totally by reusing cached information.\n\u00a0 \u00a0 Semantic rating \u00a0(Langacache) : 0.94\n\u00a0 \u00a0 Fuzzy match rating : 0.79<\/code><\/pre>\n<\/p><\/div><\/div>\n<\/div>\n<p style=\"text-align: left;\">As you&#8217;ll be able to see, fuzzy match focuses on &#8220;seems like,&#8221; whereas semantic matching focuses on &#8220;means like.&#8221;<\/p>\n<p style=\"text-align: left;\">Full implementation of the above is offered <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/github.com\/subhashinir\/SemanticCache\/\" rel=\"noopener noreferrer\" target=\"_blank\">right here<\/a>.<\/p>\n<p style=\"text-align: left;\">Fuzzy match could be mixed with the semantic cache in a couple of methods:<\/p>\n<ol>\n<li style=\"text-align: left;\">Retailer the final &#8216;n&#8217; prompts and do a fuzzy match on these. Use semantic caching provided that there isn&#8217;t a match discovered.\u00a0<\/li>\n<li style=\"text-align: left;\">When a excessive similarity threshold\/rating is used for the semantic cache (e.g., &gt; 0.95), we find yourself caching prompts for each cache miss. This process will lead to a number of near-duplicates. We will use fuzzy match to establish these close to duplicates and retailer solely the prompts which are completely different<\/li>\n<li style=\"text-align: left;\">If the caching layer incorporates a number of close to duplicates, we are able to use fuzzy match for compaction.<\/li>\n<\/ol>\n<h2 style=\"text-align: left;\"><strong>Ultimate Ideas<\/strong><\/h2>\n<p style=\"text-align: left;\">When constructing functions with semantic caching, efficient outcomes depend upon steady testing and context-aware tuning. Similarity thresholds, immediate patterns, and cache scope ought to be adjusted based mostly on workload habits and accuracy necessities. Redis LangCache allows fine-grained management via attributes that partition and scope cached responses. Semantic caching turns into much more environment friendly when fuzzy matching logic is added, placing a steadiness between accuracy and elevated cache hit charges. When mixed, these strategies can enhance latency, decrease LLM prices, and supply constant outcomes whereas sustaining accuracy and relevance.<\/p>\n<p style=\"text-align: left;\">Completely satisfied coding!<\/p>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Semantic cache is a complicated caching mechanism that differs from conventional caching, which depends on precise key phrase matching; it shops and retrieves information based mostly on semantic similarity. Redis LangCache\u00a0is a totally hosted semantic caching service that helps cache LLM prompts and responses semantically, thereby decreasing LLM utilization prices. On this tutorial, let&#8217;s learn [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":10498,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[7278,78,7279,185,3171,5527],"class_list":["post-10496","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-caching","tag-guide","tag-langcache","tag-practical","tag-redis","tag-semantic"],"_links":{"self":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/10496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10496"}],"version-history":[{"count":1,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/10496\/revisions"}],"predecessor-version":[{"id":10497,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/10496\/revisions\/10497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/media\/10498"}],"wp:attachment":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- This website is optimized by Airlift. Learn more: https://airlift.net. Template:. Learn more: https://airlift.net. Template: 69d9690a190636c2e0989534. Config Timestamp: 2026-04-10 21:18:02 UTC, Cached Timestamp: 2026-07-29 10:10:48 UTC -->