mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-20 06:35:46 +02:00
105 KiB
105 KiB
In [1]:
from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langgraph.prebuilt import chat_agent_executorIn [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()In [3]:
app = chat_agent_executor.create_tool_calling_executor(model, tools)In [4]:
app.get_graph().print_ascii() +-----------+
| __start__ |
+-----------+
*
*
*
+-------+
| agent |
+-------+
* ..
** ..
* .
+--------+ +---------+
| action | | __end__ |
+--------+ +---------+
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;
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))In [7]:
#%%capture --no-stderr
%pip install pygraphvizRequirement 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())In [9]:
# %%capture --no-stderr
%pip install pyppeteer
# %%capture --no-stderr
%pip install nest_asyncioRequirement 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) [2K [38;2;114;156;31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m82.9/82.9 kB[0m [31m2.1 MB/s[0m eta [36m0:00:00[0m [?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) [2K [38;2;114;156;31m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m97.9/97.9 kB[0m [31m6.2 MB/s[0m eta [36m0:00:00[0m [?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 [31mERROR: 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.[0m[31m [0mSuccessfully 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
In [11]:
display_image(app.get_graph().draw_mermaid_png(
draw_method=MermaidDrawMethod.API,
))