Penyimpanan Tembolok Semantik untuk Respons LLM
Pelajari cara penyimpanan tembolok semantik menggunakan kembali jawaban untuk kueri serupa dengan mencocokkan makna, bukan teks yang persis sama, sehingga biaya dan latensi LLM berkurang drastis.
Penyimpanan Tembolok Semantik untuk Respons LLM adalah pelajaran LLM Apps in Production (RAG + Vector DB + Caching) gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar LLM Apps in Production (RAG + Vector DB + Caching), dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus LLM Apps in Production (RAG + Vector DB + Caching) mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Penyimpanan Tembolok Semantik untuk Respons LLM” gratis?
Ya — teks lengkap “Penyimpanan Tembolok Semantik untuk Respons LLM” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus LLM Apps in Production (RAG + Vector DB + Caching), upgrade ke CoddyKit PRO. Kursus LLM Apps in Production (RAG + Vector DB + Caching) mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Penyimpanan Tembolok Semantik untuk Respons LLM”?
Pelajari cara penyimpanan tembolok semantik menggunakan kembali jawaban untuk kueri serupa dengan mencocokkan makna, bukan teks yang persis sama, sehingga biaya dan latensi LLM berkurang drastis. Kamu berlatih LLM Apps in Production (RAG + Vector DB + Caching) dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai LLM Apps in Production (RAG + Vector DB + Caching)?
Tidak diperlukan pengalaman sebelumnya. LLM Apps in Production (RAG + Vector DB + Caching) di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Penyimpanan Tembolok Semantik untuk Respons LLM” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran LLM Apps in Production (RAG + Vector DB + Caching) ini?
Ya. Setiap pelajaran LLM Apps in Production (RAG + Vector DB + Caching) menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Tembolok Terdistribusi dengan Redis/Memcached
- Pengelolaan Sesi dan Persistensi Konteks
- Strategi Invalidasi Tembolok Tingkat Lanjut
- Penyimpanan Tembolok Semantik untuk Respons LLM