{ "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 Hugging Face Agents Course, a free course from beginner to expert, where you learn to build Agents.\n", "\n", "![Agents course share](https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/share.png)\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", "![Example trace in Langfuse](https://langfuse.com/images/cookbook/huggingface-agent-course/langgraph-trace-legit.png)\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", "![Example trace in Langfuse](https://langfuse.com/images/cookbook/huggingface-agent-course/langgraph-trace-spam.png)\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 }