unitxt.formats module

class unitxt.formats.Format(*argv, **kwargs)

Bases: StreamInstanceOperator

class unitxt.formats.SystemFormat(*argv, **kwargs)

Bases: Format

Generates the whole input to the model, from constant strings that are given as args, and from values found in specified fields of the instance.

SystemFormat expects the input instance to contain: 1. A field named “source” whose value is a string verbalizing the original values in the instance (as read from the source dataset), in the context of the underlying task. 2. A field named “instruction” that contains a (non-None) string. 3. A field named with the value in arg ‘demos_field’, containing a list of dicts, each dict with fields “source” and “target”, representing a single demo.

SystemFormat formats the above fields into a single string to be inputted to the model. This string overwrites field “source” of the instance. Formatting is driven by two args: ‘demo_format’ and ‘model_input_format’. SystemFormat also pops field “instruction” and the field containing the demos out from the input instance.

Parameters:
  • demos_field (str) – the name of the field that contains the demos, being a list of dicts, each with “source” and “target” keys

  • demo_format (str) – formatting string for a single demo, combining fields “source” and “target”

  • model_input_format (str) –

  • instance) (and "source" of the input) –

  • demos (together with) –

Example

when input instance:

{
    "source": "1+1",
    "target": "2",
    "instruction": "Solve the math exercises.",
    "demos": [{"source": "1+2", "target": "3"}, {"source": "4-2", "target": "2"}]
}

is processed by

system_format = SystemFormat(
    demos_field="demos",
    demo_format="Input: {source}\nOutput: {target}\n\n",
    model_input_format="Instruction: {instruction}\n\n{demos}Input: {source}\nOutput: ",
)

the resulting instance is:

{
    "target": "2",
    "source": "Instruction: Solve the math exercises.\n\nInput: 1+2\nOutput: 3\n\nInput: 4-2\nOutput: 2\n\nInput: 1+1\nOutput: ",
}