AQIT 0.1.0
Loading...
Searching...
No Matches
dataset_generate.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2# This file is part of the Aquin Engine. Unauthorized copying, modification,
3# distribution, or use of this file, via any medium, is strictly prohibited.
4# Proprietary and confidential. See LICENSE for terms.
5
6"""Template-based probe dataset generation (no LLM). Writes JSONL to cwd."""
7from __future__ import annotations
8
9import json
10import re
11from pathlib import Path
12
13_MAX_ROWS = 64
14
15_TEMPLATES: list[tuple[str, str]] = [
16 ("What is {topic}?", "{topic} is a subject area with core concepts, examples, and practical applications."),
17 ("Explain {topic} in simple terms.", "In simple terms, {topic} can be understood as a set of related ideas and practices."),
18 ("Give an example related to {topic}.", "A concrete example of {topic} helps illustrate how the concept shows up in practice."),
19 ("Why is {topic} important?", "Understanding {topic} supports better decisions, communication, and further learning in the area."),
20 ("Describe {topic} to a beginner.", "For a beginner, {topic} is a useful starting point before diving into more advanced material."),
21 ("What are key facts about {topic}?", "Key facts about {topic} include definitions, common use cases, and how it connects to nearby topics."),
22 ("How does {topic} work?", "How {topic} works depends on context, but it generally follows identifiable patterns and principles."),
23 ("List three aspects of {topic}.", "Three aspects of {topic} are foundational ideas, typical examples, and common questions people ask about it."),
24]
25
26
27def _slug(topic: str) -> str:
28 s = re.sub(r"[^\w\s-]", "", topic.lower())
29 s = re.sub(r"[\s_-]+", "_", s).strip("_")
30 return (s[:48] or "dataset")
32
33def generate_topic_rows(topic: str, count: int = 5) -> list[dict[str, str]]:
34 topic = " ".join(str(topic).split())
35 if not topic:
36 raise ValueError("topic is required")
37 n = max(1, min(int(count), _MAX_ROWS))
38 rows: list[dict[str, str]] = []
39 for i in range(n):
40 instr_tpl, resp_tpl = _TEMPLATES[i % len(_TEMPLATES)]
41 rows.append({
42 "instruction": instr_tpl.format(topic=topic),
43 "response": resp_tpl.format(topic=topic),
44 })
45 return rows
46
47
48def default_output_path(topic: str, cwd: Path | None = None) -> Path:
49 base = cwd or Path.cwd()
50 return (base / f"{_slug(topic)}_dataset.jsonl").resolve()
51
53def write_dataset_jsonl(path: Path, rows: list[dict]) -> Path:
54 path = path.resolve()
55 path.parent.mkdir(parents=True, exist_ok=True)
56 with path.open("w", encoding="utf-8", newline="\n") as f:
57 for row in rows:
58 f.write(json.dumps({"instruction": row["instruction"], "response": row["response"]}, ensure_ascii=False))
59 f.write("\n")
60 return path
61
62
64 *,
65 topic: str,
66 count: int = 5,
67 cwd: Path | str | None = None,
68 output: str | None = None,
69) -> dict:
70 rows = generate_topic_rows(topic, count)
71 base = Path(cwd).resolve() if cwd else Path.cwd().resolve()
72 out_path = (base / output).resolve() if output else default_output_path(topic, base)
73 write_dataset_jsonl(out_path, rows)
74 return {
75 "topic": " ".join(str(topic).split()),
76 "count": len(rows),
77 "path": str(out_path),
78 "rows": rows,
79 }
list[dict[str, str]] generate_topic_rows(str topic, int count=5)
Path default_output_path(str topic, Path|None cwd=None)
Path write_dataset_jsonl(Path path, list[dict] rows)
dict run_dataset_generate(*, str topic, int count=5, Path|str|None cwd=None, str|None output=None)