Skip to content

printer

RecordType module-attribute

Types allowed in the prompt records.

Indexing

Indexing(
    method: Optional[str] = None,
    ind: int = 0,
    prefix: str = "",
    suffix: Optional[str] = None,
)

The indexing method for the printer.

Source code in src/appl/core/printer.py
def __init__(
    self,
    method: Optional[str] = None,
    ind: int = 0,
    prefix: str = "",
    suffix: Optional[str] = None,
):
    """Initialize the indexing method."""
    self._method = method
    self._ind = ind
    self._prefix = prefix
    self._suffix = suffix

get_index

get_index(ind: Optional[int] = None) -> str

Get the index string for the current or given index.

Source code in src/appl/core/printer.py
def get_index(self, ind: Optional[int] = None) -> str:
    """Get the index string for the current or given index."""
    if ind is None:
        ind = self._ind
        self._ind += 1
    if ind < 0:
        raise ValueError("Indexing method does not support negative indexing.")
    return self._get_index(ind)

PrinterPop dataclass

PrinterPop()

A record to pop the last printer state from the stack.

PrinterPush dataclass

PrinterPush(
    new_role: Optional[MessageRole] = None,
    separator: Optional[str] = None,
    indexing: Optional[Indexing] = None,
    inc_indent: str = "",
    new_indent: Optional[str] = None,
    is_inline: Optional[bool] = False,
)

A record to push a new printer state to the stack.

inc_indent class-attribute instance-attribute

inc_indent: str = ''

The increment of the indent.

indexing class-attribute instance-attribute

indexing: Optional[Indexing] = None

The indexing method to be used.

is_inline class-attribute instance-attribute

is_inline: Optional[bool] = False

Whether the state is inline.

new_indent class-attribute instance-attribute

new_indent: Optional[str] = None

The new indent to be used.

new_role class-attribute instance-attribute

new_role: Optional[MessageRole] = None

The new role to be used for the message.

separator class-attribute instance-attribute

separator: Optional[str] = None

The separator to be used between texts.

PrinterState dataclass

PrinterState(
    role: Optional[MessageRole] = None,
    separator: str = "\n",
    indexing: Indexing = Indexing(None, 0),
    indent: str = "",
    is_inline: bool = False,
    is_start: bool = True,
    current_sep: str = "",
)

A state of the printer.

current_sep class-attribute instance-attribute

current_sep: str = ''

The current separator to be used between texts.

indent class-attribute instance-attribute

indent: str = ''

The indent to be used in the beginning of each line.

indexing class-attribute instance-attribute

indexing: Indexing = Indexing(None, 0)

The indexing method to be used.

is_inline class-attribute instance-attribute

is_inline: bool = False

Whether the state is inline. Inline means the first indent and indexing is inherited from the previous non-inline state.

is_start class-attribute instance-attribute

is_start: bool = True

Whether the state is at the start of the scope.

role class-attribute instance-attribute

role: Optional[MessageRole] = None

The role to be used for the message.

separator class-attribute instance-attribute

separator: str = '\n'

The separator to be used between texts.

PromptPrinter

PromptPrinter(
    states: Optional[List[PrinterState]] = None,
    is_newline: bool = True,
)

A class to print prompt records as conversation.

The printer maintains a stack of printer states about the current role, separator, indexing, and indentation.

Source code in src/appl/core/printer.py
def __init__(
    self, states: Optional[List[PrinterState]] = None, is_newline: bool = True
) -> None:
    """Initialize the prompt printer."""
    if states is None:
        states = [PrinterState()]
    self._states = states
    self._is_newline = is_newline

states property

states: List[PrinterState]

The stack of printer states.

pop

pop() -> None

Pop the last printer state from the stack.

Source code in src/appl/core/printer.py
def pop(self) -> None:
    """Pop the last printer state from the stack."""
    if len(self._states) == 1:
        raise ValueError("Cannot pop the first state.")
    self._states.pop()

push

push(data: PrinterPush) -> None

Push a new printer state to the stack.

Source code in src/appl/core/printer.py
def push(self, data: PrinterPush) -> None:
    """Push a new printer state to the stack."""
    self._push(**data.__dict__)

PromptRecords

PromptRecords()

A class represents a list of prompt records.

Source code in src/appl/core/printer.py
def __init__(self) -> None:
    """Initialize the prompt records."""
    self._records: List[RecordType] = []

records property

records: List[RecordType]

The list of records.

as_convo

as_convo() -> Conversation

Convert the prompt records to a conversation.

Source code in src/appl/core/printer.py
def as_convo(self) -> Conversation:
    """Convert the prompt records to a conversation."""
    return PromptPrinter()(self)

copy

copy() -> 'PromptRecords'

Copy the prompt records.

Source code in src/appl/core/printer.py
def copy(self) -> "PromptRecords":
    """Copy the prompt records."""
    return copy.deepcopy(self)

extend

extend(record: 'PromptRecords') -> None

Extend the prompt records with another prompt records.

Source code in src/appl/core/printer.py
def extend(self, record: "PromptRecords") -> None:
    """Extend the prompt records with another prompt records."""
    self._records.extend(record._records)

record

record(record: Union[str, RecordType]) -> None

Record a string, message, image, printer push or printer pop.

Source code in src/appl/core/printer.py
def record(self, record: Union[str, RecordType]) -> None:
    """Record a string, message, image, printer push or printer pop."""
    if isinstance(record, str):  # compatible to str
        record = StringFuture(record)
    if (
        isinstance(record, StringFuture)
        or isinstance(record, Image)
        or isinstance(record, BaseMessage)
        or isinstance(record, PrinterPush)
        or isinstance(record, PrinterPop)
    ):
        self._records.append(record)
    else:
        raise ValueError("Can only record Message, PrinterPush or PrinterPop")