Skip to content

Context manager

ContextManager #

ContextManager(openapi_service: OpenAPIService)

Shortens a datapoint's context for the specific question an annotator answers.

A long, general context (e.g. a full scene description) is often far more detail than a single question needs. This manager tunes a context down to what is relevant for the question, which keeps it within the length the backend accepts and focuses the annotator. Results are cached server-side.

Source code in src/rapidata/rapidata_client/context/context_manager.py
def __init__(self, openapi_service: OpenAPIService):
    self._openapi_service = openapi_service
    logger.debug("ContextManager initialized")

shorten_context #

shorten_context(context: str, question: str) -> str

Shorten a single context for the given question.

Parameters:

Name Type Description Default
context str

The (potentially long) context to shorten.

required
question str

The question the context will be shown alongside. The context is tuned to what this question needs.

required

Returns:

Type Description
str

The shortened context.

Source code in src/rapidata/rapidata_client/context/context_manager.py
def shorten_context(self, context: str, question: str) -> str:
    """Shorten a single context for the given question.

    Args:
        context: The (potentially long) context to shorten.
        question: The question the context will be shown alongside. The
            context is tuned to what this question needs.

    Returns:
        The shortened context.
    """
    return self.shorten_contexts([(context, question)])[0]

shorten_contexts #

shorten_contexts(
    pairs: Sequence[tuple[str, str]],
) -> list[str]

Shorten a batch of (context, question) pairs in one request.

Parameters:

Name Type Description Default
pairs Sequence[tuple[str, str]]

The (context, question) pairs to shorten.

required

Returns:

Type Description
list[str]

The shortened contexts, in the same order as pairs.

Source code in src/rapidata/rapidata_client/context/context_manager.py
def shorten_contexts(self, pairs: Sequence[tuple[str, str]]) -> list[str]:
    """Shorten a batch of ``(context, question)`` pairs in one request.

    Args:
        pairs: The ``(context, question)`` pairs to shorten.

    Returns:
        The shortened contexts, in the same order as ``pairs``.
    """
    from rapidata.api_client.models.shorten_context_endpoint_input import (
        ShortenContextEndpointInput,
    )
    from rapidata.api_client.models.shorten_context_endpoint_input_item import (
        ShortenContextEndpointInputItem,
    )

    if not pairs:
        return []

    with tracer.start_as_current_span("ContextManager.shorten_contexts"):
        output = self._openapi_service.dataset.context_shortening_api.datasets_shorten_context_post(
            shorten_context_endpoint_input=ShortenContextEndpointInput(
                items=[
                    ShortenContextEndpointInputItem(
                        context=context, question=question
                    )
                    for context, question in pairs
                ]
            )
        )
        return [item.shortened_context for item in output.items]