Module: mellea.stdlib.base

Basic stdlib data structures.

Functions

mellea.stdlib.base.blockify(s: str | CBlock | Component)

blockify is a helper function that turns raw strings into CBlocks.

Classes

class mellea.stdlib.base.CBlock(value: str | None, meta: dict[str, Any] | None = None)

A CBlock is a block of content that can serve as input to or output from an LLM.

Constructor

Initializes the CBlock with a string and some metadata.

Methods

mellea.stdlib.base.CBlock.value()
Gets the value of the block.
mellea.stdlib.base.CBlock.value(v: str)
Sets the value of the block.
mellea.stdlib.base.CBlock.__str__()
Stringifies the block.
mellea.stdlib.base.CBlock.__repr__()
Provides a python-parsable representation of the block (usually).

class mellea.stdlib.base.Component()

A Component is a composite data structure that is intended to be represented to an LLM.

Methods

mellea.stdlib.base.Component.parts()
The set of all the constituent parts of the Component.
mellea.stdlib.base.Component.format_for_llm()
Formats the Component into a TemplateRepresentation or string.

class mellea.stdlib.base.ModelOutputThunk(value: str | None, meta: dict[str, Any] | None = None, parsed_repr: CBlock | Component | Any | None = None, tool_calls: dict[str, ModelToolCall] | None = None)

A ModelOutputThunk is a special type of CBlock that we know came from a model’s output. It is possible to instantiate one without the output being computed yet.

Constructor

Initializes as a cblock, optionally also with a parsed representation from an output formatter.

Methods

mellea.stdlib.base.ModelOutputThunk.is_computed()
Returns true only if this Thunk has already been filled.

class mellea.stdlib.base.ContextTurn()

A turn of model input and model output.

class mellea.stdlib.base.Context()

A Context is used to track the state of a MelleaSession.

Methods

mellea.stdlib.base.Context.reset()
Resets the context to a fresh state. Note: resetting a context does NOT free memory or clear cache. For this reason, you probably want to be calling this method from a Session.
mellea.stdlib.base.Context.insert(value: CBlock | Component, key: Any | None = None, generate_logs: list[GenerateLog] | None = None)
Each Context must define its own semantics for inserting something into the context.

Arguments

  • value: CBlock | Component: the thing to insert.
  • key: Optional[Any]: a key by which the value is indexed to. This is optional and only needed for fairly sophisticated Context types. Note that this is NOT necessarily a key that can be used for KV cache lookups!
  • generate_logs: Adding log information about the insertion. Should only be used for output objects.

mellea.stdlib.base.Context.insert_turn(turn: ContextTurn, generate_logs: list[GenerateLog] | None = None)
Insert a turn into the chat history.

Arguments

  • turn: the turn to insert.
  • generate_logs: Adding log information about the insertion. Will be bound to the output part of the turn.
None
mellea.stdlib.base.Context.copy()
Produces a deep copy of the current Context’s contents, allowing for branch-and-merge style semantics over a Context.
mellea.stdlib.base.Context._hash_for_kv_cache()
A Context is responsible for maintaining a hash representation of itself. This hash is used by backends to refer to a Context’s state.
mellea.stdlib.base.Context.linearize()
Provides a linear list of context components. This is not always possible, or None if that is not possible to construct.
mellea.stdlib.base.Context.last_output()
The last output thunk of the context.
mellea.stdlib.base.Context.last_turn()
The last input/output turn of the context.
mellea.stdlib.base.Context.logs()
Returns a list of all logs in the context.
mellea.stdlib.base.Context.get_logs_by_index(index: int)
Returns a GenerateLog for the given index.
mellea.stdlib.base.Context.last_output_and_logs(all_intermediate_results: bool = False)
Returns a ModelOutputThunk for the last output and the corresponding GenerateLog.

Arguments

  • all_intermediate_results: if False (default), only returns the Log for the that led to the final output, if True, a list of all intermediate results (including the final one) is returned.


class mellea.stdlib.base.BasicContext()

Implementing some common functionality for Contexts.

Constructor

Constructs a basic context.

Methods

