Callbacksο
- class llama_index.callbacks.AimCallback(repo: Optional[str] = None, experiment_name: Optional[str] = None, system_tracking_interval: Optional[int] = 1, log_system_params: Optional[bool] = True, capture_terminal_logs: Optional[bool] = True, event_starts_to_ignore: Optional[List[CBEventType]] = None, event_ends_to_ignore: Optional[List[CBEventType]] = None, run_params: Optional[Dict[str, Any]] = None)ο
AimCallback callback class.
- Parameters
repo (
str
, optional) β Aim repository path or Repo object to which Run object is bound. If skipped, default Repo is used.experiment_name (
str
, optional) β Sets Runβs experiment property. βdefaultβ if not specified. Can be used later to query runs/sequences.system_tracking_interval (
int
, optional) β Sets the tracking interval in seconds for system usage metrics (CPU, Memory, etc.). Set to None to disable system metrics tracking.log_system_params (
bool
, optional) β Enable/Disable logging of system params such as installed packages, git info, environment variables, etc.capture_terminal_logs (
bool
, optional) β Enable/Disable terminal stdout logging.event_starts_to_ignore (Optional[List[CBEventType]]) β list of event types to ignore when tracking event starts.
event_ends_to_ignore (Optional[List[CBEventType]]) β list of event types to ignore when tracking event ends.
- end_trace(trace_id: Optional[str] = None, trace_map: Optional[Dict[str, List[str]]] = None) None ο
Run when an overall trace is exited.
- on_event_end(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', **kwargs: Any) None ο
- Parameters
event_type (CBEventType) β event type to store.
payload (Optional[Dict[str, Any]]) β payload to store.
event_id (str) β event id to store.
- on_event_start(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', **kwargs: Any) str ο
- Parameters
event_type (CBEventType) β event type to store.
payload (Optional[Dict[str, Any]]) β payload to store.
event_id (str) β event id to store.
- start_trace(trace_id: Optional[str] = None) None ο
Run when an overall trace is launched.
- class llama_index.callbacks.CBEvent(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, time: str = '', id_: str = '')ο
Generic class to store event information.
- class llama_index.callbacks.CBEventType(value)ο
Callback manager event types.
- 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.
- class llama_index.callbacks.CallbackManager(handlers: List[BaseCallbackHandler])ο
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 remove from the stack.
- 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
handlers (List[BaseCallbackHandler]) β list of handlers to use.
- add_handler(handler: BaseCallbackHandler) None ο
Add a handler to the callback manager.
- as_trace(trace_id: str) Generator[None, None, None] ο
Context manager tracer for lanching and shutdown of traces.
- end_trace(trace_id: Optional[str] = None, trace_map: Optional[Dict[str, List[str]]] = None) None ο
Run when an overall trace is exited.
- on_event_end(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', **kwargs: Any) None ο
Run handlers when an event ends.
- on_event_start(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', **kwargs: Any) str ο
Run handlers when an event starts and return id of event.
- remove_handler(handler: BaseCallbackHandler) None ο
Remove a handler from the callback manager.
- set_handlers(handlers: List[BaseCallbackHandler]) None ο
Set handlers as the only handlers on the callback manager.
- start_trace(trace_id: Optional[str] = None) None ο
Run when an overall trace is launched.
- class llama_index.callbacks.LlamaDebugHandler(event_starts_to_ignore: Optional[List[CBEventType]] = None, event_ends_to_ignore: Optional[List[CBEventType]] = None, print_trace_on_end: bool = True)ο
Callback handler that keeps track of debug info.
NOTE: this is a beta feature. The usage within our codebase, and the interface may change.
This handler simply keeps track of event starts/ends, separated by event types. You can use this callback handler to keep track of and debug events.
- Parameters
event_starts_to_ignore (Optional[List[CBEventType]]) β list of event types to ignore when tracking event starts.
event_ends_to_ignore (Optional[List[CBEventType]]) β list of event types to ignore when tracking event ends.
- end_trace(trace_id: Optional[str] = None, trace_map: Optional[Dict[str, List[str]]] = None) None ο
Shutdown the current trace.
- flush_event_logs() None ο
Clear all events from memory.
- get_event_pairs(event_type: Optional[CBEventType] = None) List[List[CBEvent]] ο
Pair events by ID, either all events or a sepcific type.
- get_events(event_type: Optional[CBEventType] = None) List[CBEvent] ο
Get all events for a specific event type.
- on_event_end(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', **kwargs: Any) None ο
Store event end data by event type.
- Parameters
event_type (CBEventType) β event type to store.
payload (Optional[Dict[str, Any]]) β payload to store.
event_id (str) β event id to store.
- on_event_start(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', **kwargs: Any) str ο
Store event start data by event type.
- Parameters
event_type (CBEventType) β event type to store.
payload (Optional[Dict[str, Any]]) β payload to store.
event_id (str) β event id to store.
- print_trace_map() None ο
Print simple trace map to terminal for debugging of the most recent trace.
- start_trace(trace_id: Optional[str] = None) None ο
Launch a trace.
- class llama_index.callbacks.WandbCallbackHandler(run_args: Optional[WandbRunArgs] = None, event_starts_to_ignore: Optional[List[CBEventType]] = None, event_ends_to_ignore: Optional[List[CBEventType]] = None)ο
Callback handler that logs events to wandb.
NOTE: this is a beta feature. The usage within our codebase, and the interface may change.
Use the WandbCallbackHandler to log trace events to wandb. This handler is useful for debugging and visualizing the trace events. It captures the payload of the events and logs them to wandb. The handler also tracks the start and end of events. This is particularly useful for debugging your LLM calls.
The WandbCallbackHandler can also be used to log the indices and graphs to wandb using the persist_index method. This will save the indexes as artifacts in wandb. The load_storage_context method can be used to load the indexes from wandb artifacts. This method will return a StorageContext object that can be used to build the index, using load_index_from_storage, load_indices_from_storage or load_graph_from_storage functions.
- Parameters
event_starts_to_ignore (Optional[List[CBEventType]]) β list of event types to ignore when tracking event starts.
event_ends_to_ignore (Optional[List[CBEventType]]) β list of event types to ignore when tracking event ends.
- end_trace(trace_id: Optional[str] = None, trace_map: Optional[Dict[str, List[str]]] = None) None ο
Run when an overall trace is exited.
- finish() None ο
Finish the callback handler.
- load_storage_context(artifact_url: str, index_download_dir: Optional[str] = None) StorageContext ο
Download an index from wandb and return a storage context.
Use this storage context to load the index into memory using load_index_from_storage, load_indices_from_storage or load_graph_from_storage functions.
- Parameters
artifact_url (str) β url of the artifact to download. The artifact url will be of the form: entity/project/index_name:version and can be found in the W&B UI.
index_download_dir (Union[str, None]) β directory to download the index to.
- log_trace_tree() None ο
Log the trace tree to wandb.
- on_event_end(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', **kwargs: Any) None ο
Store event end data by event type.
- Parameters
event_type (CBEventType) β event type to store.
payload (Optional[Dict[str, Any]]) β payload to store.
event_id (str) β event id to store.
- on_event_start(event_type: CBEventType, payload: Optional[Dict[str, Any]] = None, event_id: str = '', **kwargs: Any) str ο
Store event start data by event type.
- Parameters
event_type (CBEventType) β event type to store.
payload (Optional[Dict[str, Any]]) β payload to store.
event_id (str) β event id to store.
- persist_index(index: IndexType, index_name: str, persist_dir: Optional[str] = None) None ο
Upload an index to wandb as an artifact. You can learn more about W&B artifacts here: https://docs.wandb.ai/guides/artifacts.
For the ComposableGraph index, the root id is stored as artifact metadata.
- Parameters
index (IndexType) β index to upload.
index_name (str) β name of the index. This will be used as the artifact name.
persist_dir (Union[str, None]) β directory to persist the index. If None, a temporary directory will be created and used.
- start_trace(trace_id: Optional[str] = None) None ο
Launch a trace.