Skip to main content

Context filtering module that scans your data and returns only relevant portions based on a query.

Project description

nzachapy

Context Filtering for LLMs — Scan your entire dataset and return only the portion relevant to your query.

When working with large datasets and language models, stuffing everything into the context window is noisy and expensive. nzachapy solves this by chunking your data, embedding it semantically, and returning only the chunks most relevant to a given query — so your model sees signal, not noise.


Installation

pip install nzachapy

Quick Start

from nzachapy import Nzacha

# Initialize the module
nz = Nzacha()

# Add your dataset (must be a string)
nz.add_by_words(data)

# Query for relevant context
results = nz.query("your query here")

⚠️ Important: Your dataset must be converted to a string before passing it to add_by_words().


Configuration

When initializing Nzacha, you can customize the following parameters. If left unset, the defaults are used.

Parameter Default Description
chunk_size 200 Number of words per chunk
overlap 20 Number of overlapping words between consecutive chunks
openai_api_key None OpenAI API key for semantic embeddings (optional)
threshold 0.6 Minimum similarity score for a chunk to be returned

Example with custom config

nz = Nzacha(
    chunk_size=150,
    overlap=30,
    openai_api_key="sk-...",
    threshold=0.75
)

Embedding Backends

nzachapy supports two embedding strategies:

  • OpenAI — Provide your openai_api_key at initialization to use OpenAI's embedding models for higher-quality semantic search.
  • Sentence Transformers (default) — If no API key is provided, the module falls back to a local sentence-transformers model. No API key required.

How It Works

  1. Chunkingadd_by_words(data) splits your string dataset into overlapping word-based chunks. chunk_size controls how many words each chunk contains; overlap controls how many words are shared between adjacent chunks to preserve context across boundaries.

  2. Embedding — Each chunk is embedded into a vector using either OpenAI or Sentence Transformers.

  3. Retrieval — When you run a query, it is embedded the same way and compared against all chunk embeddings. Only chunks that meet or exceed the threshold similarity score are returned.


API Reference

Nzacha(chunk_size, overlap, openai_api_key, threshold)

Initializes the context filter with the given configuration.

nz.add_by_words(data: str)

Chunks the provided string dataset by word count and stores the embeddings internally. Must be called before querying.

  • data — Your full dataset as a string. Convert lists, dicts, DataFrames, or any other structure to a string first.

Notes

  • Always pass data as a plain string. Use str(), json.dumps(), or .to_string() (for DataFrames) to convert before calling add_by_words().
  • A lower threshold returns more (but less precise) chunks. A higher threshold is stricter.
  • Chunk overlap helps avoid losing context that falls on chunk boundaries — tune it based on your data's structure.

Contributing

Contributions are welcome! If you spot an area for improvement — whether it's a new chunking strategy, a better embedding backend, performance optimizations, or additional retrieval methods — feel free to open an issue or submit a pull request.

Please make sure any changes are well-tested and clearly documented.


License

MIT

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

nzachapy-0.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nzachapy-0.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file nzachapy-0.1.0.tar.gz.

File metadata

  • Download URL: nzachapy-0.1.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for nzachapy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1016eb5ac514ff24ed15aff92040dacc476559547fc77add5d504597fbc7e623
MD5 2f90028b15ba9120887e2d2e67e4ff59
BLAKE2b-256 dc2d7920ce3277329e2ccca986a52c61d80a22aca63c278294280575af145bf8

See more details on using hashes here.

File details

Details for the file nzachapy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nzachapy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for nzachapy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f5a5d92c8ca9142dfcf7664629aab9b94752ac8f049713c893a21e07e3b9625
MD5 44a3baf7af2db5b847a654cb40e0e628
BLAKE2b-256 c0cf9021fbc8230439a43b5fce292656fb8563be36a5dbe16fdcf93ab4d3fe80

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page