Skip to content
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

feat: document evaluation aggregation #2888

Merged
merged 1 commit into from
Apr 13, 2024
Merged
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
8 changes: 0 additions & 8 deletions src/phoenix/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,6 @@ def get_span_evaluation_span_ids(self, name: EvaluationName) -> Tuple[SpanID, ..
def get_evaluations_by_span_id(self, span_id: SpanID) -> List[pb.Evaluation]:
return self._evals.get_evaluations_by_span_id(span_id)

def get_document_evaluation_span_ids(self, name: EvaluationName) -> Tuple[SpanID, ...]:
return self._evals.get_document_evaluation_span_ids(name)

def get_document_evaluations_by_span_id(self, span_id: SpanID) -> List[pb.Evaluation]:
return self._evals.get_document_evaluations_by_span_id(span_id)

Expand Down Expand Up @@ -671,11 +668,6 @@ def get_evaluations_by_span_id(self, span_id: SpanID) -> List[pb.Evaluation]:
evaluations = self._evaluations_by_span_id.get(span_id)
return list(evaluations.values()) if evaluations else []

def get_document_evaluation_span_ids(self, name: EvaluationName) -> Tuple[SpanID, ...]:
with self._lock:
document_evaluations = self._document_evaluations_by_name.get(name)
return tuple(document_evaluations.keys()) if document_evaluations else ()

def get_document_evaluations_by_span_id(self, span_id: SpanID) -> List[pb.Evaluation]:
all_evaluations: List[pb.Evaluation] = []
with self._lock:
Expand Down
3 changes: 3 additions & 0 deletions src/phoenix/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class Span(Base):
cumulative_llm_token_count_completion: Mapped[int]

trace: Mapped["Trace"] = relationship("Trace", back_populates="spans")
document_annotations: Mapped[List["DocumentAnnotation"]] = relationship(back_populates="span")

__table_args__ = (
UniqueConstraint(
Expand Down Expand Up @@ -267,6 +268,8 @@ class DocumentAnnotation(Base):
updated_at: Mapped[datetime] = mapped_column(
UtcTimeStamp, server_default=func.now(), onupdate=func.now()
)
span: Mapped["Span"] = relationship(back_populates="document_annotations")

__table_args__ = (
UniqueConstraint(
"span_rowid",
Expand Down
55 changes: 31 additions & 24 deletions src/phoenix/server/api/types/Project.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from datetime import datetime
from typing import List, Optional

import numpy as np
import strawberry
from openinference.semconv.trace import SpanAttributes
from sqlalchemy import and_, func, select
from sqlalchemy.orm import contains_eager
from sqlalchemy.orm import contains_eager, selectinload
from sqlalchemy.sql.functions import coalesce
from strawberry import ID, UNSET
from strawberry.types import Info
Expand Down Expand Up @@ -293,37 +294,43 @@ def span_evaluation_summary(
return EvaluationSummary(evaluations, labels)

@strawberry.field
def document_evaluation_summary(
async def document_evaluation_summary(
self,
info: Info[Context, None],
evaluation_name: str,
time_range: Optional[TimeRange] = UNSET,
filter_condition: Optional[str] = UNSET,
) -> Optional[DocumentEvaluationSummary]:
project = self.project
predicate = (
SpanFilter(condition=filter_condition, evals=project) if filter_condition else None
)
span_ids = project.get_document_evaluation_span_ids(evaluation_name)
if not span_ids:
return None
spans = project.get_spans(
start_time=time_range.start if time_range else None,
stop_time=time_range.end if time_range else None,
span_ids=span_ids,
stmt = (
select(models.Span)
.join(models.Trace)
.where(
models.Trace.project_rowid == self.id_attr,
)
.options(selectinload(models.Span.document_annotations))
.options(contains_eager(models.Span.trace))
)
if predicate:
spans = filter(predicate, spans)
if time_range:
stmt = stmt.where(
and_(
time_range.start <= models.Span.start_time,
models.Span.start_time < time_range.end,
)
)
# todo: add filter_condition
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add filter condition to document evaluation summary

async with info.context.db() as session:
sql_spans = await session.scalars(stmt)
metrics_collection = []
for span in spans:
span_id = span.context.span_id
num_documents = project.get_num_documents(span_id)
if not num_documents:
for sql_span in sql_spans:
span = to_gql_span(sql_span, self.project)
if not (num_documents := span.num_documents):
continue
evaluation_scores = project.get_document_evaluation_scores(
span_id=span_id,
evaluation_name=evaluation_name,
num_documents=num_documents,
)
evaluation_scores: List[float] = [np.nan] * num_documents
for annotation in sql_span.document_annotations:
if (score := annotation.score) is not None and (
document_position := annotation.document_index
) < num_documents:
evaluation_scores[document_position] = score
metrics_collection.append(RetrievalMetrics(evaluation_scores))
if not metrics_collection:
return None
Expand Down
Loading