File size: 15,474 Bytes
fa36169 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": [
"# Alfred the Mail Sorting Butler: A LangGraph Example\n",
"\n",
"In this notebook, **we're going to build a complete email processing workflow using LangGraph**.\n",
"\n",
"This notebook is part of the <a href=\"https://www.hf.co/learn/agents-course\">Hugging Face Agents Course</a>, a free course from beginner to expert, where you learn to build Agents.\n",
"\n",
"\n",
"\n",
"## What You'll Learn\n",
"\n",
"In this notebook, you'll learn how to:\n",
"1. Set up a LangGraph workflow\n",
"2. Define state and nodes for email processing\n",
"3. Create conditional branching in a graph\n",
"4. Connect an LLM for classification and content generation\n",
"5. Visualize the workflow graph\n",
"6. Execute the workflow with example data"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# Install the required packages\n",
"%pip install -q langgraph langchain_openai langchain_huggingface"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Setting Up Our Environment\n",
"\n",
"First, let's import all the necessary libraries. LangGraph provides the graph structure, while LangChain offers convenient interfaces for working with LLMs."
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"import os\n",
"from typing import TypedDict, List, Dict, Any, Optional\n",
"from langgraph.graph import StateGraph, END\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_core.messages import HumanMessage\n",
"\n",
"# Set your OpenAI API key here\n",
"os.environ[\"OPENAI_API_KEY\"] = \"sk-xxxxx\" # Replace with your actual API key\n",
"\n",
"# Initialize our LLM\n",
"model = ChatOpenAI(model=\"gpt-4o\", temperature=0)"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Step 1: Define Our State\n",
"\n",
"In LangGraph, **State** is the central concept. It represents all the information that flows through our workflow.\n",
"\n",
"For Alfred's email processing system, we need to track:\n",
"- The email being processed\n",
"- Whether it's spam or not\n",
"- The draft response (for legitimate emails)\n",
"- Conversation history with the LLM"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"class EmailState(TypedDict):\n",
" email: Dict[str, Any]\n",
" is_spam: Optional[bool]\n",
" spam_reason: Optional[str]\n",
" email_category: Optional[str]\n",
" email_draft: Optional[str]\n",
" messages: List[Dict[str, Any]]"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Step 2: Define Our Nodes"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"def read_email(state: EmailState):\n",
" email = state[\"email\"]\n",
" print(f\"Alfred is processing an email from {email['sender']} with subject: {email['subject']}\")\n",
" return {}\n",
"\n",
"\n",
"def classify_email(state: EmailState):\n",
" email = state[\"email\"]\n",
"\n",
" prompt = f\"\"\"\n",
"As Alfred the butler of Mr wayne and it's SECRET identity Batman, analyze this email and determine if it is spam or legitimate and should be brought to Mr wayne's attention.\n",
"\n",
"Email:\n",
"From: {email['sender']}\n",
"Subject: {email['subject']}\n",
"Body: {email['body']}\n",
"\n",
"First, determine if this email is spam.\n",
"answer with SPAM or HAM if it's legitimate. Only return the answer\n",
"Answer :\n",
" \"\"\"\n",
" messages = [HumanMessage(content=prompt)]\n",
" response = model.invoke(messages)\n",
"\n",
" response_text = response.content.lower()\n",
" print(response_text)\n",
" is_spam = \"spam\" in response_text and \"ham\" not in response_text\n",
"\n",
" if not is_spam:\n",
" new_messages = state.get(\"messages\", []) + [\n",
" {\"role\": \"user\", \"content\": prompt},\n",
" {\"role\": \"assistant\", \"content\": response.content}\n",
" ]\n",
" else:\n",
" new_messages = state.get(\"messages\", [])\n",
"\n",
" return {\n",
" \"is_spam\": is_spam,\n",
" \"messages\": new_messages\n",
" }\n",
"\n",
"\n",
"def handle_spam(state: EmailState):\n",
" print(f\"Alfred has marked the email as spam.\")\n",
" print(\"The email has been moved to the spam folder.\")\n",
" return {}\n",
"\n",
"\n",
"def drafting_response(state: EmailState):\n",
" email = state[\"email\"]\n",
"\n",
" prompt = f\"\"\"\n",
"As Alfred the butler, draft a polite preliminary response to this email.\n",
"\n",
"Email:\n",
"From: {email['sender']}\n",
"Subject: {email['subject']}\n",
"Body: {email['body']}\n",
"\n",
"Draft a brief, professional response that Mr. Wayne can review and personalize before sending.\n",
" \"\"\"\n",
"\n",
" messages = [HumanMessage(content=prompt)]\n",
" response = model.invoke(messages)\n",
"\n",
" new_messages = state.get(\"messages\", []) + [\n",
" {\"role\": \"user\", \"content\": prompt},\n",
" {\"role\": \"assistant\", \"content\": response.content}\n",
" ]\n",
"\n",
" return {\n",
" \"email_draft\": response.content,\n",
" \"messages\": new_messages\n",
" }\n",
"\n",
"\n",
"def notify_mr_wayne(state: EmailState):\n",
" email = state[\"email\"]\n",
"\n",
" print(\"\\n\" + \"=\" * 50)\n",
" print(f\"Sir, you've received an email from {email['sender']}.\")\n",
" print(f\"Subject: {email['subject']}\")\n",
" print(\"\\nI've prepared a draft response for your review:\")\n",
" print(\"-\" * 50)\n",
" print(state[\"email_draft\"])\n",
" print(\"=\" * 50 + \"\\n\")\n",
"\n",
" return {}\n",
"\n",
"\n",
"# Define routing logic\n",
"def route_email(state: EmailState) -> str:\n",
" if state[\"is_spam\"]:\n",
" return \"spam\"\n",
" else:\n",
" return \"legitimate\"\n",
"\n",
"\n",
"# Create the graph\n",
"email_graph = StateGraph(EmailState)\n",
"\n",
"# Add nodes\n",
"email_graph.add_node(\"read_email\", read_email) # the read_email node executes the read_mail function\n",
"email_graph.add_node(\"classify_email\", classify_email) # the classify_email node will execute the classify_email function\n",
"email_graph.add_node(\"handle_spam\", handle_spam) #same logic\n",
"email_graph.add_node(\"drafting_response\", drafting_response) #same logic\n",
"email_graph.add_node(\"notify_mr_wayne\", notify_mr_wayne) # same logic\n"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Step 3: Define Our Routing Logic"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# Add edges\n",
"email_graph.add_edge(START, \"read_email\") # After starting we go to the \"read_email\" node\n",
"\n",
"email_graph.add_edge(\"read_email\", \"classify_email\") # after_reading we classify\n",
"\n",
"# Add conditional edges\n",
"email_graph.add_conditional_edges(\n",
" \"classify_email\", # after classify, we run the \"route_email\" function\"\n",
" route_email,\n",
" {\n",
" \"spam\": \"handle_spam\", # if it return \"Spam\", we go the \"handle_span\" node\n",
" \"legitimate\": \"drafting_response\" # and if it's legitimate, we go to the \"drafting response\" node\n",
" }\n",
")\n",
"\n",
"# Add final edges\n",
"email_graph.add_edge(\"handle_spam\", END) # after handling spam we always end\n",
"email_graph.add_edge(\"drafting_response\", \"notify_mr_wayne\")\n",
"email_graph.add_edge(\"notify_mr_wayne\", END) # after notifyinf Me wayne, we can end too\n"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Step 4: Create the StateGraph and Define Edges"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# Compile the graph\n",
"compiled_graph = email_graph.compile()"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"from IPython.display import Image, display\n",
"\n",
"display(Image(compiled_graph.get_graph().draw_mermaid_png()))"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
" # Example emails for testing\n",
"legitimate_email = {\n",
" \"sender\": \"Joker\",\n",
" \"subject\": \"Found you Batman ! \",\n",
" \"body\": \"Mr. Wayne,I found your secret identity ! I know you're batman ! Ther's no denying it, I have proof of that and I'm coming to find you soon. I'll get my revenge. JOKER\"\n",
"}\n",
"\n",
"spam_email = {\n",
" \"sender\": \"Crypto bro\",\n",
" \"subject\": \"The best investment of 2025\",\n",
" \"body\": \"Mr Wayne, I just launched an ALT coin and want you to buy some !\"\n",
"}\n",
"# Process legitimate email\n",
"print(\"\\nProcessing legitimate email...\")\n",
"legitimate_result = compiled_graph.invoke({\n",
" \"email\": legitimate_email,\n",
" \"is_spam\": None,\n",
" \"spam_reason\": None,\n",
" \"email_category\": None,\n",
" \"email_draft\": None,\n",
" \"messages\": []\n",
"})\n",
"\n",
"# Process spam email\n",
"print(\"\\nProcessing spam email...\")\n",
"spam_result = compiled_graph.invoke({\n",
" \"email\": spam_email,\n",
" \"is_spam\": None,\n",
" \"spam_reason\": None,\n",
" \"email_category\": None,\n",
" \"email_draft\": None,\n",
" \"messages\": []\n",
"})"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"## Step 5: Inspecting Our Mail Sorting Agent with Langfuse ๐ก\n",
"\n",
"As Alfred fine-tunes the Main Sorting Agent, he's growing weary of debugging its runs. Agents, by nature, are unpredictable and difficult to inspect. But since he aims to build the ultimate Spam Detection Agent and deploy it in production, he needs robust traceability for future monitoring and analysis.\n",
"\n",
"To do this, Alfred can use an observability tool such as [Langfuse](https://langfuse.com/) to trace and monitor the inner steps of the agent.\n",
"\n",
"First, we need to install the necessary dependencies:"
]
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "%pip install -q langfuse"
},
{
"metadata": {},
"cell_type": "markdown",
"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)."
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"import os\n",
"\n",
"# Get keys for your project from the project settings page: https://cloud.langfuse.com\n",
"os.environ[\"LANGFUSE_PUBLIC_KEY\"] = \"pk-lf-...\"\n",
"os.environ[\"LANGFUSE_SECRET_KEY\"] = \"sk-lf-...\"\n",
"os.environ[\"LANGFUSE_HOST\"] = \"https://cloud.langfuse.com\" # ๐ช๐บ EU region\n",
"# os.environ[\"LANGFUSE_HOST\"] = \"https://us.cloud.langfuse.com\" # ๐บ๐ธ US region"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Now, we configure the [Langfuse `callback_handler`](https://langfuse.com/docs/integrations/langchain/tracing#add-langfuse-to-your-langchain-application)."
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"from langfuse.callback import CallbackHandler\n",
"\n",
"# Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)\n",
"langfuse_handler = CallbackHandler()"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "We then add `config={\"callbacks\": [langfuse_handler]}` to the invocation of the agents and run them again."
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# Process legitimate email\n",
"print(\"\\nProcessing legitimate email...\")\n",
"legitimate_result = compiled_graph.invoke(\n",
" input={\n",
" \"email\": legitimate_email,\n",
" \"is_spam\": None,\n",
" \"draft_response\": None,\n",
" \"messages\": []\n",
" },\n",
" config={\"callbacks\": [langfuse_handler]}\n",
")\n",
"\n",
"# Process spam email\n",
"print(\"\\nProcessing spam email...\")\n",
"spam_result = compiled_graph.invoke(\n",
" input={\n",
" \"email\": spam_email,\n",
" \"is_spam\": None,\n",
" \"draft_response\": None,\n",
" \"messages\": []\n",
" },\n",
" config={\"callbacks\": [langfuse_handler]}\n",
")"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"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",
"\n",
"\n",
"\n",
"_[Public link to the trace with the legit email](https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/f5d6d72e-20af-4357-b232-af44c3728a7b?timestamp=2025-03-17T10%3A13%3A28.413Z&observation=6997ba69-043f-4f77-9445-700a033afba1)_\n",
"\n",
"\n",
"\n",
"_[Public link to the trace with the spam email](https://langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/6e498053-fee4-41fd-b1ab-d534aca15f82?timestamp=2025-03-17T10%3A13%3A30.884Z&observation=84770fc8-4276-4720-914f-bf52738d44ba)_\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|