Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/raglite/_insert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Index documents."""

import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from contextlib import nullcontext
from functools import partial
Expand Down Expand Up @@ -89,7 +90,11 @@ def _create_chunk_records(
return document, chunk_records, chunk_embedding_records_list


def insert_documents( # noqa: C901
# Cap the number of worker threads to avoid excessive resource usage.
MAX_DEFAULT_WORKERS = 4 # Prevents oversubscription and high memory usage.


def insert_documents( # noqa: C901, PLR0912
documents: list[Document],
*,
max_workers: int | None = None,
Expand Down Expand Up @@ -130,6 +135,11 @@ def insert_documents( # noqa: C901
documents = [doc for doc in documents if doc.id not in existing_doc_ids]
if not documents:
return

# Heuristic based on cpu count, amount of documents, and a cap of 4.
if max_workers is None:
max_workers = min(os.cpu_count() or 1, len(documents), MAX_DEFAULT_WORKERS)

# For DuckDB databases, acquire a lock on the database.
if engine.dialect.name == "duckdb":
db_url = make_url(config.db_url) if isinstance(config.db_url, str) else config.db_url
Expand Down