Skip to content

base

langroid/embedding_models/base.py

EmbeddingModel

Bases: ABC

Abstract base class for an embedding model.

similarity(text1, text2)

Compute cosine similarity between two texts.

Source code in langroid/embedding_models/base.py
def similarity(self, text1: str, text2: str) -> float:
    """Compute cosine similarity between two texts."""
    [emb1, emb2] = self.embedding_fn()([text1, text2])
    return float(
        np.array(emb1)
        @ np.array(emb2)
        / (np.linalg.norm(emb1) * np.linalg.norm(emb2))
    )