-
Notifications
You must be signed in to change notification settings - Fork 1
/
template_utils.py
60 lines (44 loc) · 2.22 KB
/
template_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from typing import Any, Dict, List
import pandas as pd
def load_template(sheet_path: str, sheet_name: str) -> pd.DataFrame:
"""Loads excel sheet from excel file.
Args:
sheet_path (str): Path to the file
sheet_name (str): Name of the sheet to load
Returns:
pd.DataFrame: Data frame containing templates
"""
with open(sheet_path, "rb") as input_file:
return pd.read_excel(input_file, sheet_name=sheet_name)
def create_instruction_templates(raw_templates: List[str], task_type, **kwargs) -> List[Dict[str, Any]]:
base_template = {
"task_type": task_type,
"prompt_language": "en_us",
# TODO: Add support for customization of the header. Currently this is not being used
"header": "Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.",
# Currently this is not used.
"prompt_template": "with_inputs",
}
if task_type == "classification":
if not "task_sub_type" in kwargs:
raise ValueError("classification task type requires `task_sub_type`")
base_template["task_sub_type"] = kwargs.pop("task_sub_type")
base_template["prompt_class"] = "ClassificationPrompt"
elif task_type == "translation":
base_template["source_language"] = kwargs.pop("source_language")
base_template["target_language"] = kwargs.pop("target_language")
base_template["prompt_class"] = "TranslationPrompt"
else:
base_template["prompt_class"] = "Prompt"
outputs = []
for template in raw_templates:
base_template_copy = base_template.copy()
base_template_copy["instruction"] = template
outputs.append(base_template_copy)
return outputs
def get_instruction_templates_from_excel(path, sheet_name, task_type, **kwargs):
templates_df = load_template(path, sheet_name)
instruction_templates = create_instruction_templates(templates_df.instruction.tolist(), task_type, **kwargs)
return instruction_templates
if __name__ == "__main__":
print(get_instruction_templates_from_excel("../resources/Template Generation.xlsx", "NER", "NER"))