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>
1234567815 unmodified lines2425262728293031323334353637383940411 unmodified line434445464748495051529 unmodified lines62636465666768697 unmodified lines777879808182838485865 unmodified lines9293949596979899100101102103104105106105 unmodified lines21221321421521621721821922022122222322422522617 unmodified lines2442452462472482492502512522532542552562572582592602612622632642652662672681 unmodified line27027127227327427527627727827931 unmodified lines3113123133143153163173185 unmodified lines3243253263273283293303313323333343353363373383393403413423433443455 unmodified lines35135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138221 unmodified lines404405406407408409410411{"cells": [{"metadata": {},"cell_type": "markdown","source": ["# Alfred the Mail Sorting Butler: A LangGraph Example\n","\n",15 unmodified lines]},{"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",1 unmodified line]},{"metadata": {},"cell_type": "code","outputs": [],"execution_count": null,"source": ["import os\n","from typing import TypedDict, List, Dict, Any, Optional\n",9 unmodified lines]},{"metadata": {},"cell_type": "markdown","source": ["## Step 1: Define Our State\n","\n",7 unmodified lines]},{"metadata": {},"cell_type": "code","outputs": [],"execution_count": null,"source": ["class EmailState(TypedDict):\n"," email: Dict[str, Any]\n",5 unmodified lines]},{"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",105 unmodified lines]},{"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",17 unmodified lines]},{"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",1 unmodified line]},{"metadata": {},"cell_type": "code","outputs": [],"execution_count": null,"source": [" # Example emails for testing\n","legitimate_email = {\n",31 unmodified lines]},{"metadata": {},"cell_type": "markdown","source": ["## Step 5: Inspecting Our Mail Sorting Agent with Langfuse 📡\n","\n",5 unmodified lines]},{"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",5 unmodified lines]},{"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",21 unmodified lines]},{"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",1234567815 unmodified lines2425262728293031323334353637383940411 unmodified line434445464748495051529 unmodified lines62636465666768697 unmodified lines777879808182838485865 unmodified lines9293949596979899100101102103104105106107108105 unmodified lines21421521621721821922022122222322422522622722822923017 unmodified lines2482492502512522532542552562572582592602612622632642652662672682692702712722732741 unmodified line27627727827928028128228328428531 unmodified lines3173183193203213223233245 unmodified lines3303313323333343353363373383393403413423433443453463473483493503513523533543555 unmodified lines36136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539621 unmodified lines418419420421422423424425{"cells": [{"cell_type": "markdown","metadata": {},"source": ["# Alfred the Mail Sorting Butler: A LangGraph Example\n","\n",15 unmodified lines]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["# Install the required packages\n","%pip install -q langgraph langchain_openai langchain_huggingface"]},{"cell_type": "markdown","metadata": {},"source": ["## Setting Up Our Environment\n","\n",1 unmodified line]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["import os\n","from typing import TypedDict, List, Dict, Any, Optional\n",9 unmodified lines]},{"cell_type": "markdown","metadata": {},"source": ["## Step 1: Define Our State\n","\n",7 unmodified lines]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["class EmailState(TypedDict):\n"," email: Dict[str, Any]\n",5 unmodified lines]},{"cell_type": "markdown","metadata": {},"source": ["## Step 2: Define Our Nodes"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["def read_email(state: EmailState):\n"," email = state[\"email\"]\n",105 unmodified lines]},{"cell_type": "markdown","metadata": {},"source": ["## Step 3: Define Our Routing Logic"]},{"cell_type": "code","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]},{"cell_type": "markdown","metadata": {},"source": ["## Step 4: Create the StateGraph and Define Edges"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["# Compile the graph\n","compiled_graph = email_graph.compile()"]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["from IPython.display import Image, display\n","\n",1 unmodified line]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": [" # Example emails for testing\n","legitimate_email = {\n",31 unmodified lines]},{"cell_type": "markdown","metadata": {},"source": ["## Step 5: Inspecting Our Mail Sorting Agent with Langfuse 📡\n","\n",5 unmodified lines]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["%pip install -q langfuse"]},{"cell_type": "markdown","metadata": {},"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)."]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["import os\n","\n",5 unmodified lines]},{"cell_type": "markdown","metadata": {},"source": ["Now, we configure the [Langfuse `callback_handler`](https://langfuse.com/docs/integrations/langchain/tracing#add-langfuse-to-your-langchain-application)."]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["from langfuse.langchain import CallbackHandler\n","\n","# Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)\n","langfuse_handler = CallbackHandler()"]},{"cell_type": "markdown","metadata": {},"source": ["We then add `config={\"callbacks\": [langfuse_handler]}` to the invocation of the agents and run them again."]},{"cell_type": "code","execution_count": null,"metadata": {},"outputs": [],"source": ["# Process legitimate email\n","print(\"\\nProcessing legitimate email...\")\n",21 unmodified lines]},{"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