Metadata-Version: 2.5
Name: cerebrate-file
Version: 1.0.38
Project-URL: Documentation, https://github.com/twardoch/cerebrate-file#readme
Project-URL: Issues, https://github.com/twardoch/cerebrate-file/issues
Project-URL: Source, https://github.com/twardoch/cerebrate-file
Author: Adam Twardoch
License: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=3.10
Requires-Dist: cerebras-cloud-sdk>=1.0.0
Requires-Dist: fire>=0.6.0
Requires-Dist: loguru>=0.7.0
Requires-Dist: openai>=1.0.0
Requires-Dist: pathspec>=0.12.1
Requires-Dist: python-dotenv>=1.0.0
Requires-Dist: python-frontmatter>=1.1.0
Requires-Dist: qwen-tokenizer>=0.0.8
Requires-Dist: rich>=13.0.0
Requires-Dist: semantic-text-splitter>=0.13.0
Requires-Dist: tenacity>=8.2.0
Requires-Dist: tomli>=2.0.0; python_version < '3.11'
Provides-Extra: all
Provides-Extra: dev
Requires-Dist: absolufy-imports>=0.3.1; extra == 'dev'
Requires-Dist: isort>=6.0.1; extra == 'dev'
Requires-Dist: mypy>=1.15.0; extra == 'dev'
Requires-Dist: pre-commit>=4.1.0; extra == 'dev'
Requires-Dist: pyupgrade>=3.19.1; extra == 'dev'
Requires-Dist: ruff>=0.9.7; extra == 'dev'
Provides-Extra: docs
Requires-Dist: myst-parser>=3.0.0; extra == 'docs'
Requires-Dist: sphinx-autodoc-typehints>=2.0.0; extra == 'docs'
Requires-Dist: sphinx-rtd-theme>=2.0.0; extra == 'docs'
Requires-Dist: sphinx>=7.2.6; extra == 'docs'
Provides-Extra: test
Requires-Dist: coverage[toml]>=7.6.12; extra == 'test'
Requires-Dist: pytest-asyncio>=0.25.3; extra == 'test'
Requires-Dist: pytest-benchmark[histogram]>=5.1.0; extra == 'test'
Requires-Dist: pytest-cov>=6.0.0; extra == 'test'
Requires-Dist: pytest-mock>=3.14.0; extra == 'test'
Requires-Dist: pytest-timeout>=2.3.1; extra == 'test'
Requires-Dist: pytest-xdist>=3.6.1; extra == 'test'
Requires-Dist: pytest>=8.3.4; extra == 'test'
Description-Content-Type: text/markdown

<p align="center"><img src="icon.png" alt="cerebrate-file" width="160"></p>

# cerebrate-file

Feed large documents to the Cerebras AI cloud — chunk by chunk, at inference speeds that make OpenAI look sluggish.

## The problem it solves

Cerebras runs inference on dedicated wafer-scale hardware. It is genuinely fast — thousands of tokens per second. But like every language model, it has a context window. A 100-page document does not fit in one API call.

`cerebrate-file` solves this by splitting your document into overlapping chunks, processing each one in sequence, and stitching the outputs back together. A "continuity block" carries a sample of the previous chunk's input and output into the next request, so the model knows it is in the middle of something.

## What you can use it for

Any task where you want an LLM to transform a document chunk by chunk:

- Rewrite or summarise long reports
- Apply a style guide to a manuscript
- Extract structured data from multi-page documents
- Edit Markdown files with a consistent voice
- Run any freeform instruction over content too large for one prompt

## Install

```bash
pip install cerebrate-file
# or
uv pip install cerebrate-file
```

Set your API key:

```bash
export CEREBRAS_API_KEY="csk-..."
```

## Quick start

```bash
# Process a single file (overwrites in place)
cerebrate-file document.md --prompt "Fix grammar and improve clarity"

# Write to a separate output file
cerebrate-file input.md output.md --prompt "Translate to formal English"

# Use a prompt file instead of inline text
cerebrate-file report.md --file-prompt prompts/summarize.md

# Dry run — shows chunking plan without calling the API
cerebrate-file large.md --dry-run --verbose

# Process a directory recursively
cerebrate-file ./docs --recurse "**/*.md" --prompt "Fix typos" --workers 4
```

## FreeLLM and Poe through askllm2

