MJannik commited on
Commit
7f84cfb
·
verified ·
1 Parent(s): 6add786

fix: upgrade to langfuse sdk v3

Browse files
Files changed (1) hide show
  1. unit2/langgraph/mail_sorting.ipynb +57 -43
unit2/langgraph/mail_sorting.ipynb CHANGED
@@ -1,8 +1,8 @@
1
  {
2
  "cells": [
3
  {
4
- "metadata": {},
5
  "cell_type": "markdown",
 
6
  "source": [
7
  "# Alfred the Mail Sorting Butler: A LangGraph Example\n",
8
  "\n",
@@ -24,18 +24,18 @@
24
  ]
25
  },
26
  {
27
- "metadata": {},
28
  "cell_type": "code",
29
- "outputs": [],
30
  "execution_count": null,
 
 
31
  "source": [
32
  "# Install the required packages\n",
33
  "%pip install -q langgraph langchain_openai langchain_huggingface"
34
  ]
35
  },
36
  {
37
- "metadata": {},
38
  "cell_type": "markdown",
 
39
  "source": [
40
  "## Setting Up Our Environment\n",
41
  "\n",
@@ -43,10 +43,10 @@
43
  ]
44
  },
45
  {
46
- "metadata": {},
47
  "cell_type": "code",
48
- "outputs": [],
49
  "execution_count": null,
 
 
50
  "source": [
51
  "import os\n",
52
  "from typing import TypedDict, List, Dict, Any, Optional\n",
@@ -62,8 +62,8 @@
62
  ]
63
  },
64
  {
65
- "metadata": {},
66
  "cell_type": "markdown",
 
67
  "source": [
68
  "## Step 1: Define Our State\n",
69
  "\n",
@@ -77,10 +77,10 @@
77
  ]
78
  },
79
  {
80
- "metadata": {},
81
  "cell_type": "code",
82
- "outputs": [],
83
  "execution_count": null,
 
 
84
  "source": [
85
  "class EmailState(TypedDict):\n",
86
  " email: Dict[str, Any]\n",
@@ -92,15 +92,17 @@
92
  ]
93
  },
94
  {
95
- "metadata": {},
96
  "cell_type": "markdown",
97
- "source": "## Step 2: Define Our Nodes"
 
 
 
98
  },
99
  {
100
- "metadata": {},
101
  "cell_type": "code",
102
- "outputs": [],
103
  "execution_count": null,
 
 
104
  "source": [
105
  "def read_email(state: EmailState):\n",
106
  " email = state[\"email\"]\n",
@@ -212,15 +214,17 @@
212
  ]
213
  },
214
  {
215
- "metadata": {},
216
  "cell_type": "markdown",
217
- "source": "## Step 3: Define Our Routing Logic"
 
 
 
218
  },
219
  {
220
- "metadata": {},
221
  "cell_type": "code",
222
- "outputs": [],
223
  "execution_count": null,
 
 
224
  "source": [
225
  "# Add edges\n",
226
  "email_graph.add_edge(START, \"read_email\") # After starting we go to the \"read_email\" node\n",
@@ -244,25 +248,27 @@
244
  ]
245
  },
246
  {
247
- "metadata": {},
248
  "cell_type": "markdown",
249
- "source": "## Step 4: Create the StateGraph and Define Edges"
 
 
 
250
  },
251
  {
252
- "metadata": {},
253
  "cell_type": "code",
254
- "outputs": [],
255
  "execution_count": null,
 
 
256
  "source": [
257
  "# Compile the graph\n",
258
  "compiled_graph = email_graph.compile()"
259
  ]
260
  },
261
  {
262
- "metadata": {},
263
  "cell_type": "code",
264
- "outputs": [],
265
  "execution_count": null,
 
 
266
  "source": [
267
  "from IPython.display import Image, display\n",
268
  "\n",
@@ -270,10 +276,10 @@
270
  ]
271
  },
272
  {
273
- "metadata": {},
274
  "cell_type": "code",
275
- "outputs": [],
276
  "execution_count": null,
 
 
277
  "source": [
278
  " # Example emails for testing\n",
279
  "legitimate_email = {\n",
@@ -311,8 +317,8 @@
311
  ]
312
  },
313
  {
314
- "metadata": {},
315
  "cell_type": "markdown",
 
316
  "source": [
317
  "## Step 5: Inspecting Our Mail Sorting Agent with Langfuse 📡\n",
318
  "\n",
@@ -324,22 +330,26 @@
324
  ]
325
  },
326
  {
327
- "metadata": {},
328
  "cell_type": "code",
329
- "outputs": [],
330
  "execution_count": null,
331
- "source": "%pip install -q langfuse"
 
 
 
 
332
  },
333
  {
334
- "metadata": {},
335
  "cell_type": "markdown",
336
- "source": "Next, we set the Langfuse API keys and host address as environment variables. You can get your Langfuse credentials by signing up for [Langfuse Cloud](https://cloud.langfuse.com) or [self-hosting Langfuse](https://langfuse.com/self-hosting)."
 
 
 
337
  },
338
  {
339
- "metadata": {},
340
  "cell_type": "code",
341
- "outputs": [],
342
  "execution_count": null,
 
 
343
  "source": [
344
  "import os\n",
345
  "\n",
@@ -351,32 +361,36 @@
351
  ]
352
  },
353
  {
354
- "metadata": {},
355
  "cell_type": "markdown",
356
- "source": "Now, we configure the [Langfuse `callback_handler`](https://langfuse.com/docs/integrations/langchain/tracing#add-langfuse-to-your-langchain-application)."
 
 
 
357
  },
358
  {
359
- "metadata": {},
360
  "cell_type": "code",
361
- "outputs": [],
362
  "execution_count": null,
 
 
363
  "source": [
364
- "from langfuse.callback import CallbackHandler\n",
365
  "\n",
366
  "# Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)\n",
367
  "langfuse_handler = CallbackHandler()"
368
  ]
369
  },
370
  {
371
- "metadata": {},
372
  "cell_type": "markdown",
373
- "source": "We then add `config={\"callbacks\": [langfuse_handler]}` to the invocation of the agents and run them again."
 
 
 
374
  },
375
  {
376
- "metadata": {},
377
  "cell_type": "code",
378
- "outputs": [],
379
  "execution_count": null,
 
 
380
  "source": [
381
  "# Process legitimate email\n",
382
  "print(\"\\nProcessing legitimate email...\")\n",
@@ -404,8 +418,8 @@
404
  ]
405
  },
406
  {
407
- "metadata": {},
408
  "cell_type": "markdown",
 
409
  "source": [
410
  "Alfred is now connected 🔌! The runs from LangGraph are being logged in Langfuse, giving him full visibility into the agent's behavior. With this setup, he's ready to revisit previous runs and refine his Mail Sorting Agent even further.\n",
411
  "\n",
 
1
  {
2
  "cells": [
3
  {
 
4
  "cell_type": "markdown",
5
+ "metadata": {},
6
  "source": [
7
  "# Alfred the Mail Sorting Butler: A LangGraph Example\n",
8
  "\n",
 
24
  ]
25
  },
26
  {
 
27
  "cell_type": "code",
 
28
  "execution_count": null,
29
+ "metadata": {},
30
+ "outputs": [],
31
  "source": [
32
  "# Install the required packages\n",
33
  "%pip install -q langgraph langchain_openai langchain_huggingface"
34
  ]
35
  },
36
  {
 
37
  "cell_type": "markdown",
38
+ "metadata": {},
39
  "source": [
40
  "## Setting Up Our Environment\n",
41
  "\n",
 
43
  ]
44
  },
45
  {
 
46
  "cell_type": "code",
 
47
  "execution_count": null,
48
+ "metadata": {},
49
+ "outputs": [],
50
  "source": [
51
  "import os\n",
52
  "from typing import TypedDict, List, Dict, Any, Optional\n",
 
62
  ]
63
  },
64
  {
 
65
  "cell_type": "markdown",
66
+ "metadata": {},
67
  "source": [
68
  "## Step 1: Define Our State\n",
69
  "\n",
 
77
  ]
78
  },
79
  {
 
80
  "cell_type": "code",
 
81
  "execution_count": null,
82
+ "metadata": {},
83
+ "outputs": [],
84
  "source": [
85
  "class EmailState(TypedDict):\n",
86
  " email: Dict[str, Any]\n",
 
92
  ]
93
  },
94
  {
 
95
  "cell_type": "markdown",
96
+ "metadata": {},
97
+ "source": [
98
+ "## Step 2: Define Our Nodes"
99
+ ]
100
  },
101
  {
 
102
  "cell_type": "code",
 
103
  "execution_count": null,
104
+ "metadata": {},
105
+ "outputs": [],
106
  "source": [
107
  "def read_email(state: EmailState):\n",
108
  " email = state[\"email\"]\n",
 
214
  ]
215
  },
216
  {
 
217
  "cell_type": "markdown",
218
+ "metadata": {},
219
+ "source": [
220
+ "## Step 3: Define Our Routing Logic"
221
+ ]
222
  },
223
  {
 
224
  "cell_type": "code",
 
225
  "execution_count": null,
226
+ "metadata": {},
227
+ "outputs": [],
228
  "source": [
229
  "# Add edges\n",
230
  "email_graph.add_edge(START, \"read_email\") # After starting we go to the \"read_email\" node\n",
 
248
  ]
249
  },
250
  {
 
251
  "cell_type": "markdown",
252
+ "metadata": {},
253
+ "source": [
254
+ "## Step 4: Create the StateGraph and Define Edges"
255
+ ]
256
  },
257
  {
 
258
  "cell_type": "code",
 
259
  "execution_count": null,
260
+ "metadata": {},
261
+ "outputs": [],
262
  "source": [
263
  "# Compile the graph\n",
264
  "compiled_graph = email_graph.compile()"
265
  ]
266
  },
267
  {
 
268
  "cell_type": "code",
 
269
  "execution_count": null,
270
+ "metadata": {},
271
+ "outputs": [],
272
  "source": [
273
  "from IPython.display import Image, display\n",
274
  "\n",
 
276
  ]
277
  },
278
  {
 
279
  "cell_type": "code",
 
280
  "execution_count": null,
281
+ "metadata": {},
282
+ "outputs": [],
283
  "source": [
284
  " # Example emails for testing\n",
285
  "legitimate_email = {\n",
 
317
  ]
318
  },
319
  {
 
320
  "cell_type": "markdown",
321
+ "metadata": {},
322
  "source": [
323
  "## Step 5: Inspecting Our Mail Sorting Agent with Langfuse 📡\n",
324
  "\n",
 
330
  ]
331
  },
332
  {
 
333
  "cell_type": "code",
 
334
  "execution_count": null,
335
+ "metadata": {},
336
+ "outputs": [],
337
+ "source": [
338
+ "%pip install -q langfuse"
339
+ ]
340
  },
341
  {
 
342
  "cell_type": "markdown",
343
+ "metadata": {},
344
+ "source": [
345
+ "Next, we set the Langfuse API keys and host address as environment variables. You can get your Langfuse credentials by signing up for [Langfuse Cloud](https://cloud.langfuse.com) or [self-hosting Langfuse](https://langfuse.com/self-hosting)."
346
+ ]
347
  },
348
  {
 
349
  "cell_type": "code",
 
350
  "execution_count": null,
351
+ "metadata": {},
352
+ "outputs": [],
353
  "source": [
354
  "import os\n",
355
  "\n",
 
361
  ]
362
  },
363
  {
 
364
  "cell_type": "markdown",
365
+ "metadata": {},
366
+ "source": [
367
+ "Now, we configure the [Langfuse `callback_handler`](https://langfuse.com/docs/integrations/langchain/tracing#add-langfuse-to-your-langchain-application)."
368
+ ]
369
  },
370
  {
 
371
  "cell_type": "code",
 
372
  "execution_count": null,
373
+ "metadata": {},
374
+ "outputs": [],
375
  "source": [
376
+ "from langfuse.langchain import CallbackHandler\n",
377
  "\n",
378
  "# Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)\n",
379
  "langfuse_handler = CallbackHandler()"
380
  ]
381
  },
382
  {
 
383
  "cell_type": "markdown",
384
+ "metadata": {},
385
+ "source": [
386
+ "We then add `config={\"callbacks\": [langfuse_handler]}` to the invocation of the agents and run them again."
387
+ ]
388
  },
389
  {
 
390
  "cell_type": "code",
 
391
  "execution_count": null,
392
+ "metadata": {},
393
+ "outputs": [],
394
  "source": [
395
  "# Process legitimate email\n",
396
  "print(\"\\nProcessing legitimate email...\")\n",
 
418
  ]
419
  },
420
  {
 
421
  "cell_type": "markdown",
422
+ "metadata": {},
423
  "source": [
424
  "Alfred is now connected 🔌! The runs from LangGraph are being logged in Langfuse, giving him full visibility into the agent's behavior. With this setup, he's ready to revisit previous runs and refine his Mail Sorting Agent even further.\n",
425
  "\n",