Skip to content

About

A (fast!) reranker for text files and SQLite table fields, powered by TypeSafe's Jev

Resources

Stars

5 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

jev-rerank

A Python CLI for finding text files or SQLite field values that match a natural-language query, using TypeSafe's reranking recipe. Both filenames and document contents are searchable. Selected SQLite text fields can also be searched together in one ranking.

Install and run

Requires Python 3.11+ and SQLite with FTS5 (included in most Python distributions). For a checkout, the recommended path is uv:

uv sync
export TYPESAFE_API_KEY='your-key'
uv run jev-rerank --dir ./lyrics --query 'Dua Lipa Hallucinate' --top 5

Or install the CLI into your environment:

python -m pip install .
jev-rerank --dir ./documents --query 'how to recover a lost account' --top 10

python -m jev_rerank works as well. Obtain an API key from the TypeSafe console.

Run a GitHub Release download

Every GitHub Release has a small universal wheel and a source.zip bundle. The bundle is the simplest and most reproducible option: download jev-rerank-X.Y.Z-source.zip from the Releases page, extract it, enter the extracted jev-rerank-X.Y.Z directory, then run:

export TYPESAFE_API_KEY='your-key'
uv run --locked --no-dev jev-rerank --dir ./documents --query 'how to recover a lost account' --top 10

uv is the only required host tool for this route; it can provision a compatible Python itself. The archive contains the CLI source and uv.lock, not a copy of all dependencies, so the download stays small while --locked recreates the runtime versions tested for that release. This works on Windows, macOS, and Linux. In PowerShell, set the key with:

$env:TYPESAFE_API_KEY = 'your-key'
uv run --locked --no-dev jev-rerank --dir .\documents --query 'how to recover a lost account' --top 10

The wheel is useful when you want a one-command or permanent tool install. Set VERSION to the release number and use the matching release asset URL:

VERSION=X.Y.Z
WHEEL_URL="https://github.com/anteloc/jev-rerank/releases/download/v${VERSION}/jev_rerank-${VERSION}-py3-none-any.whl"

# One command; uvx is an alias for `uv tool run`.
uvx --from "$WHEEL_URL" jev-rerank --dir ./documents --query 'how to recover a lost account' --top 10

# Or install it once, then invoke `jev-rerank` normally in later shells.
uv tool install "$WHEEL_URL"

If you already use pipx, it accepts the same wheel URL (with a locally installed Python 3.11+):

pipx run --spec "$WHEEL_URL" jev-rerank --dir ./documents --query 'how to recover a lost account' --top 10
pipx install "$WHEEL_URL"

uvx/pipx resolve the wheel's declared dependencies at install time. Prefer the source ZIP when reproducing the release's locked dependency set matters. See the release checklist for the maintainer steps that create these assets.

Output is tab-separated rank, score, and path relative to --dir. Progress and warnings go to stderr, so stdout can be redirected or piped. Use --json for results plus corpus counts, cache hits, API usage, and elapsed time. JSON results also include the zero-based index of the winning passage.

uv run jev-rerank --dir ./documents --query '2025 annual report' --top 10 --json

--docs remains an alias for --dir.

Add --show to include the complete text behind each returned path, for both files and SQLite field values:

uv run jev-rerank --dir ./lyrics --query 'loneliness' --top 5 --show

With --json --show, each result has a text property. Otherwise, the text is a fourth tab-separated column encoded as a JSON string, escaping embedded tabs, newlines, and quotes so every result stays on one line. Long values are restored from all their indexed passages without duplicating overlap. The text comes from the indexed snapshot used for the search; showing it makes no additional API calls. Full values are loaded only for returned results when --show is enabled, so very large matches can increase memory usage and output size.

Query criteria

--query accepts plain text, or the same text plus an explicit yes/no pair that replaces the ranking question's default criteria. Use it to say where the line falls when a plain query is too broad:

uv run jev-rerank --dir ./lyrics --top 5 --query 'a song about missing love
| yes: the song talks about missing romantic love
| no: the song talks about something non-romantic, whatever it is like e.g. love for books or music'

| separates the sections and spaces around it are optional. The first section is the query; every later section carries a mandatory yes: or no: prefix. Sections may span several lines.

The same search as inline JSON, which is easier to generate from a script:

uv run jev-rerank --dir ./lyrics --top 5 --query '{
  "query": "a song about missing love",
  "yes": "the song talks about missing romantic love",
  "no": "the song talks about something non-romantic, like love for books or music"
}'

The JSON form is strict JSON, so a multi-line value uses \n escapes, and only query, yes and no are accepted. In both forms yes and no are both-or-neither: supplying one without the other is an error, because a Noul needs both sides of the judgment. A plain query containing a literal | must use the JSON form.

Criteria are part of the score cache key, so editing them re-asks the model. With --json they are echoed back in a criteria object next to query.

