Skip to content

Module: css

Utilities for encoding and decoding closed source completion requests. These are meant to be used in the context of solidity contracts, and allow a standardized interface for interacting with different closed source completion providers.

These utilities are used in the css_inference_service service.

CSSEndpoint

Bases: IntEnum

Enum for CSS Inference Endpoints

Source code in src/infernet_ml/utils/codec/css.py
class CSSEndpoint(IntEnum):
    """Enum for CSS Inference Endpoints"""

    completions = 0
    embeddings = 1

CSSProvider

Bases: IntEnum

Enum for CSS Inference Providers

Source code in src/infernet_ml/utils/codec/css.py
class CSSProvider(IntEnum):
    """Enum for CSS Inference Providers"""

    OPENAI = 0
    GOOSEAI = 1
    PERPLEXITYAI = 2

decode_css_completion_request(request)

Decode a closed source completion request.

Parameters:

Name Type Description Default
request bytes

The encoded request.

required

Returns:

Type Description
tuple[str, list[ConvoMessage]]

tuple[str, list[ConvoMessage]]: The model name and the conversation messages.

Source code in src/infernet_ml/utils/codec/css.py
def decode_css_completion_request(request: bytes) -> tuple[str, list[ConvoMessage]]:
    """
    Decode a closed source completion request.

    Args:
        request (bytes): The encoded request.

    Returns:
        tuple[str, list[ConvoMessage]]: The model name and the conversation messages.
    """

    _, _, model, message_tuples = decode(
        ["uint8", "uint8", "string", "(string,string)[]"], request
    )

    messages = [ConvoMessage(role=msg[0], content=msg[1]) for msg in message_tuples]

    return model, messages

decode_css_request(request)

Decode a closed source completion request.

Parameters:

Name Type Description Default
request bytes

The encoded request.

required

Returns:

Type Description
tuple[CSSProvider, CSSEndpoint]

tuple[CSSProvider, CSSEndpoint]: The provider and endpoint of the request.

Source code in src/infernet_ml/utils/codec/css.py
def decode_css_request(request: bytes) -> tuple[CSSProvider, CSSEndpoint]:
    """
    Decode a closed source completion request.

    Args:
        request (bytes): The encoded request.

    Returns:
        tuple[CSSProvider, CSSEndpoint]: The provider and endpoint of the request.

    """

    provider_int, endpoint_int = decode(["uint8", "uint8"], request, strict=False)
    return CSSProvider(provider_int), CSSEndpoint(endpoint_int)

encode_css_completion_request(provider, endpoint, model, messages)

Encode a closed source completion request, the interface for completion is unified across all providers and models.

Parameters:

Name Type Description Default
provider CSSProvider

The provider of the completion service.

required
endpoint CSSEndpoint

The endpoint of the completion service.

required
model str

The model name.

required
messages list[ConvoMessage]

The conversation messages.

required

Returns:

Name Type Description
bytes bytes

The encoded request.

Source code in src/infernet_ml/utils/codec/css.py
def encode_css_completion_request(
    provider: CSSProvider,
    endpoint: CSSEndpoint,
    model: str,
    messages: list[ConvoMessage],
) -> bytes:
    """
    Encode a closed source completion request, the interface for completion is unified
    across all providers and models.

    Args:
        provider (CSSProvider): The provider of the completion service.
        endpoint (CSSEndpoint): The endpoint of the completion service.
        model (str): The model name.
        messages (list[ConvoMessage]): The conversation messages.

    Returns:
        bytes: The encoded request.
    """
    return encode(
        ["uint8", "uint8", "string", "(string,string)[]"],
        [
            provider,
            endpoint,
            model,
            [(m.role, m.content) for m in messages],
        ],
    )