> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mellea.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# mellea.formatters.granite.base.io

> Input and output processing for chat completions-like APIs.

export const SidebarFix = () => <script dangerouslySetInnerHTML={{
  __html: `
        (function () {
          const INTERVAL_MS = 500;

          const upgradeSidebar = () => {
            const links = document.querySelectorAll('a[href^="#"]');

            links.forEach((link) => {
              if (link.dataset.badged === "true") return;

              const rawText = (link.textContent || "").trim();

              // ========== FUNC ==========
              if (rawText.startsWith("FUNC ")) {
                const label = rawText.replace(/^FUNC\\s+/, "").trim();

                while (link.firstChild) link.removeChild(link.firstChild);

                // 👉 Make the whole link a single flex row & prevent wrapping
                link.style.display = "flex";
                link.style.alignItems = "center";
                link.style.whiteSpace = "nowrap";
                link.style.columnGap = "0.5rem";

                const badge = document.createElement("span");
                badge.style.marginRight = "0.5rem";
                badge.style.display = "inline-flex";
                badge.style.alignItems = "center";
                badge.style.borderRadius = "9999px";
                badge.style.padding = "0rem 0.6rem";
                badge.style.fontSize = "0.5rem";
                badge.style.fontWeight = "700";
                badge.style.letterSpacing = "0.05em";
                badge.style.backgroundColor = "rgba(48, 100, 227, 0.20)";
                badge.style.color = "#1D4ED8";

                badge.textContent = "FUNC";

                link.appendChild(badge);
                link.appendChild(document.createTextNode(label));
                link.dataset.badged = "true";
                return;
              }

              // ========== CLASS ==========
              if (rawText.startsWith("CLASS ")) {
                const label = rawText.replace(/^CLASS\\s+/, "").trim();

                while (link.firstChild) link.removeChild(link.firstChild);

                // 👉 Same flex / nowrap treatment for class links
                link.style.display = "flex";
                link.style.alignItems = "center";
                link.style.whiteSpace = "nowrap";
                link.style.columnGap = "0.5rem";

                const badge = document.createElement("span");
                badge.style.marginRight = "0.5rem";
                badge.style.display = "inline-flex";
                badge.style.alignItems = "center";
                badge.style.borderRadius = "9999px";
                badge.style.padding = "0rem 0.6rem";
                badge.style.fontSize = "0.5rem";
                badge.style.fontWeight = "700";
                badge.style.letterSpacing = "0.05em";
                badge.style.backgroundColor = "rgba(74, 222, 128, 0.20)";
                badge.style.color = "#15803D";

                badge.textContent = "CLASS";

                link.appendChild(badge);
                link.appendChild(document.createTextNode(label));
                link.dataset.badged = "true";
                return;
              }
            });
          };

          upgradeSidebar();
          setInterval(upgradeSidebar, INTERVAL_MS);
        })();
      `
}} />;

<SidebarFix />

Input and output processing for chat completions-like APIs.

Classes and functions that implement common aspects of input and output string
processing for chat completions-like APIs.

## Classes

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#4ADE8033]/20 text-[#15803D]">CLASS</span> `InputProcessor` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L19" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Interface for generic input processors.

An input processor exposes an API to transform a chat completion request into a string prompt.

<div className="h-8" />

**Methods:**

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `transform` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L26" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
transform(self, chat_completion: ChatCompletion, add_generation_prompt: bool = True) -> str
```

Convert the structured representation of the inputs to a completion request.

Converts the structured representation of the inputs to a completion request into
the string representation of the tokens that should be sent to the model to
implement said request.

**Args:**

* `chat_completion`: Structured representation of the inputs
  to the chat completion request.
* `add_generation_prompt`: If `True`, the returned prompt string will
  contain a prefix of the next assistant response for use as a prompt to a
  generation request. Otherwise, the prompt will only contain the messages
  and documents in `chat_completion`. Defaults to `True`.

**Returns:**

* String that can be passed to the model's tokenizer to create a prompt
  for generation.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#4ADE8033]/20 text-[#15803D]">CLASS</span> `OutputProcessor` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L49" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Base class for generic output processors.

An output processor exposes an API to transform model output into a structured
representation of the information.

This interface is generic; see individual classes for more specific arguments.

<div className="h-8" />

**Methods:**

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `transform` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L59" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
transform(self, model_output: str, chat_completion: ChatCompletion | None = None) -> AssistantMessage
```

