Skip to content

Commit df5d982

Browse files
committed
Refactor imports and add graph visualization section
Cleaned up unused imports and reorganized import statements for clarity. Added a new section for graph visualization, including error handling and user guidance for rendering issues. Updated code cell execution counts and improved markdown headings for better notebook structure.
1 parent 7f901b9 commit df5d982

File tree

1 file changed

+62
-31
lines changed

1 file changed

+62
-31
lines changed

ch8/yelp-navigator/langgraph_multiagent_supervisor.ipynb

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
},
6363
{
6464
"cell_type": "code",
65-
"execution_count": 16,
65+
"execution_count": 1,
6666
"id": "d530f52a",
6767
"metadata": {},
6868
"outputs": [
@@ -81,22 +81,18 @@
8181
"# =============================================================================\n",
8282
"\n",
8383
"import os\n",
84-
"import json\n",
8584
"import requests\n",
86-
"from typing import Annotated, TypedDict, List, Dict, Any\n",
87-
"from operator import add\n",
85+
"from typing import Dict, Any\n",
8886
"\n",
8987
"# LangGraph for building agent workflows\n",
9088
"from langgraph.graph import StateGraph, START, END, MessagesState\n",
91-
"from langchain_core.messages import BaseMessage\n",
9289
"from langchain_openai import ChatOpenAI\n",
93-
"from langchain_core.tools import tool\n",
94-
"from langchain.agents import create_agent\n",
90+
"\n",
9591
"from dotenv import load_dotenv\n",
9692
"from pathlib import Path\n",
97-
"from langchain_ollama import ChatOllama\n",
98-
"\n",
9993
"\n",
94+
"# Optional if you don't want to use OpenAI\n",
95+
"from langchain_ollama import ChatOllama\n",
10096
"\n",
10197
"# Load .env from the root of ch8 directory\n",
10298
"root_dir = Path(__file__).parent.parent if \"__file__\" in globals() else Path.cwd().parent\n",
@@ -133,7 +129,7 @@
133129
},
134130
{
135131
"cell_type": "code",
136-
"execution_count": 15,
132+
"execution_count": 2,
137133
"id": "1e69191a",
138134
"metadata": {},
139135
"outputs": [
@@ -248,7 +244,7 @@
248244
"id": "e98ab8cc",
249245
"metadata": {},
250246
"source": [
251-
"## Step 3: Initialize Language Model"
247+
"## Initialize Language Model"
252248
]
253249
},
254250
{
@@ -292,7 +288,7 @@
292288
},
293289
{
294290
"cell_type": "code",
295-
"execution_count": null,
291+
"execution_count": 5,
296292
"id": "f39b8912",
297293
"metadata": {},
298294
"outputs": [],
@@ -325,7 +321,7 @@
325321
},
326322
{
327323
"cell_type": "code",
328-
"execution_count": null,
324+
"execution_count": 6,
329325
"id": "eecfdc78",
330326
"metadata": {},
331327
"outputs": [],
@@ -366,7 +362,7 @@
366362
},
367363
{
368364
"cell_type": "code",
369-
"execution_count": 14,
365+
"execution_count": 7,
370366
"id": "5b559abb",
371367
"metadata": {},
372368
"outputs": [],
@@ -410,8 +406,24 @@
410406
" return END\n",
411407
" \n",
412408
" # Supervisor wants to rerun a node\n",
413-
" return next_agent\n",
414-
"\n",
409+
" return next_agent\n"
410+
]
411+
},
412+
{
413+
"cell_type": "markdown",
414+
"id": "729127c9",
415+
"metadata": {},
416+
"source": [
417+
"## Build graph"
418+
]
419+
},
420+
{
421+
"cell_type": "code",
422+
"execution_count": 8,
423+
"id": "84da69b9",
424+
"metadata": {},
425+
"outputs": [],
426+
"source": [
415427
"def build_workflow_graph() -> StateGraph[AgentState]:\n",
416428
" \"\"\"Builds a true pipeline architecture for Yelp Navigator.\n",
417429
" \n",
@@ -483,31 +495,50 @@
483495
"graph = build_workflow_graph().compile()"
484496
]
485497
},
498+
{
499+
"cell_type": "markdown",
500+
"id": "88b77d8e",
501+
"metadata": {},
502+
"source": [
503+
"## Graph Visualization\n",
504+
"\n",
505+
"Shows nodes, edges, conditional routing points, and feedback loops (clarification loop, supervisor revisions)."
506+
]
507+
},
486508
{
487509
"cell_type": "code",
488-
"execution_count": 9,
510+
"execution_count": 11,
489511
"id": "2dfa7acb",
490512
"metadata": {},
491-
"outputs": [],
513+
"outputs": [
514+
{
515+
"name": "stdout",
516+
"output_type": "stream",
517+
"text": [
518+
"Could not display graph: Failed to reach https://mermaid.ink API while trying to render your graph. Status code: 400.\n",
519+
"\n",
520+
"To resolve this issue:\n",
521+
"1. Check your internet connection and try again\n",
522+
"2. Try with higher retry settings: `draw_mermaid_png(..., max_retries=5, retry_delay=2.0)`\n",
523+
"3. Use the Pyppeteer rendering method which will render your graph locally in a browser: `draw_mermaid_png(..., draw_method=MermaidDrawMethod.PYPPETEER)`\n",
524+
"\n",
525+
"Alternatively, you can view the saved image at ./images/langgraph-supervisor.png\n"
526+
]
527+
}
528+
],
492529
"source": [
493530
"# Visualize the workflow graph\n",
494531
"try:\n",
495532
" from IPython.display import Image, display\n",
496-
" #display(Image(graph.get_graph().draw_mermaid_png()))\n",
533+
" display(Image(graph.get_graph().draw_mermaid_png()))\n",
534+
"except ImportError:\n",
535+
" # Fallback: print text representation\n",
536+
" print(\"Graph visualization requires additional dependencies.\")\n",
537+
" print(\"\\nGraph structure:\")\n",
538+
" print(graph.get_graph())\n",
497539
"except Exception as e:\n",
498540
" print(f\"Could not display graph: {e}\")\n",
499-
" print(\"\\nGraph structure:\")\n",
500-
" print(graph.get_graph())"
501-
]
502-
},
503-
{
504-
"cell_type": "markdown",
505-
"id": "88b77d8e",
506-
"metadata": {},
507-
"source": [
508-
"## Graph Visualization\n",
509-
"\n",
510-
"Shows nodes, edges, conditional routing points, and feedback loops (clarification loop, supervisor revisions)."
541+
" print(\"\\nAlternatively, you can view the saved image at ./images/langgraph-supervisor.png\")"
511542
]
512543
},
513544
{

0 commit comments

Comments
 (0)