File size: 1,010 Bytes
20f1ed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from langchain_huggingface import HuggingFaceEmbeddings

def get_embeddings(model_name: str = "BAAI/bge-base-en-v1.5", device: str = "cpu") -> HuggingFaceEmbeddings:
    """
    Initializes and returns the BGE embeddings model.
    
    Explanation:
    - Embeddings: Dense vector representations of text where mathematically similar vectors represent semantically similar text.
    - Vector representations: Text mapped to high-dimensional floating-point arrays.
    - Semantic similarity: The ability to find answers not by exact keyword match, but by meaning. 
      (e.g., "elevated glucose" vs "high blood sugar").
    
    We use BAAI/bge-base-en-v1.5 as it provides highly competitive retrieval performance for RAG.
    """
    model_kwargs = {"device": device}
    encode_kwargs = {"normalize_embeddings": True} # Normalizing helps with cosine similarity
    
    return HuggingFaceEmbeddings(
        model_name=model_name,
        model_kwargs=model_kwargs,
        encode_kwargs=encode_kwargs
    )