Skip to content

Commit 24cc2b0

Browse files
anticorrelatormikeldking
authored andcommitted
Update notebook and clean up client ergonomics (#7394)
1 parent c949229 commit 24cc2b0

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

packages/phoenix-client/src/phoenix/client/resources/spans/__init__.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def get_span_annotations_dataframe(
120120
*,
121121
spans_dataframe: Optional["pd.DataFrame"] = None,
122122
span_ids: Optional[Iterable[str]] = None,
123-
project: str,
123+
project: str = "default",
124124
limit: int = 1000,
125125
timeout: Optional[int] = DEFAULT_TIMEOUT_IN_SECONDS,
126126
) -> "pd.DataFrame":
@@ -175,7 +175,7 @@ def get_span_annotations_dataframe(
175175
annotations: list[v1.SpanAnnotation] = []
176176
path = f"v1/projects/{project}/span_annotations"
177177

178-
for i in range(len(span_ids_list), _MAX_SPAN_IDS_PER_REQUEST):
178+
for i in range(0, len(span_ids_list), _MAX_SPAN_IDS_PER_REQUEST):
179179
batch_ids = span_ids_list[i : i + _MAX_SPAN_IDS_PER_REQUEST]
180180
cursor: Optional[str] = None
181181
while True:
@@ -201,7 +201,9 @@ def get_span_annotations_dataframe(
201201
if not cursor:
202202
break # finished paginating this batch
203203

204-
return pd.DataFrame(annotations)
204+
df = pd.DataFrame(annotations)
205+
df.set_index("span_id", inplace=True)
206+
return df
205207

206208
def get_span_annotations(
207209
self,
@@ -416,7 +418,7 @@ async def get_span_annotations_dataframe(
416418
annotations: list[v1.SpanAnnotation] = []
417419
path = f"v1/projects/{project}/span_annotations"
418420

419-
for i in range(len(span_ids_list), _MAX_SPAN_IDS_PER_REQUEST):
421+
for i in range(0, len(span_ids_list), _MAX_SPAN_IDS_PER_REQUEST):
420422
batch_ids = span_ids_list[i : i + _MAX_SPAN_IDS_PER_REQUEST]
421423
cursor: Optional[str] = None
422424
while True:
@@ -441,7 +443,9 @@ async def get_span_annotations_dataframe(
441443
if not cursor:
442444
break
443445

444-
return pd.DataFrame(annotations)
446+
df = pd.DataFrame(annotations)
447+
df.set_index("span_id", inplace=True)
448+
return df
445449

446450
async def get_span_annotations(
447451
self,

tutorials/human_feedback/chatbot_with_human_feedback.ipynb

+50-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
{
3636
"cell_type": "code",
37-
"execution_count": null,
37+
"execution_count": 1,
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [
@@ -89,7 +89,7 @@
8989
},
9090
{
9191
"cell_type": "code",
92-
"execution_count": null,
92+
"execution_count": 2,
9393
"metadata": {},
9494
"outputs": [],
9595
"source": [
@@ -114,7 +114,7 @@
114114
},
115115
{
116116
"cell_type": "code",
117-
"execution_count": null,
117+
"execution_count": 4,
118118
"metadata": {},
119119
"outputs": [],
120120
"source": [
@@ -222,11 +222,57 @@
222222
"# Display the chat interface\n",
223223
"display(chat_history, input_box, send_button)"
224224
]
225+
},
226+
{
227+
"cell_type": "markdown",
228+
"metadata": {},
229+
"source": [
230+
"## Analyze feedback using the Phoenix Client\n",
231+
"\n",
232+
"We can use the Phoenix client to pull the annotated spans. By combining `get_spans_dataframe`\n",
233+
"and `get_span_annotations_dataframe` we can create a dataframe of all annotations alongside\n",
234+
"span data for analysis!"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": 1,
240+
"metadata": {},
241+
"outputs": [],
242+
"source": [
243+
"spans_df = client.spans.get_spans_dataframe(project_name=\"default\")\n",
244+
"annotations_df = client.spans.get_span_annotations_dataframe(\n",
245+
" spans_dataframe=spans_df, project=\"default\"\n",
246+
")"
247+
]
248+
},
249+
{
250+
"cell_type": "code",
251+
"execution_count": null,
252+
"metadata": {},
253+
"outputs": [],
254+
"source": [
255+
"annotations_df.join(spans_df, how=\"inner\", lsuffix=\"_annotation\", rsuffix=\"_span\")"
256+
]
225257
}
226258
],
227259
"metadata": {
260+
"kernelspec": {
261+
"display_name": "dev",
262+
"language": "python",
263+
"name": "python3"
264+
},
228265
"language_info": {
229-
"name": "python"
266+
"codemirror_mode": {
267+
"name": "ipython",
268+
"version": 3
269+
},
270+
"file_extension": ".py",
271+
"mimetype": "text/x-python",
272+
"name": "python",
273+
"nbconvert_exporter": "python",
274+
"pygments_lexer": "ipython3",
275+
"version": "3.11.9"
230276
}
231277
},
232278
"nbformat": 4,

0 commit comments

Comments
 (0)