Skip to main content

sentence-embedder

A lightweight internal Python library for generating sentence/semantic embeddings with a clean, unified API.


Features

Feature Details
Backends SentenceTransformers (local, offline) · OpenAI API
Single & batch embed() and embed_batch()
Similarity Cosine similarity between two sentences
Semantic search most_similar() over an in-memory corpus
Disk cache EmbeddingCache — skip re-embedding seen texts
Typed Full type hints, compatible with mypy

Installation

# Clone / copy this package into your project, then:

# SentenceTransformers backend (local, recommended)
pip install -e ".[st]"

# OpenAI backend
pip install -e ".[openai]"

# Both
pip install -e ".[all]"

# With dev tools (pytest, ruff, mypy)
pip install -e ".[all,dev]"

Quick Start

1 — Basic embedding

from sentence_embedder import SentenceEmbedder

embedder = SentenceEmbedder()                 # defaults: SentenceTransformers, all-MiniLM-L6-v2

vec = embedder.embed("The cat sat on the mat.")
print(vec.shape)   # (384,)

2 — Batch embedding

texts = ["Hello world", "Machine learning is fun", "I love Python"]
vecs = embedder.embed_batch(texts)
print(vecs.shape)  # (3, 384)

3 — Cosine similarity

score = embedder.similarity("fast car", "quick automobile")
print(score)  # ~0.85
corpus = [
    "The stock market crashed today.",
    "Scientists discover a new planet.",
    "Football team wins the championship.",
    "A new AI model beats human performance.",
]

results = embedder.most_similar("breakthrough in artificial intelligence", corpus, top_k=2)
for sentence, score in results:
    print(f"{score:.3f}  {sentence}")

5 — Disk cache (avoid re-embedding)

from sentence_embedder import SentenceEmbedder, EmbeddingCache

base = SentenceEmbedder()
embedder = EmbeddingCache(base, cache_dir=".cache/embeddings")

vec = embedder.embed("Hello world")   # computed → stored on disk
vec = embedder.embed("Hello world")   # loaded from cache instantly

OpenAI Backend

from sentence_embedder import SentenceEmbedder

embedder = SentenceEmbedder(
    backend="openai",
    model_name="text-embedding-3-small",
    openai_api_key="sk-...",   # or set OPENAI_API_KEY env var
)

vec = embedder.embed("Hello from OpenAI!")

API Reference

SentenceEmbedder

Method Signature Description
embed (text: str) → ndarray Embed one sentence → 1-D vector
embed_batch (texts: List[str]) → ndarray Embed many → 2-D array (N, dim)
similarity (a: str, b: str) → float Cosine similarity in [-1, 1]
most_similar (query, corpus, top_k=5) → List[tuple] Ranked (sentence, score) pairs

EmbeddingCache

Method Signature Description
embed (text: str) → ndarray Cache-aware single embed
embed_batch (texts: List[str]) → ndarray Cache-aware batch embed
clear () → None Wipe the cache database

Running Tests

pytest
# With coverage:
pytest --cov=sentence_embedder --cov-report=term-missing

Package Structure

sentence_embedder/
├── sentence_embedder/
│   ├── __init__.py       # Public exports
│   ├── embedder.py       # SentenceEmbedder (core)
│   └── cache.py          # EmbeddingCache (disk cache)
├── tests/
│   └── test_embedder.py  # Unit tests (no model/network needed)
├── pyproject.toml        # Build config & dependencies
└── README.md

Choosing a Model

Model Dim Speed Quality Use case
all-MiniLM-L6-v2 384 ⚡⚡⚡ ★★★ Default, general purpose
all-mpnet-base-v2 768 ⚡⚡ ★★★★ Higher quality
multi-qa-MiniLM-L6-cos-v1 384 ⚡⚡⚡ ★★★★ Q&A / search
text-embedding-3-small (OpenAI) 1536 API ★★★★★ Best quality, needs key

Metadata

Release files for sentence-embedder 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sentence-embedder 1.0.0
File Size Uploaded Provenance
sentence_embedder-1.0.0.tar.gz 9.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sentence-embedder 1.0.0
File Interpreter ABI Platform
sentence_embedder-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / sentence_embedder-1.0.0.tar.gz

Download URL sentence_embedder-1.0.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
22c040f1811e9546a147e29fc4e9c38161ffe182a382252c7c0ec65796d2cbd7
BLAKE2b-256 checksum
How to use checksums
bfa84b82b42a18d65f681d9d5aaadad288b1d4d530c1b60ac143c2508d19d9ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 23, 2026.

Transparency log

Release files / sentence_embedder-1.0.0-py3-none-any.whl

Download URL sentence_embedder-1.0.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9587087c90be0e819d938f20a8e1a09ca6eb242d77fab21c0f30dbe5b339964c
BLAKE2b-256 checksum
How to use checksums
460d299e0628157c288ca2c9dfb69ed52fe9cf977688ffa4f2a0a041b60b3179
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 23, 2026.

Transparency log

Security for sentence-embedder 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sentence_embedder-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page