Skip to main content

A Python library that uses LLMs as comparators for semantic sorting.

Project description

llm-sorter

A Python library that uses LLMs as comparators for semantic sorting. Sort any list by meaning, tone, complexity, urgency, or any criteria expressible in natural language. This library uses an OpenRouter API key to interface with LLM providers.

Installation

pip install llm-sorter

Requires Python 3.10+

Quick Start

from llm_sorter import LLMSorter

sorter = LLMSorter(api_key="your-openrouter-api-key")

# Sort by reading complexity
passages = [
    "The quantum entanglement phenomenon demonstrates non-local correlations...",
    "The cat sat on the mat.",
    "Climate change affects global weather patterns in complex ways."
]
result = sorter.sort(
    items=passages,
    prompt="Sort by reading level, simplest first"
)

# Sort support tickets by urgency
tickets = [
    "Login button color looks off",
    "CRITICAL: Production database is down, all users affected",
    "Would be nice to have dark mode"
]
urgent_first = sorter.sort(
    items=tickets,
    prompt="Sort by urgency, most critical first"
)

How It Works

llm-sorter implements merge sort with LLM-powered comparisons. Instead of numeric comparison, the model decides which of two items should come first based on your prompt.

This enables sorting by criteria that have no numeric representation: persuasiveness, professionalism, emotional intensity, policy compliance, or any semantic property. It can also enable the sorting of large lists that cannot be sorted within a single LLM call (with the tradeoff being significant more API calls, and the associated cost and latency of each call).

When to Use This

Good fit:

  • Semantic/subjective ordering (urgency, quality, tone, reading level)
  • No training data available — works zero-shot
  • Rapid prototyping or internal tools
  • Small to medium lists (n < 100-300)
  • Approximate "human-like" rankings are acceptable

Examples:

  • Sort writing samples by reading level
  • Prioritize support tickets by urgency
  • Rank answers by helpfulness
  • Order reviews by sentiment strength

When NOT to Use This

Avoid when:

  • You need strict determinism or reproducibility
  • A simple numeric key exists (timestamps, severity levels)
  • Very large n — O(n log n) API calls get expensive

Limitations

  1. Non-transitive comparisons: LLMs can produce A > B, B > C, C > A cycles
  2. Non-deterministic: Results may vary between runs
  3. Cost at scale: 100 items ≈ 664 comparisons, 1000 items ≈ 10,000 comparisons
  4. Prompt sensitivity: Small wording changes can affect results
  5. Potential bias: Length, style, and positional biases exist

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

llm_sorter-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

llm_sorter-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for llm_sorter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0022e7e3112f35329144e8c5d8a77b9ebb4d6ffb1c243dacc6994ca41ef68d3d
MD5 fb70cfc9281964513027241413a63603
BLAKE2b-256 6842a7a65996f97472da18e56abb06b552138ebdafcc1736739599e7027e6fce

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for llm_sorter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 963038aed4d3d7f0213c4de9fc26c0f95435098a80c3bd975348b3f1c3e67b18
MD5 de98268a32ec1ad5e68b1a30fd66ccd0
BLAKE2b-256 3df6f7923e80b2a1ec806a30aa78189f269d7b4ff01c7e5b974c6493439f3a33

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