mellea.backends.formatter
mellea.backends.formatter._simplify_model_string(input: str)
mellea.backends.formatter._get_package_name(module: str)
class mellea.backends.formatter.Formatter()
Component
s into strings and parses ModelOutputThunk
s into Component
s (or CBlock
s).
mellea.backends.formatter.Formatter.print(c: Component | CBlock)
mellea.backends.formatter.Formatter.print_context(ctx: Context)
mellea.backends.formatter.Formatter.parse(source_component: Component | CBlock, result: ModelOutputThunk)
mellea.backends.formatter.Formatter.to_chat_messages(cs: list[Component | CBlock])
class mellea.backends.formatter.TemplateFormatter(model_id: str | ModelIdentifier, template_path: str = '', use_template_cache: bool = True)
model_id
: Describes the model for which templates will be looked up. Should match the template dir structure.template_path
: Specify an alternate location where templates can be found. Will be preferred over all other template dirs even if a less exact match is found.use_template_cache
: Cache the location of the most recent templates so that future lookups don’t need to be performed. Set to false if you plan on changing the model_id or template_path after the TemplateFormatter has been created.mellea.backends.formatter.TemplateFormatter.parse(source_component: Component | CBlock, result: ModelOutputThunk)
mellea.backends.formatter.TemplateFormatter._parse(source_component: Component | CBlock, result: ModelOutputThunk)
mellea.backends.formatter.TemplateFormatter.print_context(ctx: Context)
mellea.backends.formatter.TemplateFormatter._stringify(c: str | Component | CBlock | Iterable | Mapping | TemplateRepresentation | None)
mellea.backends.formatter.TemplateFormatter.print(c: Component | CBlock)
mellea.backends.formatter.TemplateFormatter._load_template(repr: TemplateRepresentation)
mellea.backends.formatter.TemplateFormatter._get_template(root_path: str, template_name: str)
prompts/default/
directory for a templatemellea.backends.formatter.TemplateFormatter._get_model_id()
class mellea.backends.formatter.FormatterBackend(model_id: str | ModelIdentifier, formatter: Formatter, model_options: dict | None = None)
FormatterBackend
s support legacy model types.
The mellea
library was designed to support generative computing with spanned attention over generative programming primitives.
In the ideal world, context management is handled via span scope-relations and all generative programming primitives are baked into the model via fine-tuning.
I.e., the model’s instruction tuning is done in terms of generative programming primitives, and the model is then prompted with the same set of templates that were used for that tuning.
Today, most models do not yet support spans and even those that do are not properly tuned to leverage generative programming primitives.
The mellea
library supports these legacy models primarily through prompt engineering surfaced via FormatterBackends
.
A FormatterBackend
is a backend that uses hand-engineered prompts for rendering generative programming primitives to a model and parsing responses from the model back into mellea
.
By default, a FormatterBackend
uses jinja2 templates for pretty-printing, and relies on the user’s ad-hoc logic for parsing.
model_id
.
model_id
: str
: The model_id to use.formatter
: Formatter
: The formatter to use for converting components into (fragments of) prompts.model_options
: Optional[dict]
: The model options to use; if None, sensible defaults will be provided.