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

fix: Propagate span annotation metadata to examples on all mutations #4195

Merged
merged 5 commits into from
Aug 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
64 changes: 52 additions & 12 deletions src/phoenix/server/api/mutations/dataset_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ async def add_examples_to_dataset(
) -> DatasetMutationPayload:
dataset_id = input.dataset_id
# Extract the span rowids from the input examples if they exist
span_ids = span_ids = [example.span_id for example in input.examples if example.span_id]
span_ids = [example.span_id for example in input.examples if example.span_id]
span_rowids = {
from_global_id_with_expected_type(global_id=span_id, expected_type_name=Span.__name__)
for span_id in set(span_ids)
Expand Down Expand Up @@ -260,16 +260,45 @@ async def add_examples_to_dataset(
)
.returning(models.DatasetVersion.id)
)

# Fetch spans and span annotations
spans = (
await session.execute(
select(models.Span.id)
.select_from(models.Span)
.where(models.Span.id.in_(span_rowids))
)
).all()
# Just validate that the number of spans matches the number of span_ids
# to ensure that the span_ids are valid
assert len(spans) == len(span_rowids)

span_annotations = (
await session.execute(
select(
models.SpanAnnotation.span_rowid,
models.SpanAnnotation.name,
models.SpanAnnotation.label,
models.SpanAnnotation.score,
models.SpanAnnotation.explanation,
models.SpanAnnotation.metadata_,
models.SpanAnnotation.annotator_kind,
)
.select_from(models.SpanAnnotation)
.where(models.SpanAnnotation.span_rowid.in_(span_rowids))
)
).all()

span_annotations_by_span: Dict[int, Dict[Any, Any]] = {span.id: {} for span in spans}
for annotation in span_annotations:
span_id = annotation.span_rowid
if span_id not in span_annotations_by_span:
span_annotations_by_span[span_id] = dict()
span_annotations_by_span[span_id][annotation.name] = {
"label": annotation.label,
"score": annotation.score,
"explanation": annotation.explanation,
"metadata": annotation.metadata_,
"annotator_kind": annotation.annotator_kind,
}

DatasetExample = models.DatasetExample
dataset_example_rowids = (
await session.scalars(
Expand All @@ -291,21 +320,32 @@ async def add_examples_to_dataset(
assert len(dataset_example_rowids) == len(input.examples)
assert all(map(lambda id: isinstance(id, int), dataset_example_rowids))
DatasetExampleRevision = models.DatasetExampleRevision
await session.execute(
insert(DatasetExampleRevision),
[

dataset_example_revisions = []
for dataset_example_rowid, example in zip(dataset_example_rowids, input.examples):
span_annotation = {}
if example.span_id:
span_id = from_global_id_with_expected_type(
global_id=example.span_id,
expected_type_name=Span.__name__,
)
span_annotation = span_annotations_by_span.get(span_id, {})
dataset_example_revisions.append(
{
DatasetExampleRevision.dataset_example_id.key: dataset_example_rowid,
DatasetExampleRevision.dataset_version_id.key: dataset_version_rowid,
DatasetExampleRevision.input.key: example.input,
DatasetExampleRevision.output.key: example.output,
DatasetExampleRevision.metadata_.key: example.metadata,
DatasetExampleRevision.metadata_.key: {
**(example.metadata or {}),
"annotations": span_annotation,
},
DatasetExampleRevision.revision_kind.key: "CREATE",
}
for dataset_example_rowid, example in zip(
dataset_example_rowids, input.examples
)
],
)
await session.execute(
insert(DatasetExampleRevision),
dataset_example_revisions,
)
info.context.event_queue.put(DatasetInsertEvent((dataset.id,)))
return DatasetMutationPayload(dataset=to_gql_dataset(dataset))
Expand Down
22 changes: 20 additions & 2 deletions src/phoenix/server/api/types/Span.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ async def descendants(
@strawberry.field(
description="The span's attributes translated into an example revision for a dataset",
) # type: ignore
def as_example_revision(self) -> SpanAsExampleRevision:
async def as_example_revision(self, info: Info[Context, None]) -> SpanAsExampleRevision:
db_span = self.db_span
attributes = db_span.attributes
span_io = _SpanIO(
Expand All @@ -256,10 +256,28 @@ def as_example_revision(self) -> SpanAsExampleRevision:
llm_output_messages=get_attribute_value(attributes, LLM_OUTPUT_MESSAGES),
retrieval_documents=get_attribute_value(attributes, RETRIEVAL_DOCUMENTS),
)

# Fetch annotations associated with this span
span_annotations = await self.span_annotations(info)
annotations = dict()
for annotation in span_annotations:
annotations[annotation.name] = {
"label": annotation.label,
"score": annotation.score,
"explanation": annotation.explanation,
"metadata": annotation.metadata,
"annotator_kind": annotation.annotator_kind.value,
}
# Merge annotations into the metadata
metadata = {
**attributes,
"annotations": annotations,
}

return SpanAsExampleRevision(
input=get_dataset_example_input(span_io),
output=get_dataset_example_output(span_io),
metadata=attributes,
metadata=metadata,
)

@strawberry.field(description="The project that this span belongs to.") # type: ignore
Expand Down
Loading