Skip to main content

OpenAI Embeddings Model

A high-performance Python library for generating embeddings using OpenAI's API and other OpenAI-compatible providers, with intelligent caching, batch processing, and similarity search.

Installation

pip install openai-embeddings-model

Quick Start

import openai
from openai_embeddings_model import OpenAIEmbeddingsModel, ModelSettings

client = openai.OpenAI(api_key="your-api-key")
model = OpenAIEmbeddingsModel(model="text-embedding-3-small", openai_client=client)

response = model.get_embeddings(
    input=["Hello, world!", "How are you?"],
    model_settings=ModelSettings(dimensions=512)
)

embeddings = response.to_numpy()  # NumPy array
print(f"Shape: {embeddings.shape}, Tokens: {response.usage.total_tokens}")

Async

import asyncio, openai
from openai_embeddings_model import AsyncOpenAIEmbeddingsModel, ModelSettings

async def main():
    client = openai.AsyncOpenAI(api_key="your-api-key")
    async with AsyncOpenAIEmbeddingsModel(
        model="text-embedding-3-small", openai_client=client
    ) as model:
        response = await model.get_embeddings(
            input=["Hello, world!"],
            model_settings=ModelSettings(dimensions=512)
        )
        print(response.to_numpy().shape)

asyncio.run(main())

AsyncOpenAIEmbeddingsModel owns a ThreadPoolExecutor for cache I/O. Use it as an async context manager, or call await model.aclose(), so the worker thread are released deterministically instead of at garbage-collection time.

Similarity Search

Find the most relevant documents for a query — results are sorted by relevance score:

query = "What is the capital of France?"
documents = [
    "The capital of Germany is Berlin.",
    "The capital of France is Paris.",
    "The capital of Italy is Rome.",
]

response = model.get_similarity(query, documents, model_settings=ModelSettings(dimensions=512))

for result in response.results:
    print(f"[{result.index}] score={result.relevance_score:.4f}  {documents[result.index]}")

Also available as await model.get_similarity(...) on AsyncOpenAIEmbeddingsModel.

Supported Providers

Provider Example model
OpenAI text-embedding-3-small, text-embedding-3-large
Azure OpenAI text-embedding-3-small via AzureOpenAI client
Gemini text-embedding-004
Voyage AI voyage-3, voyage-3-lite
Self-hosted nomic-embed-text via Ollama / LocalAI

Voyage AI

import openai
from openai_embeddings_model import OpenAIEmbeddingsModel, ModelSettings

client = openai.OpenAI(
    base_url="https://api.voyageai.com/v1",
    api_key="your-voyage-api-key"
)
model = OpenAIEmbeddingsModel(model="voyage-3-lite", openai_client=client)
response = model.get_embeddings(input=["Hello"], model_settings=ModelSettings(dimensions=512))

Caching

import diskcache
from openai_embeddings_model import get_default_cache

# Default cache
cache = get_default_cache()

# Custom location
cache = diskcache.Cache('/path/to/cache')

model = OpenAIEmbeddingsModel(
    model="text-embedding-3-small",
    openai_client=client,
    cache=cache
)

Cache hits are tracked in response.usage.cache_hits and never re-billed. The cache key covers the model name, dimensions, the text, the client's base_url, and extra_body, so different providers can share one directory safely. Entries that do not decode into an embedding of the expected shape are discarded and re-fetched.

0.6.0 invalidates existing caches. The key layout changed, so entries written by 0.5.x are ignored rather than misread, and the first run after upgrading re-embeds everything. Old entries are not deleted — clear the directory yourself once you no longer need to roll back.

API Reference

Classes

Class Description
OpenAIEmbeddingsModel Synchronous model
AsyncOpenAIEmbeddingsModel Async model (dedicated ThreadPoolExecutor for cache I/O)

Methods

Method Returns
get_embeddings(input, model_settings) ModelResponse
get_embeddings_generator(input, model_settings, chunk_size=100) Generator[ModelResponse]
get_similarity(query, documents, model_settings) SimilarityResponse
aclose() — async model only, releases the cache-I/O thread pool None

ModelSettings

Parameter Type Default Description
dimensions int | None None Custom output dimensions
timeout float | None None Request timeout (seconds)
extra_body dict | None None Provider-specific parameters merged into the request

Constructor Parameters

Beyond model and openai_client: cache, encoding, max_batch_size, max_input_tokens, max_tokens_a_request, token_limit_policy, token_limit_usage_percent, dimensions_parameter, max_retries, retry_base_delay, and — async only — executor_max_workers and max_concurrent_batches. See the documentation for types and defaults.

Responses

ModelResponse

  • to_numpy()NDArray[np.float32] — a writable copy
  • to_python()List[List[float]]
  • usage.input_tokens, usage.total_tokens, usage.cache_hits
  • usage.truncated_texts — texts shortened to fit the token limit; non-zero means input was dropped before embedding

ModelResponse is immutable.

SimilarityResponse

  • results: list[SimilarityResult] — sorted by relevance_score descending
  • usage — same as ModelResponse

SimilarityResult

  • index: int — original document index
  • relevance_score: float

Performance

executor_max_workers defaults to 1, and threads help only while you are waiting on the provider — everything else on the hot path is GIL-bound. Full measurements, including what was tried and rejected, are in Benchmarks.

Requirements

  • Python 3.11+
  • OpenAI API key (or compatible provider)

License

MIT — Allen Chou <f1470891079@gmail.com>

Download files

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

Source Distribution

openai_embeddings_model-0.6.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

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

openai_embeddings_model-0.6.0-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file openai_embeddings_model-0.6.0.tar.gz.

File metadata

  • Download URL: openai_embeddings_model-0.6.0.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.13 Darwin/25.6.0

File hashes

Hashes for openai_embeddings_model-0.6.0.tar.gz
Algorithm Hash digest
SHA256 aa29c7c34c0848cabbf8f4834c783a9fdd11c4f82d4447d30798b8b00f90c3ed
MD5 36b5db5cabc27313f90a048698697f7c
BLAKE2b-256 9f584a071092d561104e1aad3c57f8cf756b366655b3b65b86703c0ee712472c

See more details on using hashes here.

File details

Details for the file openai_embeddings_model-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_embeddings_model-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d02f2a07ad03592df106b07342941523cd4ff71f10032703dda3bb97d8acfda8
MD5 13fedf79a1962b0f4e91b0f15f10b065
BLAKE2b-256 cc9ce99351b37c3c1dc2d47be3f07d02a0ac2837b0090c8c3ed6cc8056f3196c

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.0

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

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