API Reference#
Pipeline orchestration#
- lit2vec.pipeline.embedding_pipeline(input_dir, output_dir, input_type='parquet', model_name='dunzhang/stella_en_400M_v5', vec_dim=1024, hdf5_name='embedding_v2', group_name='pmids', table_name=None, batch_size=512, overwrite_embeddings=False, overwrite_faiss=False, overwrite_bm25=False, overwrite_years=None, text_field='auto', build_annoy=True, annoy_trees=100, n_results=5, bm25_threshold=12.85, zip_output=False, ui=None)[source]#
Run the embedding + indexing pipeline on pre-built parquet or SQL data.
The final output tree matches the structure documented in
sample_output/OUTPUT_STRUCTURE.md:output_dir/ ├── config.json ├── <model>.ann # monolithic Annoy index ├── bm25/ │ ├── authors/<YYYY>/... │ └── title_abstract/<YYYY>/... ├── faiss/ │ ├── faiss_quant_<YYYY>_.index │ └── pmids/embedding_v2_<YYYY>_.parquet └── metadb_by_year/ └── pubmed_<YYYY>_.parquetIntermediate HDF5 embeddings are written to a scratch directory and removed on successful completion so they do not appear in the final tree.
- Parameters:
input_dir (str | Path) – Directory containing year-wise parquet files or a SQL database.
output_dir (str | Path) – Root directory for the final index bundle.
input_type (Literal['parquet', 'sql']) – “parquet” or “sql”.
model_name (str) – HuggingFace sentence-transformer model name.
vec_dim (int) – Embedding dimensionality (must match the model).
hdf5_name (str) – Base name for intermediate HDF5 embedding files.
group_name (str) – HDF5 group name for PMID embeddings.
table_name (str | None) – Table name when input_type is “sql”.
batch_size (int) – Embedding batch size.
overwrite_embeddings (bool) – If True, regenerate all embeddings.
overwrite_faiss (bool) – If True, rebuild all FAISS indices.
overwrite_bm25 (bool) – If True, rebuild all BM25 indices.
overwrite_years (list[int] | None) – Optional list of years to rebuild BM25 for.
text_field (str) – Text field strategy for parquet input.
build_annoy (bool) – If True, build the monolithic Annoy index.
annoy_trees (int) – Number of trees for the Annoy index.
n_results (int) – Default number of search results (written to config.json).
bm25_threshold (float) – BM25 score threshold (written to config.json).
zip_output (bool) – If True, create
data.zipinside the output directory.ui (PipelineUI | None) – Optional PipelineUI instance for live, step-by-step updates.
- lit2vec.pipeline.zip_output_directory(output_dir, archive_name='data.zip')[source]#
Create a zip archive of the final pipeline output directory.
Hidden files/directories (
.embeddings,.prepared,.pipeline.lock,__pycache__, etc.) and the archive itself are excluded. The archive is written directly insideoutput_dir.
- lit2vec.pipeline.export_faiss_pmid_lists(faiss_year_files, hdf5_name, out_dir)[source]#
Export per-year Parquet files with a single Int64 ‘pmid’ column.
- Output filenames follow the sample-output convention:
<out_dir>/<hdf5_name>_<YYYY>_.parquet
Preserves the HDF5 group enumeration order so that row indices align with the corresponding FAISS index.
Corpus preparation#
Convert a litsync JSONL corpus into year-wise Parquet files for lit2vec.
Embeddings#
- lit2vec.vectordb.create_embeddings.normalize(v, axis=-1, epsilon=1e-08)[source]#
Normalizes an ndarray along the specified axis by dividing by its vector norm.
- lit2vec.vectordb.create_embeddings.embed_query(queries, model, tokenizer, device='cuda')[source]#
Embeds a batch of queries using a transformer model and normalizes the embeddings.
- lit2vec.vectordb.create_embeddings.save_single_embedding_to_hdf5(hdf5_name, group_name, batch_ids, batch_texts, model, tokenizer, device, batch_size=32)[source]#
Saves batch embeddings to an HDF5 file.
- lit2vec.vectordb.create_embeddings.get_model(model_name='dunzhang/stella_en_400M_v5', use_multi_gpu=True, device_map='auto', torch_dtype=torch.float32, low_cpu_mem_usage=True)[source]#
Loads the transformer model and tokenizer with optimized GPU usage.
- Parameters:
model_name (str) – Name of the pre-trained model.
use_multi_gpu (bool) – Whether to use multiple GPUs if available.
device_map (str) – Device mapping strategy (“auto”, “balanced”, or specific mapping).
torch_dtype (torch.dtype) – Data type for model weights (float16 saves memory).
low_cpu_mem_usage (bool) – Whether to use low CPU memory during loading.
- Returns:
The loaded transformer model. tokenizer: The corresponding tokenizer. device: The primary device or device map information.
- Return type:
model
- lit2vec.vectordb.create_embeddings.get_model_with_memory_check(model_name='dunzhang/stella_en_400M_v5', use_multi_gpu=True, max_memory_per_gpu='0.8')[source]#
Alternative function that checks GPU memory and loads model accordingly.
- lit2vec.vectordb.create_embeddings.load_data_from_sql(filename, table_name, columns, limited_rows=100)[source]#
Load data from a DuckDB/SQLite database and return a pandas DataFrame.
- Parameters:
- Returns:
The resulting DataFrame with the queried data.
- Return type:
pd.DataFrame
- lit2vec.vectordb.create_embeddings.from_sql(db_file, hdf5_name, hdf5_path=None, group_name='pmids', table_name='PubmedDB', model_name='dunzhang/stella_en_400M_v5', batch_size=32, columns=['pmid', 'abstract', 'title', 'authors'], progress_callback=None)[source]#
- lit2vec.vectordb.create_embeddings.from_parquet(db_path, hdf5_name, hdf5_path, group_name='pmids', model_name='dunzhang/stella_en_400M_v5', batch_size=1024, overwrite=True, text_field='auto', years=None, progress_callback=None)[source]#
Stream Parquet rows in batches to minimize memory usage.
Reads only the required columns per batch using PyArrow and processes each batch without materializing the whole file.
- Parameters:
text_field (str) – Strategy for selecting text to embed. One of: - “auto”: abstract if non-empty, else body (default) - “title”: title only - “abstract”: abstract only - “body”: body/full-text only - “title+abstract”: title and abstract concatenated - “title+abstract+body”: title, abstract, and body concatenated
years (List[str] | None) – Optional list of year labels (e.g. [“2024”]) to restrict processing to. When given, parquet files for other years are skipped entirely (important for efficient resume: completed years are not re-embedded on the GPU only to be skipped at write time).
db_path (str)
hdf5_name (str)
hdf5_path (str)
group_name (str)
model_name (str)
batch_size (int)
overwrite (bool)
progress_callback (Callable[[int, int, str | None], None] | None)
FAISS index#
- lit2vec.vectordb.create_faiss_index.create_compressed_index(hdf5_paths, faiss_path)[source]#
Creates compund index HNSW and IVF
- lit2vec.vectordb.create_faiss_index.create_full_index(hdf5_paths, faiss_path)[source]#
Creates uncompressed HNSW index: SOTA
Annoy index#
Build a monolithic Annoy index from per-year HDF5 embedding files.
- lit2vec.vectordb.create_annoy_index.build_annoy_index(hdf5_paths, output_path, dim=1024, n_trees=100, metric='angular', progress_callback=None)[source]#
Build a single Annoy index from all per-year HDF5 embeddings.
- Parameters:
hdf5_paths (List[str | Path]) – List of paths to yearly HDF5 embedding files. Each file is expected to contain a
pmidsgroup with one subgroup per PMID, and each subgroup must have anabstractdataset holding the embedding vector.output_path (str | Path) – Destination path for the Annoy index (e.g.
stella_en_400M_v5.ann).dim (int) – Vector dimensionality. Must match the embedding model output.
n_trees (int) – Number of Annoy trees. More trees improve recall at the cost of build time and index size.
metric (str) – Annoy distance metric.
angularis appropriate for cosine similarity on L2-normalized vectors.progress_callback (Callable[[int, int, str | None], None] | None) – Optional callback(current, total, message).
- Returns:
Dict with
index_path,total_vectors,dim,n_trees,metric.- Return type:
BM25 index#
Metadata databases#
Verification (proctor)#
Proctor agent — verify Lit2Vec pipeline outputs for integrity and consistency.
This module validates the final output tree produced by lit2vec run (with or
without --zip). It checks that every artifact is readable, non-corrupted,
internally consistent, and aligned across the different index types.
- class lit2vec.proctor.CheckResult[source]#
Bases:
objectCheckResult(name: ‘str’, passed: ‘bool’, message: ‘str’ = ‘’, details: ‘dict[str, Any]’ = <factory>, severity: ‘str’ = ‘error’)
- class lit2vec.proctor.ProctorReport[source]#
Bases:
objectProctorReport(output_dir: ‘Path’, checks: ‘list[CheckResult]’ = <factory>)
- checks: list[CheckResult]#
- property failed: list[CheckResult]#
- __init__(output_dir, checks=<factory>)#
- Parameters:
output_dir (Path)
checks (list[CheckResult])
- Return type:
None
- lit2vec.proctor.proctor_pipeline_output(output_dir, *, expect_zip=False, model_name='dunzhang/stella_en_400M_v5', vec_dim=1024)[source]#
Run the full proctor suite against a Lit2Vec pipeline output directory.
- Parameters:
- Returns:
A
ProctorReportwith oneCheckResultper validation step.- Return type:
- lit2vec.proctor.print_report(report)[source]#
Print a human-readable summary of a proctor report.
- Parameters:
report (ProctorReport)
- Return type:
None
Utilities#
- lit2vec.utils.find_files(dir, prefix=None, suffix=None, file_format=None, recursive=False, include_patterns=None, exclude_patterns=None, min_size_bytes=None, max_files=None, sort_by='name', reverse=False)[source]#
Find files in a directory with flexible filtering and sorting.
- Parameters:
prefix (str | None) – Filename should start with this value (case-sensitive). If None, ignored.
suffix (str | None) – Filename should end with this value (before extension). If None, ignored.
file_format (str | None) – Required extension without dot (e.g., “parquet”). If None, any extension allowed.
recursive (bool) – If True, search subdirectories.
include_patterns (list[str] | None) – Additional fnmatch-style patterns (match if any). Applied on basename.
exclude_patterns (list[str] | None) – Fnmatch patterns to exclude (exclude if any matches). Applied on basename.
min_size_bytes (int | None) – Keep files whose size >= this value.
max_files (int | None) – If set, return at most this many files after sorting.
sort_by (str) – Sort key: “name” (default), “mtime” (modification time), or “size” (bytes).
reverse (bool) – Reverse the sort order.
- Returns:
FindFilesResult with matching file paths and metadata.
- Raises:
FileNotFoundError – If the directory does not exist.
NotADirectoryError – If the path is not a directory.
ValueError – If sort_by is invalid.
- Return type:
FindFilesResult
- lit2vec.utils.sort_files_by_year(files, sort_by='name')[source]#
Group files by 4-digit year inferred from filename and return a dict mapping year->list[Path].
Extraction priority: - ‘pubmed_sorted_YYYY’ - ‘pubmed_YYYY’ - any 4-digit year (1900-2099) in the basename
- lit2vec.utils.attach_function_as_method(target, func, name=None, method_kind='instance')[source]#
Attach a standalone function to an object or class as a method at runtime.
- Parameters:
- Return type:
- Behavior:
- instance: binds to a single instance (if target is an instance) using MethodType;
if target is a class, sets an unbound function on the class so future instances receive it as a normal method.
class: attaches as a classmethod on the class.
static: attaches as a staticmethod on the class.
- lit2vec.utils.attach_methods_bulk(target, funcs, method_kind='instance')[source]#
Attach multiple functions to a target as methods.
- lit2vec.utils.search_files(root, pattern=None, regex=None, case_insensitive=True, max_results=None)[source]#
Recursively search for files under ‘root’. - pattern: glob pattern (fast). If provided, uses rglob. - regex: match against full path string. - If both provided, results must satisfy BOTH.