-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add IncrementalEvaluator and IncrementalEvaluationLogic
Task: IL-315
- Loading branch information
1 parent
9a58a4b
commit b67d3e8
Showing
3 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from pydantic import BaseModel | ||
|
||
from intelligence_layer.core.task import Task | ||
from intelligence_layer.core.tracer.tracer import Tracer | ||
from intelligence_layer.evaluation.dataset.domain import Example | ||
from intelligence_layer.evaluation.dataset.in_memory_dataset_repository import ( | ||
InMemoryDatasetRepository, | ||
) | ||
from intelligence_layer.evaluation.evaluation.evaluator import ( | ||
IncrementalEvaluationLogic, | ||
IncrementalEvaluator, | ||
) | ||
from intelligence_layer.evaluation.evaluation.in_memory_evaluation_repository import ( | ||
InMemoryEvaluationRepository, | ||
) | ||
from intelligence_layer.evaluation.run.domain import SuccessfulExampleOutput | ||
from intelligence_layer.evaluation.run.in_memory_run_repository import ( | ||
InMemoryRunRepository, | ||
) | ||
from intelligence_layer.evaluation.run.runner import Runner | ||
|
||
|
||
class DummyEvaluation(BaseModel): | ||
new_run_ids: list[str] | ||
old_run_ids: list[str] | ||
|
||
|
||
class DummyIncrementalLogic(IncrementalEvaluationLogic[str, str, str, DummyEvaluation]): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
def do_incremental_evaluate( | ||
self, | ||
example: Example[str, str], | ||
outputs: list[SuccessfulExampleOutput[str]], | ||
evaluated_outputs: list[SuccessfulExampleOutput[str]], | ||
) -> DummyEvaluation: | ||
return DummyEvaluation( | ||
new_run_ids=[output.run_id for output in outputs], | ||
old_run_ids=[output.run_id for output in evaluated_outputs], | ||
) | ||
|
||
|
||
class DummyTask(Task[str, str]): | ||
def __init__(self, info: str) -> None: | ||
super().__init__() | ||
self._info = info | ||
|
||
def do_run(self, input: str, tracer: Tracer) -> str: | ||
return f"{input} {self._info}" | ||
|
||
|
||
def test_incremental_evaluator_should_filter_previous_run_ids() -> None: | ||
# Given | ||
examples = [Example(input="a", expected_output="0", id="id_0")] | ||
|
||
dataset_repository = InMemoryDatasetRepository() | ||
dataset = dataset_repository.create_dataset( | ||
examples=examples, dataset_name="test_examples" | ||
) | ||
|
||
run_repository = InMemoryRunRepository() | ||
old_runner = Runner( | ||
task=DummyTask("Task0"), | ||
dataset_repository=dataset_repository, | ||
run_repository=run_repository, | ||
description="test_runner_0", | ||
) | ||
old_run = old_runner.run_dataset(dataset.id) | ||
|
||
evaluation_repository = InMemoryEvaluationRepository() | ||
evaluator = IncrementalEvaluator( | ||
dataset_repository=dataset_repository, | ||
run_repository=run_repository, | ||
evaluation_repository=evaluation_repository, | ||
description="test_incremental_evaluator", | ||
incremental_evaluation_logic=DummyIncrementalLogic(), | ||
) | ||
evaluation_overview = evaluator.evaluate_additional_runs(old_run.id) | ||
|
||
new_runner = Runner( | ||
task=DummyTask("Task2"), | ||
dataset_repository=dataset_repository, | ||
run_repository=run_repository, | ||
description="test_runner_2", | ||
) | ||
new_run = new_runner.run_dataset(dataset.id) | ||
|
||
# When | ||
new_evaluation_overview = evaluator.evaluate_additional_runs( | ||
old_run.id, new_run.id, previous_evaluation_id=evaluation_overview.id | ||
) | ||
|
||
# Then | ||
result = next( | ||
iter(evaluator.evaluation_lineages(new_evaluation_overview.id)) | ||
).evaluation.result | ||
assert isinstance(result, DummyEvaluation) | ||
assert result.new_run_ids == [new_run.id] | ||
assert result.old_run_ids == [old_run.id] |