Criteria are the lever that matters. A query whose words carry more than one sense is the case the default question cannot settle on its own. Searching 35,257 LEGO part descriptions for get me wall decorations puts Sticker ... X-wing Starfighter ... Wall Pattern at rank 1 — "wall" in an unrelated sense. Rewriting the same search with criteria puts paintings, signs and flags in the whole top ten:

uv run jev-rerank --db ./parts.db --table-field parts_descriptions.description \
  --top 10 --query '{
    "query": "wall decorations",
    "yes": "the part is a decorative object meant to be displayed on a wall, such as a painting, mirror, poster, picture or tapestry",
    "no": "the part is not a wall decoration, including any part whose description merely contains the word wall, such as a wall panel or a pattern named wall"
  }'

Measured over labelled candidates for that query, criteria separated wanted from trap results by +0.31, where four different phrasings of the default question all managed between +0.03 and +0.09. Spend the effort on criteria, not on rewording the query.

SQLite records

Use --db instead of --dir and repeat --table-field TABLE_NAME.FIELD_NAME to search the union of the selected fields, including fields from different tables:

uv run jev-rerank --db ./library.db \
  --table-field songs.lyrics \
  --table-field articles.body \
  --query 'feeling isolated' --top 10 --json

Each non-NULL field value is a separate candidate. All selected fields share the same shortlist and top-k ranking. Fields from one row are not concatenated; that row can appear more than once if different selected fields match. Equal text in different records keeps its source identity. Repeating the same selector does not duplicate candidates.

--table-field also accepts views. Selected table columns must have a declared text type such as TEXT, CLOB, VARCHAR, NVARCHAR, or CHAR, following SQLite's text affinity rules. Missing tables/views/columns and table columns with numeric, BLOB, or undeclared types are rejected before scoring. A type literally named STRING has numeric affinity in SQLite and is also rejected. A view column computed from an expression (e.g. a || b) has no declared type in SQLite even when it always yields text, so that check is skipped for views; any non-text value it produces is still skipped at read time like any other field. Because SQLite can store BLOBs even in text columns, stored non-text values are skipped with warnings. NULLs are silently skipped and counted in stats.skipped; empty strings remain searchable candidates.

The source database is opened read-only, and schema validation and row scans use one consistent snapshot. Only the selected fields and record identifiers are read. Primary keys, including composite keys in WITHOUT ROWID tables, identify records. Rows without a usable primary key use an accessible rowid alias; tables with neither are rejected. Rowids may change after database maintenance, so declared primary keys provide more stable identifiers. Views have no primary key or rowid of their own, so their rows are numbered within each scan instead; that number shifts when rows are added, removed, or reordered upstream, causing unrelated rows to look changed on the next sync. Selectors accept table/field names with spaces or quotes when shell-quoted, but names containing dots are not supported.

JSON results contain a source object alongside rank, score, passage, and a unique sqlite://... locator in path:

{
  "type": "sqlite",
  "database": "/path/to/library.db",
  "table": "songs",
  "field": "lyrics",
  "key": {"id": 42}
}

That source metadata identifies the result for you; it is not sent to TypeSafe, which only ever sees the query and the field value itself. Text output uses the locator as its third column; --json provides the table, field, and key without needing to parse it. BLOB primary keys are encoded as {"blob_hex": "..."}.

SQLite records use the same chunking, BM25 retrieval, concurrency, and score caching as files. Each run streams the selected rows and hashes their values, reindexing only changed values and removing deleted/NULL values. This detects committed WAL changes as well as edits to the main database. The corpus is not loaded into memory, but a single row's selected values must fit in memory. Index caches are separated by database path, selected field set, and passage size. --rebuild reindexes every value; --candidates 0 scores every passage of every selected text value.

How it scales

  1. Recursively scan non-hidden regular files. UTF-8 and UTF-8-with-BOM text is accepted regardless of extension. Binary, invalid UTF-8, and unreadable files are skipped with warnings; symlinks and hidden entries are ignored. Empty files are indexed so they can match by filename.
  2. Stream files into overlapping passages (4,000 characters by default). Store the complete text in a persistent SQLite FTS5 index on disk. Later runs check file size, modification time, and change time, and reread only changed files. Deleted files are removed from the index. There is no silent truncation of long files.
  3. For large collections, BM25 selects up to max(500, 10 * top) distinct files, weighting their relative paths five times as strongly as passage text. Score the best matching passage from each shortlisted file. If lexical matches do not fill the shortlist, fill remaining slots by alphabetical path.
  4. If the collection fits within the shortlist budget, or --candidates 0 is set, score every passage of every file. Keep each file's highest passage score, so a file occupies only one result slot.
  5. A fixed pool of async workers shares one TypeSafe client. The SDK reuses HTTP connections and retries transient failures, including rate limits. The tool creates only --concurrency tasks, even for millions of passages, and retains results in a bounded top-k heap. Cached scores avoid repeated API requests.

Every scoring request carries the query and one block of text, and nothing else:

