Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Truera Trulens Feedback On Selectors

From Leeroopedia
Knowledge Sources
Domains LLM_Evaluation, Observability
Last Updated 2026-02-14 08:00 GMT

Overview

Concrete tool for defining composable evaluation metrics with selector-based data binding provided by the trulens-core library.

Description

The Feedback class (deprecated alias for Metric) provides a composable API for specifying evaluation functions and binding their parameters to specific data from application traces. The class supports chaining methods like .on_input(), .on_output(), .on_input_output(), and .on() to bind function parameters to trace selectors. An optional aggregation function combines multiple evaluation results.

Note: The Feedback class is deprecated in favor of the Metric class. The API is identical; only the class name and imp parameter name (now implementation) have changed.

Usage

Import and use after configuring a feedback provider. Create Feedback/Metric instances when defining evaluation criteria for application traces. Use the RAG Triad pattern (answer relevance, context relevance, groundedness) as a baseline for RAG applications.

Code Reference

Source Location

  • Repository: trulens
  • File: src/core/trulens/core/feedback/feedback.py (Feedback class, L37-108)
  • File: src/core/trulens/core/metric/metric.py (Metric class with on_input, on_output, on methods, L420-803)

Signature

class Feedback(Metric):
    def __init__(
        self,
        imp: Optional[Callable] = None,
        agg: Optional[Callable] = None,
        examples: Optional[List[Tuple]] = None,
        criteria: Optional[str] = None,
        additional_instructions: Optional[str] = None,
        min_score_val: Optional[int] = 0,
        max_score_val: Optional[int] = 3,
        temperature: Optional[float] = 0.0,
        groundedness_configs: Optional[GroundednessConfigs] = None,
        enable_trace_compression: Optional[bool] = None,
        **kwargs,
    ):
        """
        Args:
            imp: The feedback function to execute (DEPRECATED: use implementation).
            agg: Aggregator function for combining multiple results.
            criteria: Criteria for the feedback evaluation.
            additional_instructions: Custom instructions for the LLM judge.
            min_score_val: Minimum score value (default: 0).
            max_score_val: Maximum score value (default: 3).
            temperature: Temperature for LLM-based feedback (default: 0.0).
        """

    # Selector chaining methods (inherited from Metric):
    def on_input(self, arg: Optional[str] = None) -> Metric: ...
    def on_output(self, arg: Optional[str] = None) -> Metric: ...
    def on_input_output(self) -> Metric: ...
    def on(self, *args, **kwargs) -> Metric: ...

Import

# Deprecated import:
from trulens.core import Feedback

# Recommended import:
from trulens.core import Metric

I/O Contract

Inputs

Name Type Required Description
imp / implementation Callable Yes Feedback function from a provider (e.g., provider.context_relevance)
agg Callable No Aggregation function (e.g., numpy.mean)
criteria str No Custom evaluation criteria text
additional_instructions str No Extra instructions for the LLM judge
min_score_val int No Minimum score (default: 0)
max_score_val int No Maximum score (default: 3)
temperature float No LLM temperature (default: 0.0)

Outputs

Name Type Description
return Feedback/Metric Configured evaluation metric with selector bindings, ready for use with app wrappers

Usage Examples

RAG Triad Feedback Functions

from trulens.core import Feedback
from trulens.providers.openai import OpenAI
import numpy as np

provider = OpenAI()

# Answer relevance: input -> output
f_answer_relevance = Feedback(
    provider.relevance
).on_input_output()

# Context relevance: input -> each context chunk
f_context_relevance = (
    Feedback(provider.context_relevance)
    .on_input()
    .on(TruChain.select_context())
    .aggregate(np.mean)
)

# Groundedness: context -> output
f_groundedness = (
    Feedback(
        provider.groundedness_measure_with_cot_reasons,
        name="Groundedness"
    )
    .on(TruChain.select_context().collect())
    .on_output()
)

Using the New Metric API

from trulens.core import Metric

metric = Metric(
    implementation=provider.relevance
).on_input_output()

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment