Skip to content

function

PromptFunc

PromptFunc(
    func: Callable,
    ctx_method: str = "new",
    compositor: Optional[Compositor] = None,
    default_return: Optional[Literal["prompt"]] = None,
    docstring_as: Optional[str] = None,
    new_ctx_func: Callable = PromptContext,
)

A wrapper for an APPL function, can be called as a normal function.

The function contains a prompt context, which could be same as or copied from its caller function, or created from scratch, or resumed from the last run.

Parameters:

  • func (Callable) –

    the function being wrapped

  • ctx_method (str, default: 'new' ) –

    the method to deal with the child context, available methods includes:

    • (default) "new" or "new_ctx": create a brand new context.
    • "copy" or "copy_ctx": copy from the parent's context, the change will not affect the parent's context.
    • "same" or "same_ctx": use the same context as the parent's, the change will affect the parent's context.
    • "resume" or "resume_ctx": resume its own context from the last run. For the first run, it will copy the parent's context.
  • compositor (Compositor, default: None ) –

    the default compositor to be used. Defaults to None.

  • default_return (str, default: None ) –

    The default return value, "prompt" means return the prompt within the function. Defaults to None.

  • docstring_as (str, default: None ) –

    Include the triple-quoted docstring as a message in the prompt. Options include "user" and "system". Defaults to None.

  • new_ctx_func (Callable, default: PromptContext ) –

    the function to create a new context. Defaults to PromptContext.

Source code in src/appl/core/function.py
def __init__(
    self,
    func: Callable,
    ctx_method: str = "new",
    compositor: Optional[Compositor] = None,
    default_return: Optional[Literal["prompt"]] = None,
    docstring_as: Optional[str] = None,
    new_ctx_func: Callable = PromptContext,
    # default_sep: Optional[str] = None,
    # ? set the default printer behavior for the prompt function?
):
    """Initialize the PromptFunc.

    Args:
        func (Callable): the function being wrapped
        ctx_method (str):
            the method to deal with the child context, available methods includes:

            - (default) "new" or "new_ctx": create a brand new context.
            - "copy" or "copy_ctx":
                copy from the parent's context, the change will not
                affect the parent's context.
            - "same" or "same_ctx":
                use the same context as the parent's, the change will
                affect the parent's context.
            - "resume" or "resume_ctx":
                resume its own context from the last run.
                For the first run, it will copy the parent's context.

        compositor (Compositor, optional):
            the default compositor to be used. Defaults to None.
        default_return (str, optional):
            The default return value, "prompt" means return the prompt within
            the function. Defaults to None.
        docstring_as (str, optional):
            Include the triple-quoted docstring as a message in the prompt.
            Options include "user" and "system". Defaults to None.
        new_ctx_func (Callable, optional):
            the function to create a new context. Defaults to PromptContext.
    """
    self._func = appl_compile(func)
    self._signature = inspect.signature(func)
    self._doc = func.__doc__
    self._name = func.__name__
    self._qualname = func.__qualname__
    self._default_ctx_method = self._process_ctx_method(ctx_method)
    self._default_compositor = compositor
    if default_return is not None and default_return != "prompt":
        raise NotImplementedError("Only support default_return='prompt' now.")
    self._default_return = default_return
    self._docstring_as = docstring_as
    self._new_ctx_func = new_ctx_func
    self._persist_ctx: Optional[PromptContext] = None
    self._reset_context_func: Optional[Callable[[], None]] = None

compiled_func property

compiled_func

The compiled function.