This commit is contained in:
1 parent
3d4a54b783
commit
a58856fa00
4 files changed
+60
No files matched your search
@@ -0,0 +1,22 @@
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import List
|
||||
from functools import lru_cache
|
||||
|
||||
from .config import get_settings
|
||||
|
||||
# Lazy import so startup stays fast
|
||||
@lru_cache
|
||||
def _load_model():
|
||||
from sentence_transformers import SentenceTransformer
|
||||
settings = get_settings()
|
||||
model = SentenceTransformer(settings.MODEL_NAME, device="cpu")
|
||||
return model
|
||||
|
||||
def embed_texts(texts: List[str]) -> list[list[float]]:
|
||||
model = _load_model()
|
||||
# Normalize to list of str
|
||||
texts = [t if isinstance(t, str) else str(t) for t in texts]
|
||||
vecs = model.encode(texts, normalize_embeddings=True, convert_to_numpy=False)
|
||||
# Ensure pure-python list of floats
|
||||
return [list(map(float, v)) for v in vecs]
|
||||
Reference in new issue
Block a user