pythonintermediate

Cache Embeddings in Redis

Cache expensive embedding API calls in Redis to avoid redundant computation and reduce costs.

python
import json, hashlib, redis
from openai import OpenAI

oai = OpenAI()
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
TTL = 86_400  # 1 day

def get_embedding(text: str, model: str = 'text-embedding-3-small') -> list[float]:
    key = 'emb:' + hashlib.sha256(f'{model}:{text}'.encode()).hexdigest()[:16]
    cached = r.get(key)
    if cached:
        return json.loads(cached)
    resp = oai.embeddings.create(input=text, model=model)
    emb = resp.data[0].embedding
    r.setex(key, TTL, json.dumps(emb))
    return emb

v = get_embedding('Hello, semantic world!')
print(f'Embedding dim: {len(v)}')

Use Cases

  • cost reduction
  • embedding caching
  • semantic search optimization

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.