mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-17 21:25:46 +02:00
270 KiB
270 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_function_calling_executor(model, tools)In [4]:
app.get_graph().print_ascii() +-----------+
| __start__ |
+-----------+
*
*
*
+-------+
| agent |
+-------+*
*** ***
* *
** ***
+-----------------+ *
| should_continue | *
+-----------------+. *
. ..... *
. ... *
. ... *
+---------+ +--------+
| __end__ | | action |
+---------+ +--------+
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;
should_continue([should_continue]):::otherclass;
__start__ --> agent;
action --> agent;
agent --> should_continue;
should_continue -. continue .-> action;
should_continue -. 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 install pygraphvizIn [8]:
display_image(app.get_graph().draw_png())In [9]:
# %%capture --no-stderr
%pip install install pyppeteer
# %%capture --no-stderr
%pip install install nest_asyncioIn [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
))In [11]:
display_image(app.get_graph().draw_mermaid_png(
draw_method=MermaidDrawMethod.API,
))In [21]:
display_image(app.get_graph(add_condition_nodes=False).draw_mermaid_png(
draw_method=MermaidDrawMethod.PYPPETEER,
))