方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Implement a sampling strategy for production evaluation

Implement a Sampling Strategy for Production Evaluation

You cannot run an expensive LLM judge on every production request. A good sampling strategy applies the expensive scorer to the right requests — not random ones.

What to build

Implement EvalSampler:

  1. should_sample_random(sample_rate: float) -> bool — Returns True with probability sample_rate. Use random.random().

  2. should_sample_rule_based(event: dict) -> bool — Returns True if ANY of these apply: event has "user_feedback": "negative", event has "finish_reason": "max_tokens", event has "auto_score" and that score is between 0.4 and 0.7 (the uncertain zone), event has "is_new_query_type": True.

  3. classify(event: dict, sample_rate: float = 0.05) -> dict — Returns {"sampled": bool, "reason": str}. Reason values: "rule_triggered", "random_sample", "not_sampled". Rule-based takes priority over random.

  4. batch_classify(events: list[dict], sample_rate: float = 0.05) -> dict — Returns {"total", "sampled", "by_reason": dict, "sample_rate_actual": float}.

Constraints

  • Standard library only. Make should_sample_random deterministic when called with a fixed random.seed.

Evaluation / medium / Step 34 of 36

Practice stage

Evaluation and review loops

Hint

Separate the scoring logic from the interpretation logic. Your goal is not just a number; it is a useful next action.

Success criteria
  • - Produces a useful signal, not decorative output
  • - Makes regression review easier
  • - Would support a benchmark or observability loop
Review checklist
  • - Would this output help decide what to fix next?
  • - Are important failure modes visible?
  • - Does the score hide any ambiguity I should record?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.