Skip to content
Open
Show file tree
Hide file tree
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
41 changes: 38 additions & 3 deletions packages/leann-core/src/leann/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@

logger = logging.getLogger(__name__)

# Passage ID schemes recorded in <index>.meta.json["passage_id_scheme"].
# - "sequential": today's default; IDs are str(insertion_index) (api.py:add_text).
# - "content-hash": planned in #329; IDs are sha256(text)[:16], stable across
# file moves and reorderings.
# Older indexes have no passage_id_scheme field — readers must default to
# "sequential" when the key is absent. See #329 for the rollout plan.
PASSAGE_ID_SCHEME_SEQUENTIAL = "sequential"
PASSAGE_ID_SCHEME_CONTENT_HASH = "content-hash"


def get_registered_backends() -> list[str]:
"""Get list of registered backend names."""
Expand Down Expand Up @@ -361,8 +370,19 @@ def __init__(
dimensions: Optional[int] = None,
embedding_mode: str = "sentence-transformers",
embedding_options: Optional[dict[str, Any]] = None,
passage_id_scheme: str = PASSAGE_ID_SCHEME_SEQUENTIAL,
**backend_kwargs,
):
if passage_id_scheme not in (
PASSAGE_ID_SCHEME_SEQUENTIAL,
PASSAGE_ID_SCHEME_CONTENT_HASH,
):
raise ValueError(
f"Unknown passage_id_scheme: {passage_id_scheme!r}. "
f"Expected one of: {PASSAGE_ID_SCHEME_SEQUENTIAL!r}, "
f"{PASSAGE_ID_SCHEME_CONTENT_HASH!r}."
)
self.passage_id_scheme = passage_id_scheme
self.backend_name = backend_name
# Normalize incompatible combinations early (for consistent metadata)
if backend_name == "hnsw":
Expand Down Expand Up @@ -457,10 +477,23 @@ def __init__(
self.backend_kwargs = backend_kwargs
self.chunks: list[dict[str, Any]] = []

def _generate_passage_id(self, text: str) -> str:
"""Generate a passage ID per the configured scheme.

sequential: str(insertion index) — fast, position-dependent, current default.
content-hash: sha256(text)[:16] — content-stable, dedup-friendly across
file moves and reorderings. See #329 for the design.
"""
if self.passage_id_scheme == PASSAGE_ID_SCHEME_CONTENT_HASH:
import hashlib

return hashlib.sha256(text.encode("utf-8")).hexdigest()[:16]
return str(len(self.chunks))

def add_text(self, text: str, metadata: Optional[dict[str, Any]] = None):
if metadata is None:
metadata = {}
passage_id = metadata.get("id", str(len(self.chunks)))
passage_id = metadata.get("id") or self._generate_passage_id(text)
chunk_data = {"id": passage_id, "text": text, "metadata": metadata}
self.chunks.append(chunk_data)

Expand Down Expand Up @@ -550,12 +583,13 @@ def build_index(self, index_path: str):
builder_instance.build(embeddings, string_ids, index_path, **current_backend_kwargs)
leann_meta_path = index_dir / f"{index_name}.meta.json"
meta_data = {
"version": "1.0",
"version": "1.1",
"backend_name": self.backend_name,
"embedding_model": self.embedding_model,
"dimensions": self.dimensions,
"backend_kwargs": self.backend_kwargs,
"embedding_mode": self.embedding_mode,
"passage_id_scheme": self.passage_id_scheme,
"passage_sources": [
{
"type": "jsonl",
Expand Down Expand Up @@ -675,12 +709,13 @@ def build_index_from_arrays(self, index_path: str, ids: list, embeddings: np.nda
# Create metadata file
leann_meta_path = index_dir / f"{index_name}.meta.json"
meta_data = {
"version": "1.0",
"version": "1.1",
"backend_name": self.backend_name,
"embedding_model": self.embedding_model,
"dimensions": self.dimensions,
"backend_kwargs": self.backend_kwargs,
"embedding_mode": self.embedding_mode,
"passage_id_scheme": self.passage_id_scheme,
"passage_sources": [
{
"type": "jsonl",
Expand Down
12 changes: 12 additions & 0 deletions packages/leann-core/src/leann/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ def create_parser(self) -> argparse.ArgumentParser:
default=True,
help="Fall back to traditional chunking if AST chunking fails (default: True)",
)
build_parser.add_argument(
"--id-scheme",
choices=["sequential", "content-hash"],
default="sequential",
help=(
"How passage IDs are assigned. 'sequential' (default) keys by insertion "
"order; 'content-hash' uses sha256(text)[:16], stable across file moves "
"and reorderings. See #329."
),
)

# Watch command
watch_parser = subparsers.add_parser(
Expand Down Expand Up @@ -1885,6 +1895,7 @@ def _make_incremental_builder(self, args) -> "LeannBuilder":
is_compact=args.compact,
is_recompute=args.recompute,
num_threads=args.num_threads,
passage_id_scheme=getattr(args, "id_scheme", "sequential"),
)

def _incremental_add_only(
Expand Down Expand Up @@ -2378,6 +2389,7 @@ async def build_index(self, args):
is_compact=args.compact,
is_recompute=args.recompute,
num_threads=args.num_threads,
passage_id_scheme=getattr(args, "id_scheme", "sequential"),
)

for chunk in all_texts:
Expand Down
Loading