trulens.apps.gepa.fitness¶
trulens.apps.gepa.fitness
¶
GEPA fitness function adapter for TruLens feedback functions.
GEPA (Genetic/Evolutionary Prompt Adaptation) optimizes prompts using evolutionary algorithms. A fitness function is any callable that scores a candidate prompt as a float in [0, 1].
This module provides:
- :class:
TruGEPAβ wraps any TruLens feedback callable into the GEPA fitness-function interface and optionally logs each evaluation as aTruVirtualrecord for dashboard visibility when bothapp_nameandapp_versionare supplied. - :func:
run_evolutionβ a simple (ΞΌ+Ξ») evolutionary loop that uses a fitness function to iteratively improve a base prompt.
Classes¶
TruGEPA
¶
Adapts a TruLens feedback callable as a GEPA-compatible fitness function.
A GEPA fitness function is any callable (prompt: str, **kwargs) -> float.
TruGEPA forwards each call to a TruLens feedback function implementation
and, when both app_name and app_version are supplied, logs every
evaluation as a TruVirtual record so the full optimization trajectory
is visible in the TruLens dashboard.
Logging behaviour:
- Both
app_nameandapp_versionsupplied β aTruVirtualrecorder is created eagerly and every evaluation is logged. ATruSessionmust be active at construction time. - Neither supplied β logging is disabled; evaluations run silently.
- Only one supplied β raises
ValueErrorimmediately.
| PARAMETER | DESCRIPTION |
|---|---|
feedback_fn
|
A TruLens feedback function implementation β any callable
that accepts text keyword arguments and returns a
TYPE:
|
optimize_key
|
The keyword argument name in feedback_fn that receives
the evolving candidate prompt string. Defaults to
TYPE:
|
feedback_args
|
All other keyword arguments forwarded unchanged to
feedback_fn on every call (e.g. |
app_name
|
Name of the virtual app used for TruLens logging. Must be
supplied together with |
app_version
|
Version string of the virtual app. Must be supplied
together with |
Functions¶
run_evolution
¶
run_evolution(
base_prompt: str,
fitness_fn: Callable[[str], float],
mutate_fn: Callable[[str], str],
*,
n_generations: int = 10,
population_size: int = 5,
top_k: int = 2,
seed: Optional[int] = None
) -> Tuple[str, float, List[Tuple[str, float]]]
Run a (ΞΌ+Ξ») evolutionary loop to optimize a prompt.
At each generation:
- Score all candidates with fitness_fn.
- Keep the top-
top_kcandidates (elitist selection). - Fill the next population by mutating survivors at random.
When fitness_fn is a :class:TruGEPA, every prompt evaluation is logged
as a TruLens virtual record, giving a generation-by-generation audit trail
in the dashboard.
| PARAMETER | DESCRIPTION |
|---|---|
base_prompt
|
The starting prompt for the evolutionary search.
TYPE:
|
fitness_fn
|
Any callable |
mutate_fn
|
Callable that takes a prompt string and returns a mutated variant (e.g. rephrasing, adding instructions, changing tone). |
n_generations
|
Number of evolutionary generations to run.
TYPE:
|
population_size
|
Number of candidate prompts evaluated per generation.
TYPE:
|
top_k
|
Number of top-scoring candidates carried over to the next generation (elitist survivors).
TYPE:
|
seed
|
Optional random seed for reproducibility. |
| RETURNS | DESCRIPTION |
|---|---|
str
|
A three-tuple |
float
|
is a list of |
List[Tuple[str, float]]
|
pairs, one entry per generation. |