cache_keys.py 1.03 KB
"""Shared cache key helpers for embedding inputs.

Current canonical raw-key format:
- text (TEI/BGE): ``embed:norm1:<text>`` / ``embed:norm0:<text>``
- image (CLIP):   ``embed:norm1:<url>``  / ``embed:norm0:<url>``
- clip_text (CN-CLIP 文本,与图同空间): ``clip_mm:norm1:<text>`` / ``clip_mm:norm0:<text>``

`RedisEmbeddingCache` adds the configured key prefix and optional namespace on top.
"""

from __future__ import annotations


def build_text_cache_key(text: str, *, normalize: bool) -> str:
    normalized_text = str(text or "").strip()
    return f"embed:norm{1 if normalize else 0}:{normalized_text}"


def build_image_cache_key(url: str, *, normalize: bool) -> str:
    normalized_url = str(url or "").strip()
    return f"embed:norm{1 if normalize else 0}:{normalized_url}"


def build_clip_text_cache_key(text: str, *, normalize: bool) -> str:
    """CN-CLIP / multimodal text (same vector space as /embed/image)."""
    normalized_text = str(text or "").strip()
    return f"clip_mm:norm{1 if normalize else 0}:{normalized_text}"