Skip to main content

EmbedPrefix

A lightweight Python utility for managing embedding prefixes across different embedding models.

Many embedding models require specific prefixes (e.g. query:, passage:, search_document:) depending on the task being performed. EmbedPrefix provides a simple interface for registering those prefixes and applying them consistently.

Features

  • Simple API
  • Multiple prefix strategies per model
  • Configurable prefix position (start or end)
  • Optional separators
  • Built-in loaders for common embedding models
  • Easy to extend for custom models

Installation

Clone the repository:

pip install embedsavior

No external dependencies are required.


Quick Start

IntFloat E5

from embedprefix import EmbedPrefixLoaders

emb = EmbedPrefixLoaders.load_intfloat_e5()

query = emb.capsule(
    "What is artificial intelligence?",
    strategy="query"
)

document = emb.capsule(
    "Artificial intelligence is a branch of computer science.",
    strategy="input"
)

print(query)
# query: What is artificial intelligence?

print(document)
# passage: Artificial intelligence is a branch of computer science.

Nomic Embed

from embedprefix import EmbedPrefixLoaders

emb = EmbedPrefixLoaders.load_nomic()

query = emb.capsule(
    "What is machine learning?",
    strategy="query"
)

document = emb.capsule(
    "Machine learning is a field of AI.",
    strategy="input"
)

print(query)
# search_query: What is machine learning?

print(document)
# search_document: Machine learning is a field of AI.

Creating Your Own Configuration

from embedprefix import EmbedPrefix

emb = EmbedPrefix("my-model")

emb.add_prefix(
    prefix="[QUERY]",
    name="query",
    position="start",
    sep=" "
)

emb.add_prefix(
    prefix="[DOC]",
    name="document",
    position="start",
    sep=" "
)

text = emb.capsule(
    "Hello world",
    strategy="query"
)

print(text)
# [QUERY] Hello world

API

EmbedPrefix

Represents a collection of prefix strategies for a single embedding model.

Constructor

EmbedPrefix(model_name: str)

add_prefix(...)

Registers a new prefix strategy.

add_prefix(
    prefix: str,
    name: str,
    position: Literal["start", "end"],
    sep: str = "",
    set_default: bool = False,
    force: bool = False
)

Parameters

Parameter Description
prefix Prefix string.
name Strategy name.
position Prefix placement (start or end).
sep Separator between prefix and text.
set_default Marks this strategy as the default.
force Overwrites an existing strategy with the same name.

capsule(...)

Applies a prefix strategy to a text.

capsule(
    text: str,
    strategy: str | None = None
)

Returns a prefixed string.


list_strategies()

Returns all registered strategy names.

strategies = emb.list_strategies()

Example

['query', 'input']

Built-in Loaders

IntFloat E5

EmbedPrefixLoaders.load_intfloat_e5()

Registered strategies

Strategy Prefix
query query:
input passage:

Nomic Embed

EmbedPrefixLoaders.load_nomic()

Registered strategies

Strategy Prefix
query search_query:
input search_document:
cluster clustering:
clf classification:

Supported Models

Currently included:

  • IntFloat E5
  • Nomic Embed

Additional loaders can easily be implemented for other embedding models.


License

This project is released under the Apache License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

embedsavior-1.0.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

embedsavior-1.0.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file embedsavior-1.0.0.tar.gz.

File metadata

  • Download URL: embedsavior-1.0.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.9

File hashes

Hashes for embedsavior-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c57cd4b2a6ff47cbce70f576d795258ec394bec49065fb88a454c6bc9ad48ab8
MD5 d8b7f2a4647f8461d257fa00731320cf
BLAKE2b-256 e345810138785b4ad133bc3301e7445bb450fe92cd08b40b5f31aa831cab303d

See more details on using hashes here.

File details

Details for the file embedsavior-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: embedsavior-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.9

File hashes

Hashes for embedsavior-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aadae0efe3502d9406ace73d51cb651ffcba1a8dc7f6d9bab0365b705127fc9c
MD5 dc0fb289d1799f47cc9fe820393f251b
BLAKE2b-256 1b2614508ea883f77c21b661233bf67e2cee115798cd22275473cf731b91ec15

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page