File size: 775 Bytes
6733714
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from typing import List
import numpy as np
from sentence_transformers import SentenceTransformer

# all-MiniLM-L6-v2 dimension
VECTOR_DIM = 384

# Initialize model once (globally or singleton)
print("Loading Local Embedding Model (all-MiniLM-L6-v2)...")
model = SentenceTransformer('all-MiniLM-L6-v2') 
print("Model loaded.")

def embed_texts(texts: List[str]) -> np.ndarray:
    """
    Generate embeddings using local Sentence Transformers (CPU friendly).
    """
    # SentenceTransformer handles batching internally, but we can explicit if needed.
    # It returns a numpy array by default if convert_to_numpy=True (default).
    
    embeddings = model.encode(texts, convert_to_numpy=True, normalize_embeddings=True)
    
    return np.array(embeddings, dtype="float32")