Files
langgraph/examples/visualization.ipynb
T

245 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]:
!pip install langchain-openai
In [2]:
from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langgraph.prebuilt import chat_agent_executor
In [ ]:
# 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 [ ]:
app=chat_agent_executor.create_function_calling_executor(model, tools)

Ascii

We can easily visualize this graph in ascii

In [ ]:
app.get_graph().print_ascii()

Mermaid

We can also convert a graph class into Mermaid syntax.

In [ ]:
print(app.get_graph().draw_mermaid())

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 [ ]:
from IPython.display import Image

Using Graphviz

In [8]:
# !pip install pygraphviz
In [ ]:
Image(app.get_graph().draw_png())

Using Mermaid + Pyppeteer

In [10]:
# !pip install pyppeteer
In [ ]:
from langchain_core.runnables.graph import CurveStyle, NodeColors, MermaidDrawMethod

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
))

Using Mermaid.Ink

In [12]:
Image(app.get_graph().draw_mermaid_png(
    draw_method=MermaidDrawMethod.API,
))

Excluding condition nodes

By default, condition nods like 'should_continue' will be added. In case you want to exclude these, you can use add_condition_nodes parameter

In [ ]:
Image(app.get_graph(add_condition_nodes=False).draw_mermaid_png(
    draw_method=MermaidDrawMethod.API,
))
In [ ]: