fix: upgrade to langfuse sdk v3 (#102)
Browse files- fix: upgrade to langfuse sdk v3 (7f84cfb66e7e695adc8b785beeffe3b135a3f277)
Co-authored-by: Jannik <MJannik@users.noreply.huggingface.co>
12344567815 unmodified lines2425262727292829303132333435363737383940411 unmodified line4344454646484748495051529 unmodified lines6263646565666768697 unmodified lines7778798080828182838485865 unmodified lines92939495959697979899100101100102102103104105106107108105 unmodified lines21421521621521721821921722022122222322022422222522622722822923017 unmodified lines2482492502472512522532492542552562572522582542592602612622632642652662672622682642692702712722732741 unmodified line27627727827327927528028128228328428531 unmodified lines3173183193143203213223233245 unmodified lines3303313323273333293343353363373313383393403413343423433443363453463473483393493413503513523533543555 unmodified lines36136236335436436536635636736836937035937136137237337437536437637737837938038138237138338438537338638738838937639037839139239339439539621 unmodified lines418419420407421422423424425{ "cells": [ { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Alfred the Mail Sorting Butler: A LangGraph Example\n", "\n",15 unmodified lines ] }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Install the required packages\n", "%pip install -q langgraph langchain_openai langchain_huggingface" ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Setting Up Our Environment\n", "\n",1 unmodified line ] }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from typing import TypedDict, List, Dict, Any, Optional\n",9 unmodified lines ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Define Our State\n", "\n",7 unmodified lines ] }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class EmailState(TypedDict):\n", " email: Dict[str, Any]\n",5 unmodified lines ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "source": "## Step 2: Define Our Nodes" "## Step 2: Define Our Nodes" ] }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def read_email(state: EmailState):\n", " email = state[\"email\"]\n",105 unmodified lines ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "source": "## Step 3: Define Our Routing Logic" "## Step 3: Define Our Routing Logic" ] }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Add edges\n", "email_graph.add_edge(START, \"read_email\") # After starting we go to the \"read_email\" node\n",17 unmodified lines ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "source": "## Step 4: Create the StateGraph and Define Edges" "## Step 4: Create the StateGraph and Define Edges" ] }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Compile the graph\n", "compiled_graph = email_graph.compile()" ] }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import Image, display\n", "\n",1 unmodified line ] }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}, "outputs": [], "source": [ " # Example emails for testing\n", "legitimate_email = {\n",31 unmodified lines ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Step 5: Inspecting Our Mail Sorting Agent with Langfuse 📡\n", "\n",5 unmodified lines ] }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}, "outputs": [], "source": [ "source": "%pip install -q langfuse" "%pip install -q langfuse" ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "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)." "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, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n",5 unmodified lines ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "source": "Now, we configure the [Langfuse `callback_handler`](https://langfuse.com/docs/integrations/langchain/tracing#add-langfuse-to-your-langchain-application)." "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, "metadata": {}, "outputs": [], "source": [ "from langfuse.callback import CallbackHandler\n", "from langfuse.langchain import CallbackHandler\n", "\n", "# Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)\n", "langfuse_handler = CallbackHandler()" ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "source": [ "source": "We then add `config={\"callbacks\": [langfuse_handler]}` to the invocation of the agents and run them again." "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, "metadata": {}, "outputs": [], "source": [ "# Process legitimate email\n", "print(\"\\nProcessing legitimate email...\")\n",21 unmodified lines ] }, { "metadata": {}, "cell_type": "markdown", "metadata": {}, "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",unit2/langgraph/mail_sorting.ipynb+57-43