BaseMetric is the abstract foundation for every metric in TrustifAI, including all four built-in offline metrics. You can subclass it to create custom trust signals that receive the same service dependencies and integrate transparently with get_trust_score and the async batch pipeline. The only method you must implement is calculate.
Constructor
ExternalService
required
The shared service layer for LLM calls, embedding calls, and document text extraction. TrustifAI injects this automatically — you do not construct it directly.
Config
required
The parsed configuration object loaded from your YAML config file. Exposes threshold values, model names, and pipeline settings. TrustifAI injects this automatically.
Inherited attributes
All subclasses have access to these attributes after callingsuper().__init__():
Abstract methods
calculate
MetricContext (with embeddings already computed) and must return a MetricResult.
a_calculate (optional override)
calculate via the synchronous path. Override this method if your metric can make non-blocking LLM or embedding calls natively — for example, using await self.service.llm_call_async(...).
Custom metric example
The following example implements a query-answer relevance metric using cosine similarity between the query and answer embeddings:Registering the custom metric
UseTrustifai.register_metric to add your class to the global metric registry, then configure its weight in config_file.yaml. Call register_metric before instantiating any engine.
metrics (for thresholds) and score_weights (for its contribution weight):