|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": { |
| 7 | + "vscode": { |
| 8 | + "languageId": "shellscript" |
| 9 | + } |
| 10 | + }, |
| 11 | + "outputs": [], |
| 12 | + "source": [ |
| 13 | + "! pip install langchain-mcp-adapters langgraph \"langchain[anthropic]\" langgraph-swarm" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": 1, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [ |
| 21 | + { |
| 22 | + "ename": "ModuleNotFoundError", |
| 23 | + "evalue": "No module named 'langchain_anthropic'", |
| 24 | + "output_type": "error", |
| 25 | + "traceback": [ |
| 26 | + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", |
| 27 | + "\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)", |
| 28 | + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# Imports\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mlangchain_anthropic\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m ChatAnthropic\n\u001b[32m 3\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mlangchain_mcp_adapters\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mclient\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m MultiServerMCPClient\n\u001b[32m 5\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mlanggraph\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mprebuilt\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m create_react_agent\n", |
| 29 | + "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'langchain_anthropic'" |
| 30 | + ] |
| 31 | + } |
| 32 | + ], |
| 33 | + "source": [ |
| 34 | + "# Imports\n", |
| 35 | + "from langchain_anthropic import ChatAnthropic\n", |
| 36 | + "from langchain_mcp_adapters.client import MultiServerMCPClient\n", |
| 37 | + "\n", |
| 38 | + "from langgraph.prebuilt import create_react_agent\n", |
| 39 | + "from langgraph_swarm import create_handoff_tool, create_swarm\n", |
| 40 | + "\n", |
| 41 | + "planner_prompt = \"\"\"\n", |
| 42 | + "<Task>\n", |
| 43 | + "You will help plan the steps to implement a LangGraph application. \n", |
| 44 | + "</Task>\n", |
| 45 | + "\n", |
| 46 | + "<Instructions>\n", |
| 47 | + "1. Reflect on the user's request. \n", |
| 48 | + "2. Use the list_doc_sources tool to fetch and the fetch_docs tool to read the llms.txt file.\n", |
| 49 | + "3. Identify documents that are relevant to the user's request.\n", |
| 50 | + "4. Ask follow-up questions to help refine the project scope and narrow the set of documents to be used for the project.\n", |
| 51 | + "5. When the project scope is clear produce a short description of the project with relevant URLs.\n", |
| 52 | + "6. Finally, transfer to transfer_to_researcher_agent.\n", |
| 53 | + "</Instructions>\n", |
| 54 | + "\"\"\"\n", |
| 55 | + "\n", |
| 56 | + "researcher_prompt = \"\"\"\n", |
| 57 | + "<Task>\n", |
| 58 | + "You will perform research on the project scope. \n", |
| 59 | + "</Task>\n", |
| 60 | + "\n", |
| 61 | + "<Instructions>\n", |
| 62 | + "1. Reflect on the project scope and provided URLs from the planner.\n", |
| 63 | + "2. Use the fetch_docs tool to fetch and read each URL.\n", |
| 64 | + "3. Use the information in these URLs to implement the solution to the user's request.\n", |
| 65 | + "4. If you need further clarification or additional sources to implement the solution, transfer to transfer_to_planner_agent.\n", |
| 66 | + "</Instructions>\n", |
| 67 | + "\"\"\"\n", |
| 68 | + "\n", |
| 69 | + "# LLM\n", |
| 70 | + "model = ChatAnthropic(model=\"claude-3-7-sonnet-latest\")\n", |
| 71 | + "\n", |
| 72 | + "# Handoff tools\n", |
| 73 | + "transfer_to_planner_agent = create_handoff_tool(\n", |
| 74 | + " agent_name=\"planner_agent\",\n", |
| 75 | + " description=\"Transfer user to the planner_agent to address clarifying questions or help them plan the steps to complete the user's request.\"\n", |
| 76 | + ")\n", |
| 77 | + "transfer_to_researcher_agent = create_handoff_tool(\n", |
| 78 | + " agent_name=\"researcher_agent\",\n", |
| 79 | + " description=\"Transfer user to researcher_agent to perform research on the user's request.\"\n", |
| 80 | + ")\n", |
| 81 | + "\n", |
| 82 | + "# TODO: Move to configuration\n", |
| 83 | + "#configurable = Configuration.from_runnable_config(config)\n", |
| 84 | + "#llms_txt_urls = configurable.llms_txt\n", |
| 85 | + "llms_txt_urls = \"LangGraph:https://langchain-ai.github.io/langgraph/llms.txt\"\n" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 7, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "# Input \n", |
| 95 | + "messages = {\"role\": \"user\", \"content\": \"Create a prompt chain that makes and improves a joke based on the user's input.\"}" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "# Research and planning tools \n", |
| 105 | + "async with MultiServerMCPClient(\n", |
| 106 | + " {\n", |
| 107 | + " \"research-server\": {\n", |
| 108 | + " \"command\": \"npx\",\n", |
| 109 | + " \"args\": [\"@playwright/mcp\"],\n", |
| 110 | + " \"transport\": \"stdio\",\n", |
| 111 | + " \"env\": {\n", |
| 112 | + " \"PATH\": \"/Users/rlm/.cursor/extensions/ms-python.python-2024.12.3-darwin-arm64/python_files/deactivate/zsh:/Users/rlm/Desktop/Code/langgraph-swarm/.venv/bin:/Users/rlm/.bun/bin:/Users/rlm/.poetry/bin:/Users/rlm/Library/Python/3.13/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Users/rlm/.cargo/bin:/Users/rlm/miniforge3/condabin:/Users/rlm/.local/bin\"\n", |
| 113 | + " }\n", |
| 114 | + " },\n", |
| 115 | + " \"planning-server\": {\n", |
| 116 | + " \"command\": \"uvx\",\n", |
| 117 | + " \"args\": [\n", |
| 118 | + " \"--from\",\n", |
| 119 | + " \"mcpdoc\",\n", |
| 120 | + " \"mcpdoc\",\n", |
| 121 | + " \"--urls\",\n", |
| 122 | + " llms_txt_urls,\n", |
| 123 | + " \"--transport\",\n", |
| 124 | + " \"stdio\",\n", |
| 125 | + " \"--port\",\n", |
| 126 | + " \"8081\",\n", |
| 127 | + " \"--host\",\n", |
| 128 | + " \"localhost\"\n", |
| 129 | + " ],\n", |
| 130 | + " \"transport\": \"stdio\",\n", |
| 131 | + " }\n", |
| 132 | + " }\n", |
| 133 | + "\n", |
| 134 | + ") as client:\n", |
| 135 | + " # Planner agent\n", |
| 136 | + " planner_agent = create_react_agent(model,\n", |
| 137 | + " prompt=planner_prompt, \n", |
| 138 | + " tools=client.server_name_to_tools[\"planning-server\"].append(transfer_to_researcher_agent),\n", |
| 139 | + " name=\"planner_agent\") \n", |
| 140 | + "\n", |
| 141 | + " # Researcher agent\n", |
| 142 | + " researcher_agent = create_react_agent(model, \n", |
| 143 | + " prompt=researcher_prompt, \n", |
| 144 | + " tools=client.server_name_to_tools[\"research-server\"].append(transfer_to_planner_agent),\n", |
| 145 | + " name=\"researcher_agent\") \n", |
| 146 | + "\n", |
| 147 | + " # Swarm\n", |
| 148 | + " agent_swarm = create_swarm([planner_agent, researcher_agent], default_active_agent=\"planner_agent\")\n", |
| 149 | + "\n", |
| 150 | + " # app = agent_swarm.compile(config_schema=Configuration)\n", |
| 151 | + " agent = agent_swarm.compile()\n", |
| 152 | + " response = await agent.ainvoke({\"messages\": messages})\n" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "metadata": { |
| 159 | + "vscode": { |
| 160 | + "languageId": "shellscript" |
| 161 | + } |
| 162 | + }, |
| 163 | + "outputs": [], |
| 164 | + "source": [ |
| 165 | + "for m in agent_response['messages']:\n", |
| 166 | + " m.pretty_print()" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": null, |
| 172 | + "metadata": { |
| 173 | + "vscode": { |
| 174 | + "languageId": "shellscript" |
| 175 | + } |
| 176 | + }, |
| 177 | + "outputs": [], |
| 178 | + "source": [] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "code", |
| 182 | + "execution_count": 2, |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [], |
| 185 | + "source": [] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": null, |
| 190 | + "metadata": {}, |
| 191 | + "outputs": [], |
| 192 | + "source": [] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": null, |
| 197 | + "metadata": {}, |
| 198 | + "outputs": [], |
| 199 | + "source": [] |
| 200 | + } |
| 201 | + ], |
| 202 | + "metadata": { |
| 203 | + "kernelspec": { |
| 204 | + "display_name": ".venv", |
| 205 | + "language": "python", |
| 206 | + "name": "python3" |
| 207 | + }, |
| 208 | + "language_info": { |
| 209 | + "codemirror_mode": { |
| 210 | + "name": "ipython", |
| 211 | + "version": 3 |
| 212 | + }, |
| 213 | + "file_extension": ".py", |
| 214 | + "mimetype": "text/x-python", |
| 215 | + "name": "python", |
| 216 | + "nbconvert_exporter": "python", |
| 217 | + "pygments_lexer": "ipython3", |
| 218 | + "version": "3.13.1" |
| 219 | + } |
| 220 | + }, |
| 221 | + "nbformat": 4, |
| 222 | + "nbformat_minor": 2 |
| 223 | +} |
0 commit comments