Skip to main content

mellea.stdlib.funcs

Functions for Mellea operations like Instruct, Chat, etc…

Functions

act

act(action: Component, context: Context, backend: Backend) -> tuple[ModelOutputThunk, Context] | SamplingResult
Runs a generic action, and adds both the action and the result to the context. Args:
  • action: the Component from which to generate.
  • context: the context being used as a history from which to generate the response.
  • backend: the backend used to generate the response.
  • requirements: used as additional requirements when a sampling strategy is provided.
  • strategy: a SamplingStrategy that describes the strategy for validating and repairing/retrying for the instruct-validate-repair pattern. None means that no particular sampling strategy is used.
  • return_sampling_results: attach the (successful and failed) sampling attempts to the results.
  • format: if set, the BaseModel to use for constrained decoding.
  • model_options: additional model options, which will upsert into the model/backend’s defaults.
  • tool_calls: if true, tool calling is enabled.
Returns:
  • A ModelOutputThunk if return_sampling_results is False, else returns a SamplingResult.

instruct

instruct(description: str, context: Context, backend: Backend) -> tuple[ModelOutputThunk, Context] | SamplingResult
Generates from an instruction. Args:
  • description: The description of the instruction.
  • context: the context being used as a history from which to generate the response.
  • backend: the backend used to generate the response.
  • requirements: A list of requirements that the instruction can be validated against.
  • icl_examples: A list of in-context-learning examples that the instruction can be validated against.
  • grounding_context: A list of grounding contexts that the instruction can use. They can bind as variables using a (key: str, value: str | ContentBlock) tuple.
  • user_variables: A dict of user-defined variables used to fill in Jinja placeholders in other parameters. This requires that all other provided parameters are provided as strings.
  • prefix: A prefix string or ContentBlock to use when generating the instruction.
  • output_prefix: A string or ContentBlock that defines a prefix for the output generation. Usually you do not need this.
  • strategy: A SamplingStrategy that describes the strategy for validating and repairing/retrying for the instruct-validate-repair pattern. None means that no particular sampling strategy is used.
  • return_sampling_results: attach the (successful and failed) sampling attempts to the results.
  • format: If set, the BaseModel to use for constrained decoding.
  • model_options: Additional model options, which will upsert into the model/backend’s defaults.
  • tool_calls: If true, tool calling is enabled.
  • images: A list of images to be used in the instruction or None if none.

chat

chat(content: str, context: Context, backend: Backend) -> tuple[Message, Context]
Sends a simple chat message and returns the response. Adds both messages to the Context.

validate

validate(reqs: Requirement | list[Requirement], context: Context, backend: Backend) -> list[ValidationResult]
Validates a set of requirements over the output (if provided) or the current context (if the output is not provided).

query

query(obj: Any, query: str, context: Context, backend: Backend) -> tuple[ModelOutputThunk, Context]
Query method for retrieving information from an object. Args:
  • obj : The object to be queried. It should be an instance of MObject or can be converted to one if necessary.
  • query: The string representing the query to be executed against the object.
  • context: the context being used as a history from which to generate the response.
  • backend: the backend used to generate the response.
  • format: format for output parsing.
  • model_options: Model options to pass to the backend.
  • tool_calls: If true, the model may make tool calls. Defaults to False.
Returns:
  • The result of the query as processed by the backend.

transform

transform(obj: Any, transformation: str, context: Context, backend: Backend) -> tuple[ModelOutputThunk | Any, Context]
Transform method for creating a new object with the transformation applied. Args:
  • obj: The object to be queried. It should be an instance of MObject or can be converted to one if necessary.
  • transformation: The string representing the query to be executed against the object.
  • context: the context being used as a history from which to generate the response.
  • backend: the backend used to generate the response.
  • format: format for output parsing; usually not needed with transform.
  • model_options: Model options to pass to the backend.
Returns:
  • ModelOutputThunk|Any: The result of the transformation as processed by the backend. If no tools were called,
  • the return type will be always be ModelOutputThunk. If a tool was called, the return type will be the return type
  • of the function called, usually the type of the object passed in.
I