Select the unified client explicitly to process a file with the active FreeLLM chain:

```bash
cerebrate-file document.md --provider askllm2 --model auto --prompt "Fix grammar"
cerebrate-file document.md --provider askllm2 --model poe:Gemma-4-31B --prompt "Fix grammar"
```

This requires an executable `~/bin/askllm2.py` and its configured provider credentials. System and user messages are passed to it as JSON on stdin. File output is replaced only after every chunk succeeds; streamed stdout may already contain earlier chunks if a later request fails. Existing Cerebras behavior remains the default. Model context limits and routing depend on the selected provider.

## Python library

Import `process_text` to transform text and return the result:

```python
from cerebrate_file import process_text

edited = process_text(
    "The sky are blue.",
    "Correct grammar. Return only the corrected text.",
)
print(edited)
```

Use `process_file(input_path, prompt, output_path=...)` to read a UTF-8 file and optionally write the result atomically. Omitting `output_path` returns the result without changing a file; passing the input path explicitly replaces it after successful processing. Both functions accept `provider="cerebras"` (the default, requiring `CEREBRAS_API_KEY`) or `provider="askllm2"`, plus `model`, `data_format`, `chunk_size`, `sample_size`, `max_tokens_ratio`, `temperature`, and `top_p`. Errors and empty chunk responses raise exceptions instead of producing partial files.

Runnable examples are in [`examples/library_text.py`](examples/library_text.py) and [`examples/library_file.py`](examples/library_file.py).

## How chunking works

The document is split into chunks using one of five strategies selected by `--data-format`:

| Mode | Best for |
|---|---|
| `markdown` (default) | Markdown files — splits at headings and paragraphs |
| `text` | Plain prose — splits at sentence boundaries |
| `semantic` | Mixed content — semantic similarity grouping |
| `code` | Source code — splits at function/class boundaries |
| `xml` | XML/HTML — splits at element boundaries |

Each chunk is sized to fit within the model's context window alongside the system prompt and the continuity block. The completion budget (how many tokens the model can write back) is automatically calculated as a percentage of the input chunk size, with a generous minimum so reasoning-heavy models have room to think before outputting.

## Continuity

Between chunks, `cerebrate-file` builds a "continuity block": a sample of tokens from the tail of the previous chunk's input and a sample from the tail of its output. This gives the model just enough context to keep its voice and terminology consistent across chunks. The block is trimmed to fit the token budget if it would push the request over the context limit.

## CLI reference

```
cerebrate-file <input> [output] [options]

  --file-prompt PATH      Path to a file with system instructions
  --prompt TEXT           Freeform instructions appended to the system prompt
  --chunk-size INT        Target chunk size in tokens (from config if omitted)
  --max-tokens-ratio INT  Completion budget as % of chunk size (default: 150)
  --data-format TEXT      Chunking strategy: text|semantic|markdown|code|xml
  --sample-size INT       Tokens to carry forward as continuity context
  --temp FLOAT            Model temperature
  --top-p FLOAT           Nucleus sampling parameter
  --provider TEXT         cerebras (default) or askllm2
  --model TEXT            Model name override
  --verbose               Show per-chunk progress and token counts
  --explain               Parse YAML frontmatter and auto-generate missing fields
  --dry-run               Show chunk plan without making API calls
  --recurse GLOB          Process a directory tree matching GLOB pattern
  --workers INT           Parallel workers in recursive mode (default: 4)
  --force                 Overwrite existing output files
```

## Configuration

Settings load from three locations in order (later overrides earlier):

1. Built-in defaults (in the package)
2. `~/.config/cerebrate-file/config.toml`
3. `.cerebrate-file.toml` in the current directory

```toml
[inference]
model = "llama-4-scout-17b-16e-instruct"
chunk_size = 8000
max_tokens_ratio = 150
sample_size = 512
temperature = 0.3
top_p = 0.9
```

## Progressive output

Output is written to a temporary file and renamed atomically on completion. If the process is killed mid-run, the input file is untouched. In streaming mode (`--output -`), each processed chunk is written to stdout immediately so you can pipe it elsewhere.

## Cerebras context window

The Cerebras cloud API currently supports a 131,072 token context window. The tool enforces this limit before sending each request and raises an error if a chunk plus its system prompt would exceed it.

## License

MIT
