MCPcopy Create free account
hub / github.com/Continual-Intelligence/SEAL / build_train_sequences

Function build_train_sequences

general-knowledge/src/utils.py:193–237  ·  view source on GitHub ↗

Turn a raw completion + article context into the list of sequences that the inner-loop fine-tuning consumes. - `---` splits into separate training examples - if `split_newlines`, each line inside a segment becomes its own example - the original article context is always app

(
    completion_raw: str,
    context: str,
    title: str,
    *,
    split_newlines: bool = False,
    add_context: bool = True,
)

Source from the content-addressed store, hash-verified

191MAX_TRAIN_SEQS_PER_COMPLETION=30
192
193def build_train_sequences(
194 completion_raw: str,
195 context: str,
196 title: str,
197 *,
198 split_newlines: bool = False,
199 add_context: bool = True,
200) -> List[str]:
201 """
202 Turn a raw completion + article context into the list of sequences
203 that the inner-loop fine-tuning consumes.
204
205 - `---` splits into separate training examples
206 - if `split_newlines`, each line inside a segment becomes its own example
207 - the original article context is always appended as the last example
208 - each example is prefixed with the title
209 - if the second sequence begins with "1.", remove the first one
210 """
211 # For chain-of-thought, keep only content after the first "\nImplications:" marker
212 m = re.search(r"\nImplications:\s*", completion_raw)
213 if m:
214 completion_raw = completion_raw[m.end():].lstrip()
215
216 segs = _split_segments(completion_raw) or [completion_raw.strip()]
217 if split_newlines:
218 if re.search(r'Question\s+\d+:', completion_raw) and re.search(r'Answer\s*:', completion_raw):
219 # deal with self-QA responses
220 # split wherever a new "Question N:" begins
221 segs = re.split(r'\n(?=Question\s+\d+:)', completion_raw.strip())
222 # ensure the very first segment has an explicit "Question 1:" prefix
223 if not segs[0].lstrip().startswith("Question"):
224 segs[0] = "Question 1: " + segs[0].strip()
225 else:
226 # deal with responses where first line is along the lines of "Sure, let's give a list of implications:"
227 segs = [ln.strip() for seg in segs for ln in seg.splitlines() if ln.strip()]
228 if len(segs) > 1 and segs[1].startswith("1."):
229 segs = segs[1:]
230
231 if len(segs) > MAX_TRAIN_SEQS_PER_COMPLETION:
232 segs = segs[:MAX_TRAIN_SEQS_PER_COMPLETION]
233 segs = [s for s in segs if s.strip()]
234 seqs = [TRAINING_SEQUENCE_TEMPLATE.format(title=title, completion_text=s) for s in segs]
235 if add_context:
236 seqs.append(TRAINING_SEQUENCE_TEMPLATE.format(title=title, completion_text=context.strip()))
237 return seqs
238
239# ------------------- PROXY GRADING ---------------------------------- #
240# This is a proxy used to evaluate the quality of synthetic data generated by the model.

Callers 4

run_one_sequenceFunction · 0.85
evaluate_completionFunction · 0.85
mainFunction · 0.85
mainFunction · 0.85

Calls 1

_split_segmentsFunction · 0.85

Tested by

no test coverage detected