{
  "query": "2025 annual report",
  "text": "annual-report-2025.txt\n\n...document text..."
}

A file's text is its passage prefixed with the filename, because a filename often carries a title or author the contents never repeat. A database field value is sent on its own. Relative paths, directories, table and field names, record keys and the database path never leave your machine.

One Noul question serves both sources: “is the thing text describes one of the things query is asking for?” The judgment rests on the text alone — locators, table names and keys identify a result for you, they are not evidence about what it means. Its wording follows the jev-1.13 guidance: no indirection, query stated to be a description rather than an order to carry out, and the shared-word traps spelled out in the criteria. Query criteria replace those criteria. The score is TypeSafe's probability of that yes/no judgment, between 0 and 1, sorted descending. Equal scores are ordered by path. These are model judgments, not guaranteed relevance measurements.

Speed and coverage

Up to 500 documents the default scores everything, so a natural-language query is never narrowed by keyword retrieval first. Beyond that the shortlist makes API work independent of corpus size once the index is built. The initial run still reads the corpus; subsequent runs still scan file metadata. The index consumes disk space proportional to the text, including overlap and search structures. Application memory does not hold the entire corpus or create one task per file; it holds active passages, the shortlist, and top-k results. SQLite may use temporary disk space for search operations.

Keyword retrieval can miss semantic matches with different wording, and on a large corpus that, not the judgment, is usually the ceiling. For wall decorations over those 35,257 part descriptions, BM25 matches only ~105 rows; the rest of the 500-value shortlist is filled alphabetically, and parts described as Painting, Mirror or Poster are never reached at all, because they share no word with the query. Raise --candidates when recall matters more than cost. In shortlist mode, only one passage per selected file is judged; the rest of that file is not sent to TypeSafe. Increase the shortlist or select exhaustive scoring for broader coverage:

# Fast shortlist: at most 200 files get semantic judgments.
uv run jev-rerank --dir ./documents --query 'access recovery' --top 10 --candidates 200

# Full semantic coverage, including files without any matching query words.
uv run jev-rerank --dir ./documents --query 'songs about feeling isolated' --top 10 --candidates 0

Exhaustive mode makes one request per passage unless cached. It can cost more and take much longer for a large corpus. Max-passage scoring favors a document with a strong local match; it does not assess whole-document constraints or combine facts spread across separate passages. Larger passages can help when context matters.

Options

Option Default Purpose
--dir / --docs one source required Corpus directory; mutually exclusive with --db
--db one source required SQLite database; requires --table-field
--table-field required with --db Repeatable TABLE.FIELD selector; fields share one ranking
--query, --top required Search intent (see Query criteria) and result count
--candidates max(500, 10 * top) Distinct files or field values to shortlist; 0 scores everything
--concurrency 16 Maximum simultaneous API calls
--model jev-1.13.0 Model ID; also accepts TYPESAFE_DEFAULT_MODEL
--cache-dir $XDG_CACHE_HOME/jev-rerank or ~/.cache/jev-rerank Local text index and score cache
--chunk-chars 4000 Passage size, 256–16000 characters; overlap is 10%, capped at 400
--timeout 60 SDK request timeout, seconds
--retries 4 SDK retries for transient failures
--rebuild off Reindex all source text even if unchanged
--no-score-cache off Bypass cached judgments
--json off Structured output
--show off Include the complete matched file or database field text
--quiet off Suppress progress, retain warnings

Use jev-rerank --help for the full CLI. The top count must be positive; an explicit nonzero candidate count must be at least the top count. Fewer results are returned if the corpus contains fewer valid files or field values. An empty corpus returns no results.

TYPESAFE_BASE_URL overrides the API endpoint; TYPESAFE_ENDPOINT is also accepted for compatibility with the cookbook. Do not put credentials in the URL.

The model version is pinned so cached results remain meaningful. Score cache keys include the query, the question definition with any yes/no criteria, endpoint, model, relative filename/path, passage index, and content hash. Model aliases may change remotely; use --no-score-cache when fresh alias results are needed. Indexes are isolated by corpus directory and passage size. The cache holds document text locally and grows with new query/document pairs; removing the cache directory forces a fresh index and fresh judgments. A key is required even for a fully cached invocation.

Queries, passages, and the filenames of matching files are sent to TypeSafe. Relative paths, directory names, and SQLite source metadata are not. Query length and serialized state are bounded to avoid oversized requests; use smaller passages if an unusually large state is rejected. No text is silently cut to fit the API. A permanent API failure returns exit code 1 without emitting a partial ranking. Completed scores remain cached; Ctrl-C exits with code 130.

Development

uv sync
uv run pytest
uv run ruff check .

Tests use temporary corpora and a mocked TypeSafe HTTP transport; they make no external API calls. See the Python SDK and model documentation for service details.

About

A (fast!) reranker for text files and SQLite table fields, powered by TypeSafe's Jev

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages