Skip to content

context

PromptContext

PromptContext(globals_: Optional[Namespace] = None)

The context of the APPL function.

Parameters:

  • globals_ (Optional[Namespace], default: None ) –

    The global namespace of the APPL function.

Source code in src/appl/core/context.py
def __init__(self, globals_: Optional[Namespace] = None):
    """Initialize the PromptContext object.

    Args:
        globals_: The global namespace of the APPL function.
    """
    if globals_ is None:
        # create a new namespace (should inside __init__)
        globals_ = Namespace()
    self.globals = globals_
    # set default values
    if "messages" not in globals_:
        self.messages = Conversation(system_messages=[], messages=[])
    if "printer" not in globals_:
        self.printer = PromptPrinter()
    if "is_outmost" not in globals_:
        self.is_outmost = True

    # local vars start with "_"
    self.locals = Namespace()
    self._records = PromptRecords()
    self._func_name: Optional[str] = None
    self._func_docstring: Optional[str] = None
    self._docstring_as: Optional[str] = None
    self._docstring_quote_count: Optional[int] = None
    self._is_first_str: bool = True

records property

records: PromptRecords

The prompt records of the context.

add_content_part

add_content_part(content_part: ContentPart) -> None

Add a content part to the prompt context.

Source code in src/appl/core/context.py
def add_content_part(self, content_part: ContentPart) -> None:
    """Add a content part to the prompt context."""
    self.messages.extend(self.printer(content_part))
    self.records.record(content_part)

add_message

add_message(message: BaseMessage) -> None

Add a message to the prompt context.

Source code in src/appl/core/context.py
def add_message(self, message: BaseMessage) -> None:
    """Add a message to the prompt context."""
    self.messages.extend(self.printer(message))
    self.records.record(message)

add_records

add_records(
    records: PromptRecords, write_to_prompt: bool = True
) -> None

Add prompt records to the prompt context.

Source code in src/appl/core/context.py
def add_records(self, records: PromptRecords, write_to_prompt: bool = True) -> None:
    """Add prompt records to the prompt context."""
    if write_to_prompt:
        self.messages.extend(self.printer(records))
    self.records.extend(records)

add_string

add_string(string: String) -> None

Add a string to the prompt context.

Source code in src/appl/core/context.py
def add_string(self, string: String) -> None:
    """Add a string to the prompt context."""
    if isinstance(string, str):
        string = StringFuture(string)
    self.messages.extend(self.printer(string))
    self.records.record(string)

copy

copy() -> 'PromptContext'

Create a new prompt context that copies the globals.

Source code in src/appl/core/context.py
def copy(self) -> "PromptContext":
    """Create a new prompt context that copies the globals."""
    return PromptContext(globals_=deepcopy(self.globals))

inherit

inherit() -> 'PromptContext'

Create a new prompt context that has the same globals.

Source code in src/appl/core/context.py
def inherit(self) -> "PromptContext":
    """Create a new prompt context that has the same globals."""
    return PromptContext(globals_=self.globals)

pop_printer

pop_printer() -> None

Pop a printer state from the prompt context.

Source code in src/appl/core/context.py
def pop_printer(self) -> None:
    """Pop a printer state from the prompt context."""
    self.printer.pop()
    self.records.record(PrinterPop())

push_printer

push_printer(push_args: PrinterPush) -> None

Push a new printer state to the prompt context.

Source code in src/appl/core/context.py
def push_printer(self, push_args: PrinterPush) -> None:
    """Push a new printer state to the prompt context."""
    self.printer.push(push_args)
    self.records.record(push_args)

set_records

set_records(records: PromptRecords) -> None

Set the prompt records of the context.

Source code in src/appl/core/context.py
def set_records(self, records: PromptRecords) -> None:
    """Set the prompt records of the context."""
    self._records = records