# synthetic_data.py
import random
from typing import List, Tuple
import json
from dataclasses import dataclass
@dataclass
class TrainingExample:
user_input: str
intent: str
entities: List[str]
actions: List[Dict[str, Any]]
response_text: str
class SyntheticDataGenerator:
def __init__(self):
self.entity_types = {
"light": ["light.living_room", "light.bedroom", "light.kitchen", "light.bathroom"],
"switch": ["switch.fan", "switch.coffee_maker", "switch.tv"],
"climate": ["climate.thermostat", "climate.bedroom_ac"],
"cover": ["cover.garage_door", "cover.blinds"],
"lock": ["lock.front_door", "lock.back_door"]
}
self.intent_templates = {
"lighting_control": [
"Turn {action} the {room} {device}",
"{action} the {device} in the {room}",
"Can you {action} the {room} {device}?",
"Please {action} all the {device}s",
"Set the {room} {device} brightness to {brightness}%"
],
"climate_control": [
"Set the temperature to {temperature} degrees",
"Make it {temperature_adj} in here",
"Turn {action} the {room} AC",
"Adjust the thermostat to {temperature}"
],
"security": [
"{action} the {device}",
"Is the {device} {state}?",
"Make sure the {device} is {state}"
]
}
self.rooms = ["living room", "bedroom", "kitchen", "bathroom", "office"]
self.devices = ["light", "lamp", "switch", "fan"]
self.actions = ["turn on", "turn off", "toggle"]
self.temperatures = list(range(65, 80))
self.brightness_levels = list(range(10, 101, 10))
def generate_lighting_example(self) -> TrainingExample:
"""Generate synthetic lighting control example"""
room = random.choice(self.rooms)
device = random.choice(self.devices)
action = random.choice(self.actions)
# Select template and fill
template = random.choice(self.intent_templates["lighting_control"])
if "brightness" in template:
brightness = random.choice(self.brightness_levels)
user_input = template.format(
room=room, device=device, action=action, brightness=brightness
)
actions = [{
"action_type": "set_brightness",
"entity_id": f"light.{room.replace(' ', '_')}",
"parameters": {"brightness_pct": brightness}
}]
else:
user_input = template.format(room=room, device=device, action=action)
action_type = "turn_on" if "on" in action else "turn_off" if "off" in action else "toggle"
actions = [{
"action_type": action_type,
"entity_id": f"light.{room.replace(' ', '_')}",
"parameters": None
}]
return TrainingExample(
user_input=user_input,
intent="lighting_control",
entities=[f"light.{room.replace(' ', '_')}"],
actions=actions,
response_text=f"I'll {action} the {room} {device} for you."
)
def generate_climate_example(self) -> TrainingExample:
"""Generate synthetic climate control example"""
temperature = random.choice(self.temperatures)
room = random.choice(self.rooms)
template = random.choice(self.intent_templates["climate_control"])
if "temperature_adj" in template:
temp_adj = "warmer" if temperature > 72 else "cooler"
user_input = template.format(temperature_adj=temp_adj)
else:
user_input = template.format(temperature=temperature, room=room, action="on")
actions = [{
"action_type": "set_temperature",
"entity_id": "climate.thermostat",
"parameters": {"temperature": temperature}
}]
return TrainingExample(
user_input=user_input,
intent="climate_control",
entities=["climate.thermostat"],
actions=actions,
response_text=f"I'll set the temperature to {temperature} degrees."
)
def generate_security_example(self) -> TrainingExample:
"""Generate synthetic security control example"""
device = random.choice(["front door", "back door", "garage door"])
action = random.choice(["lock", "unlock", "open", "close"])
state = "locked" if "lock" in action else "closed"
template = random.choice(self.intent_templates["security"])
user_input = template.format(device=device, action=action, state=state)
entity_map = {
"front door": "lock.front_door",
"back door": "lock.back_door",
"garage door": "cover.garage_door"
}
action_type = "turn_on" if action in ["lock", "close"] else "turn_off"
actions = [{
"action_type": action_type,
"entity_id": entity_map[device],
"parameters": None
}]
return TrainingExample(
user_input=user_input,
intent="security",
entities=[entity_map[device]],
actions=actions,
response_text=f"I'll {action} the {device}."
)
def generate_dataset(self, num_examples: int = 1000) -> List[TrainingExample]:
"""Generate complete synthetic dataset"""
examples = []
generators = [
self.generate_lighting_example,
self.generate_climate_example,
self.generate_security_example
]
for _ in range(num_examples):
generator = random.choice(generators)
examples.append(generator())
return examples
def save_dataset(self, examples: List[TrainingExample], filename: str):
"""Save dataset to JSON file"""
data = []
for example in examples:
data.append({
"user_input": example.user_input,
"intent": example.intent,
"entities": example.entities,
"actions": example.actions,
"response_text": example.response_text
})
with open(filename, 'w') as f:
json.dump(data, f, indent=2)