File size: 1,841 Bytes
794ae1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""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()}")