css - Infernet ML
Description: Infernet ML Documentation

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`

    16
    17
    18
    19
    20
    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`

    23
    24
    25
    26
    27
    28
    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`

    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    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`

    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    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`

    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    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],
            ],
        )