Skip to content

# View models

Bases: `Generic[T]`, `HashableBaseModel`

Source code in `great_ai/views/trace.py`

    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
    class Trace(Generic[T], HashableBaseModel):
        trace_id: str
        created: str
        original_execution_time_ms: float
        logged_values: Dict[str, Any]
        models: List[Model]
        exception: Optional[str]
        output: Optional[T]
        feedback: Any = None
        tags: List[str]

        class Config:
            extra = Extra.ignore

        @property
        def input(self) -> Any:
            return (
                self.logged_values["input"]
                if list(self.logged_values.keys()) == ["input"]
                else self.logged_values
            )

        @property
        def models_flat(self) -> str:
            return ", ".join(f"{m.key}:{m.version}" for m in self.models)

        @property
        def output_flat(self) -> str:
            return pformat(self.output, indent=2, compact=True)

        @property
        def exception_flat(self) -> str:
            return (
                "null"
                if self.exception is None
                else pformat(self.exception, indent=2, compact=True)
            )

        @property
        def feedback_flat(self) -> str:
            return (
                "null"
                if self.feedback is None
                else pformat(self.feedback, indent=2, compact=True)
            )

        @property
        def tags_flat(self) -> str:
            return ",\n".join(self.tags)

        def to_flat_dict(self, include_original: bool = True) -> Dict[str, Any]:
            return {
                **(
                    self.dict()
                    if include_original
                    else {
                        "trace_id": self.trace_id,
                        "created": self.created,
                        "original_execution_time_ms": self.original_execution_time_ms,
                    }
                ),
                **{
                    k: pformat(v, indent=2, compact=True)
                    for k, v in self.logged_values.items()
                },
                "models_flat": self.models_flat,
                "exception_flat": self.exception_flat,
                "output_flat": self.output_flat,
                "feedback_flat": self.feedback_flat,
                "tags_flat": self.tags_flat,
            }

        def __repr__(self) -> str:
            return f"Trace[{type(self.output).__name__}]({pformat(self.dict(), indent=2, compact=True)})"

Last update: July 10, 2022