|
| 1 | +package com.launchdarkly.sdk.server.ai; |
| 2 | + |
| 3 | +import com.launchdarkly.logging.LDLogger; |
| 4 | +import com.launchdarkly.logging.Logs; |
| 5 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.JudgeResult; |
| 6 | + |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.concurrent.ExecutionException; |
| 13 | +import java.util.concurrent.ExecutorService; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.concurrent.Future; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.concurrent.TimeoutException; |
| 18 | + |
| 19 | +/** |
| 20 | + * Runs a fixed set of {@link Judge}s against one input/output pair and collects their results. |
| 21 | + * <p> |
| 22 | + * Each judge runs with <strong>fault isolation</strong>: a judge that throws or times out yields a |
| 23 | + * failed {@link JudgeResult} for that judge while every other judge's result is preserved, in the |
| 24 | + * original order. Judges run concurrently and each is bounded by a per-judge timeout so a single |
| 25 | + * hung judge cannot stall the whole evaluation. |
| 26 | + * <p> |
| 27 | + * The evaluator does not record results; recording the returned {@link JudgeResult}s (for example |
| 28 | + * via a tracker) is the caller's responsibility. Instances are immutable and thread-safe. |
| 29 | + */ |
| 30 | +public final class Evaluator { |
| 31 | + /** |
| 32 | + * Default per-judge timeout used when one is not supplied. |
| 33 | + */ |
| 34 | + public static final Duration DEFAULT_PER_JUDGE_TIMEOUT = Duration.ofSeconds(30); |
| 35 | + |
| 36 | + private final List<Judge> judges; |
| 37 | + private final Duration perJudgeTimeout; |
| 38 | + private final LDLogger logger; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates an evaluator using the {@link #DEFAULT_PER_JUDGE_TIMEOUT default per-judge timeout}. |
| 42 | + * |
| 43 | + * @param judges the judges to run; must not be {@code null} |
| 44 | + * @param logger the logger; must not be {@code null} |
| 45 | + */ |
| 46 | + public Evaluator(List<Judge> judges, LDLogger logger) { |
| 47 | + this(judges, DEFAULT_PER_JUDGE_TIMEOUT, Objects.requireNonNull(logger, "logger")); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates an evaluator with an explicit per-judge timeout. |
| 52 | + * |
| 53 | + * @param judges the judges to run; must not be {@code null} |
| 54 | + * @param perJudgeTimeout the maximum time to wait for each judge; must not be {@code null} |
| 55 | + * @param logger the logger; must not be {@code null} |
| 56 | + */ |
| 57 | + public Evaluator(List<Judge> judges, Duration perJudgeTimeout, LDLogger logger) { |
| 58 | + this.judges = Collections.unmodifiableList(new ArrayList<>(Objects.requireNonNull(judges, "judges"))); |
| 59 | + this.perJudgeTimeout = Objects.requireNonNull(perJudgeTimeout, "perJudgeTimeout"); |
| 60 | + this.logger = Objects.requireNonNull(logger, "logger"); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns an evaluator with no judges. Its {@link #evaluate} returns an empty list and logs |
| 65 | + * nothing. |
| 66 | + * |
| 67 | + * @return a no-op evaluator |
| 68 | + */ |
| 69 | + public static Evaluator noop() { |
| 70 | + return new Evaluator( |
| 71 | + Collections.emptyList(), DEFAULT_PER_JUDGE_TIMEOUT, LDLogger.withAdapter(Logs.none(), "")); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Runs every judge against the given input and output. |
| 76 | + * |
| 77 | + * @param input the input that was provided to the AI being evaluated |
| 78 | + * @param output the AI-generated response to score |
| 79 | + * @return one {@link JudgeResult} per judge, in the judges' order; empty when there are no judges |
| 80 | + */ |
| 81 | + public List<JudgeResult> evaluate(String input, String output) { |
| 82 | + if (judges.isEmpty()) { |
| 83 | + return Collections.emptyList(); |
| 84 | + } |
| 85 | + |
| 86 | + ExecutorService pool = Executors.newFixedThreadPool(judges.size()); |
| 87 | + try { |
| 88 | + List<Future<JudgeResult>> futures = new ArrayList<>(judges.size()); |
| 89 | + for (Judge judge : judges) { |
| 90 | + futures.add(pool.submit(() -> judge.evaluate(input, output))); |
| 91 | + } |
| 92 | + |
| 93 | + List<JudgeResult> results = new ArrayList<>(judges.size()); |
| 94 | + for (int i = 0; i < judges.size(); i++) { |
| 95 | + results.add(awaitResult(judges.get(i), futures.get(i))); |
| 96 | + } |
| 97 | + return results; |
| 98 | + } finally { |
| 99 | + pool.shutdownNow(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private JudgeResult awaitResult(Judge judge, Future<JudgeResult> future) { |
| 104 | + String key = judge.getAIConfig().getKey(); |
| 105 | + try { |
| 106 | + return future.get(perJudgeTimeout.toMillis(), TimeUnit.MILLISECONDS); |
| 107 | + } catch (TimeoutException e) { |
| 108 | + future.cancel(true); |
| 109 | + logger.warn("Judge {} timed out after {} ms", key, perJudgeTimeout.toMillis()); |
| 110 | + return failed(key, "Judge evaluation timed out"); |
| 111 | + } catch (ExecutionException e) { |
| 112 | + Throwable cause = e.getCause() != null ? e.getCause() : e; |
| 113 | + logger.error("Judge {} failed: {}", key, cause.toString()); |
| 114 | + return failed(key, cause.getMessage() != null ? cause.getMessage() : "Unknown error"); |
| 115 | + } catch (InterruptedException e) { |
| 116 | + Thread.currentThread().interrupt(); |
| 117 | + future.cancel(true); |
| 118 | + return failed(key, "Judge evaluation interrupted"); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static JudgeResult failed(String key, String message) { |
| 123 | + return JudgeResult.builder(true, false).judgeConfigKey(key).errorMessage(message).build(); |
| 124 | + } |
| 125 | +} |
0 commit comments