File size: 4,348 Bytes
c32c04e 762ba08 c32c04e 1e86838 f89178d c32c04e f89178d c32c04e f89178d c32c04e f89178d 1e86838 f89178d c32c04e f89178d 1e86838 f89178d c32c04e 1e86838 c32c04e 762ba08 c32c04e f89178d c32c04e 1e86838 c32c04e 1e86838 c32c04e f89178d c32c04e f89178d | 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 | import dataclasses
from typing import Any
from cua_lite.data.utils import batch_proc
# @dataclasses.dataclass
# class BaseDataInterface:
# # -- Helpers (shared between grounding and trajectory) --
# def _add_system_prompt(self, row: dict[str, Any], system_prompt: str | None = None) -> dict[str, Any]:
# if system_prompt and row["messages"][0]["role"] != "system":
# row["messages"].insert(0, {"role": "system", "content": [{"type": "text", "text": system_prompt}]})
# return row
# def _process_action(self, row: dict[str, Any], **kwargs) -> dict[str, Any]:
# return row
# # -- Grounding ---
# def process_grounding(self, row: dict[str, Any], system_prompt: str | None = None, **kwargs) -> dict[str, Any]:
# return self._add_system_prompt(
# self._process_action(row, **kwargs),
# system_prompt=system_prompt
# )
# def process_grounding_batch(self, batch: dict[str, list[Any]], **kwargs) -> dict[str, list[Any]]:
# return batch_proc(self.process_grounding, batch, **kwargs)
# # --- Trajectory ---
# def _process_context(self, row: dict[str, Any], **kwargs) -> dict[str, Any]:
# return row
# def process_trajectory(self, row: dict[str, Any], system_prompt: str | None = None, **kwargs) -> dict[str, Any]:
# return self._add_system_prompt(
# self._process_context(self._process_action(row, **kwargs), **kwargs),
# system_prompt=system_prompt
# )
# def process_trajectory_batch(self, batch: dict[str, list[Any]], **kwargs) -> dict[str, list[Any]]:
# return batch_proc(self.process_trajectory, batch, **kwargs)
@dataclasses.dataclass
class BaseDataInterface:
grounding_system_prompt: str | None = None
trajectory_system_prompt: str | None = None
# -- Helpers (shared between grounding and trajectory) --
def _add_system_prompt(self, row: dict[str, Any], system_prompt: str | None = None) -> dict[str, Any]:
if system_prompt and row["messages"][0]["role"] != "system":
row["messages"].insert(0, {"role": "system", "content": [{"type": "text", "text": system_prompt}]})
return row
def _process_action(self, row: dict[str, Any], **kwargs) -> dict[str, Any]:
# TODO: overwrite this in subclass
return row
# -- Grounding ---
def process_grounding(self, row: dict[str, Any], **kwargs) -> dict[str, Any]:
return self._add_system_prompt(
self._process_action(row, **kwargs),
system_prompt=self.grounding_system_prompt
)
def process_grounding_batch(self, batch: dict[str, list[Any]], **kwargs) -> dict[str, list[Any]]:
return batch_proc(self.process_grounding, batch, **kwargs)
# --- Trajectory ---
def _process_context(self, row: dict[str, Any], **kwargs) -> dict[str, Any]:
# TODO: overwrite this in subclass
return row
def process_trajectory(self, row: dict[str, Any], **kwargs) -> dict[str, Any]:
return self._add_system_prompt(
self._process_context(self._process_action(row, **kwargs), **kwargs),
system_prompt=self.trajectory_system_prompt
)
def process_trajectory_batch(self, batch: dict[str, list[Any]], **kwargs) -> dict[str, list[Any]]:
return batch_proc(self.process_trajectory, batch, **kwargs)
@dataclasses.dataclass
class UnrolledContextDataInterface(BaseDataInterface):
"""Useful for reasoning models"""
def process_trajectory_batch(
self, batch: dict[str, list[Any]], **kwargs) -> dict[str, list[Any]]:
"""
Handles 1-to-N data expansion (Unrolling conversation history).
Note: This changes the number of rows.
"""
messages_list = []
for messages in batch["messages"]:
assistant_indices = [
i for i, msg in enumerate(messages) if msg.get("role") == "assistant"
]
for assistant_index in assistant_indices:
# Slicing includes the assistant message
processed_messages = self.process_trajectory({"messages": messages[: assistant_index + 1]}, **kwargs)["messages"]
messages_list.append(processed_messages)
batch = {"messages": messages_list}
return batch
|