Core Callback Classes#
CallbackManager #
Bases: BaseCallbackHandler
, ABC
Callback manager that handles callbacks for events within LlamaIndex.
The callback manager provides a way to call handlers on event starts/ends.
Additionally, the callback manager traces the current stack of events. It does this by using a few key attributes. - trace_stack - The current stack of events that have not ended yet. When an event ends, it's removed from the stack. Since this is a contextvar, it is unique to each thread/task. - trace_map - A mapping of event ids to their children events. On the start of events, the bottom of the trace stack is used as the current parent event for the trace map. - trace_id - A simple name for the current trace, usually denoting the entrypoint (query, index_construction, insert, etc.)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handlers |
List[BaseCallbackHandler]
|
list of handlers to use. |
None
|
Usage
with callback_manager.event(CBEventType.QUERY) as event: event.on_start(payload={key, val}) ... event.on_end(payload={key, val})
Source code in llama-index-core/llama_index/core/callbacks/base.py
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
on_event_start #
on_event_start(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: Optional[str] = None, parent_id: Optional[str] = None, **kwargs: Any) -> str
Run handlers when an event starts and return id of event.
Source code in llama-index-core/llama_index/core/callbacks/base.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
on_event_end #
on_event_end(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: Optional[str] = None, **kwargs: Any) -> None
Run handlers when an event ends.
Source code in llama-index-core/llama_index/core/callbacks/base.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
add_handler #
add_handler(handler: BaseCallbackHandler) -> None
Add a handler to the callback manager.
Source code in llama-index-core/llama_index/core/callbacks/base.py
144 145 146 |
|
remove_handler #
remove_handler(handler: BaseCallbackHandler) -> None
Remove a handler from the callback manager.
Source code in llama-index-core/llama_index/core/callbacks/base.py
148 149 150 |
|
set_handlers #
set_handlers(handlers: List[BaseCallbackHandler]) -> None
Set handlers as the only handlers on the callback manager.
Source code in llama-index-core/llama_index/core/callbacks/base.py
152 153 154 |
|
event #
event(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: Optional[str] = None) -> Generator[EventContext, None, None]
Context manager for lanching and shutdown of events.
Handles sending on_evnt_start and on_event_end to handlers for specified event.
Usage
with callback_manager.event(CBEventType.QUERY, payload={key, val}) as event: ... event.on_end(payload={key, val}) # optional
Source code in llama-index-core/llama_index/core/callbacks/base.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
as_trace #
as_trace(trace_id: str) -> Generator[None, None, None]
Context manager tracer for lanching and shutdown of traces.
Source code in llama-index-core/llama_index/core/callbacks/base.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
start_trace #
start_trace(trace_id: Optional[str] = None) -> None
Run when an overall trace is launched.
Source code in llama-index-core/llama_index/core/callbacks/base.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
end_trace #
end_trace(trace_id: Optional[str] = None, trace_map: Optional[Dict[str, List[str]]] = None) -> None
Run when an overall trace is exited.
Source code in llama-index-core/llama_index/core/callbacks/base.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
BaseCallbackHandler #
Bases: ABC
Base callback handler that can be used to track event starts and ends.
Source code in llama-index-core/llama_index/core/callbacks/base_handler.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 |
|
on_event_start
abstractmethod
#
on_event_start(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', parent_id: str = '', **kwargs: Any) -> str
Run when an event starts and return id of event.
Source code in llama-index-core/llama_index/core/callbacks/base_handler.py
24 25 26 27 28 29 30 31 32 33 |
|
on_event_end
abstractmethod
#
on_event_end(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', **kwargs: Any) -> None
Run when an event ends.
Source code in llama-index-core/llama_index/core/callbacks/base_handler.py
35 36 37 38 39 40 41 42 43 |
|
start_trace
abstractmethod
#
start_trace(trace_id: Optional[str] = None) -> None
Run when an overall trace is launched.
Source code in llama-index-core/llama_index/core/callbacks/base_handler.py
45 46 47 |
|
end_trace
abstractmethod
#
end_trace(trace_id: Optional[str] = None, trace_map: Optional[Dict[str, List[str]]] = None) -> None
Run when an overall trace is exited.
Source code in llama-index-core/llama_index/core/callbacks/base_handler.py
49 50 51 52 53 54 55 |
|
Base schema for callback managers.
CBEvent
dataclass
#
Generic class to store event information.
Source code in llama-index-core/llama_index/core/callbacks/schema.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
CBEventType #
Bases: str
, Enum
Callback manager event types.
Attributes:
Name | Type | Description |
---|---|---|
CHUNKING |
Logs for the before and after of text splitting. |
|
NODE_PARSING |
Logs for the documents and the nodes that they are parsed into. |
|
EMBEDDING |
Logs for the number of texts embedded. |
|
LLM |
Logs for the template and response of LLM calls. |
|
QUERY |
Keeps track of the start and end of each query. |
|
RETRIEVE |
Logs for the nodes retrieved for a query. |
|
SYNTHESIZE |
Logs for the result for synthesize calls. |
|
TREE |
Logs for the summary and level of summaries generated. |
|
SUB_QUESTION |
Logs for a generated sub question and answer. |
Source code in llama-index-core/llama_index/core/callbacks/schema.py
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 |
|
EventPayload #
Bases: str
, Enum
Source code in llama-index-core/llama_index/core/callbacks/schema.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|