|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2024 Oracle and/or its affiliates. |
| 3 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 4 | +"""Contains the data structure for logging and reporting.""" |
| 5 | +import copy |
| 6 | +import json |
| 7 | +from dataclasses import asdict, dataclass, field |
| 8 | +from typing import Optional, Union |
| 9 | + |
| 10 | +from ads.llm.autogen.constants import Events |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class LogData: |
| 15 | + """Base class for the data field of LogRecord.""" |
| 16 | + |
| 17 | + def to_dict(self): |
| 18 | + """Convert the log data to dictionary.""" |
| 19 | + return asdict(self) |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class LogRecord: |
| 24 | + """Represents a log record. |
| 25 | +
|
| 26 | + The `data` field is for pre-defined structured data, which should be an instance of LogData. |
| 27 | + The `kwargs` field is for freeform key value pairs. |
| 28 | + """ |
| 29 | + |
| 30 | + session_id: str |
| 31 | + thread_id: int |
| 32 | + timestamp: str |
| 33 | + event_name: str |
| 34 | + source_id: Optional[int] = None |
| 35 | + source_name: Optional[str] = None |
| 36 | + # Structured data for specific type of logs |
| 37 | + data: Optional[LogData] = None |
| 38 | + # Freeform data |
| 39 | + kwargs: dict = field(default_factory=dict) |
| 40 | + |
| 41 | + def to_dict(self): |
| 42 | + """Convert the log record to dictionary.""" |
| 43 | + return asdict(self) |
| 44 | + |
| 45 | + def to_string(self): |
| 46 | + """Serialize the log record to JSON string.""" |
| 47 | + return json.dumps(self.to_dict(), default=str) |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def from_dict(cls, data: dict) -> "LogRecord": |
| 51 | + """Initializes a LogRecord object from dictionary.""" |
| 52 | + event_mapping = { |
| 53 | + Events.NEW_AGENT: AgentData, |
| 54 | + Events.TOOL_CALL: ToolCallData, |
| 55 | + Events.LLM_CALL: LLMCompletionData, |
| 56 | + } |
| 57 | + if Events.KEY not in data: |
| 58 | + raise KeyError("event_name not found in data.") |
| 59 | + |
| 60 | + data = copy.deepcopy(data) |
| 61 | + |
| 62 | + event_name = data["event_name"] |
| 63 | + if event_name in event_mapping and data.get("data"): |
| 64 | + data["data"] = event_mapping[event_name](**data.pop("data")) |
| 65 | + |
| 66 | + return cls(**data) |
| 67 | + |
| 68 | + |
| 69 | +@dataclass |
| 70 | +class AgentData(LogData): |
| 71 | + """Represents agent log Data.""" |
| 72 | + |
| 73 | + agent_name: str |
| 74 | + agent_class: str |
| 75 | + agent_module: Optional[str] = None |
| 76 | + is_manager: Optional[bool] = None |
| 77 | + |
| 78 | + |
| 79 | +@dataclass |
| 80 | +class LLMCompletionData(LogData): |
| 81 | + """Represents LLM completion log data.""" |
| 82 | + |
| 83 | + invocation_id: str |
| 84 | + request: dict |
| 85 | + response: dict |
| 86 | + start_time: str |
| 87 | + end_time: str |
| 88 | + cost: Optional[float] = None |
| 89 | + is_cached: Optional[bool] = None |
| 90 | + |
| 91 | + |
| 92 | +@dataclass |
| 93 | +class ToolCallData(LogData): |
| 94 | + """Represents tool call log data.""" |
| 95 | + |
| 96 | + tool_name: str |
| 97 | + start_time: str |
| 98 | + end_time: str |
| 99 | + agent_name: str |
| 100 | + agent_class: str |
| 101 | + agent_module: Optional[str] = None |
| 102 | + input_args: dict = field(default_factory=dict) |
| 103 | + returns: Optional[Union[str, list, dict, tuple]] = None |
0 commit comments