ZHZisZZ
tmp save
f89178d
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