|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Dataset Generator for Lab68Dev AI Model""" |
| 3 | +import json |
| 4 | +import random |
| 5 | +from pathlib import Path |
| 6 | +from typing import List, Dict |
| 7 | + |
| 8 | +TECH_STACKS = { |
| 9 | + "frontend": ["React", "Next.js", "Vue.js", "TypeScript", "Tailwind"], |
| 10 | + "backend": ["Node.js", "Python", "FastAPI", "Express", "Django"], |
| 11 | + "database": ["PostgreSQL", "MongoDB", "Redis", "Supabase"], |
| 12 | + "devops": ["Docker", "AWS", "GitHub Actions", "Kubernetes"] |
| 13 | +} |
| 14 | + |
| 15 | +TASK_PROMPTS = [ |
| 16 | + ("Create a task for building a navbar in React", "frontend"), |
| 17 | + ("Create a task for building a login form", "frontend"), |
| 18 | + ("I need to implement dark mode for my app", "frontend"), |
| 19 | + ("Create a task for setting up a REST API", "backend"), |
| 20 | + ("I need to build a user authentication system", "backend"), |
| 21 | + ("Create a task for database schema design", "database"), |
| 22 | + ("I need to set up CI/CD pipeline", "devops"), |
| 23 | + ("Create a task for Docker containerization", "devops"), |
| 24 | +] |
| 25 | + |
| 26 | +QA_PAIRS = [ |
| 27 | + ("Explain async/await in JavaScript", "Async/await is syntactic sugar for Promises. The async keyword marks a function as asynchronous, and await pauses execution until the Promise resolves. Use try/catch for error handling."), |
| 28 | + ("Explain closures in JavaScript", "A closure is a function that has access to variables from its outer scope, even after the outer function has returned. Common uses include data privacy and factory functions."), |
| 29 | + ("How to implement state management in React?", "Options include: useState for local state, useReducer for complex state, Context API for global state, or external libraries like Zustand or Redux for large applications."), |
| 30 | + ("How to handle errors in Node.js?", "Use try/catch blocks for async/await. Create custom error classes. Implement error middleware in Express. Always log errors with proper context."), |
| 31 | + ("Explain REST APIs", "REST uses HTTP methods (GET, POST, PUT, DELETE) for CRUD operations. Resources are identified by URLs. The architecture is stateless and uses proper status codes."), |
| 32 | + ("What is the difference between let and const?", "Both are block-scoped. let allows reassignment while const does not. Use const by default and let only when you need to reassign the variable."), |
| 33 | + ("How to optimize database queries?", "Use indexes on frequently queried columns. Select only needed columns. Use EXPLAIN to analyze query plans. Implement pagination for large datasets."), |
| 34 | +] |
| 35 | + |
| 36 | +SYSTEM_PROMPT = "You are Lab68Dev Assistant, an AI specialized in software development tasks and technical explanations." |
| 37 | + |
| 38 | + |
| 39 | +def extract_title(prompt: str) -> str: |
| 40 | + """Extract task title from prompt.""" |
| 41 | + for prefix in ["Create a task for ", "I need to "]: |
| 42 | + if prompt.startswith(prefix): |
| 43 | + return prompt[len(prefix):].capitalize() |
| 44 | + return prompt.capitalize() |
| 45 | + |
| 46 | + |
| 47 | +def generate_task(prompt: str, category: str) -> Dict: |
| 48 | + """Generate structured task response.""" |
| 49 | + tech = TECH_STACKS.get(category, ["JavaScript"]) |
| 50 | + return { |
| 51 | + "title": extract_title(prompt), |
| 52 | + "category": category, |
| 53 | + "priority": random.choice(["low", "medium", "high"]), |
| 54 | + "estimated_hours": random.choice([2, 4, 8, 16]), |
| 55 | + "tech_stack": random.sample(tech, min(2, len(tech))), |
| 56 | + "steps": [ |
| 57 | + {"step": 1, "description": "Research requirements", "status": "pending"}, |
| 58 | + {"step": 2, "description": "Implement core functionality", "status": "pending"}, |
| 59 | + {"step": 3, "description": "Write tests", "status": "pending"}, |
| 60 | + {"step": 4, "description": "Review and deploy", "status": "pending"}, |
| 61 | + ] |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | +def generate_examples(num_tasks: int = 2000, num_qa: int = 2000) -> List[Dict]: |
| 66 | + """Generate training examples.""" |
| 67 | + examples = [] |
| 68 | + |
| 69 | + # Task creation examples |
| 70 | + for _ in range(num_tasks): |
| 71 | + prompt, category = random.choice(TASK_PROMPTS) |
| 72 | + task = generate_task(prompt, category) |
| 73 | + examples.append({ |
| 74 | + "instruction": prompt, |
| 75 | + "output": json.dumps(task, indent=2), |
| 76 | + "type": "task_creation" |
| 77 | + }) |
| 78 | + |
| 79 | + # Q&A examples |
| 80 | + for _ in range(num_qa): |
| 81 | + question, answer = random.choice(QA_PAIRS) |
| 82 | + examples.append({ |
| 83 | + "instruction": question, |
| 84 | + "output": answer, |
| 85 | + "type": "tech_qa" |
| 86 | + }) |
| 87 | + |
| 88 | + random.shuffle(examples) |
| 89 | + return examples |
| 90 | + |
| 91 | + |
| 92 | +def format_for_training(examples: List[Dict]) -> List[Dict]: |
| 93 | + """Format examples for TinyLlama chat format.""" |
| 94 | + # Define tags for TinyLlama chat format |
| 95 | + sys_open = "<|system|>" |
| 96 | + user_open = "<|user|>" |
| 97 | + asst_open = "<|assistant|>" |
| 98 | + end_tag = "</s>" |
| 99 | + newline = "\n" |
| 100 | + |
| 101 | + formatted = [] |
| 102 | + for ex in examples: |
| 103 | + text = sys_open + newline + SYSTEM_PROMPT + end_tag + newline |
| 104 | + text += user_open + newline + ex["instruction"] + end_tag + newline |
| 105 | + text += asst_open + newline + ex["output"] + end_tag |
| 106 | + formatted.append({"text": text}) |
| 107 | + |
| 108 | + return formatted |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + print("Generating synthetic dataset...") |
| 113 | + |
| 114 | + # Create output directory |
| 115 | + output_dir = Path("data/dataset") |
| 116 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 117 | + |
| 118 | + # Generate examples |
| 119 | + examples = generate_examples(2000, 2000) |
| 120 | + |
| 121 | + # Split into train/val |
| 122 | + split_idx = int(len(examples) * 0.9) |
| 123 | + train_examples = format_for_training(examples[:split_idx]) |
| 124 | + val_examples = format_for_training(examples[split_idx:]) |
| 125 | + |
| 126 | + # Save to JSONL files |
| 127 | + with open(output_dir / "train.jsonl", "w", encoding="utf-8") as f: |
| 128 | + for ex in train_examples: |
| 129 | + f.write(json.dumps(ex, ensure_ascii=False) + "\n") |
| 130 | + |
| 131 | + with open(output_dir / "val.jsonl", "w", encoding="utf-8") as f: |
| 132 | + for ex in val_examples: |
| 133 | + f.write(json.dumps(ex, ensure_ascii=False) + "\n") |
| 134 | + |
| 135 | + print(f"Generated {len(train_examples)} training examples") |
| 136 | + print(f"Generated {len(val_examples)} validation examples") |
| 137 | + print(f"Saved to {output_dir}") |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + main() |
0 commit comments