|
62 | 62 | }, |
63 | 63 | { |
64 | 64 | "cell_type": "code", |
65 | | - "execution_count": 16, |
| 65 | + "execution_count": 1, |
66 | 66 | "id": "d530f52a", |
67 | 67 | "metadata": {}, |
68 | 68 | "outputs": [ |
|
81 | 81 | "# =============================================================================\n", |
82 | 82 | "\n", |
83 | 83 | "import os\n", |
84 | | - "import json\n", |
85 | 84 | "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", |
88 | 86 | "\n", |
89 | 87 | "# LangGraph for building agent workflows\n", |
90 | 88 | "from langgraph.graph import StateGraph, START, END, MessagesState\n", |
91 | | - "from langchain_core.messages import BaseMessage\n", |
92 | 89 | "from langchain_openai import ChatOpenAI\n", |
93 | | - "from langchain_core.tools import tool\n", |
94 | | - "from langchain.agents import create_agent\n", |
| 90 | + "\n", |
95 | 91 | "from dotenv import load_dotenv\n", |
96 | 92 | "from pathlib import Path\n", |
97 | | - "from langchain_ollama import ChatOllama\n", |
98 | | - "\n", |
99 | 93 | "\n", |
| 94 | + "# Optional if you don't want to use OpenAI\n", |
| 95 | + "from langchain_ollama import ChatOllama\n", |
100 | 96 | "\n", |
101 | 97 | "# Load .env from the root of ch8 directory\n", |
102 | 98 | "root_dir = Path(__file__).parent.parent if \"__file__\" in globals() else Path.cwd().parent\n", |
|
133 | 129 | }, |
134 | 130 | { |
135 | 131 | "cell_type": "code", |
136 | | - "execution_count": 15, |
| 132 | + "execution_count": 2, |
137 | 133 | "id": "1e69191a", |
138 | 134 | "metadata": {}, |
139 | 135 | "outputs": [ |
|
248 | 244 | "id": "e98ab8cc", |
249 | 245 | "metadata": {}, |
250 | 246 | "source": [ |
251 | | - "## Step 3: Initialize Language Model" |
| 247 | + "## Initialize Language Model" |
252 | 248 | ] |
253 | 249 | }, |
254 | 250 | { |
|
292 | 288 | }, |
293 | 289 | { |
294 | 290 | "cell_type": "code", |
295 | | - "execution_count": null, |
| 291 | + "execution_count": 5, |
296 | 292 | "id": "f39b8912", |
297 | 293 | "metadata": {}, |
298 | 294 | "outputs": [], |
|
325 | 321 | }, |
326 | 322 | { |
327 | 323 | "cell_type": "code", |
328 | | - "execution_count": null, |
| 324 | + "execution_count": 6, |
329 | 325 | "id": "eecfdc78", |
330 | 326 | "metadata": {}, |
331 | 327 | "outputs": [], |
|
366 | 362 | }, |
367 | 363 | { |
368 | 364 | "cell_type": "code", |
369 | | - "execution_count": 14, |
| 365 | + "execution_count": 7, |
370 | 366 | "id": "5b559abb", |
371 | 367 | "metadata": {}, |
372 | 368 | "outputs": [], |
|
410 | 406 | " return END\n", |
411 | 407 | " \n", |
412 | 408 | " # 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": [ |
415 | 427 | "def build_workflow_graph() -> StateGraph[AgentState]:\n", |
416 | 428 | " \"\"\"Builds a true pipeline architecture for Yelp Navigator.\n", |
417 | 429 | " \n", |
|
483 | 495 | "graph = build_workflow_graph().compile()" |
484 | 496 | ] |
485 | 497 | }, |
| 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 | + }, |
486 | 508 | { |
487 | 509 | "cell_type": "code", |
488 | | - "execution_count": 9, |
| 510 | + "execution_count": 11, |
489 | 511 | "id": "2dfa7acb", |
490 | 512 | "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 | + ], |
492 | 529 | "source": [ |
493 | 530 | "# Visualize the workflow graph\n", |
494 | 531 | "try:\n", |
495 | 532 | " 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", |
497 | 539 | "except Exception as e:\n", |
498 | 540 | " 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\")" |
511 | 542 | ] |
512 | 543 | }, |
513 | 544 | { |
|
0 commit comments