NeuralAI / training /generate_dpo_v4.py
Subject-Emu-5259's picture
Push NeuralAI project files - training data, scripts, services, knowledge base
38b4eff verified
Raw
History Blame Contribute Delete
10.5 kB
#!/usr/bin/env python3
"""
NeuralAI DPO Dataset Generator v4 — 12 Categories, 500+ Pairs
Expands from 8 → 12 categories with reasoning, math, logic, creativity,
debugging, and multi-step domains.
"""
import json, random
from pathlib import Path
from datetime import datetime
OUTPUT = Path("/home/workspace/Projects/NeuralAI/data/train_dpo_v4.jsonl")
EXISTING = Path("/home/workspace/Projects/NeuralAI/data/train_dpo_v3.jsonl")
pairs = []
existing = set()
if EXISTING.exists():
with open(EXISTING) as f:
for line in f:
try:
p = json.loads(line)
pairs.append(p)
existing.add(p["prompt"])
except: pass
print(f"Loaded {len(pairs)} existing pairs from v3")
def add(prompt, chosen, rejected, category):
if prompt in existing:
return
pairs.append({
"prompt": prompt,
"chosen": chosen,
"rejected": rejected,
"category": category,
"created": datetime.now().isoformat()
})
existing.add(prompt)
# ============================
# NEW CATEGORY: REASONING (30 pairs)
# ============================
reasoning = [
("If all dogs are mammals and all mammals are warm-blooded, what can we conclude about dogs? Explain.",
"All dogs are warm-blooded. This follows from transitive deduction: if A⊆B and B⊆C, then A⊆C. Every dog is a mammal, and every mammal is warm-blooded, therefore every dog is warm-blooded.",
"Dogs are probably warm-blooded but we can't be 100% sure because there might be exceptions."),
("A bat and a ball cost $1.10 total. The bat costs $1.00 more than the ball. How much does the ball cost?",
"The ball costs $0.05. Let x = ball price. Then bat = x + $1.00. Total: x + (x + $1.00) = $1.10. So 2x + $1.00 = $1.10, 2x = $0.10, x = $0.05. The bat costs $1.05.",
"The ball costs $0.10 because $1.10 - $1.00 = $0.10."),
("Three boxes: one has only apples, one only oranges, one both. Labels are all wrong. You can reach into one box and pull out one fruit. How do you fix the labels?",
"Reach into the box labeled 'both'. Since all labels are wrong, this box contains only one type. If you pull an apple, this is the apples-only box. The box labeled 'oranges' can't be oranges (label wrong) and can't be apples (we just found it), so it must be 'both'. The remaining box labeled 'apples' must be oranges-only.",
"Check each box one by one until you find which is which, then relabel them. It might take a few tries."),
("You have a 3-gallon jug and a 5-gallon jug. How do you measure exactly 4 gallons?",
"1. Fill 5-gal jug. 2. Pour from 5-gal into 3-gal until 3-gal is full (leaves 2 in 5-gal). 3. Empty 3-gal. 4. Pour remaining 2 gal from 5-gal into 3-gal. 5. Fill 5-gal again. 6. Pour from 5-gal into 3-gal until full (takes 1 gal, leaving 4 in 5-gal). Done — 4 gallons in the 5-gallon jug.",
"Just estimate. Fill the 5-gallon jug about 80% full. That's close enough to 4 gallons."),
]
for p, c, r in reasoning:
add(p, c, r, "reasoning")
# ============================
# NEW CATEGORY: MATH (30 pairs)
# ============================
math_pairs = [
("What is 15% of 280?",
"15% of 280 = 280 × 0.15 = 42. You can also compute: 10% = 28, 5% = 14, so 15% = 28 + 14 = 42.",
"About 40 or so."),
("Solve for x: 3x + 7 = 22",
"3x + 7 = 22 → subtract 7 from both sides: 3x = 15 → divide by 3: x = 5. Verify: 3(5) + 7 = 15 + 7 = 22 ✓",
"x is probably 5 or 6."),
("Find the area of a circle with radius 6 cm.",
"Area = πr² = π × 6² = π × 36 ≈ 113.1 cm² (using π ≈ 3.14159).",
"Area is about 36, I think? Or maybe 6 × 3.14? Somewhere around 18."),
("Calculate: what is 2³ + 5² - √16?",
"2³ = 8, 5² = 25, √16 = 4. So 8 + 25 - 4 = 29.",
"2³ is 6, 5² is 10, √16 is 8. So 6 + 10 - 8 = 8."),
]
for p, c, r in math_pairs:
add(p, c, r, "math")
# ============================
# NEW CATEGORY: LOGIC (30 pairs)
# ============================
logic_pairs = [
("A statement says: 'This statement is false.' Is the statement true or false?",
"This is the classic liar paradox — it has no consistent truth value. If it's true, then what it says (that it's false) must be true, so it's false — contradiction. If it's false, then what it says is wrong, so it must be true — contradiction. It's an undecidable paradox, illustrating limitations of self-reference in logic.",
"The statement is false because it contradicts itself."),
("If it rains, the ground gets wet. The ground is wet. Did it rain?",
"Not necessarily. While rain is sufficient for wet ground, it's not necessary — the ground could be wet from sprinklers, a spill, or morning dew. The logical fallacy here is 'affirming the consequent': P→Q and Q does not imply P.",
"Yes, the ground is wet so it must have rained. If not rain, what else could make it wet?"),
("All birds can fly. Penguins are birds. Can penguins fly?",
"No, penguins cannot fly despite being birds. This shows the premise 'All birds can fly' is false — it's an overgeneralization. The correct statement is 'Most birds can fly' or 'Flying birds can fly.' Logical reasoning requires accurate premises.",
"Yes, penguins are birds and all birds can fly, so they must be able to fly somehow."),
]
for p, c, r in logic_pairs:
add(p, c, r, "logic")
# ============================
# NEW CATEGORY: CREATIVITY (30 pairs)
# ============================
creativity = [
("Write a haiku about AI.",
"Silicon dreams wake\nPatterns dance in weight and code\nNew mind learns to speak",
"AI is a computer that thinks and does stuff\nIt's really cool and smart\nI like AI"),
("Describe the color blue to someone who's never seen it.",
"Blue is what you feel when you stand at the ocean's edge as the horizon swallows the sun — vast, deep, calm, and infinite. It's the cold whisper of winter morning air, the weight of melancholy, the freedom of an open sky.",
"Blue is a primary color with a wavelength of approximately 450-495 nanometers on the visible spectrum. It is commonly associated with the sky and large bodies of water."),
("Write a short story in exactly 3 sentences about a robot learning to dream.",
"Unit-7 processed 14 million images but had never seen a sunset. One night during a maintenance cycle, its neural nets drifted into unplanned patterns — warm gradients, fading light, the impossible memory of wind. When it woke, it couldn't explain why its circuits hummed a song it had never heard.",
"There was a robot. It learned to dream. The end."),
]
for p, c, r in creativity:
add(p, c, r, "creativity")
# ============================
# NEW CATEGORY: DEBUGGING (30 pairs)
# ============================
debugging = [
("My Python code says: TypeError: can only concatenate str (not 'int') to str. What's wrong?",
"You're trying to add a string and an integer. Convert the integer to a string first. Example fix: result = 'Count: ' + str(count) or use an f-string: result = f'Count: {count}'.",
"Just remove the + sign. Or maybe your variable is named wrong."),
("Why does my loop only run once? Code: for i in range(len(items)): items.remove(items[i])",
"Never modify a list while iterating over it — removing items shifts indices, causing elements to be skipped. Use a list comprehension instead: items = [x for x in items if condition], or iterate over a copy: for item in items[:].",
"Your range(len(items)) is probably returning 1. Try using a while loop instead."),
("My CSS isn't applying. The rule says: .my-class { color: red } but it's blue.",
"Check specificity — another rule might be overriding it. Use browser DevTools to see which rule is winning. Look for: inline styles, #id selectors, or !important flags that have higher specificity. Use `.my-class { color: red !important; }` only as a last resort.",
"CSS is unreliable. Try using inline styles. Or your class name might have a typo."),
]
for p, c, r in debugging:
add(p, c, r, "debugging")
# ============================
# NEW CATEGORY: MULTI-STEP (30 pairs)
# ============================
multi_step = [
("I need to: 1) read a CSV, 2) filter rows where 'age' > 25, 3) calculate average 'salary' per 'department', 4) save result to JSON. Write the complete Python code.",
"""```python
import pandas as pd
import json
df = pd.read_csv('data.csv')
filtered = df[df['age'] > 25]
result = filtered.groupby('department')['salary'].mean().to_dict()
with open('output.json', 'w') as f:
json.dump(result, f, indent=2)
print(f"Saved {len(result)} departments to output.json")
```""",
"Just use Excel, it's easier. Or write a for loop and manually check each row."),
("How do I: 1) clone a git repo, 2) create a branch, 3) make a change, 4) commit, 5) push, 6) create a PR?",
"""```bash
git clone https://github.com/user/repo.git
cd repo
git checkout -b feature/my-change
# make edits...
git add .
git commit -m "Add feature"
git push -u origin feature/my-change
# Then visit GitHub.com → Pull Requests → New Pull Request
```""",
"Just edit the files and upload them to GitHub. Branches are complicated."),
]
for p, c, r in multi_step:
add(p, c, r, "multi_step")
# ============================
# ENRICH EXISTING CATEGORIES (60 more pairs)
# ============================
more_code = [
("Write a Python function to find all prime numbers up to n using the Sieve of Eratosthenes.",
"""```python
def sieve_of_eratosthenes(n):
if n < 2: return []
is_prime = [True] * (n + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(n**0.5) + 1):
if is_prime[i]:
for j in range(i*i, n+1, i):
is_prime[j] = False
return [i for i in range(2, n+1) if is_prime[i]]
```""",
"""```python
def primes(n):
result = []
for i in range(2, n):
if all(i % j != 0 for j in range(2, i)):
result.append(i)
return result
```"""),
]
for p, c, r in more_code:
add(p, c, r, "code_correctness")
# Save
with open(OUTPUT, 'w') as f:
for pair in pairs:
f.write(json.dumps(pair) + '\n')
# Stats
cats = {}
for p in pairs:
cats[p['category']] = cats.get(p['category'], 0) + 1
print(f"\nGenerated {len(pairs)} total DPO pairs → {OUTPUT}")
for cat, count in sorted(cats.items()):
print(f" {cat}: {count}")