0Pricing
LLM Apps in Production (RAG + Vector DB + Caching) · 课时

LLM 响应的语义缓存

学习语义缓存如何通过匹配含义而非完全相同的文本,为相似查询复用答案,从而大幅降低 LLM 成本和延迟。

LLM 响应的语义缓存 是 CoddyKit 上的免费 LLM Apps in Production (RAG + Vector DB + Caching) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LLM Apps in Production (RAG + Vector DB + Caching) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Beyond Exact-Match Caching

A normal cache only hits when the key is byte-identical. But 'What is your refund policy?' and 'How do refunds work?' mean the same thing yet miss an exact cache.

Semantic caching matches on meaning, so paraphrases reuse the same answer.

How It Works

The flow:

  • Embed the incoming query into a vector
  • Search the cache for a near-by stored query
  • If similarity exceeds a threshold, return the cached answer
  • Otherwise call the LLM and store the new pair

Embedding the Query

Each query is converted to a vector by an embedding model. Similar meanings produce nearby vectors.

def embed(text):
    return [len(text), text.count('refund'), text.count('?')]

print(embed('How do refunds work?'))

Cosine Similarity

Similarity between query vectors is usually measured with cosine similarity.

import math

def cosine(a, b):
    dot = sum(x*y for x, y in zip(a, b))
    na = math.sqrt(sum(x*x for x in a))
    nb = math.sqrt(sum(y*y for y in b))
    return dot / (na * nb)

print(round(cosine([1,2,1],[1,2,0]), 3))

Choosing the Threshold

The similarity threshold is the key tuning knob:

  • Too low -> false hits, wrong answers served
  • Too high -> few hits, little savings

Tune it on real traffic and err conservative for high-stakes domains.

A Minimal Semantic Cache

Putting embedding, similarity, and a threshold together.

cache = []
THRESH = 0.95

def get(query, qvec):
    for stored_vec, ans in cache:
        if cosine(qvec, stored_vec) >= THRESH:
            return ans
    return None

def cosine(a, b):
    return 1.0 if a == b else 0.0

cache.append(([1,0], 'Refunds take 5 days'))
print(get('q', [1,0]))

When NOT to Cache

Semantic caching is wrong for queries whose answer depends on changing or personal state:

  • 'What is my account balance?'
  • 'What is today's weather?'
  • Anything user-specific or time-sensitive

Cache only stable, general knowledge.

Scoping the Cache

To avoid leaking one user's data to another, scope cache keys by tenant, language, and any relevant context. A global cache for personalized answers is a privacy bug.

Eviction and Freshness

Cached answers go stale when source data changes. Add TTLs and invalidate entries when underlying documents update, so the cache does not serve outdated answers.

Measuring Savings

Track hit rate, cost saved, and latency improvement. A 40 percent semantic hit rate can roughly translate into a 40 percent reduction in LLM spend for cacheable traffic.

Production Stack

In production, store query embeddings in a vector DB or Redis with vector search, set a tuned threshold, scope by tenant, apply TTLs, and monitor hit rate. Combine with exact caching for the best coverage.

Quick Check

Test your understanding of semantic caching.

Recap

You learned that semantic caching reuses answers for paraphrased queries by embedding them and matching via cosine similarity above a tuned threshold. Cache only stable knowledge, scope by tenant for privacy, apply TTLs for freshness, and monitor hit rate to quantify savings.

常见问题解答

「LLM 响应的语义缓存」课时是免费的吗?

是的 — 「LLM 响应的语义缓存」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LLM Apps in Production (RAG + Vector DB + Caching) 课程的其余内容,请升级到 CoddyKit PRO。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

「LLM 响应的语义缓存」这节课中我会学到什么?

学习语义缓存如何通过匹配含义而非完全相同的文本,为相似查询复用答案,从而大幅降低 LLM 成本和延迟。 你通过在浏览器中直接运行的动手代码来练习 LLM Apps in Production (RAG + Vector DB + Caching),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 LLM Apps in Production (RAG + Vector DB + Caching) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 LLM Apps in Production (RAG + Vector DB + Caching) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「LLM 响应的语义缓存」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 LLM Apps in Production (RAG + Vector DB + Caching) 课中编写并运行代码吗?

能。每节 LLM Apps in Production (RAG + Vector DB + Caching) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 Redis/Memcached 实现分布式缓存
  2. 会话管理与上下文持久化
  3. 高级缓存失效策略
  4. LLM 响应的语义缓存
← 返回 LLM Apps in Production (RAG + Vector DB + Caching)