{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a3e3ebc4-57af-4fe4-bdd3-36aff67bf276",
   "metadata": {},
   "source": [
    "## Hierarchical Agent Teams\n",
    "\n",
    "In our previous example ([Agent Supervisor](./agent_supervisor.ipynb)), we introduced the concept of a single supervisor node to route work between different worker nodes.\n",
    "\n",
    "But what if the job for a single worker becomes too complex? What if the number of workers becomes too large?\n",
    "\n",
    "For some applications, the system may be more effective if work is distributed _hierarchically_.\n",
    "\n",
    "You can do this by composing different subgraphs and creating a top-level supervisor, along with mid-level supervisors.\n",
    "\n",
    "To do this, let's build a simple research assistant! The graph will look something like the following:\n",
    "\n",
    "![diagram](./img/hierarchical-diagram.png)\n",
    "\n",
    "This notebook is inspired by the paper [AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation](https://arxiv.org/abs/2308.08155), by Wu, et. al. In the rest of this notebook, you will:\n",
    "\n",
    "1. Define the agents' tools to access the web and write files\n",
    "2. Define some utilities to help create the graph and agents\n",
    "3. Create and define each team (web research + doc writing)\n",
    "4. Compose everything together.\n",
    "\n",
    "But before all of that, some setup:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0d30b6f7-3bec-4d9f-af50-43dfdc81ae6c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:42.364369Z",
     "start_time": "2024-05-15T08:19:42.359273Z"
    }
   },
   "outputs": [],
   "source": ["# %%capture --no-stderr\n# %pip install -U langgraph langchain langchain_openai langchain_experimental"]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "30c2f3de-c730-4aec-85a6-af2c2f058803",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:42.395571Z",
     "start_time": "2024-05-15T08:19:42.365662Z"
    }
   },
   "outputs": [],
   "source": ["import getpass\nimport os\n\n\ndef _set_if_undefined(var: str):\n    if not os.environ.get(var):\n        os.environ[var] = getpass.getpass(f\"Please provide your {var}\")\n\n\n_set_if_undefined(\"OPENAI_API_KEY\")\n_set_if_undefined(\"LANGCHAIN_API_KEY\")\n_set_if_undefined(\"TAVILY_API_KEY\")\n\n# Optional, add tracing in LangSmith.\n# This will help you visualize and debug the control flow\nos.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\nos.environ[\"LANGCHAIN_PROJECT\"] = \"Multi-agent Collaboration\""]
  },
  {
   "cell_type": "markdown",
   "id": "354568e2-aef0-4af9-8a79-e64d3eea752f",
   "metadata": {},
   "source": [
    "## Create Tools\n",
    "\n",
    "Each team will be composed of one or more agents each with one or more tools. Below, define all the tools to be used by your different teams.\n",
    "\n",
    "We'll start with the research team.\n",
    "\n",
    "**ResearchTeam tools**\n",
    "\n",
    "The research team can use a search engine and url scraper to find information on the web. Feel free to add additional functionality below to boost the team performance!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4024eb89-843d-4cc3-ab3f-e1eb4d031179",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:44.477064Z",
     "start_time": "2024-05-15T08:19:42.397083Z"
    }
   },
   "outputs": [],
   "source": ["from typing import Annotated, List\n\nfrom langchain_community.document_loaders import WebBaseLoader\nfrom langchain_community.tools.tavily_search import TavilySearchResults\nfrom langchain_core.tools import tool\n\ntavily_tool = TavilySearchResults(max_results=5)\n\n\n@tool\ndef scrape_webpages(urls: List[str]) -> str:\n    \"\"\"Use requests and bs4 to scrape the provided web pages for detailed information.\"\"\"\n    loader = WebBaseLoader(urls)\n    docs = loader.load()\n    return \"\\n\\n\".join(\n        [\n            f'<Document name=\"{doc.metadata.get(\"title\", \"\")}\">\\n{doc.page_content}\\n</Document>'\n            for doc in docs\n        ]\n    )"]
  },
  {
   "cell_type": "markdown",
   "id": "1c427982-fadf-4721-a77e-2465df9fc6bc",
   "metadata": {},
   "source": [
    "**Document writing team tools**\n",
    "\n",
    "Next up, we will give some tools for the doc writing team to use.\n",
    "We define some bare-bones file-access tools below.\n",
    "\n",
    "Note that this gives the agents access to your file-system, which can be unsafe. We also haven't optimized the tool descriptions for performance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f20a18ca-2709-4c12-84f3-88678591a9fa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:44.538421Z",
     "start_time": "2024-05-15T08:19:44.479132Z"
    }
   },
   "outputs": [],
   "source": ["from pathlib import Path\nfrom tempfile import TemporaryDirectory\nfrom typing import Dict, Optional\n\nfrom langchain_experimental.utilities import PythonREPL\nfrom typing_extensions import TypedDict\n\n_TEMP_DIRECTORY = TemporaryDirectory()\nWORKING_DIRECTORY = Path(_TEMP_DIRECTORY.name)\n\n\n@tool\ndef create_outline(\n    points: Annotated[List[str], \"List of main points or sections.\"],\n    file_name: Annotated[str, \"File path to save the outline.\"],\n) -> Annotated[str, \"Path of the saved outline file.\"]:\n    \"\"\"Create and save an outline.\"\"\"\n    with (WORKING_DIRECTORY / file_name).open(\"w\") as file:\n        for i, point in enumerate(points):\n            file.write(f\"{i + 1}. {point}\\n\")\n    return f\"Outline saved to {file_name}\"\n\n\n@tool\ndef read_document(\n    file_name: Annotated[str, \"File path to save the document.\"],\n    start: Annotated[Optional[int], \"The start line. Default is 0\"] = None,\n    end: Annotated[Optional[int], \"The end line. Default is None\"] = None,\n) -> str:\n    \"\"\"Read the specified document.\"\"\"\n    with (WORKING_DIRECTORY / file_name).open(\"r\") as file:\n        lines = file.readlines()\n    if start is not None:\n        start = 0\n    return \"\\n\".join(lines[start:end])\n\n\n@tool\ndef write_document(\n    content: Annotated[str, \"Text content to be written into the document.\"],\n    file_name: Annotated[str, \"File path to save the document.\"],\n) -> Annotated[str, \"Path of the saved document file.\"]:\n    \"\"\"Create and save a text document.\"\"\"\n    with (WORKING_DIRECTORY / file_name).open(\"w\") as file:\n        file.write(content)\n    return f\"Document saved to {file_name}\"\n\n\n@tool\ndef edit_document(\n    file_name: Annotated[str, \"Path of the document to be edited.\"],\n    inserts: Annotated[\n        Dict[int, str],\n        \"Dictionary where key is the line number (1-indexed) and value is the text to be inserted at that line.\",\n    ],\n) -> Annotated[str, \"Path of the edited document file.\"]:\n    \"\"\"Edit a document by inserting text at specific line numbers.\"\"\"\n\n    with (WORKING_DIRECTORY / file_name).open(\"r\") as file:\n        lines = file.readlines()\n\n    sorted_inserts = sorted(inserts.items())\n\n    for line_number, text in sorted_inserts:\n        if 1 <= line_number <= len(lines) + 1:\n            lines.insert(line_number - 1, text + \"\\n\")\n        else:\n            return f\"Error: Line number {line_number} is out of range.\"\n\n    with (WORKING_DIRECTORY / file_name).open(\"w\") as file:\n        file.writelines(lines)\n\n    return f\"Document edited and saved to {file_name}\"\n\n\n# Warning: This executes code locally, which can be unsafe when not sandboxed\n\nrepl = PythonREPL()\n\n\n@tool\ndef python_repl(\n    code: Annotated[str, \"The python code to execute to generate your chart.\"],\n):\n    \"\"\"Use this to execute python code. If you want to see the output of a value,\n    you should print it out with `print(...)`. This is visible to the user.\"\"\"\n    try:\n        result = repl.run(code)\n    except BaseException as e:\n        return f\"Failed to execute. Error: {repr(e)}\"\n    return f\"Successfully executed:\\n```python\\n{code}\\n```\\nStdout: {result}\""]
  },
  {
   "cell_type": "markdown",
   "id": "504ee1c6-2b6a-439d-9046-df54e1e15698",
   "metadata": {},
   "source": [
    "## Helper Utilities\n",
    "\n",
    "We are going to create a few utility functions to make it more concise when we want to:\n",
    "\n",
    "1. Create a worker agent.\n",
    "2. Create a supervisor for the sub-graph.\n",
    "\n",
    "These will simplify the graph compositional code at the end for us so it's easier to see what's going on."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e09fb60f-1aac-455b-b67d-8d2e4ccfd747",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:46.559082Z",
     "start_time": "2024-05-15T08:19:44.541330Z"
    }
   },
   "outputs": [],
   "source": ["from typing import List, Optional\n\nfrom langchain.agents import AgentExecutor, create_openai_functions_agent\nfrom langchain.output_parsers.openai_functions import JsonOutputFunctionsParser\nfrom langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\nfrom langchain_openai import ChatOpenAI\n\nfrom langgraph.graph import END, StateGraph, START\n\n\ndef create_agent(\n    llm: ChatOpenAI,\n    tools: list,\n    system_prompt: str,\n) -> str:\n    \"\"\"Create a function-calling agent and add it to the graph.\"\"\"\n    system_prompt += \"\\nWork autonomously according to your specialty, using the tools available to you.\"\n    \" Do not ask for clarification.\"\n    \" Your other team members (and other teams) will collaborate with you with their own specialties.\"\n    \" You are chosen for a reason! You are one of the following team members: {team_members}.\"\n    prompt = ChatPromptTemplate.from_messages(\n        [\n            (\n                \"system\",\n                system_prompt,\n            ),\n            MessagesPlaceholder(variable_name=\"messages\"),\n            MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n        ]\n    )\n    agent = create_openai_functions_agent(llm, tools, prompt)\n    executor = AgentExecutor(agent=agent, tools=tools)\n    return executor\n\n\ndef agent_node(state, agent, name):\n    result = agent.invoke(state)\n    return {\"messages\": [HumanMessage(content=result[\"output\"], name=name)]}\n\n\ndef create_team_supervisor(llm: ChatOpenAI, system_prompt, members) -> str:\n    \"\"\"An LLM-based router.\"\"\"\n    options = [\"FINISH\"] + members\n    function_def = {\n        \"name\": \"route\",\n        \"description\": \"Select the next role.\",\n        \"parameters\": {\n            \"title\": \"routeSchema\",\n            \"type\": \"object\",\n            \"properties\": {\n                \"next\": {\n                    \"title\": \"Next\",\n                    \"anyOf\": [\n                        {\"enum\": options},\n                    ],\n                },\n            },\n            \"required\": [\"next\"],\n        },\n    }\n    prompt = ChatPromptTemplate.from_messages(\n        [\n            (\"system\", system_prompt),\n            MessagesPlaceholder(variable_name=\"messages\"),\n            (\n                \"system\",\n                \"Given the conversation above, who should act next?\"\n                \" Or should we FINISH? Select one of: {options}\",\n            ),\n        ]\n    ).partial(options=str(options), team_members=\", \".join(members))\n    return (\n        prompt\n        | llm.bind_functions(functions=[function_def], function_call=\"route\")\n        | JsonOutputFunctionsParser()\n    )"]
  },
  {
   "cell_type": "markdown",
   "id": "00282b1f-bb4d-4ee7-9bae-e8e6f586f12e",
   "metadata": {},
   "source": [
    "## Define Agent Teams\n",
    "\n",
    "Now we can get to define our hierarchical teams. \"Choose your player!\"\n",
    "\n",
    "### Research Team\n",
    "\n",
    "The research team will have a search agent and a web scraping \"research_agent\" as the two worker nodes. Let's create those, as well as the team supervisor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "53db0c78-e357-48ba-ae5f-3fc04735a3b7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:48.810290Z",
     "start_time": "2024-05-15T08:19:46.561088Z"
    }
   },
   "outputs": [],
   "source": ["import functools\nimport operator\n\nfrom langchain_core.messages import BaseMessage, HumanMessage\nfrom langchain_openai.chat_models import ChatOpenAI\n\n\n# ResearchTeam graph state\nclass ResearchTeamState(TypedDict):\n    # A message is added after each team member finishes\n    messages: Annotated[List[BaseMessage], operator.add]\n    # The team members are tracked so they are aware of\n    # the others' skill-sets\n    team_members: List[str]\n    # Used to route work. The supervisor calls a function\n    # that will update this every time it makes a decision\n    next: str\n\n\nllm = ChatOpenAI(model=\"gpt-4-1106-preview\")\n\nsearch_agent = create_agent(\n    llm,\n    [tavily_tool],\n    \"You are a research assistant who can search for up-to-date info using the tavily search engine.\",\n)\nsearch_node = functools.partial(agent_node, agent=search_agent, name=\"Search\")\n\nresearch_agent = create_agent(\n    llm,\n    [scrape_webpages],\n    \"You are a research assistant who can scrape specified urls for more detailed information using the scrape_webpages function.\",\n)\nresearch_node = functools.partial(agent_node, agent=research_agent, name=\"WebScraper\")\n\nsupervisor_agent = create_team_supervisor(\n    llm,\n    \"You are a supervisor tasked with managing a conversation between the\"\n    \" following workers:  Search, WebScraper. Given the following user request,\"\n    \" respond with the worker to act next. Each worker will perform a\"\n    \" task and respond with their results and status. When finished,\"\n    \" respond with FINISH.\",\n    [\"Search\", \"WebScraper\"],\n)"]
  },
  {
   "cell_type": "markdown",
   "id": "b01c6ee8-a461-4081-8a97-a3a06ec0f994",
   "metadata": {},
   "source": [
    "Now that we've created the necessary components, defining their interactions is easy. Add the nodes to the team graph, and define the edges, which determine the transition criteria."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1a7a1260-d9f6-4011-b2b1-13fab5126997",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:48.825649Z",
     "start_time": "2024-05-15T08:19:48.811753Z"
    }
   },
   "outputs": [],
   "source": ["research_graph = StateGraph(ResearchTeamState)\nresearch_graph.add_node(\"Search\", search_node)\nresearch_graph.add_node(\"WebScraper\", research_node)\nresearch_graph.add_node(\"supervisor\", supervisor_agent)\n\n# Define the control flow\nresearch_graph.add_edge(\"Search\", \"supervisor\")\nresearch_graph.add_edge(\"WebScraper\", \"supervisor\")\nresearch_graph.add_conditional_edges(\n    \"supervisor\",\n    lambda x: x[\"next\"],\n    {\"Search\": \"Search\", \"WebScraper\": \"WebScraper\", \"FINISH\": END},\n)\n\n\nresearch_graph.add_edge(START, \"supervisor\")\nchain = research_graph.compile()\n\n\n# The following functions interoperate between the top level graph state\n# and the state of the research sub-graph\n# this makes it so that the states of each graph don't get intermixed\ndef enter_chain(message: str):\n    results = {\n        \"messages\": [HumanMessage(content=message)],\n    }\n    return results\n\n\nresearch_chain = enter_chain | chain"]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "110f59bed6134685",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:51.936523Z",
     "start_time": "2024-05-15T08:19:48.827798Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 30092 characters]",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": ["from IPython.display import Image, display\n\ndisplay(Image(chain.get_graph(xray=True).draw_mermaid_png()))"]
  },
  {
   "cell_type": "markdown",
   "id": "63ee8f2c-fbde-427b-ba54-ae0c7ce5fbfb",
   "metadata": {},
   "source": [
    "We can give this team work directly. Try it out below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "912b0604-a178-4246-a36f-2dedae606680",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:51.952470Z",
     "start_time": "2024-05-15T08:19:51.937879Z"
    }
   },
   "outputs": [],
   "source": ["for s in research_chain.stream(\n    \"when is Taylor Swift's next tour?\", {\"recursion_limit\": 100}\n):\n    if \"__end__\" not in s:\n        print(s)\n        print(\"---\")"]
  },
  {
   "cell_type": "markdown",
   "id": "749b99ab-f6f0-4c5d-a90b-10102465d186",
   "metadata": {},
   "source": [
    "### Document Writing Team\n",
    "\n",
    "Create the document writing team below using a similar approach. This time, we will give each agent access to different file-writing tools.\n",
    "\n",
    "Note that we are giving file-system access to our agent here, which is not safe in all cases."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1bcdbf44-9481-430c-8429-fa142ed8a626",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:53.677722Z",
     "start_time": "2024-05-15T08:19:51.953933Z"
    }
   },
   "outputs": [],
   "source": ["import operator\nfrom pathlib import Path\n\n\n# Document writing team graph state\nclass DocWritingState(TypedDict):\n    # This tracks the team's conversation internally\n    messages: Annotated[List[BaseMessage], operator.add]\n    # This provides each worker with context on the others' skill sets\n    team_members: str\n    # This is how the supervisor tells langgraph who to work next\n    next: str\n    # This tracks the shared directory state\n    current_files: str\n\n\n# This will be run before each worker agent begins work\n# It makes it so they are more aware of the current state\n# of the working directory.\ndef prelude(state):\n    written_files = []\n    if not WORKING_DIRECTORY.exists():\n        WORKING_DIRECTORY.mkdir()\n    try:\n        written_files = [\n            f.relative_to(WORKING_DIRECTORY) for f in WORKING_DIRECTORY.rglob(\"*\")\n        ]\n    except Exception:\n        pass\n    if not written_files:\n        return {**state, \"current_files\": \"No files written.\"}\n    return {\n        **state,\n        \"current_files\": \"\\nBelow are files your team has written to the directory:\\n\"\n        + \"\\n\".join([f\" - {f}\" for f in written_files]),\n    }\n\n\nllm = ChatOpenAI(model=\"gpt-4-1106-preview\")\n\ndoc_writer_agent = create_agent(\n    llm,\n    [write_document, edit_document, read_document],\n    \"You are an expert writing a research document.\\n\"\n    # The {current_files} value is populated automatically by the graph state\n    \"Below are files currently in your directory:\\n{current_files}\",\n)\n# Injects current directory working state before each call\ncontext_aware_doc_writer_agent = prelude | doc_writer_agent\ndoc_writing_node = functools.partial(\n    agent_node, agent=context_aware_doc_writer_agent, name=\"DocWriter\"\n)\n\nnote_taking_agent = create_agent(\n    llm,\n    [create_outline, read_document],\n    \"You are an expert senior researcher tasked with writing a paper outline and\"\n    \" taking notes to craft a perfect paper.{current_files}\",\n)\ncontext_aware_note_taking_agent = prelude | note_taking_agent\nnote_taking_node = functools.partial(\n    agent_node, agent=context_aware_note_taking_agent, name=\"NoteTaker\"\n)\n\nchart_generating_agent = create_agent(\n    llm,\n    [read_document, python_repl],\n    \"You are a data viz expert tasked with generating charts for a research project.\"\n    \"{current_files}\",\n)\ncontext_aware_chart_generating_agent = prelude | chart_generating_agent\nchart_generating_node = functools.partial(\n    agent_node, agent=context_aware_note_taking_agent, name=\"ChartGenerator\"\n)\n\ndoc_writing_supervisor = create_team_supervisor(\n    llm,\n    \"You are a supervisor tasked with managing a conversation between the\"\n    \" following workers:  {team_members}. Given the following user request,\"\n    \" respond with the worker to act next. Each worker will perform a\"\n    \" task and respond with their results and status. When finished,\"\n    \" respond with FINISH.\",\n    [\"DocWriter\", \"NoteTaker\", \"ChartGenerator\"],\n)"]
  },
  {
   "cell_type": "markdown",
   "id": "aee2cd9b-29aa-458e-903d-4e49179e5d59",
   "metadata": {},
   "source": [
    "With the objects themselves created, we can form the graph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9c5c644f-8966-4d2e-98d2-80d73520e9fe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:53.693123Z",
     "start_time": "2024-05-15T08:19:53.678906Z"
    }
   },
   "outputs": [],
   "source": ["# Create the graph here:\n# Note that we have unrolled the loop for the sake of this doc\nauthoring_graph = StateGraph(DocWritingState)\nauthoring_graph.add_node(\"DocWriter\", doc_writing_node)\nauthoring_graph.add_node(\"NoteTaker\", note_taking_node)\nauthoring_graph.add_node(\"ChartGenerator\", chart_generating_node)\nauthoring_graph.add_node(\"supervisor\", doc_writing_supervisor)\n\n# Add the edges that always occur\nauthoring_graph.add_edge(\"DocWriter\", \"supervisor\")\nauthoring_graph.add_edge(\"NoteTaker\", \"supervisor\")\nauthoring_graph.add_edge(\"ChartGenerator\", \"supervisor\")\n\n# Add the edges where routing applies\nauthoring_graph.add_conditional_edges(\n    \"supervisor\",\n    lambda x: x[\"next\"],\n    {\n        \"DocWriter\": \"DocWriter\",\n        \"NoteTaker\": \"NoteTaker\",\n        \"ChartGenerator\": \"ChartGenerator\",\n        \"FINISH\": END,\n    },\n)\n\nauthoring_graph.add_edge(START, \"supervisor\")\nchain = authoring_graph.compile()\n\n\n# The following functions interoperate between the top level graph state\n# and the state of the research sub-graph\n# this makes it so that the states of each graph don't get intermixed\ndef enter_chain(message: str, members: List[str]):\n    results = {\n        \"messages\": [HumanMessage(content=message)],\n        \"team_members\": \", \".join(members),\n    }\n    return results\n\n\n# We reuse the enter/exit functions to wrap the graph\nauthoring_chain = (\n    functools.partial(enter_chain, members=authoring_graph.nodes)\n    | authoring_graph.compile()\n)"]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "58e7d1e48a9c39a5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:32:13.913188Z",
     "start_time": "2024-05-15T08:32:11.598993Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 26868 characters]",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": ["from IPython.display import Image, display\n\ndisplay(Image(chain.get_graph().draw_mermaid_png()))"]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "9860fd46-c24d-40a5-a6ba-e8fddcd43369",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:53.723467Z",
     "start_time": "2024-05-15T08:19:53.709307Z"
    }
   },
   "outputs": [],
   "source": ["for s in authoring_chain.stream(\n    \"Write an outline for poem and then write the poem to disk.\",\n    {\"recursion_limit\": 100},\n):\n    if \"__end__\" not in s:\n        print(s)\n        print(\"---\")"]
  },
  {
   "cell_type": "markdown",
   "id": "f4b5b08d-9a9a-474a-94b4-f7aaa8ff19e6",
   "metadata": {},
   "source": [
    "## Add Layers\n",
    "\n",
    "In this design, we are enforcing a top-down planning policy. We've created two graphs already, but we have to decide how to route work between the two.\n",
    "\n",
    "We'll create a _third_ graph to orchestrate the previous two, and add some connectors to define how this top-level state is shared between the different graphs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "95ae7e52-92ed-41a3-88c4-21b6d7c8b041",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:55.454047Z",
     "start_time": "2024-05-15T08:19:53.725466Z"
    }
   },
   "outputs": [],
   "source": ["from langchain_core.messages import BaseMessage\nfrom langchain_openai.chat_models import ChatOpenAI\n\nllm = ChatOpenAI(model=\"gpt-4-1106-preview\")\n\nsupervisor_node = create_team_supervisor(\n    llm,\n    \"You are a supervisor tasked with managing a conversation between the\"\n    \" following teams: {team_members}. Given the following user request,\"\n    \" respond with the worker to act next. Each worker will perform a\"\n    \" task and respond with their results and status. When finished,\"\n    \" respond with FINISH.\",\n    [\"ResearchTeam\", \"PaperWritingTeam\"],\n)"]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4880e573-612f-4d24-97c1-2079382a4a2f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:55.469348Z",
     "start_time": "2024-05-15T08:19:55.455831Z"
    }
   },
   "outputs": [],
   "source": ["# Top-level graph state\nclass State(TypedDict):\n    messages: Annotated[List[BaseMessage], operator.add]\n    next: str\n\n\ndef get_last_message(state: State) -> str:\n    return state[\"messages\"][-1].content\n\n\ndef join_graph(response: dict):\n    return {\"messages\": [response[\"messages\"][-1]]}\n\n\n# Define the graph.\nsuper_graph = StateGraph(State)\n# First add the nodes, which will do the work\nsuper_graph.add_node(\"ResearchTeam\", get_last_message | research_chain | join_graph)\nsuper_graph.add_node(\n    \"PaperWritingTeam\", get_last_message | authoring_chain | join_graph\n)\nsuper_graph.add_node(\"supervisor\", supervisor_node)\n\n# Define the graph connections, which controls how the logic\n# propagates through the program\nsuper_graph.add_edge(\"ResearchTeam\", \"supervisor\")\nsuper_graph.add_edge(\"PaperWritingTeam\", \"supervisor\")\nsuper_graph.add_conditional_edges(\n    \"supervisor\",\n    lambda x: x[\"next\"],\n    {\n        \"PaperWritingTeam\": \"PaperWritingTeam\",\n        \"ResearchTeam\": \"ResearchTeam\",\n        \"FINISH\": END,\n    },\n)\nsuper_graph.add_edge(START, \"supervisor\")\nsuper_graph = super_graph.compile()"]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "270ff3ae26cd42ff",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:32:33.694459Z",
     "start_time": "2024-05-15T08:32:31.524790Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 22080 characters]",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": ["from IPython.display import Image, display\n\ndisplay(Image(super_graph.get_graph().draw_mermaid_png()))"]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b8badbf-d728-44bd-a2a7-5b4e587c92fe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-15T08:19:55.796497Z",
     "start_time": "2024-05-15T08:19:55.796497Z"
    }
   },
   "outputs": [],
   "source": ["for s in super_graph.stream(\n    {\n        \"messages\": [\n            HumanMessage(\n                content=\"Write a brief research report on the North American sturgeon. Include a chart.\"\n            )\n        ],\n    },\n    {\"recursion_limit\": 150},\n):\n    if \"__end__\" not in s:\n        print(s)\n        print(\"---\")"]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
