|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from enum import Enum |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any, Optional, Union |
| 6 | + |
| 7 | +from continuous_eval.eval.modules import AgentModule |
| 8 | +from continuous_eval.eval.pipeline import Pipeline |
| 9 | +from continuous_eval.eval.result_types import TOOL_PREFIX |
| 10 | +from continuous_eval.eval.utils import instantiate_type |
| 11 | +from continuous_eval.utils.telemetry import telemetry_event |
| 12 | + |
| 13 | +logger = logging.getLogger("eval-manager") |
| 14 | +Serializable = Any |
| 15 | + |
| 16 | + |
| 17 | +class LogMode(Enum): |
| 18 | + APPEND = 0 |
| 19 | + REPLACE = 1 |
| 20 | + |
| 21 | + |
| 22 | +class PipelineLogger: |
| 23 | + @telemetry_event("logger") |
| 24 | + def __init__(self, pipeline: Optional[Pipeline] = None): |
| 25 | + self._pipeline: Optional[Pipeline] = pipeline |
| 26 | + self.data = dict() |
| 27 | + |
| 28 | + @property |
| 29 | + def pipeline(self) -> Pipeline: |
| 30 | + if self._pipeline is None: |
| 31 | + raise ValueError("Pipeline not set") |
| 32 | + return self._pipeline |
| 33 | + |
| 34 | + def _empty_sample(self): |
| 35 | + if self._pipeline is None: |
| 36 | + raise ValueError("Pipeline not set") |
| 37 | + empty_samples = dict() |
| 38 | + for module in self._pipeline.modules: |
| 39 | + empty_samples[module.name] = instantiate_type(module.output) |
| 40 | + if isinstance(module, AgentModule): |
| 41 | + empty_samples[f"{TOOL_PREFIX}{module.name}"] = list() |
| 42 | + return empty_samples |
| 43 | + |
| 44 | + def log( |
| 45 | + self, |
| 46 | + uid: Serializable, |
| 47 | + module: str, |
| 48 | + value: Any, |
| 49 | + mode: LogMode = LogMode.REPLACE, |
| 50 | + **kwargs, |
| 51 | + ): |
| 52 | + # Make sure everything looks good |
| 53 | + assert uid is not None, "UID cannot be None" |
| 54 | + if self._pipeline is None: |
| 55 | + raise ValueError("Pipeline not set") |
| 56 | + if uid not in self.data: |
| 57 | + self.data[uid] = self._empty_sample() |
| 58 | + if kwargs and "tool_args" in kwargs: |
| 59 | + key = f"{TOOL_PREFIX}{module}" |
| 60 | + self.data[uid][key].append({"name": value, "kwargs": kwargs["tool_args"]}) |
| 61 | + else: |
| 62 | + if mode == LogMode.REPLACE: |
| 63 | + self.data[uid][module] = value |
| 64 | + elif mode == LogMode.APPEND: |
| 65 | + if not isinstance(self.data[uid][module], list): |
| 66 | + if isinstance(value, list): |
| 67 | + self.data[uid][module].extend(value) |
| 68 | + else: |
| 69 | + self.data[uid][module].append(value) |
| 70 | + else: |
| 71 | + self.data[uid][module].add(value) |
| 72 | + |
| 73 | + def save(self, filepath: Union[str, Path]): |
| 74 | + if isinstance(filepath, str): |
| 75 | + filepath = Path(filepath) |
| 76 | + assert filepath.suffix == ".jsonl", "File must be a JSONL file" |
| 77 | + assert self.data, "No samples to save" |
| 78 | + with open(filepath, "w") as f: |
| 79 | + for uid, res in self.data.items(): |
| 80 | + line = {**{"__uid": uid}, **res} |
| 81 | + json_record = json.dumps(line, ensure_ascii=False) |
| 82 | + f.write(json_record + "\n") |
| 83 | + |
| 84 | + def load(self, filepath: Union[str, Path]): |
| 85 | + if isinstance(filepath, str): |
| 86 | + filepath = Path(filepath) |
| 87 | + assert filepath.suffix == ".jsonl", "File must be a JSONL file" |
| 88 | + with open(filepath, "r") as f: |
| 89 | + for line in f: |
| 90 | + record = json.loads(line) |
| 91 | + uid = record.pop("__uid") |
| 92 | + self.data[uid] = record |
0 commit comments