oa.batch_embeddings¶
Simplified interface for computing embeddings in bulk using OpenAI’s batch API.
This module provides a clean, reusable interface for generating embeddings from text segments using OpenAI’s batch API, handling the async nature of the API and providing status monitoring, error handling, and result aggregation.
Functions
|
Compute embeddings for text segments using OpenAI's batch API. |
|
Compute embeddings and return results as a pandas DataFrame. |
Classes
|
Counts of batch requests by status |
Status constants for batch processing |
|
|
Manages the lifecycle of batch embedding requests. |
|
Container for all stores needed during batch processing. |
Exceptions
Base exception for batch processing errors |
- exception oa.batch_embeddings.BatchError[source]¶
Bases:
ExceptionBase exception for batch processing errors
- class oa.batch_embeddings.BatchRequestCounts(completed=0, failed=0, total=0)[source]¶
Bases:
objectCounts of batch requests by status
- class oa.batch_embeddings.BatchStatus[source]¶
Bases:
objectStatus constants for batch processing
- class oa.batch_embeddings.EmbeddingsBatchProcess(segments=None, processing_mall=None, *, model='text-embedding-3-small', batch_size=1000, poll_interval=5.0, max_polls=None, verbosity=1, keep_processing_info=False, dacc=None, logger=None, **embeddings_kwargs)[source]¶
Bases:
objectManages the lifecycle of batch embedding requests.
This class handles submitting batch requests to the OpenAI API, monitoring their status, retrieving results, and aggregating them. It can be used as a context manager for automatic cleanup.
- monitor_batches()[source]¶
Monitor the status of all submitted batches until completion.
- Return type:
- property result: tuple[list[str], list[list[float]]] | None¶
Get the aggregated results if available
- class oa.batch_embeddings.ProcessingMall(*, current=None, segments=None, finished=None, erred=None, embeddings=None)[source]¶
Bases:
MappingContainer for all stores needed during batch processing.
The ProcessingMall contains five stores, all keyed by batch_id:
current: Batches that are currently being processed
segments: The text segments corresponding to each batch
finished: Completed batches
erred: Batches that encountered errors
embeddings: The computed embeddings for each batch
- oa.batch_embeddings.compute_embeddings(segments, model='text-embedding-3-small', *, batch_size=1000, poll_interval=5.0, max_polls=None, verbosity=1, processing_mall=None, keep_processing_info=False, dacc=None, return_process=False, logger=None, **embeddings_kwargs)[source]¶
Compute embeddings for text segments using OpenAI’s batch API.
This function manages the complete lifecycle of batch embedding requests, from submitting batches to the API, monitoring their status, and aggregating the results.
- Parameters:
segments (
Union[list[str],dict[str,str]]) – Text segments to embed, either as a list or a dictionarymodel (
str) – OpenAI embedding model to usebatch_size (
int) – Maximum number of segments per batchpoll_interval (
float) – Seconds between status checksmax_polls (
int|None) – Maximum number of status checks before timing outverbosity (
int) – Level of logging detail (0-2)processing_mall (
ProcessingMall|None) – Optional custom ProcessingMallkeep_processing_info (
bool) – Whether to keep mall data after completiondacc (
OaDacc|None) – Optional custom OaDacc instancereturn_process (
bool) – If True, return the EmbeddingsBatchProcess object instead of results**embeddings_kwargs – Additional parameters for embedding generation
- Returns:
Tuple of (segments, embeddings) If return_process is True:
EmbeddingsBatchProcess object for further interaction
- Return type:
tuple[list[str],list[list[float]]] |EmbeddingsBatchProcess