kiln-ai 0.16.0__py3-none-any.whl → 0.17.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of kiln-ai might be problematic. Click here for more details.
- kiln_ai/adapters/__init__.py +2 -0
- kiln_ai/adapters/adapter_registry.py +22 -44
- kiln_ai/adapters/chat/__init__.py +8 -0
- kiln_ai/adapters/chat/chat_formatter.py +234 -0
- kiln_ai/adapters/chat/test_chat_formatter.py +131 -0
- kiln_ai/adapters/data_gen/test_data_gen_task.py +19 -6
- kiln_ai/adapters/eval/base_eval.py +8 -6
- kiln_ai/adapters/eval/eval_runner.py +4 -1
- kiln_ai/adapters/eval/g_eval.py +23 -5
- kiln_ai/adapters/eval/test_base_eval.py +166 -15
- kiln_ai/adapters/eval/test_eval_runner.py +3 -0
- kiln_ai/adapters/eval/test_g_eval.py +1 -0
- kiln_ai/adapters/fine_tune/base_finetune.py +2 -2
- kiln_ai/adapters/fine_tune/dataset_formatter.py +138 -272
- kiln_ai/adapters/fine_tune/test_base_finetune.py +10 -10
- kiln_ai/adapters/fine_tune/test_dataset_formatter.py +287 -353
- kiln_ai/adapters/fine_tune/test_fireworks_tinetune.py +3 -3
- kiln_ai/adapters/fine_tune/test_openai_finetune.py +6 -6
- kiln_ai/adapters/fine_tune/test_together_finetune.py +1 -0
- kiln_ai/adapters/fine_tune/test_vertex_finetune.py +4 -4
- kiln_ai/adapters/fine_tune/together_finetune.py +12 -1
- kiln_ai/adapters/ml_model_list.py +80 -43
- kiln_ai/adapters/model_adapters/base_adapter.py +73 -26
- kiln_ai/adapters/model_adapters/litellm_adapter.py +79 -97
- kiln_ai/adapters/model_adapters/litellm_config.py +3 -2
- kiln_ai/adapters/model_adapters/test_base_adapter.py +235 -60
- kiln_ai/adapters/model_adapters/test_litellm_adapter.py +56 -21
- kiln_ai/adapters/model_adapters/test_saving_adapter_results.py +41 -0
- kiln_ai/adapters/model_adapters/test_structured_output.py +44 -12
- kiln_ai/adapters/prompt_builders.py +0 -16
- kiln_ai/adapters/provider_tools.py +27 -9
- kiln_ai/adapters/repair/test_repair_task.py +24 -3
- kiln_ai/adapters/test_adapter_registry.py +88 -28
- kiln_ai/adapters/test_ml_model_list.py +158 -0
- kiln_ai/adapters/test_prompt_adaptors.py +17 -3
- kiln_ai/adapters/test_prompt_builders.py +3 -16
- kiln_ai/adapters/test_provider_tools.py +69 -20
- kiln_ai/datamodel/__init__.py +0 -2
- kiln_ai/datamodel/datamodel_enums.py +38 -13
- kiln_ai/datamodel/finetune.py +12 -7
- kiln_ai/datamodel/task.py +68 -7
- kiln_ai/datamodel/test_basemodel.py +2 -1
- kiln_ai/datamodel/test_dataset_split.py +0 -8
- kiln_ai/datamodel/test_models.py +33 -10
- kiln_ai/datamodel/test_task.py +168 -2
- kiln_ai/utils/config.py +3 -2
- kiln_ai/utils/dataset_import.py +1 -1
- kiln_ai/utils/logging.py +165 -0
- kiln_ai/utils/test_config.py +23 -0
- kiln_ai/utils/test_dataset_import.py +30 -0
- {kiln_ai-0.16.0.dist-info → kiln_ai-0.17.0.dist-info}/METADATA +1 -1
- {kiln_ai-0.16.0.dist-info → kiln_ai-0.17.0.dist-info}/RECORD +54 -49
- {kiln_ai-0.16.0.dist-info → kiln_ai-0.17.0.dist-info}/WHEEL +0 -0
- {kiln_ai-0.16.0.dist-info → kiln_ai-0.17.0.dist-info}/licenses/LICENSE.txt +0 -0
|
@@ -6,9 +6,13 @@ from pathlib import Path
|
|
|
6
6
|
from typing import Any, Dict, Protocol
|
|
7
7
|
from uuid import uuid4
|
|
8
8
|
|
|
9
|
-
from kiln_ai.adapters.
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
from kiln_ai.adapters.chat.chat_formatter import (
|
|
10
|
+
ChatMessage,
|
|
11
|
+
get_chat_formatter,
|
|
12
|
+
)
|
|
13
|
+
from kiln_ai.datamodel import DatasetSplit, TaskRun
|
|
14
|
+
from kiln_ai.datamodel.datamodel_enums import THINKING_DATA_STRATEGIES, ChatStrategy
|
|
15
|
+
from kiln_ai.utils.exhaustive_error import raise_exhaustive_enum_error
|
|
12
16
|
|
|
13
17
|
|
|
14
18
|
class DatasetFormat(str, Enum):
|
|
@@ -35,45 +39,23 @@ class DatasetFormat(str, Enum):
|
|
|
35
39
|
VERTEX_GEMINI = "vertex_gemini"
|
|
36
40
|
|
|
37
41
|
|
|
38
|
-
@dataclass
|
|
39
|
-
class ModelTrainingData:
|
|
40
|
-
input: str
|
|
41
|
-
system_message: str
|
|
42
|
-
final_output: str
|
|
43
|
-
# These 3 are optional, and used for COT/Thinking style multi-message responses
|
|
44
|
-
thinking_instructions: str | None = None
|
|
45
|
-
thinking: str | None = None
|
|
46
|
-
thinking_final_answer_prompt: str | None = None
|
|
47
|
-
thinking_r1_style: bool = False
|
|
48
|
-
|
|
49
|
-
def supports_cot(self) -> bool:
|
|
50
|
-
if self.thinking_r1_style:
|
|
51
|
-
raise ValueError("R1 style does not support COT")
|
|
52
|
-
|
|
53
|
-
return (
|
|
54
|
-
self.thinking_instructions is not None
|
|
55
|
-
and self.thinking is not None
|
|
56
|
-
and self.thinking_final_answer_prompt is not None
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
|
|
60
42
|
class FormatGenerator(Protocol):
|
|
61
43
|
"""Protocol for format generators"""
|
|
62
44
|
|
|
63
45
|
def __call__(
|
|
64
46
|
self,
|
|
65
|
-
|
|
47
|
+
training_chat: list[ChatMessage],
|
|
66
48
|
) -> Dict[str, Any]: ...
|
|
67
49
|
|
|
68
50
|
|
|
69
|
-
def
|
|
51
|
+
def build_training_chat(
|
|
70
52
|
task_run: TaskRun,
|
|
71
53
|
system_message: str,
|
|
72
|
-
data_strategy:
|
|
54
|
+
data_strategy: ChatStrategy,
|
|
73
55
|
thinking_instructions: str | None = None,
|
|
74
|
-
) ->
|
|
56
|
+
) -> list[ChatMessage]:
|
|
75
57
|
"""
|
|
76
|
-
Generate
|
|
58
|
+
Generate chat message list for training.
|
|
77
59
|
|
|
78
60
|
For final output, get the best task output from the task run, preferring repaired output if available.
|
|
79
61
|
|
|
@@ -84,52 +66,53 @@ def build_training_data(
|
|
|
84
66
|
final_output = task_run.repaired_output.output
|
|
85
67
|
|
|
86
68
|
thinking = None
|
|
87
|
-
thinking_final_answer_prompt = None
|
|
88
|
-
thinking_r1_style = False
|
|
89
|
-
parent_task = task_run.parent_task()
|
|
90
69
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
70
|
+
chat_formatter = get_chat_formatter(
|
|
71
|
+
data_strategy,
|
|
72
|
+
system_message,
|
|
73
|
+
task_run.input,
|
|
74
|
+
thinking_instructions,
|
|
75
|
+
)
|
|
76
|
+
# First turn already has it's content (user message)
|
|
77
|
+
chat_formatter.next_turn(None)
|
|
78
|
+
|
|
79
|
+
match data_strategy:
|
|
80
|
+
case ChatStrategy.single_turn:
|
|
81
|
+
chat_formatter.next_turn(final_output)
|
|
82
|
+
case ChatStrategy.two_message_cot:
|
|
83
|
+
thinking = get_thinking_data(task_run)
|
|
84
|
+
chat_formatter.next_turn(thinking)
|
|
85
|
+
chat_formatter.next_turn(final_output)
|
|
86
|
+
case ChatStrategy.two_message_cot_legacy:
|
|
87
|
+
thinking = get_thinking_data(task_run)
|
|
88
|
+
chat_formatter.next_turn(thinking)
|
|
89
|
+
chat_formatter.next_turn(final_output)
|
|
90
|
+
case ChatStrategy.single_turn_r1_thinking:
|
|
100
91
|
if thinking_instructions:
|
|
101
92
|
raise ValueError(
|
|
102
93
|
"Thinking instructions are not supported when fine-tuning thinking models (R1, QwQ, etc). Please remove the thinking instructions."
|
|
103
94
|
)
|
|
104
|
-
thinking_r1_style = True
|
|
105
|
-
elif (
|
|
106
|
-
data_strategy == FinetuneDataStrategy.final_and_intermediate
|
|
107
|
-
and task_run.has_thinking_training_data()
|
|
108
|
-
):
|
|
109
|
-
if not parent_task:
|
|
110
|
-
raise ValueError(
|
|
111
|
-
"TaskRuns for training required a parent Task for building a chain of thought prompts. Train without COT, or save this TaskRun to a parent Task."
|
|
112
|
-
)
|
|
113
95
|
|
|
114
|
-
|
|
96
|
+
thinking = get_thinking_data(task_run)
|
|
97
|
+
response_msg = serialize_r1_style_message(thinking, final_output)
|
|
98
|
+
chat_formatter.next_turn(response_msg)
|
|
99
|
+
case _:
|
|
100
|
+
raise_exhaustive_enum_error(data_strategy)
|
|
115
101
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
thinking_final_answer_prompt=thinking_final_answer_prompt,
|
|
131
|
-
thinking_r1_style=thinking_r1_style,
|
|
132
|
-
)
|
|
102
|
+
return chat_formatter.messages
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def get_thinking_data(task_run: TaskRun) -> str:
|
|
106
|
+
"""
|
|
107
|
+
Raises an error if thinking data is not present.
|
|
108
|
+
"""
|
|
109
|
+
thinking = task_run.thinking_training_data()
|
|
110
|
+
if thinking is None:
|
|
111
|
+
raise ValueError(
|
|
112
|
+
"Thinking data is required when fine-tuning thinking models. Please ensure your fine-tuning dataset contains reasoning or chain of thought output for every entry."
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
return thinking
|
|
133
116
|
|
|
134
117
|
|
|
135
118
|
def serialize_r1_style_message(thinking: str | None, final_output: str):
|
|
@@ -141,125 +124,78 @@ def serialize_r1_style_message(thinking: str | None, final_output: str):
|
|
|
141
124
|
return f"<think>\n{thinking}\n</think>\n\n{final_output}"
|
|
142
125
|
|
|
143
126
|
|
|
144
|
-
def
|
|
145
|
-
|
|
146
|
-
) ->
|
|
147
|
-
"""Generate OpenAI chat
|
|
127
|
+
def generate_chat_message_list(
|
|
128
|
+
training_chat: list[ChatMessage],
|
|
129
|
+
) -> list[dict[str, str | None]]:
|
|
130
|
+
"""Generate OpenAI chat list. Not the full OpenAI body, just the list of messages."""
|
|
148
131
|
|
|
149
|
-
messages: list[dict[str, str | None]] = [
|
|
150
|
-
{"role": "system", "content": training_data.system_message},
|
|
151
|
-
{"role": "user", "content": training_data.input},
|
|
152
|
-
]
|
|
132
|
+
messages: list[dict[str, str | None]] = []
|
|
153
133
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
{
|
|
158
|
-
"role": "assistant",
|
|
159
|
-
"content": serialize_r1_style_message(
|
|
160
|
-
thinking=training_data.thinking,
|
|
161
|
-
final_output=training_data.final_output,
|
|
162
|
-
),
|
|
163
|
-
}
|
|
164
|
-
]
|
|
165
|
-
)
|
|
134
|
+
for msg in training_chat:
|
|
135
|
+
if msg.role not in ["user", "assistant", "system"]:
|
|
136
|
+
raise ValueError(f"Unsupported role for OpenAI chat format: {msg.role}")
|
|
166
137
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
{"role": "assistant", "content": training_data.thinking},
|
|
173
|
-
{
|
|
174
|
-
"role": "user",
|
|
175
|
-
"content": training_data.thinking_final_answer_prompt,
|
|
176
|
-
},
|
|
177
|
-
]
|
|
138
|
+
messages.append(
|
|
139
|
+
{
|
|
140
|
+
"role": msg.role,
|
|
141
|
+
"content": msg.content,
|
|
142
|
+
}
|
|
178
143
|
)
|
|
179
144
|
|
|
180
|
-
messages
|
|
145
|
+
return messages
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def generate_chat_message_response(
|
|
149
|
+
training_chat: list[ChatMessage],
|
|
150
|
+
) -> Dict[str, Any]:
|
|
151
|
+
"""Generate OpenAI chat format with plaintext response"""
|
|
152
|
+
|
|
153
|
+
messages: list[dict[str, str | None]] = generate_chat_message_list(training_chat)
|
|
181
154
|
|
|
182
155
|
return {"messages": messages}
|
|
183
156
|
|
|
184
157
|
|
|
185
|
-
def
|
|
186
|
-
|
|
187
|
-
)
|
|
188
|
-
|
|
189
|
-
# Load and dump to ensure it's valid JSON and goes to 1 line
|
|
158
|
+
def last_message_structured_content(training_chat: list[ChatMessage]) -> Dict:
|
|
159
|
+
"""Get the structured content of the last message"""
|
|
160
|
+
if len(training_chat) < 1:
|
|
161
|
+
raise ValueError("Training chat is empty")
|
|
190
162
|
try:
|
|
191
|
-
json_data = json.loads(
|
|
163
|
+
json_data = json.loads(training_chat[-1].content or "")
|
|
192
164
|
except json.JSONDecodeError as e:
|
|
193
165
|
raise ValueError(
|
|
194
|
-
f"
|
|
195
|
-
) from e
|
|
196
|
-
json_string = json.dumps(json_data, ensure_ascii=False)
|
|
197
|
-
|
|
198
|
-
messages: list[dict[str, str | None]] = [
|
|
199
|
-
{"role": "system", "content": training_data.system_message},
|
|
200
|
-
{"role": "user", "content": training_data.input},
|
|
201
|
-
]
|
|
202
|
-
|
|
203
|
-
if training_data.thinking_r1_style:
|
|
204
|
-
messages.extend(
|
|
205
|
-
[
|
|
206
|
-
{
|
|
207
|
-
"role": "assistant",
|
|
208
|
-
"content": serialize_r1_style_message(
|
|
209
|
-
thinking=training_data.thinking,
|
|
210
|
-
final_output=training_data.final_output,
|
|
211
|
-
),
|
|
212
|
-
}
|
|
213
|
-
]
|
|
166
|
+
f"Last message is not JSON (structured), and this format expects structured data: {e}"
|
|
214
167
|
)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
messages.extend(
|
|
219
|
-
[
|
|
220
|
-
{"role": "user", "content": training_data.thinking_instructions},
|
|
221
|
-
{"role": "assistant", "content": training_data.thinking},
|
|
222
|
-
{
|
|
223
|
-
"role": "user",
|
|
224
|
-
"content": training_data.thinking_final_answer_prompt,
|
|
225
|
-
},
|
|
226
|
-
]
|
|
168
|
+
if not isinstance(json_data, dict):
|
|
169
|
+
raise ValueError(
|
|
170
|
+
"Last message is not a JSON Dictionary (structured data), and this format expects structured_data."
|
|
227
171
|
)
|
|
172
|
+
return json_data
|
|
228
173
|
|
|
229
|
-
messages.append({"role": "assistant", "content": json_string})
|
|
230
174
|
|
|
231
|
-
|
|
175
|
+
def generate_json_schema_message(
|
|
176
|
+
training_chat: list[ChatMessage],
|
|
177
|
+
) -> Dict[str, Any]:
|
|
178
|
+
"""Generate OpenAI chat format with validated JSON response"""
|
|
179
|
+
# Load and dump to ensure it's valid JSON and goes to 1 line
|
|
180
|
+
last_msg_data = last_message_structured_content(training_chat)
|
|
181
|
+
|
|
182
|
+
# re-format the json string in the last message for consistency
|
|
183
|
+
json_string = json.dumps(last_msg_data, ensure_ascii=False)
|
|
184
|
+
training_chat[-1].content = json_string
|
|
185
|
+
|
|
186
|
+
return generate_chat_message_response(training_chat)
|
|
232
187
|
|
|
233
188
|
|
|
234
189
|
def generate_chat_message_toolcall(
|
|
235
|
-
|
|
190
|
+
training_chat: list[ChatMessage],
|
|
236
191
|
) -> Dict[str, Any]:
|
|
237
192
|
"""Generate OpenAI chat format with tool call response"""
|
|
238
|
-
|
|
239
|
-
arguments = json.loads(training_data.final_output)
|
|
240
|
-
except json.JSONDecodeError as e:
|
|
241
|
-
raise ValueError(f"Invalid JSON in for tool call: {e}") from e
|
|
193
|
+
last_message_data = last_message_structured_content(training_chat)
|
|
242
194
|
|
|
243
|
-
messages: list[dict[str, Any]] =
|
|
244
|
-
{"role": "system", "content": training_data.system_message},
|
|
245
|
-
{"role": "user", "content": training_data.input},
|
|
246
|
-
]
|
|
195
|
+
messages: list[dict[str, Any]] = generate_chat_message_list(training_chat)
|
|
247
196
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
"R1 style thinking is not supported for tool call downloads. Please use a different training strategy."
|
|
251
|
-
)
|
|
252
|
-
elif training_data.supports_cot():
|
|
253
|
-
messages.extend(
|
|
254
|
-
[
|
|
255
|
-
{"role": "user", "content": training_data.thinking_instructions},
|
|
256
|
-
{"role": "assistant", "content": training_data.thinking},
|
|
257
|
-
{
|
|
258
|
-
"role": "user",
|
|
259
|
-
"content": training_data.thinking_final_answer_prompt,
|
|
260
|
-
},
|
|
261
|
-
]
|
|
262
|
-
)
|
|
197
|
+
# remove the last message, we're going to replace it with a toolcall
|
|
198
|
+
messages = messages[:-1]
|
|
263
199
|
|
|
264
200
|
messages.append(
|
|
265
201
|
{
|
|
@@ -271,8 +207,7 @@ def generate_chat_message_toolcall(
|
|
|
271
207
|
"type": "function",
|
|
272
208
|
"function": {
|
|
273
209
|
"name": "task_response",
|
|
274
|
-
|
|
275
|
-
"arguments": json.dumps(arguments, ensure_ascii=False),
|
|
210
|
+
"arguments": json.dumps(last_message_data, ensure_ascii=False),
|
|
276
211
|
},
|
|
277
212
|
}
|
|
278
213
|
],
|
|
@@ -283,76 +218,26 @@ def generate_chat_message_toolcall(
|
|
|
283
218
|
|
|
284
219
|
|
|
285
220
|
def generate_huggingface_chat_template(
|
|
286
|
-
|
|
221
|
+
training_chat: list[ChatMessage],
|
|
287
222
|
) -> Dict[str, Any]:
|
|
288
223
|
"""Generate HuggingFace chat template"""
|
|
289
224
|
|
|
290
|
-
conversations: list[dict[str, Any]] =
|
|
291
|
-
{"role": "system", "content": training_data.system_message},
|
|
292
|
-
{"role": "user", "content": training_data.input},
|
|
293
|
-
]
|
|
294
|
-
|
|
295
|
-
if training_data.thinking_r1_style:
|
|
296
|
-
conversations.extend(
|
|
297
|
-
[
|
|
298
|
-
{
|
|
299
|
-
"role": "assistant",
|
|
300
|
-
"content": serialize_r1_style_message(
|
|
301
|
-
thinking=training_data.thinking,
|
|
302
|
-
final_output=training_data.final_output,
|
|
303
|
-
),
|
|
304
|
-
}
|
|
305
|
-
]
|
|
306
|
-
)
|
|
307
|
-
return {"conversations": conversations}
|
|
308
|
-
|
|
309
|
-
if training_data.supports_cot():
|
|
310
|
-
conversations.extend(
|
|
311
|
-
[
|
|
312
|
-
{"role": "user", "content": training_data.thinking_instructions},
|
|
313
|
-
{"role": "assistant", "content": training_data.thinking},
|
|
314
|
-
{
|
|
315
|
-
"role": "user",
|
|
316
|
-
"content": training_data.thinking_final_answer_prompt,
|
|
317
|
-
},
|
|
318
|
-
]
|
|
319
|
-
)
|
|
320
|
-
|
|
321
|
-
conversations.append({"role": "assistant", "content": training_data.final_output})
|
|
225
|
+
conversations: list[dict[str, Any]] = generate_chat_message_list(training_chat)
|
|
322
226
|
|
|
323
227
|
return {"conversations": conversations}
|
|
324
228
|
|
|
325
229
|
|
|
326
230
|
def generate_huggingface_chat_template_toolcall(
|
|
327
|
-
|
|
231
|
+
training_chat: list[ChatMessage],
|
|
328
232
|
) -> Dict[str, Any]:
|
|
329
233
|
"""Generate HuggingFace chat template with tool calls"""
|
|
330
|
-
|
|
331
|
-
arguments = json.loads(training_data.final_output)
|
|
332
|
-
except json.JSONDecodeError as e:
|
|
333
|
-
raise ValueError(f"Invalid JSON in for tool call: {e}") from e
|
|
234
|
+
last_message_data = last_message_structured_content(training_chat)
|
|
334
235
|
|
|
335
236
|
# See https://huggingface.co/docs/transformers/en/chat_templating
|
|
336
|
-
conversations: list[dict[str, Any]] =
|
|
337
|
-
{"role": "system", "content": training_data.system_message},
|
|
338
|
-
{"role": "user", "content": training_data.input},
|
|
339
|
-
]
|
|
237
|
+
conversations: list[dict[str, Any]] = generate_chat_message_list(training_chat)
|
|
340
238
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
"R1 style thinking is not supported for tool call downloads. Please use a different training strategy."
|
|
344
|
-
)
|
|
345
|
-
elif training_data.supports_cot():
|
|
346
|
-
conversations.extend(
|
|
347
|
-
[
|
|
348
|
-
{"role": "user", "content": training_data.thinking_instructions},
|
|
349
|
-
{"role": "assistant", "content": training_data.thinking},
|
|
350
|
-
{
|
|
351
|
-
"role": "user",
|
|
352
|
-
"content": training_data.thinking_final_answer_prompt,
|
|
353
|
-
},
|
|
354
|
-
]
|
|
355
|
-
)
|
|
239
|
+
# remove the last message, we're going to replace it with a toolcall
|
|
240
|
+
conversations = conversations[:-1]
|
|
356
241
|
|
|
357
242
|
conversations.append(
|
|
358
243
|
{
|
|
@@ -363,7 +248,7 @@ def generate_huggingface_chat_template_toolcall(
|
|
|
363
248
|
"function": {
|
|
364
249
|
"name": "task_response",
|
|
365
250
|
"id": str(uuid4()).replace("-", "")[:9],
|
|
366
|
-
"arguments":
|
|
251
|
+
"arguments": last_message_data,
|
|
367
252
|
},
|
|
368
253
|
}
|
|
369
254
|
],
|
|
@@ -373,60 +258,41 @@ def generate_huggingface_chat_template_toolcall(
|
|
|
373
258
|
return {"conversations": conversations}
|
|
374
259
|
|
|
375
260
|
|
|
261
|
+
VERTEX_GEMINI_ROLE_MAP = {
|
|
262
|
+
"system": "system",
|
|
263
|
+
"user": "user",
|
|
264
|
+
"assistant": "model",
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
|
|
376
268
|
def generate_vertex_gemini(
|
|
377
|
-
|
|
269
|
+
training_chat: list[ChatMessage],
|
|
378
270
|
) -> Dict[str, Any]:
|
|
379
|
-
"""Generate Vertex Gemini
|
|
271
|
+
"""Generate Vertex Gemini format (flash and pro)"""
|
|
380
272
|
# See https://cloud.google.com/vertex-ai/generative-ai/docs/models/gemini-supervised-tuning-prepare
|
|
381
273
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
274
|
+
# System message get's it's own entry in top level UI
|
|
275
|
+
system_instruction = training_chat[0].content
|
|
276
|
+
|
|
277
|
+
messages: list[Dict[str, Any]] = []
|
|
278
|
+
for msg in training_chat[1:]:
|
|
279
|
+
messages.append(
|
|
385
280
|
{
|
|
386
|
-
"
|
|
281
|
+
"role": VERTEX_GEMINI_ROLE_MAP[msg.role],
|
|
282
|
+
"parts": [{"text": msg.content}],
|
|
387
283
|
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
{
|
|
392
|
-
"role": "
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
"systemInstruction": {
|
|
288
|
+
"role": "system",
|
|
393
289
|
"parts": [
|
|
394
290
|
{
|
|
395
|
-
"text":
|
|
291
|
+
"text": system_instruction,
|
|
396
292
|
}
|
|
397
293
|
],
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
if training_data.thinking_r1_style:
|
|
402
|
-
raise ValueError(
|
|
403
|
-
"R1 style thinking is not supported for Vertex Gemini. Please use a different training strategy."
|
|
404
|
-
)
|
|
405
|
-
elif training_data.supports_cot():
|
|
406
|
-
contents.extend(
|
|
407
|
-
[
|
|
408
|
-
{
|
|
409
|
-
"role": "user",
|
|
410
|
-
"parts": [{"text": training_data.thinking_instructions}],
|
|
411
|
-
},
|
|
412
|
-
{"role": "model", "parts": [{"text": training_data.thinking}]},
|
|
413
|
-
{
|
|
414
|
-
"role": "user",
|
|
415
|
-
"parts": [{"text": training_data.thinking_final_answer_prompt}],
|
|
416
|
-
},
|
|
417
|
-
]
|
|
418
|
-
)
|
|
419
|
-
|
|
420
|
-
contents.append(
|
|
421
|
-
{
|
|
422
|
-
"role": "model",
|
|
423
|
-
"parts": [{"text": training_data.final_output}],
|
|
424
|
-
}
|
|
425
|
-
)
|
|
426
|
-
|
|
427
|
-
return {
|
|
428
|
-
"systemInstruction": system_instruction,
|
|
429
|
-
"contents": contents,
|
|
294
|
+
},
|
|
295
|
+
"contents": messages,
|
|
430
296
|
}
|
|
431
297
|
|
|
432
298
|
|
|
@@ -462,7 +328,7 @@ class DatasetFormatter:
|
|
|
462
328
|
self,
|
|
463
329
|
split_name: str,
|
|
464
330
|
format_type: DatasetFormat,
|
|
465
|
-
data_strategy:
|
|
331
|
+
data_strategy: ChatStrategy,
|
|
466
332
|
path: Path | None = None,
|
|
467
333
|
) -> Path:
|
|
468
334
|
"""
|
|
@@ -508,13 +374,13 @@ class DatasetFormatter:
|
|
|
508
374
|
f"Task run {run_id} not found. This is required by this dataset."
|
|
509
375
|
)
|
|
510
376
|
|
|
511
|
-
|
|
377
|
+
training_chat = build_training_chat(
|
|
512
378
|
task_run=task_run,
|
|
513
379
|
system_message=self.system_message,
|
|
514
380
|
data_strategy=data_strategy,
|
|
515
381
|
thinking_instructions=self.thinking_instructions,
|
|
516
382
|
)
|
|
517
|
-
example = generator(
|
|
383
|
+
example = generator(training_chat)
|
|
518
384
|
# Allow non-ascii characters in the dataset.
|
|
519
385
|
# Better readability for non-English users. If you don't support UTF-8... you should.
|
|
520
386
|
f.write(json.dumps(example, ensure_ascii=False) + "\n")
|
|
@@ -4,13 +4,13 @@ import pytest
|
|
|
4
4
|
|
|
5
5
|
from kiln_ai.adapters.fine_tune.base_finetune import (
|
|
6
6
|
BaseFinetuneAdapter,
|
|
7
|
-
FinetuneDataStrategy,
|
|
8
7
|
FineTuneParameter,
|
|
9
8
|
FineTuneStatus,
|
|
10
9
|
FineTuneStatusType,
|
|
11
10
|
)
|
|
12
11
|
from kiln_ai.datamodel import DatasetSplit, Task
|
|
13
12
|
from kiln_ai.datamodel import Finetune as FinetuneModel
|
|
13
|
+
from kiln_ai.datamodel.datamodel_enums import ChatStrategy
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
class MockFinetune(BaseFinetuneAdapter):
|
|
@@ -162,7 +162,7 @@ async def test_create_and_start_success(mock_dataset):
|
|
|
162
162
|
train_split_name="train",
|
|
163
163
|
parameters={"epochs": 10}, # Required parameter
|
|
164
164
|
system_message="Test system message",
|
|
165
|
-
data_strategy=
|
|
165
|
+
data_strategy=ChatStrategy.single_turn,
|
|
166
166
|
thinking_instructions=None,
|
|
167
167
|
)
|
|
168
168
|
|
|
@@ -176,7 +176,7 @@ async def test_create_and_start_success(mock_dataset):
|
|
|
176
176
|
assert datamodel.parameters == {"epochs": 10}
|
|
177
177
|
assert datamodel.system_message == "Test system message"
|
|
178
178
|
assert datamodel.path.exists()
|
|
179
|
-
assert datamodel.data_strategy ==
|
|
179
|
+
assert datamodel.data_strategy == ChatStrategy.single_turn
|
|
180
180
|
assert datamodel.thinking_instructions is None
|
|
181
181
|
|
|
182
182
|
|
|
@@ -192,7 +192,7 @@ async def test_create_and_start_with_all_params(mock_dataset):
|
|
|
192
192
|
description="Custom Description",
|
|
193
193
|
validation_split_name="test",
|
|
194
194
|
system_message="Test system message",
|
|
195
|
-
data_strategy=
|
|
195
|
+
data_strategy=ChatStrategy.two_message_cot,
|
|
196
196
|
thinking_instructions="Custom thinking instructions",
|
|
197
197
|
)
|
|
198
198
|
|
|
@@ -202,7 +202,7 @@ async def test_create_and_start_with_all_params(mock_dataset):
|
|
|
202
202
|
assert datamodel.parameters == {"epochs": 10, "learning_rate": 0.001}
|
|
203
203
|
assert datamodel.system_message == "Test system message"
|
|
204
204
|
assert adapter.datamodel == datamodel
|
|
205
|
-
assert datamodel.data_strategy ==
|
|
205
|
+
assert datamodel.data_strategy == ChatStrategy.two_message_cot
|
|
206
206
|
assert datamodel.thinking_instructions == "Custom thinking instructions"
|
|
207
207
|
|
|
208
208
|
# load the datamodel from the file, confirm it's saved
|
|
@@ -221,7 +221,7 @@ async def test_create_and_start_invalid_parameters(mock_dataset):
|
|
|
221
221
|
parameters={"learning_rate": 0.001}, # Missing required 'epochs'
|
|
222
222
|
system_message="Test system message",
|
|
223
223
|
thinking_instructions=None,
|
|
224
|
-
data_strategy=
|
|
224
|
+
data_strategy=ChatStrategy.single_turn,
|
|
225
225
|
)
|
|
226
226
|
|
|
227
227
|
|
|
@@ -240,7 +240,7 @@ async def test_create_and_start_no_parent_task():
|
|
|
240
240
|
train_split_name="train",
|
|
241
241
|
parameters={"epochs": 10},
|
|
242
242
|
system_message="Test system message",
|
|
243
|
-
data_strategy=
|
|
243
|
+
data_strategy=ChatStrategy.single_turn,
|
|
244
244
|
thinking_instructions=None,
|
|
245
245
|
)
|
|
246
246
|
|
|
@@ -263,7 +263,7 @@ async def test_create_and_start_no_parent_task_path():
|
|
|
263
263
|
train_split_name="train",
|
|
264
264
|
parameters={"epochs": 10},
|
|
265
265
|
system_message="Test system message",
|
|
266
|
-
data_strategy=
|
|
266
|
+
data_strategy=ChatStrategy.single_turn,
|
|
267
267
|
thinking_instructions=None,
|
|
268
268
|
)
|
|
269
269
|
|
|
@@ -282,7 +282,7 @@ async def test_create_and_start_invalid_train_split(mock_dataset):
|
|
|
282
282
|
train_split_name="invalid_train", # Invalid train split
|
|
283
283
|
parameters={"epochs": 10},
|
|
284
284
|
system_message="Test system message",
|
|
285
|
-
data_strategy=
|
|
285
|
+
data_strategy=ChatStrategy.single_turn,
|
|
286
286
|
thinking_instructions=None,
|
|
287
287
|
)
|
|
288
288
|
|
|
@@ -302,6 +302,6 @@ async def test_create_and_start_invalid_validation_split(mock_dataset):
|
|
|
302
302
|
validation_split_name="invalid_test", # Invalid validation split
|
|
303
303
|
parameters={"epochs": 10},
|
|
304
304
|
system_message="Test system message",
|
|
305
|
-
data_strategy=
|
|
305
|
+
data_strategy=ChatStrategy.single_turn,
|
|
306
306
|
thinking_instructions=None,
|
|
307
307
|
)
|