-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembedder.py
More file actions
30 lines (25 loc) · 781 Bytes
/
Copy pathembedder.py
File metadata and controls
30 lines (25 loc) · 781 Bytes
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
# embedder.py
from sentence_transformers import SentenceTransformer
from typing import List
import threading
import json
# Single global model instance (thread-safe lazy init)
_model = None
_lock = threading.Lock()
def _load_config():
with open("config.json", "r", encoding="utf-8") as f:
return json.load(f)
def get_model():
global _model
if _model is None:
with _lock:
if _model is None:
cfg = _load_config()
name = cfg["rag"]["embedding_model"]
_model = SentenceTransformer(name)
return _model
def embed_texts(texts: List[str]):
model = get_model()
return model.encode(texts, normalize_embeddings=True).tolist()
def embed_text(text: str):
return embed_texts([text])[0]