BatchStorage Classes
langbatch.batch_storages.BatchStorage
Bases: ABC
Abstract class for batch storage. Implementations should provide a way to save and load batches.
Usage:
batch = OpenAIChatCompletionBatch("path/to/file.jsonl")
batch.start()
# Using default FileBatchStorage
batch.save(storage=FileBatchStorage()) # same as batch.save()
batch = OpenAIChatCompletionBatch.load("1ff73c3f", storage=FileBatchStorage()) # same as OpenAIChatCompletionBatch.load("1ff73c3f")
# With custom storage
class MyCustomBatchStorage(BatchStorage):
def save(self, id: str, data_file: Path, meta_data: Dict[str, Any]):
# Custom save logic
def load(self, id: str) -> Tuple[Path, Path]:
# Custom load logic
custom_storage = MyCustomBatchStorage()
# Using custom storage
batch.save(storage=custom_storage)
batch = OpenAIChatCompletionBatch.load("1ff73c3f", storage=custom_storage)
Source code in langbatch\batch_storages.py
save
abstractmethod
Save the batch data and metadata to the storage.
Parameters:
-
id
(str
) –The id of the batch.
-
data_file
(Path
) –The path to the batch data file.
-
meta_data
(Dict[str, Any]
) –The metadata of the batch.
Source code in langbatch\batch_storages.py
load
abstractmethod
Load the batch data and metadata from the storage.
Parameters:
-
id
(str
) –The id of the batch.
Returns:
-
Tuple[Path, Path]
–Tuple[Path, Path]: The path to the batch data jsonlfile and the path to the metadata jsonfile.
Source code in langbatch\batch_storages.py
langbatch.batch_storages.FileBatchStorage
Bases: BatchStorage
Batch storage that saves the batch data and metadata to the file system. Automatically chooses between JSON and pickle serialization based on content: - Uses JSON for simple metadata (human-readable, portable) - Uses pickle for complex objects (like API clients)
Usage:
batch = OpenAIChatCompletionBatch("path/to/file.jsonl")
batch.start()
# Save the batch
batch.save(storage=FileBatchStorage())
# Load the batch
batch = OpenAIChatCompletionBatch.load("1ff73c3f", storage=FileBatchStorage())
Source code in langbatch\batch_storages.py
saved_batches_directory
instance-attribute
__init__
Initialize the FileBatchStorage. Will create or use a directory named 'saved_batches' in the given directory to save the batches.
Parameters:
-
directory
(str
, default:DATA_PATH
) –The directory to save the batches. Defaults to the DATA_PATH.