Convert the model output into a structured representation.

Convert the model output generated into a structured representation of the
information.

**Args:**

* `model_output`: String output of the generation request, potentially
  incomplete if it was a streaming request.
* `chat_completion`: The chat completion request that
  produced `model_output`. Parameters of the request can determine how
  the output should be decoded. Defaults to `None`.

**Returns:**

* The parsed output so far, as an instance of
  :class:`AssistantMessage` possibly with model-specific extension fields.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#4ADE8033]/20 text-[#15803D]">CLASS</span> `ChatCompletionRewriter` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L80" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Base class for objects that rewrite a chat completion request.

Base class for objects that rewrite a chat completion request into another chat
completion request.

<div className="h-8" />

**Methods:**

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `transform` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L87" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
transform(**kwargs) -> ChatCompletion
```

Rewrite a chat completion request into another one.

Rewrite a chat completion request into another chat completion request.
Does not modify the original :class:`ChatCompletion` object.

**Args:**

* `chat_completion`: Original chat completion
  request, either as a :class:`ChatCompletion` dataclass, the JSON string
  representation, or a plain dictionary.
* `**kwargs`: Additional keyword arguments forwarded to the underlying
  :meth:`_transform` implementation.

**Returns:**

* Rewritten copy of the original chat completion request.

**Raises:**

* `TypeError`: If `chat_completion` is not a :class:`ChatCompletion` object,
  a JSON string, or a dictionary.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#4ADE8033]/20 text-[#15803D]">CLASS</span> `ChatCompletionResultProcessor` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L136" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Base class for chat completion result processors.

Base class for objects that convert the raw json result of a chat completion request
into a JSON object with model-specific postprocessing applied.

<div className="h-8" />

**Methods:**

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `transform` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L143" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
transform(self, chat_completion_response: ChatCompletionResponse | dict | pydantic.BaseModel, chat_completion: ChatCompletion | None = None) -> ChatCompletionResponse
```

Parse and post-process the result of a chat completion request.

**Args:**

* `chat_completion_response`:
  Response to a chat completion request, provided as a parsed
  :class:`ChatCompletionResponse` dataclass, a raw dictionary, or
  another Pydantic model.
* `chat_completion`: The original chat completion
  request that produced `chat_completion_response`. Required by
  some implementations to decode references back to the original
  request. Defaults to `None`.

**Returns:**

* Post-processed copy of the chat completion
  response with model-specific transformations applied.

**Raises:**

* `TypeError`: If `chat_completion_response` is not a supported type.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#4ADE8033]/20 text-[#15803D]">CLASS</span> `Retriever` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L197" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Base class for document retrievers.

Provides APIs for searching by text snippet and for inserting new documents.

<div className="h-8" />

**Methods:**

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />

#### <span className="ml-2 inline-flex items-center rounded-full px-2 py-1 text-[0.7rem] font-bold tracking-wide bg-[#3064E3]/20 text-[#1D4ED8]">FUNC</span> `retrieve` <sup><a href="https://github.com/generative-computing/mellea/blob/v0.6.0/mellea/formatters/granite/base/io.py#L204" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

```python theme={null}
retrieve(self, query: str, top_k: int = 10) -> list[Document]
```

Retrieve the top-k matching documents for a query from the corpus.

**Args:**

* `query`: Query string to use for lookup.
* `top_k`: Maximum number of results to return. Defaults to `10`.

**Returns:**

* list\[Document]: List of the top-k matching :class:`Document` objects,
  each with fields such as `text`, `title`, and `doc_id`.

<div className="w-full h-px bg-gray-200 dark:bg-gray-700 my-4" />
