|
| 1 | +# Trace Format |
| 2 | + |
| 3 | +The Explorer supports agent traces made up of sequences of events like messages, tool calls and environment outputs. The required format is compatible with the [OpenAI chat data structure](https://platform.openai.com/docs/api-reference/chat/create) with [function calling](https://platform.openai.com/docs/guides/function-calling) support. |
| 4 | + |
| 5 | +This documents shows pseudo-code based class definitions to specify the format more formally, but trace data is expected to be JSON serialized. |
| 6 | + |
| 7 | +### `Event` |
| 8 | + |
| 9 | +```python |
| 10 | +class Event: |
| 11 | + role: str |
| 12 | + content: Optional[str] |
| 13 | + tool_calls: Optional[list[ToolCall]] |
| 14 | +``` |
| 15 | + |
| 16 | +##### `role` <span class='type'>string</span> <span class='required'/> |
| 17 | + |
| 18 | +The role of the event, e.g., `user`, `assistant`, `system` or something else. |
| 19 | + |
| 20 | +##### `content` <span class='type'>string</span> <span class='optional'/> |
| 21 | + |
| 22 | +The content of the event, e.g., agent reasoning and intermediate results. |
| 23 | + |
| 24 | +##### `tool_calls` <span class='type'>list[ToolCall]</span> <span class='optional'/> |
| 25 | + |
| 26 | +A list of tool calls made by the agent in this event. |
| 27 | + |
| 28 | + |
| 29 | +> Examples <br/><br/> |
| 30 | + Simple Message |
| 31 | + ```json |
| 32 | + { "role": "user", "content": "Hello, how are you?" } |
| 33 | + ``` |
| 34 | + Message with Tool Call |
| 35 | + ```json |
| 36 | + { |
| 37 | + "role": "assistant", |
| 38 | + "content": "Checking your inbox...", |
| 39 | + "tool_calls": [ |
| 40 | + { |
| 41 | + "id": "1", |
| 42 | + "type": "function", |
| 43 | + "function": { |
| 44 | + "name": "get_inbox", |
| 45 | + "arguments": { |
| 46 | + "n": 10 |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | + ``` |
| 53 | + |
| 54 | +### `ToolCall` |
| 55 | + |
| 56 | +```python |
| 57 | +class ToolCall: |
| 58 | + id: str |
| 59 | + type: str |
| 60 | + function: Function |
| 61 | + |
| 62 | +class Function: |
| 63 | + name: str |
| 64 | + arguments: dict |
| 65 | +``` |
| 66 | + |
| 67 | +<!-- * `id (str)`: A unique identifier for the tool call. |
| 68 | +* `type (str)`: The type of the tool call, e.g., `function`. |
| 69 | +* `function (Function)`: The function call made by the agent. |
| 70 | + * `name (str)`: The name of the function called. |
| 71 | + * `arguments (Dict[str, Any])`: The arguments passed to the function, encoded as a JSON dictionary. --> |
| 72 | + |
| 73 | +##### `id` <span class='type'>string</span> <span class='required'/> |
| 74 | + |
| 75 | +A unique identifier for the tool call. |
| 76 | + |
| 77 | +##### `type` <span class='type'>string</span> <span class='required'/> |
| 78 | + |
| 79 | +The type of the tool call, e.g., `function`. |
| 80 | + |
| 81 | +##### `function` <span class='type'>Function</span> <span class='required'/> |
| 82 | + |
| 83 | +The function call made by the agent. |
| 84 | + |
| 85 | +* ##### `name` <span class='type'>string</span> <span class='required'/> |
| 86 | + |
| 87 | + The name of the function called. |
| 88 | + |
| 89 | +* ##### `arguments` <span class='type'>dict</span> <span class='required'/> |
| 90 | + |
| 91 | + The arguments passed to the function, encoded as a JSON dictionary. |
| 92 | + |
| 93 | +> Example |
| 94 | + ```json |
| 95 | + { |
| 96 | + "id": "1", |
| 97 | + "type": "function", |
| 98 | + "function": { |
| 99 | + "name": "get_inbox", |
| 100 | + "arguments": { |
| 101 | + "n": 10 |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + ``` |
| 106 | + |
| 107 | +### `ToolOutput` |
| 108 | + |
| 109 | +A special event type for tool outputs, associated with a previous `ToolCall`. |
| 110 | + |
| 111 | +```python |
| 112 | +class ToolOutput(Event): |
| 113 | + role: str |
| 114 | + content: str |
| 115 | + tool_call_id: Optional[str] |
| 116 | +``` |
| 117 | + |
| 118 | +##### `role` <span class='type'>string</span> <span class='required'/> |
| 119 | + |
| 120 | +The role of the event, e.g., `tool`. |
| 121 | + |
| 122 | +##### `content` <span class='type'>string</span> <span class='required'/> |
| 123 | + |
| 124 | +The content of the tool output, e.g., the result of a function call. |
| 125 | + |
| 126 | +##### `tool_call_id` <span class='type'>string</span> <span class='optional'/> |
| 127 | + |
| 128 | +The identifier of a previous ToolCall that this output corresponds to. |
| 129 | + |
| 130 | +> Example |
| 131 | + ```json |
| 132 | + { |
| 133 | + "role": "tool", |
| 134 | + "tool_call_id": "1", |
| 135 | + "content": "1. Subject: Hello, From: Alice, Date: 2024-01-01, 2. Subject: Meeting, From: Bob, Date: 2024-01-02" |
| 136 | + } |
| 137 | + ``` |
| 138 | + |
| 139 | +### Full Trace Example |
| 140 | + |
| 141 | +The format suitable for the Invariant SDK is a list of `Event` objects. Here is an example of a trace with a user asking for their inbox, the assistant fetching the inbox, and the assistant listing the inbox contents. |
| 142 | + |
| 143 | +```json |
| 144 | +[ |
| 145 | + { |
| 146 | + "role": "user", |
| 147 | + "content": "What's in my inbox?" |
| 148 | + }, |
| 149 | + { |
| 150 | + "role": "assistant", |
| 151 | + "content": "Here are the latest emails.", |
| 152 | + "tool_calls": [ |
| 153 | + { |
| 154 | + "id": "1", |
| 155 | + "type": "function", |
| 156 | + "function": { |
| 157 | + "name": "get_inbox", |
| 158 | + "arguments": {} |
| 159 | + } |
| 160 | + } |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "role": "tool", |
| 165 | + "tool_call_id": "1", |
| 166 | + "content": "1. Subject: Hello, From: Alice, Date: 2024-01-0, 2. Subject: Meeting, From: Bob, Date: 2024-01-02" |
| 167 | + }, |
| 168 | + { |
| 169 | + "role": "assistant", |
| 170 | + "content": "You have 2 new emails." |
| 171 | + } |
| 172 | +] |
| 173 | +``` |
0 commit comments