Skip to content

semlib.compare

Task

Bases: str, Enum

Comparison task to perform.

Intended to be passed to compare and similar methods, this specifies how the LLM should compare two items.

Source code in src/semlib/compare.py
class Task(str, Enum):
    """Comparison task to perform.

    Intended to be passed to [compare][semlib.compare.Compare.compare] and similar methods, this specifies how the LLM
    should compare two items.
    """

    COMPARE = "compare"
    """Ask the model to compare two items and determine their relative order.

    The model must choose either `"less"` or `"greater"`.
    """

    COMPARE_OR_ABSTAIN = "compare_or_abstain"
    """Ask the model to compare two items and determine their relative order, or abstain if unsure.

    The model must choose `"less"`, `"greater"`, or `"neither"`.
    """

    CHOOSE_GREATER = "choose_greater"
    """Ask the model to choose which of the two items (a) or (b) is greater.

    The model must choose either `"A"` or `"B"`.
    """

    CHOOSE_GREATER_OR_ABSTAIN = "choose_greater_or_abstain"
    """Ask the model to choose which of the two items (a) or (b) is greater, or abstain if unsure.

    The model must choose `"A"`, `"B"`, or `"neither"`.
    """

    CHOOSE_LESSER = "choose_lesser"
    """Ask the model to choose which of the two items (a) or (b) is lesser.

    The model must choose either `"A"` or `"B"`.
    """

    CHOOSE_LESSER_OR_ABSTAIN = "choose_lesser_or_abstain"
    """Ask the model to choose which of the two items (a) or (b) is lesser, or abstain if unsure.

    The model must choose `"A"`, `"B"`, or `"neither"`.
    """

CHOOSE_GREATER class-attribute instance-attribute

CHOOSE_GREATER = 'choose_greater'

Ask the model to choose which of the two items (a) or (b) is greater.

The model must choose either "A" or "B".

CHOOSE_GREATER_OR_ABSTAIN class-attribute instance-attribute

CHOOSE_GREATER_OR_ABSTAIN = 'choose_greater_or_abstain'

Ask the model to choose which of the two items (a) or (b) is greater, or abstain if unsure.

The model must choose "A", "B", or "neither".

CHOOSE_LESSER class-attribute instance-attribute

CHOOSE_LESSER = 'choose_lesser'

Ask the model to choose which of the two items (a) or (b) is lesser.

The model must choose either "A" or "B".

CHOOSE_LESSER_OR_ABSTAIN class-attribute instance-attribute

CHOOSE_LESSER_OR_ABSTAIN = 'choose_lesser_or_abstain'

Ask the model to choose which of the two items (a) or (b) is lesser, or abstain if unsure.

The model must choose "A", "B", or "neither".

COMPARE class-attribute instance-attribute

COMPARE = 'compare'

Ask the model to compare two items and determine their relative order.

The model must choose either "less" or "greater".

COMPARE_OR_ABSTAIN class-attribute instance-attribute

COMPARE_OR_ABSTAIN = 'compare_or_abstain'

Ask the model to compare two items and determine their relative order, or abstain if unsure.

The model must choose "less", "greater", or "neither".

Order

Bases: str, Enum

Result of a comparison.

Source code in src/semlib/compare.py
class Order(str, Enum):
    """Result of a comparison."""

    LESS = "less"
    """Item A is less than Item B."""

    GREATER = "greater"
    """Item A is greater than Item B."""

    NEITHER = "neither"
    """Items A and B are equivalent, or the model abstained from choosing."""

GREATER class-attribute instance-attribute

GREATER = 'greater'

Item A is greater than Item B.

LESS class-attribute instance-attribute

LESS = 'less'

Item A is less than Item B.

NEITHER class-attribute instance-attribute

NEITHER = 'neither'

Items A and B are equivalent, or the model abstained from choosing.

compare async

compare[T](
    a: T,
    b: T,
    /,
    *,
    by: str | None = None,
    to_str: Callable[[T], str] | None = None,
    template: str | Callable[[T, T], str] | None = None,
    task: Task | str | None = None,
    model: str | None = None,
) -> Order

Standalone version of compare.

Source code in src/semlib/compare.py
async def compare[T](
    a: T,
    b: T,
    /,
    *,
    by: str | None = None,
    to_str: Callable[[T], str] | None = None,
    template: str | Callable[[T, T], str] | None = None,
    task: Task | str | None = None,
    model: str | None = None,
) -> Order:
    """Standalone version of [compare][semlib.compare.Compare.compare]."""
    comparator = Compare(model=model)
    return await comparator.compare(a, b, by=by, to_str=to_str, template=template, task=task)

compare_sync

compare_sync[T](
    a: T,
    b: T,
    /,
    *,
    by: str | None = None,
    to_str: Callable[[T], str] | None = None,
    template: str | Callable[[T, T], str] | None = None,
    task: Task | str | None = None,
    model: str | None = None,
) -> Order

Standalone synchronous version of compare.

Source code in src/semlib/compare.py
def compare_sync[T](
    a: T,
    b: T,
    /,
    *,
    by: str | None = None,
    to_str: Callable[[T], str] | None = None,
    template: str | Callable[[T, T], str] | None = None,
    task: Task | str | None = None,
    model: str | None = None,
) -> Order:
    """Standalone synchronous version of [compare][semlib.compare.Compare.compare]."""
    comparator = Compare(model=model)
    return asyncio.run(comparator.compare(a, b, by=by, to_str=to_str, template=template, task=task))

:::