Knowledge-Aware Re-embedding Algorithm - Efficient RAG document collection updates
Project description
KARA - Efficient RAG Document Collection Updates
KARA stands for Knowledge-Aware Reembedding Algorithm. The word "Kara" (کارآ) also means "efficient" in Persian.
KARA is a Python library that efficiently updates document collections by reducing unnecessary embedding operations. When documents change, KARA automatically identifies and reuses existing chunks, minimizing the need for new embeddings.
Installation
Install KARA with your preferred framework and provider:
# Core only
pip install kara-toolkit
# For OpenAI + LangChain
pip install "kara-toolkit[openai,langchain]"
# For Hugging Face + LangChain
pip install "kara-toolkit[huggingface,langchain]"
# Install everything
pip install "kara-toolkit[all]"
Key Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
chunk_size |
int |
1000 |
Maximum size of each chunk (typically measured in tokens). |
overlap |
int |
0 |
Number of overlapping units (tokens) between consecutive chunks. |
Quick Start
KARA is designed for token-level precision. Here is how to use it with OpenAI's tiktoken:
from kara import OpenAITokenChunker, KARAUpdater
# Initialize with token-based chunking
chunker = OpenAITokenChunker(encoding_name="cl100k_base", chunk_size=512)
updater = KARAUpdater(chunker=chunker)
# Process initial documents
result = updater.create_collection(["Your long document text..."])
# Update with modified content - reuses existing token chunks automatically
update_result = updater.update_collection(
result.new_chunked_doc,
["Your updated document text..."]
)
print(f"Efficiency: {update_result.efficiency_ratio:.1%}")
print(f"Tokens reused: {update_result.num_reused * 512} (approx)")
LangChain Integration
KARA provides dedicated factory methods for seamless LangChain integration:
from kara.integrations.langchain import KARATextSplitter
from langchain_core.documents import Document
# Create a token-aware splitter for OpenAI
splitter = KARATextSplitter.from_tiktoken_encoder(
encoding_name="cl100k_base",
chunk_size=512,
chunk_overlap=50
)
# Use as a standard LangChain splitter
docs = [Document(page_content="Your content...", metadata={"source": "manual.pdf"})]
chunks = splitter.split_documents(docs)
# Later, update with a new version of the document to see efficiency gains
# KARATextSplitter automatically manages state for incremental updates
For Hugging Face models:
splitter = KARATextSplitter.from_huggingface_tokenizer(
model_name="sentence-transformers/all-MiniLM-L6-v2",
chunk_size=256
)
Examples
See examples/ for complete usage examples.
How It Works
KARA formulates chunking as a graph optimization problem:
- Creates a Directed Acyclic Graph (DAG) where nodes are split positions and edges are potential chunks.
- Assigns costs to edges: reused chunks have a lower cost based on their fill rate, while new chunks have an additional penalty.
- Uses Dijkstra's algorithm to find the shortest path (lowest cost), which corresponds to the optimal chunking strategy that maximizes reuse.
Typical efficiency gains: 70-90% fewer embedding operations for document updates.
Limitations
While KARA provides significant efficiency improvements for document collection updates, there are some current limitations to be aware of:
-
Document Version Dependency: You need to keep the last version of documents to identify reusable chunks. However, you may be able to reconstruct document content using saved chunks in your vector store to reduce storage overhead. When compared to LangChain's indexing solution (documented here), which maintains a separate SQL database for chunk hashes while being extremely inefficient, our approach is still superior.
-
Chunking Configuration Changes: Changing splitting configurations (chunk size, separators) between updates may disrupt the algorithm's ability to reuse chunks effectively.
Roadmap to 1.0.0
- Token-Based Optimal Chunking - Support for OpenAI and Hugging Face tokenizers
- Overlapping Chunk Support - Support for overlapping units between chunks
- 100% Test Coverage - Complete test suite with full coverage
- Performance Benchmarks - Real-world efficiency testing
- Framework Support - LlamaIndex, Haystack, and others
- Complete Documentation - API reference, guides, and examples
License
CC BY 4.0 License - see LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kara_toolkit-0.3.2.tar.gz.
File metadata
- Download URL: kara_toolkit-0.3.2.tar.gz
- Upload date:
- Size: 391.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bf9b5e8d101035123f840dc58919fafc54ca3f73d63cc7394b94cd2a436e6da
|
|
| MD5 |
8820eaba0bce8c882433f04c631f6ff4
|
|
| BLAKE2b-256 |
17777a3a7d675380365645e1e50c09f4347238b91a83aa14a6171e6d665673e7
|
Provenance
The following attestation bundles were made for kara_toolkit-0.3.2.tar.gz:
Publisher:
publish.yml on mzakizadeh/kara
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kara_toolkit-0.3.2.tar.gz -
Subject digest:
6bf9b5e8d101035123f840dc58919fafc54ca3f73d63cc7394b94cd2a436e6da - Sigstore transparency entry: 1652723223
- Sigstore integration time:
-
Permalink:
mzakizadeh/kara@5f8cb43454b90c707ef722d94b9b5a86641b3e1c -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/mzakizadeh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5f8cb43454b90c707ef722d94b9b5a86641b3e1c -
Trigger Event:
release
-
Statement type:
File details
Details for the file kara_toolkit-0.3.2-py3-none-any.whl.
File metadata
- Download URL: kara_toolkit-0.3.2-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d75ea212ca466246d0272feedc368d24bc8648f92f73cd29ce8df1164c4bdb5
|
|
| MD5 |
8df50f9a99f49a320940ee2530bdbcc9
|
|
| BLAKE2b-256 |
b21e1376e2ef264a4662a45d41d07626b1d0bd817756d68005584d375bc072ff
|
Provenance
The following attestation bundles were made for kara_toolkit-0.3.2-py3-none-any.whl:
Publisher:
publish.yml on mzakizadeh/kara
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kara_toolkit-0.3.2-py3-none-any.whl -
Subject digest:
5d75ea212ca466246d0272feedc368d24bc8648f92f73cd29ce8df1164c4bdb5 - Sigstore transparency entry: 1652723267
- Sigstore integration time:
-
Permalink:
mzakizadeh/kara@5f8cb43454b90c707ef722d94b9b5a86641b3e1c -
Branch / Tag:
refs/tags/v0.3.2 - Owner: https://github.com/mzakizadeh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5f8cb43454b90c707ef722d94b9b5a86641b3e1c -
Trigger Event:
release
-
Statement type: