-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make graphql api for span evaluations read from database (#2860)
Makes the GraphQL resolver for span evaluations read from the database, using a data loader. Coauthored-by: Mikyo King mikeldking@gmail.com
- Loading branch information
1 parent
d539e77
commit 5adf750
Showing
7 changed files
with
69 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .latency_ms_quantile import LatencyMsQuantileDataLoader | ||
from .span_evaluations import SpanEvaluationsDataLoader | ||
|
||
__all__ = [ | ||
"LatencyMsQuantileDataLoader", | ||
"SpanEvaluationsDataLoader", | ||
] |
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,39 @@ | ||
from collections import defaultdict | ||
from typing import ( | ||
AsyncContextManager, | ||
Callable, | ||
DefaultDict, | ||
List, | ||
) | ||
|
||
from sqlalchemy import and_, select | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from strawberry.dataloader import DataLoader | ||
from typing_extensions import TypeAlias | ||
|
||
from phoenix.db import models | ||
from phoenix.server.api.types.Evaluation import SpanEvaluation | ||
|
||
Key: TypeAlias = int | ||
|
||
|
||
class SpanEvaluationsDataLoader(DataLoader[Key, List[SpanEvaluation]]): | ||
def __init__(self, db: Callable[[], AsyncContextManager[AsyncSession]]) -> None: | ||
super().__init__(load_fn=self._load_fn) | ||
self._db = db | ||
|
||
async def _load_fn(self, keys: List[Key]) -> List[List[SpanEvaluation]]: | ||
span_evaluations_by_id: DefaultDict[Key, List[SpanEvaluation]] = defaultdict(list) | ||
async with self._db() as session: | ||
for span_evaluation in await session.scalars( | ||
select(models.SpanAnnotation).where( | ||
and_( | ||
models.SpanAnnotation.span_rowid.in_(keys), | ||
models.SpanAnnotation.annotator_kind == "LLM", | ||
) | ||
) | ||
): | ||
span_evaluations_by_id[span_evaluation.span_rowid].append( | ||
SpanEvaluation.from_sql_span_annotation(span_evaluation) | ||
) | ||
return [span_evaluations_by_id[key] for key in keys] |
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