File size: 5,524 Bytes
4c30587 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | """
ALFWorld offline dataset helpers.
Provides loaders and chunking utilities for expert trajectories.
"""
import json
import random
from typing import Dict, List, Any, Optional, Tuple
class ALFWorldOfflineDataset:
"""Offline ALFWorld dataset loader/sampler for expert trajectories."""
def __init__(self, data: Dict[str, Dict[str, Dict[str, Any]]]):
if not isinstance(data, dict):
raise ValueError("ALFWorld offline data must be a dict keyed by task_type.")
self.data = data
@classmethod
def load(cls, path: str) -> "ALFWorldOfflineDataset":
with open(path, "r") as f:
payload = json.load(f)
return cls(payload)
def task_types(self) -> List[str]:
return list(self.data.keys())
def gamefiles_for_type(self, task_type: str) -> List[str]:
by_type = self.data.get(task_type, {})
if isinstance(by_type, dict):
return list(by_type.keys())
return []
def get_entry(self, task_type: str, gamefile: str) -> Optional[Dict[str, Any]]:
by_type = self.data.get(task_type, {})
if isinstance(by_type, dict):
return by_type.get(gamefile)
return None
def sample_batch(self, task_type: str, size: int,
exclude: Optional[set] = None) -> List[Tuple[str, str, Dict[str, Any]]]:
if size <= 0:
return []
gamefiles = self.gamefiles_for_type(task_type)
if exclude:
gamefiles = [g for g in gamefiles if g not in exclude]
if not gamefiles:
return []
if len(gamefiles) >= size:
selected = random.sample(gamefiles, size)
else:
selected = list(gamefiles)
while len(selected) < size:
selected.append(random.choice(gamefiles))
batch = []
for gamefile in selected:
entry = self.get_entry(task_type, gamefile)
if isinstance(entry, dict):
batch.append((task_type, gamefile, entry))
return batch
def sample_pair(self, batch_a_min: int, batch_a_max: int, batch_b_size: int,
same_type_prob: float = 0.8) -> Tuple[
List[Tuple[str, str, Dict[str, Any]]],
List[Tuple[str, str, Dict[str, Any]]]
]:
task_types = self.task_types()
if not task_types:
return [], []
batch_a_size = max(1, random.randint(batch_a_min, batch_a_max))
if random.random() < same_type_prob:
task_type = random.choice(task_types)
gamefiles = self.gamefiles_for_type(task_type)
if not gamefiles:
return [], []
if len(gamefiles) >= batch_a_size + batch_b_size:
selected = random.sample(gamefiles, batch_a_size + batch_b_size)
batch_a_files = selected[:batch_a_size]
batch_b_files = selected[batch_a_size:]
else:
batch_a_files = random.sample(gamefiles, min(batch_a_size, len(gamefiles)))
remaining = [g for g in gamefiles if g not in batch_a_files]
if not remaining:
remaining = list(gamefiles)
if len(remaining) >= batch_b_size:
batch_b_files = random.sample(remaining, batch_b_size)
else:
batch_b_files = list(remaining)
while len(batch_b_files) < batch_b_size and remaining:
batch_b_files.append(random.choice(remaining))
batch_a = [(task_type, g, self.get_entry(task_type, g))
for g in batch_a_files if self.get_entry(task_type, g)]
batch_b = [(task_type, g, self.get_entry(task_type, g))
for g in batch_b_files if self.get_entry(task_type, g)]
return batch_a, batch_b
task_type_a = random.choice(task_types)
other_types = [t for t in task_types if t != task_type_a]
task_type_b = random.choice(other_types) if other_types else task_type_a
batch_a = self.sample_batch(task_type_a, batch_a_size)
batch_b = self.sample_batch(task_type_b, batch_b_size)
return batch_a, batch_b
def chunk_trajectories_by_tokens(trajectories: List[str], chunk_size: Optional[int],
tokenizer_name: str = "cl100k_base",
separator: str = "\n\n=== TRAJECTORY ===\n\n") -> List[str]:
"""Group full trajectories into token-bounded chunks without splitting any trajectory."""
cleaned = [str(t).strip() for t in trajectories if isinstance(t, str) and t.strip()]
if not cleaned:
return []
if chunk_size is None or chunk_size <= 0:
return cleaned
import tiktoken
tokenizer = tiktoken.get_encoding(tokenizer_name)
chunks: List[str] = []
current: List[str] = []
current_tokens = 0
for traj in cleaned:
traj_tokens = len(tokenizer.encode(traj))
if not current:
current.append(traj)
current_tokens = traj_tokens
continue
if current_tokens + traj_tokens <= chunk_size:
current.append(traj)
current_tokens += traj_tokens
else:
chunks.append(separator.join(current))
current = [traj]
current_tokens = traj_tokens
if current:
chunks.append(separator.join(current))
return chunks
|