Skip to content

Support evaluating sync tasks #2150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions pydantic_evals/pydantic_evals/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
from contextlib import AsyncExitStack, nullcontext
from contextvars import ContextVar
from dataclasses import dataclass, field
from inspect import iscoroutinefunction
from pathlib import Path
from typing import Any, Callable, Generic, Literal, Union, cast

import anyio
import logfire_api
import yaml
from anyio import to_thread
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError, model_serializer
from pydantic._internal import _typing_extra
from pydantic_core import to_json
Expand Down Expand Up @@ -253,7 +255,7 @@ def __init__(

async def evaluate(
self,
task: Callable[[InputsT], Awaitable[OutputT]],
task: Callable[[InputsT], Awaitable[OutputT]] | Callable[[InputsT], OutputT],
name: str | None = None,
max_concurrency: int | None = None,
progress: bool = True,
Expand Down Expand Up @@ -308,7 +310,7 @@ async def _handle_case(case: Case[InputsT, OutputT, MetadataT], report_case_name

def evaluate_sync(
self,
task: Callable[[InputsT], Awaitable[OutputT]],
task: Callable[[InputsT], Awaitable[OutputT]] | Callable[[InputsT], OutputT],
name: str | None = None,
max_concurrency: int | None = None,
progress: bool = True,
Expand Down Expand Up @@ -811,7 +813,7 @@ def record_attribute(self, name: str, value: Any) -> None:


async def _run_task(
task: Callable[[InputsT], Awaitable[OutputT]], case: Case[InputsT, OutputT, MetadataT]
task: Callable[[InputsT], Awaitable[OutputT] | OutputT], case: Case[InputsT, OutputT, MetadataT]
) -> EvaluatorContext[InputsT, OutputT, MetadataT]:
"""Run a task on a case and return the context for evaluators.

Expand All @@ -836,7 +838,10 @@ async def _run_task(
with _logfire.span('execute {task}', task=get_unwrapped_function_name(task)) as task_span:
with context_subtree() as span_tree:
t0 = time.perf_counter()
task_output = await task(case.inputs)
if iscoroutinefunction(task):
task_output = cast(OutputT, await task(case.inputs))
else:
task_output = cast(OutputT, await to_thread.run_sync(task, case.inputs))
fallback_duration = time.perf_counter() - t0
finally:
_CURRENT_TASK_RUN.reset(token)
Expand Down Expand Up @@ -873,7 +878,7 @@ async def _run_task(


async def _run_task_and_evaluators(
task: Callable[[InputsT], Awaitable[OutputT]],
task: Callable[[InputsT], Awaitable[OutputT]] | Callable[[InputsT], OutputT],
case: Case[InputsT, OutputT, MetadataT],
report_case_name: str,
dataset_evaluators: list[Evaluator[InputsT, OutputT, MetadataT]],
Expand Down
28 changes: 21 additions & 7 deletions tests/evals/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,35 @@ def evaluate(self, ctx: EvaluatorContext[TaskInput, TaskOutput, TaskMetadata]):
}


@pytest.mark.parametrize('async_task', [True, False])
async def test_evaluate(
example_dataset: Dataset[TaskInput, TaskOutput, TaskMetadata],
simple_evaluator: type[Evaluator[TaskInput, TaskOutput, TaskMetadata]],
async_task: bool,
):
"""Test evaluating a dataset."""
example_dataset.add_evaluator(simple_evaluator())

async def mock_task(inputs: TaskInput) -> TaskOutput:
if inputs.query == 'What is 2+2?':
return TaskOutput(answer='4')
elif inputs.query == 'What is the capital of France?':
return TaskOutput(answer='Paris')
return TaskOutput(answer='Unknown') # pragma: no cover
if async_task:

async def mock_async_task(inputs: TaskInput) -> TaskOutput:
if inputs.query == 'What is 2+2?':
return TaskOutput(answer='4')
elif inputs.query == 'What is the capital of France?':
return TaskOutput(answer='Paris')
return TaskOutput(answer='Unknown') # pragma: no cover

report = await example_dataset.evaluate(mock_async_task)
else:

def mock_sync_task(inputs: TaskInput) -> TaskOutput:
if inputs.query == 'What is 2+2?':
return TaskOutput(answer='4')
elif inputs.query == 'What is the capital of France?':
return TaskOutput(answer='Paris')
return TaskOutput(answer='Unknown') # pragma: no cover

report = await example_dataset.evaluate(mock_task)
report = await example_dataset.evaluate(mock_sync_task)

assert report is not None
assert len(report.cases) == 2
Expand Down
Loading