mellea.stdlib.base.BasicContext.last_output()
The last output thunk of the context.
mellea.stdlib.base.BasicContext.logs()
Returns a list of all logs in the context.
mellea.stdlib.base.BasicContext.get_logs_by_index(index: int)
Returns the log of a given index from the context.
mellea.stdlib.base.BasicContext.last_output_and_logs(all_intermediate_results: bool = False)
The last output thunk of the context and the corresponding log.
mellea.stdlib.base.BasicContext.last_turn()
The last input/output turn of the context.
mellea.stdlib.base.BasicContext.__str__()
Pretty prints the context. For debugging.

class mellea.stdlib.base.LinearContext(window_size: int | None = None, log_window_size: int | None = 10, is_chat_context = True)

Initializes a linear context with unbounded window_size and is_chat=True by default.

Constructor

Initializes a linear context with unbounded window_size (log_window_size = 10) and is_chat=True by default.

Methods

mellea.stdlib.base.LinearContext.reset()
Resets the context to a fresh state. Note: resetting a context does NOT free memory or clear cache. For this reason, you probably want to be calling this method from a Session.
mellea.stdlib.base.LinearContext.insert(value: CBlock | Component, key: Any | None = None, generate_logs: list[GenerateLog] | None = None)
Inserts into the context and then shifts the window forward if necessary.
mellea.stdlib.base.LinearContext.insert_turn(turn: ContextTurn, generate_logs: list[GenerateLog] | None = None)
Insert a turn into the context.
mellea.stdlib.base.LinearContext.linearize()
Returns the underlying _ctx list.
mellea.stdlib.base.LinearContext.is_chat_history()
Returns true if everything in the LinearContext is a chat Message.
mellea.stdlib.base.LinearContext._hash_for_kv_cache()
Constructs a hash that corresponds to the string contents of the KV cache associated with this context.
mellea.stdlib.base.LinearContext.copy()
Constructs a deep copy of this Context.

class mellea.stdlib.base.SimpleContext()

A SimpleContext is a context in which each interaction is a separate and independent turn. The history of all previous turns is NOT saved. This context is intended for applications where each LLM call is (mostly) a stand-alone request. Patterns like instruct-validate-repair fall into this category. It is possible for a single turn to have many different CBlocks/Components. This can happen for a variety of reasons:
  1. Instruct/Repair is actually up to 3 (not 4!) turns: a system, a user, an assistant, and then the ALora output.
  2. It’s possible to have a Component with a bunch of other stuff in it. We haven’t decided how to represent this in Span world yet, but it’s possible that one approach would be to have any causal dependency structure represented in terms of a linearization or poset.

Constructor

Initializes a SimpleContext which contains at max one turn. with is_chat_context=True.

Methods

mellea.stdlib.base.SimpleContext.linearize()
Uses _ctx ordering.
mellea.stdlib.base.SimpleContext.reset()
Resets the context to a fresh state. Note: resetting a context does NOT free memory or clear cache. For this reason, you probably want to be calling this method from a Session.
mellea.stdlib.base.SimpleContext.insert(value: CBlock | Component, key: Any | None = None, generate_logs: list[GenerateLog] | None = None)
Adds the value to the context.
mellea.stdlib.base.SimpleContext.insert_turn(turn: ContextTurn, generate_logs: list[GenerateLog] | None = None)
Removes the previous turn and starts a new one.
mellea.stdlib.base.SimpleContext._hash_for_kv_cache()
Constructs a hash that corresponds to the string contents of the KV cache associated with this context.
mellea.stdlib.base.SimpleContext.copy()
Constructs a deep copy of this Context.

class mellea.stdlib.base.TemplateRepresentation()

Representing a component as a set of important attributes that can be consumed by the formatter.

class mellea.stdlib.base.GenerateLog()

A dataclass for capturing log entries. GenerateLog provides a structured way to include various details in log entries, making it useful for maintaining detailed records of events or operations where context and additional data are significant.

class mellea.stdlib.base.ModelToolCall()

A dataclass for capturing the tool calls a model wants to make. Provides a unified way to call tools post generation.

Methods

mellea.stdlib.base.ModelToolCall.call_func()
A helper function for calling the function/tool represented by this object.