-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d82d609
commit 8869cbd
Showing
7 changed files
with
73 additions
and
6 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
35 changes: 35 additions & 0 deletions
35
src/phoenix/server/api/dataloaders/prompt_version_sequence_number.py
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,35 @@ | ||
from typing import Optional | ||
|
||
from sqlalchemy import func, select | ||
from strawberry.dataloader import DataLoader | ||
from typing_extensions import TypeAlias | ||
|
||
from phoenix.db import models | ||
from phoenix.server.types import DbSessionFactory | ||
|
||
PromptVersionId: TypeAlias = int | ||
Key: TypeAlias = PromptVersionId | ||
Result: TypeAlias = Optional[int] | ||
|
||
|
||
class PromptVersionSequenceNumberDataLoader(DataLoader[Key, Result]): | ||
def __init__(self, db: DbSessionFactory) -> None: | ||
super().__init__(load_fn=self._load_fn) | ||
self._db = db | ||
|
||
async def _load_fn(self, keys: list[Key]) -> list[Result]: | ||
prompt_version_ids = keys | ||
row_number = ( | ||
func.row_number().over( | ||
partition_by=models.PromptVersion.prompt_id, | ||
order_by=models.PromptVersion.id, | ||
) | ||
).label("sequence_number") | ||
subq = select(models.PromptVersion.id.label("prompt_version_id"), row_number).subquery() | ||
stmt = select(subq).where(subq.c.prompt_version_id.in_(prompt_version_ids)) | ||
async with self._db() as session: | ||
result = { | ||
prompt_version_id: seq_number | ||
async for prompt_version_id, seq_number in await session.stream(stmt) | ||
} | ||
return [result.get(prompt_version_id) for prompt_version_id 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