Skip to content

Commit

Permalink
changed input and output of templates to "input_fields" and "referenc…
Browse files Browse the repository at this point in the history
…e_ fields" - Non backward compatible (#1030)

* changed input and output of templates

to "input_fields" and "reference_ fields" .

This is to continue  the work done on tasks.

Signed-off-by: Yoav Katz <katz@il.ibm.com>

* Fixed type hint

Signed-off-by: Yoav Katz <katz@il.ibm.com>

* Documentation update

Signed-off-by: Yoav Katz <katz@il.ibm.com>

---------

Signed-off-by: Yoav Katz <katz@il.ibm.com>
Signed-off-by: Benjamin Sznajder <benjams@il.ibm.com>
  • Loading branch information
yoavkatz authored and BenjSz committed Jul 29, 2024
1 parent 1d7b1ef commit 6b71581
Show file tree
Hide file tree
Showing 9 changed files with 426 additions and 288 deletions.
30 changes: 16 additions & 14 deletions docs/docs/adding_template.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,32 @@ Making Your Custom Template
----------------------------

In order to make your own template, you need to create a class inheriting from `Template` and
implementing its two abstract methods:
implementing its abstract methods:

.. code-block:: python
@abstractmethod
def inputs_to_source(self, inputs: Dict[str, object]) -> Tuple[str, str]:
@abstractmethod
def input_fields_to_source(self, input_fields: Dict[str, object]) -> str:
"""Create the textual input for the model from the input fields"""
pass
@abstractmethod
def outputs_to_target_and_references(
self, outputs: Dict[str, object]
) -> Tuple[str, List[str]]:
def reference_fields_to_target_and_references(self, reference_fields: Dict[str, object]) -> Tuple[str, List[str]]:
"""Create a list of references from the reference fields. Also returns one of the references
as the 'target' - the reference used if the instance is used as a demonstration."
pass
For instance:
For instance, this templates passes all the input fields to the model as a json string.
It also formats the references , by taking two of the dataset reference fields the 'top_answer' and 'alternative_answer'.
.. code-block:: python
class MyCustomTemplate(Template):
def inputs_to_source(self, inputs: Dict[str, object]) -> Tuple[str, str]:
return str(inputs) # use all the task inputs fields in their dictionary look
def outputs_to_target_and_references(
self, outputs: Dict[str, object]
) -> Tuple[str, List[str]]:
return outputs["label"], [outputs["label"]]
def input_fields_to_source(self, inputs_fields: Dict[str, object]) -> str:
return json.dumps(inputs_fields) # provide the json string with all fields as the input to the model
def reference_fields_to_target_and_references(self, reference_fields: Dict[str, object]) -> Tuple[str, List[str]]
return outputs_fields["top_answer"], # target
[outputs_fields["top_answer"],outputs_fields["alternative_answer"]] # all references
5 changes: 4 additions & 1 deletion src/unitxt/llm_as_judge.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def _get_input_instances(self, task_data: List[Dict]) -> List:
instance = SequentialOperator(
steps=[template, "formats.empty"]
).process_instance(
{"inputs": task_data_instance, "outputs": task_data_instance}
{
"input_fields": task_data_instance,
"reference_fields": task_data_instance,
}
)
instances.append(instance["source"])
"""
Expand Down
4 changes: 2 additions & 2 deletions src/unitxt/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Task(InstanceOperator):
Will not overwrite values if already provided in a given instance.
The output instance contains three fields:
"inputs" whose value is a sub-dictionary of the input instance, consisting of all the fields listed in Arg 'input_fields'.
"outputs" -- for the fields listed in Arg "outputs".
"input_fields" whose value is a sub-dictionary of the input instance, consisting of all the fields listed in Arg 'input_fields'.
"reference_fields" -- for the fields listed in Arg "reference_fields".
"metrics" -- to contain the value of Arg 'metrics'
"""

Expand Down
Loading

0 comments on commit 6b71581

Please sign in to comment.