docSpaceSearch / app /embeddings.py
Catal's picture
Rename embeddings.py to app/embeddings.py
4d8effa verified
Raw History Blame Contribute Delete
1.84 kB
"""Sentence embeddings on CPU with sentence-transformers.
Default model: intfloat/multilingual-e5-small (Romanian included, trained for search, 384
dimensions, reads up to 512 tokens). e5 models expect "query: " / "passage: " prefixes; other models
(e.g. sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2) are used without them.
Changing EMBED_MODEL simply re-embeds everything on the next start-up; EMBED_MODEL=none runs the
service with BM25 only (no torch needed — useful on very small hosts, weaker on paraphrases).
"""
from __future__ import annotations
import os
from typing import Sequence
import numpy as np
DEFAULT_MODEL = "intfloat/multilingual-e5-small"
def model_name() -> str:
return os.environ.get("EMBED_MODEL", DEFAULT_MODEL)
def no_embeddings(texts: Sequence[str], kind: str) -> np.ndarray:
"""EMBED_MODEL=none: zero-width vectors, so the index uses BM25 alone."""
return np.zeros((len(texts), 0), dtype=np.float32)
class SentenceEmbedder:
def __init__(self, name: str | None = None):
from sentence_transformers import SentenceTransformer # heavy import, only when used
self.name = name or model_name()
self._model = SentenceTransformer(self.name, device="cpu")
self._prefixed = "e5" in self.name.lower()
def __call__(self, texts: Sequence[str], kind: str) -> np.ndarray:
if self._prefixed:
texts = [f"{kind}: {t}" for t in texts]
vectors = self._model.encode(list(texts), batch_size=32, normalize_embeddings=True, show_progress_bar=False)
return np.asarray(vectors, dtype=np.float32)
if __name__ == "__main__":
# Docker build step: download the model into the image so a cold start does not fetch it.
if model_name() != "none":
SentenceEmbedder()
print(f"Downloaded {model_name()}")