-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_preprocess.py
286 lines (223 loc) · 8.38 KB
/
do_preprocess.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
Do classifications with Mixtral Instruct, Mistral Instruct,
and all the Llama 2 chat models.
"""
import json
import os
import pathlib
from copy import deepcopy
from typing import Iterable
import numpy as np
import jinja2
from constants import DEFAULT_PROMPTS_PATH, PATH_DATASET_A, PATH_DATASET_B
from utils import load_all_configs, load_dataset
def split_dataset(
dataset: Iterable,
split: Iterable = (0.33, 0.33, 0.33),
random_state: int = 53,
):
"""Split a dataset into three parts.
Args:
dataset (Iterable): the dataset to split
split (Iterable): the split ratios
random_state (int): the random state
Returns:
tuple: the three splits
"""
assert len(split) == 3
np.random.seed(random_state)
np.random.shuffle(dataset) # what about equipotent classes?
# normalize the split
normalized_split = np.array(split) / np.sum(split)
assert len(normalized_split) == 3
assert np.sum(normalized_split) == 1
splitted_dataset = deepcopy(dataset)
for index, tool in enumerate(dataset):
# Compute the split
subdataset_length = len(tool["dataset"])
adapted_split = np.array(
normalized_split * subdataset_length, dtype=int
).cumsum()
# Shuffle the dataset
subdataset = deepcopy(tool["dataset"])
np.random.shuffle(subdataset)
# Split the dataset
assert len(adapted_split) >= 3
train, test, validation = np.split(subdataset, adapted_split)[
:3
] # noqa
splitted_dataset[index]["dataset"] = {
"train": train,
"test": test,
"validation": validation,
}
return splitted_dataset
# def generate_prompts(tools: str):
# pass
# DoE
# models, prompts, datasets
# Features, classification performances, speed (min, max, median, average)
# extract probabilities, etc.
# https://llama-cpp-python.readthedocs.io/en/latest/api-reference/#llama_cpp.llama_cpp.llama_sample_apply_guidance
# Models
# Don't do that, use a models_configs/model_name_config_name.yaml file
models = (
# "https://huggingface.co/TheBloke/Llama-2-7b-Chat-GGUF/blob/main/llama-2-7b-chat.Q5_K_M.gguf",
)
# Do dataset splits (evaluation, training, and testing)
# Do classifications
# Save the results
# Plot the results
def load_template(
template_path: os.PathLike,
) -> dict:
"""Load all the templates from a specified directory.
Args:
template_path (os.PathLike): the path to the templates directory.
Returns:
dict: a dictionary with all the templates.
The key of the template is its filename.
"""
# load all files as template, file name is the key of the template
template_files = os.listdir(template_path)
template_dict = {}
for template in template_files:
template_file_path = os.path.join(template_path, template)
if not os.path.isfile(template_file_path):
continue
# We have a file
with open(
template_file_path, mode="r", encoding="utf-8"
) as template_file:
template_dict[template] = jinja2.Template(template_file.read())
return template_dict
def render_template(
template: jinja2.Template,
content: dict,
):
"""Render a template with a given content.
Args:
template (jinja2.Template): the template to render
content (dict): the content to render the template with
Returns:
str: the rendered template
"""
return template.render(**content)
def generate_example_text(example: dict) -> str:
"""Generate the example from test set.
Args:
example (dict): the element extracted from the test set
Returns:
str: a formatted string
"""
return (
f"```\n{example.get('user_request', None)}[/INST]\n"
f"{example.get('command', None)}\n```"
)
def make_prompts(
prompts_templates,
models_dict,
datasets,
root=DEFAULT_PROMPTS_PATH,
random_seed: int = 5876,
):
# Models
expected_class = {}
for config_file, model_config in models_dict.items():
# Extract model info
model_info = model_config.get("model", {})
friendly_name = model_info.get("friendly_name", config_file)
prompt_config = model_config.get("prompt", {})
system_prompt_template = prompt_config.get(
"system_template", "default"
)
model_template = jinja2.Template(system_prompt_template)
for dataset_name, dataset in datasets.items():
# All the tools in the dataset
tools = sorted(
list({tool.get("tool_name", None) for tool in dataset})
)
classes_list = "\n-"
classes_list = f"-{classes_list.join(tools)}"
for tool in dataset:
tool_name = tool.get("tool_name", None)
test_set = tool.get("dataset", {}).get("test", None)
train_set = deepcopy(
tool.get("dataset", {}).get("train", None)
)
if test_set is None:
raise ValueError(
f"Test set is missing for tool {tool['tool_name']}"
)
if train_set is None:
raise ValueError(
f"Train set is missing for tool {tool['tool_name']}"
)
# randomly select an element from the train set
# using a specific seed
np.random.seed(random_seed)
selected_element = np.random.choice(train_set)
# print(model)
examples_from_train_list = generate_example_text(
selected_element
)
element = test_set[0]
for (
template_name,
prompt_template,
) in prompts_templates.items():
# Create the directory for the tools prompts
save_path = pathlib.Path(
f"{root}/{friendly_name}/{dataset_name}/"
f"{template_name}/{tool_name}"
)
save_path.mkdir(parents=True, exist_ok=True)
template_params = {
"classes_list": classes_list,
"examples_from_train_list": examples_from_train_list,
}
system_prompt = render_template(
prompt_template,
template_params,
)
for index, element in enumerate(test_set):
element_prompt = render_template(
model_template,
{
"system_prompt": system_prompt,
"prompt": element.get("user_request", None),
},
)
# Save element_prompt
element_prompt_path = save_path / f"prompt_{index}.txt"
expected_class[str(element_prompt_path)] = element.get(
"command", None
)
with element_prompt_path.open(
mode="w", encoding="utf-8"
) as prompt_file:
prompt_file.write(element_prompt)
# We can save the expected class here
root = pathlib.Path(root)
ground_truth = root / "ground_truth.json"
with ground_truth.open(
mode="w", encoding="utf-8"
) as ground_truth_file:
ground_truth_file.write(json.dumps(expected_class, indent=4))
def main():
"""The main function of the script."""
# Dataset generated via StableBeluga2 using a zero-shot approach
dataset_a = load_dataset(PATH_DATASET_A)
# Dataset generated via StableBeluga2 using a one-shot approach
dataset_b = load_dataset(PATH_DATASET_B)
datasets = {
"dataset_zero_shot": split_dataset(dataset_a),
"dataset_one_shot": split_dataset(dataset_b),
}
# Build prompts
# Load templates
templates = load_template("./system_prompt_templates")
models_configs = load_all_configs("./models_configs")
make_prompts(templates, models_configs, datasets)
if __name__ == "__main__":
main()