Files
langgraph/examples/visualization.ipynb
T

105 KiB

Visualization

This notebook walks through how to visualize the graphs you create. For this example we will use a prebuilt graph, but this works with ANY graphs.

Set up the chat model and tools

Here we will define the chat model and tools that we want to use. Importantly, this model MUST support OpenAI function calling.

In [1]:
from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langgraph.prebuilt import chat_agent_executor
In [2]:
# Optional to not need .env
# import os
# os.environ['TAVILY_API_KEY'] = 'foo'
# os.environ['OPENAI_API_KEY'] = 'foo'

tools = [TavilySearchResults(max_results=1)]
model = ChatOpenAI()

Create executor

We can now use the high level interface to create the executor

In [3]:
app = chat_agent_executor.create_tool_calling_executor(model, tools)

Ascii

We can easily visualize this graph in ascii

In [4]:
app.get_graph().print_ascii()
        +-----------+           
        | __start__ |           
        +-----------+           
               *                
               *                
               *                
          +-------+             
          | agent |             
          +-------+             
          *        ..           
        **           ..         
       *               .        
+--------+         +---------+  
| action |         | __end__ |  
+--------+         +---------+  

Mermaid

We can also convert a graph class into Mermaid syntax.

In [5]:
print(app.get_graph().draw_mermaid())
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
	__start__[__start__]:::startclass;
	__end__[__end__]:::endclass;
	agent([agent]):::otherclass;
	action([action]):::otherclass;
	__start__ --> agent;
	action --> agent;
	agent -. continue .-> action;
	agent -. end .-> __end__;
	classDef startclass fill:#ffdfba;
	classDef endclass fill:#baffc9;
	classDef otherclass fill:#fad7de;

PNG

If prefered, we could render the Graph into a .png. Here we could use three options:

  • Using graphviz (which requires pip install graphviz)
  • Using Mermaid + Pyppeteer (requires pip install pyppeteer)
  • Using Mermaid.ink API (does not require additional packages)
In [6]:
from IPython.display import display, HTML
import base64

def display_image(image_bytes: bytes, width=300):
    decoded_img_bytes = base64.b64encode(image_bytes).decode('utf-8')
    html = f'<img src="data:image/png;base64,{decoded_img_bytes}" style="width: {width}px;" />'
    display(HTML(html))

Using Graphviz

In [7]:
#%%capture --no-stderr
%pip install pygraphviz
Requirement already satisfied: install in /Users/wfh/code/lc/langgraph/.venv/lib/python3.11/site-packages (1.3.5)
Collecting pygraphviz
  Using cached pygraphviz-1.12-cp311-cp311-macosx_13_0_arm64.whl
Installing collected packages: pygraphviz
Successfully installed pygraphviz-1.12
Note: you may need to restart the kernel to use updated packages.
In [8]:
display_image(app.get_graph().draw_png())

Using Mermaid + Pyppeteer

In [9]:
# %%capture --no-stderr
%pip install pyppeteer
# %%capture --no-stderr
%pip install nest_asyncio
Requirement already satisfied: install in /Users/wfh/code/lc/langgraph/.venv/lib/python3.11/site-packages (1.3.5)
Collecting pyppeteer
  Downloading pyppeteer-2.0.0-py3-none-any.whl.metadata (7.1 kB)
Collecting appdirs<2.0.0,>=1.4.3 (from pyppeteer)
  Downloading appdirs-1.4.4-py2.py3-none-any.whl.metadata (9.0 kB)
Requirement already satisfied: certifi>=2023 in /Users/wfh/code/lc/langgraph/.venv/lib/python3.11/site-packages (from pyppeteer) (2024.2.2)
Requirement already satisfied: importlib-metadata>=1.4 in /Users/wfh/code/lc/langgraph/.venv/lib/python3.11/site-packages (from pyppeteer) (6.11.0)
Collecting pyee<12.0.0,>=11.0.0 (from pyppeteer)
  Downloading pyee-11.1.0-py3-none-any.whl.metadata (2.8 kB)
Requirement already satisfied: tqdm<5.0.0,>=4.42.1 in /Users/wfh/code/lc/langgraph/.venv/lib/python3.11/site-packages (from pyppeteer) (4.66.2)
Collecting urllib3<2.0.0,>=1.25.8 (from pyppeteer)
  Using cached urllib3-1.26.18-py2.py3-none-any.whl.metadata (48 kB)
Collecting websockets<11.0,>=10.0 (from pyppeteer)
  Downloading websockets-10.4-cp311-cp311-macosx_11_0_arm64.whl.metadata (6.4 kB)
Requirement already satisfied: zipp>=0.5 in /Users/wfh/code/lc/langgraph/.venv/lib/python3.11/site-packages (from importlib-metadata>=1.4->pyppeteer) (3.17.0)
Requirement already satisfied: typing-extensions in /Users/wfh/code/lc/langgraph/.venv/lib/python3.11/site-packages (from pyee<12.0.0,>=11.0.0->pyppeteer) (4.10.0)
Downloading pyppeteer-2.0.0-py3-none-any.whl (82 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 82.9/82.9 kB 2.1 MB/s eta 0:00:00
[?25hDownloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Downloading pyee-11.1.0-py3-none-any.whl (15 kB)
Using cached urllib3-1.26.18-py2.py3-none-any.whl (143 kB)
Downloading websockets-10.4-cp311-cp311-macosx_11_0_arm64.whl (97 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 kB 6.2 MB/s eta 0:00:00
[?25hInstalling collected packages: appdirs, websockets, urllib3, pyee, pyppeteer
  Attempting uninstall: websockets
    Found existing installation: websockets 12.0
    Uninstalling websockets-12.0:
      Successfully uninstalled websockets-12.0
  Attempting uninstall: urllib3
    Found existing installation: urllib3 2.2.1
    Uninstalling urllib3-2.2.1:
      Successfully uninstalled urllib3-2.2.1
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
types-requests 2.31.0.20240311 requires urllib3>=2, but you have urllib3 1.26.18 which is incompatible.
Successfully installed appdirs-1.4.4 pyee-11.1.0 pyppeteer-2.0.0 urllib3-1.26.18 websockets-10.4
Note: you may need to restart the kernel to use updated packages.
Requirement already satisfied: install in /Users/wfh/code/lc/langgraph/.venv/lib/python3.11/site-packages (1.3.5)
Requirement already satisfied: nest_asyncio in /Users/wfh/code/lc/langgraph/.venv/lib/python3.11/site-packages (1.6.0)
Note: you may need to restart the kernel to use updated packages.
In [10]:
import nest_asyncio
from langchain_core.runnables.graph import CurveStyle, NodeColors, MermaidDrawMethod

nest_asyncio.apply() # Required for Jupyter Notebook to run async functions

display_image(app.get_graph().draw_mermaid_png(
    curve_style=CurveStyle.LINEAR,
    node_colors=NodeColors(start="#ffdfba", end="#baffc9", other="#fad7de"),
    wrap_label_n_words=9,
    output_file_path=None,
    draw_method=MermaidDrawMethod.PYPPETEER,
    background_color="white",
    padding=10
))
[INFO] Starting Chromium download.
100%|██████████| 141M/141M [00:09<00:00, 14.2Mb/s] 
[INFO] Beginning extraction
[INFO] Chromium extracted to: /Users/wfh/Library/Application Support/pyppeteer/local-chromium/1181205

Using Mermaid.Ink

In [11]:
display_image(app.get_graph().draw_mermaid_png(
    draw_method=MermaidDrawMethod.API,
))