This commit is contained in:
Lance Martin
2025-07-11 12:40:50 -07:00
parent 8daa7584e9
commit 971f6fa754
4 changed files with 41 additions and 1101 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+5 -187
View File
@@ -38,84 +38,10 @@
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7c2d5f7c",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Task supervisor with path ('__pregel_pull', 'supervisor') wrote to unknown channel is_last_step, ignoring it.\n",
"Task supervisor with path ('__pregel_pull', 'supervisor') wrote to unknown channel remaining_steps, ignoring it.\n"
]
}
],
"source": [
"from langgraph_supervisor import create_supervisor\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"# Augment the LLM with tools\n",
"from langchain.chat_models import init_chat_model\n",
"llm = init_chat_model(\"anthropic:claude-sonnet-4-20250514\", temperature=0)\n",
"\n",
"# Create specialized agents\n",
"\n",
"def add(a: float, b: float) -> float:\n",
" \"\"\"Add two numbers.\"\"\"\n",
" return a + b\n",
"\n",
"def multiply(a: float, b: float) -> float:\n",
" \"\"\"Multiply two numbers.\"\"\"\n",
" return a * b\n",
"\n",
"def web_search(query: str) -> str:\n",
" \"\"\"Search the web for information.\"\"\"\n",
" return (\n",
" \"Here are the headcounts for each of the FAANG companies in 2024:\\n\"\n",
" \"1. **Facebook (Meta)**: 67,317 employees.\\n\"\n",
" \"2. **Apple**: 164,000 employees.\\n\"\n",
" \"3. **Amazon**: 1,551,000 employees.\\n\"\n",
" \"4. **Netflix**: 14,000 employees.\\n\"\n",
" \"5. **Google (Alphabet)**: 181,269 employees.\"\n",
" )\n",
"\n",
"math_agent = create_react_agent(\n",
" model=llm,\n",
" tools=[add, multiply],\n",
" name=\"math_expert\",\n",
" prompt=\"You are a math expert. Always use one tool at a time.\"\n",
")\n",
"\n",
"research_agent = create_react_agent(\n",
" model=llm,\n",
" tools=[web_search],\n",
" name=\"research_expert\",\n",
" prompt=\"You are a world class researcher with access to web search. Do not do any math.\"\n",
")\n",
"\n",
"# Create supervisor workflow\n",
"workflow = create_supervisor(\n",
" [research_agent, math_agent],\n",
" model=llm,\n",
" prompt=(\n",
" \"You are a team supervisor managing a research expert and a math expert. \"\n",
" \"For current events, use research_agent. \"\n",
" \"For math problems, use math_agent.\"\n",
" )\n",
")\n",
"\n",
"# Compile and run\n",
"app = workflow.compile()\n",
"result = app.invoke({\n",
" \"messages\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"what's the combined headcount of the FAANG companies in 2024?\"\n",
" }\n",
" ]\n",
"})"
]
"outputs": [],
"source": "from langchain.chat_models import init_chat_model\nfrom langgraph.prebuilt import create_react_agent\nfrom langgraph_supervisor import create_supervisor\n\n# Initialize the language model\nllm = init_chat_model(\"anthropic:claude-sonnet-4-20250514\", temperature=0)\n\n\ndef add(a: float, b: float) -> float:\n \"\"\"Add two numbers.\n \n Args:\n a: First number\n b: Second number\n \n Returns:\n Sum of a and b\n \"\"\"\n return a + b\n\n\ndef multiply(a: float, b: float) -> float:\n \"\"\"Multiply two numbers.\n \n Args:\n a: First number\n b: Second number\n \n Returns:\n Product of a and b\n \"\"\"\n return a * b\n\n\ndef web_search(query: str) -> str:\n \"\"\"Mock web search function that returns FAANG company headcounts.\n \n Args:\n query: Search query (unused in this mock)\n \n Returns:\n Static information about FAANG company headcounts\n \"\"\"\n return (\n \"Here are the headcounts for each of the FAANG companies in 2024:\\n\"\n \"1. **Facebook (Meta)**: 67,317 employees.\\n\"\n \"2. **Apple**: 164,000 employees.\\n\"\n \"3. **Amazon**: 1,551,000 employees.\\n\"\n \"4. **Netflix**: 14,000 employees.\\n\"\n \"5. **Google (Alphabet)**: 181,269 employees.\"\n )\n\n\n# Create specialized agents with isolated contexts\nmath_agent = create_react_agent(\n model=llm,\n tools=[add, multiply],\n name=\"math_expert\",\n prompt=\"You are a math expert. Always use one tool at a time.\"\n)\n\nresearch_agent = create_react_agent(\n model=llm,\n tools=[web_search],\n name=\"research_expert\",\n prompt=\"You are a world class researcher with access to web search. Do not do any math.\"\n)\n\n# Create supervisor workflow for coordinating agents\nworkflow = create_supervisor(\n [research_agent, math_agent],\n model=llm,\n prompt=(\n \"You are a team supervisor managing a research expert and a math expert. \"\n \"For current events, use research_agent. \"\n \"For math problems, use math_agent.\"\n )\n)\n\n# Compile the multi-agent application\napp = workflow.compile()\n\n# Execute the multi-agent workflow\nresult = app.invoke({\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"what's the combined headcount of the FAANG companies in 2024?\"\n }\n ]\n})"
},
{
"cell_type": "code",
@@ -336,118 +262,10 @@
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a1e62a08",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; text-decoration-color: #000080\">╭─────────────────────────────────────────────────── 🧑 Human ────────────────────────────────────────────────────╮</span>\n",
"<span style=\"color: #000080; text-decoration-color: #000080\">│</span> what's 5 + 7? <span style=\"color: #000080; text-decoration-color: #000080\">│</span>\n",
"<span style=\"color: #000080; text-decoration-color: #000080\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[34m╭─\u001b[0m\u001b[34m──────────────────────────────────────────────────\u001b[0m\u001b[34m 🧑 Human \u001b[0m\u001b[34m───────────────────────────────────────────────────\u001b[0m\u001b[34m─╮\u001b[0m\n",
"\u001b[34m│\u001b[0m what's 5 + 7? \u001b[34m│\u001b[0m\n",
"\u001b[34m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">╭───────────────────────────────────────────────────── 📝 AI ─────────────────────────────────────────────────────╮</span>\n",
"<span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span> I can calculate 5 + 7 for you using the Python code sandbox. <span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span>\n",
"<span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span> <span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span>\n",
"<span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span> 🔧 Tool Call: python_code_sandbox <span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span>\n",
"<span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span> Args: { <span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span>\n",
"<span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span> \"code\": \"print(5 + 7)\" <span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span>\n",
"<span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span> } <span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span>\n",
"<span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[37m╭─\u001b[0m\u001b[37m────────────────────────────────────────────────────\u001b[0m\u001b[37m 📝 AI \u001b[0m\u001b[37m────────────────────────────────────────────────────\u001b[0m\u001b[37m─╮\u001b[0m\n",
"\u001b[37m│\u001b[0m I can calculate 5 + 7 for you using the Python code sandbox. \u001b[37m│\u001b[0m\n",
"\u001b[37m│\u001b[0m \u001b[37m│\u001b[0m\n",
"\u001b[37m│\u001b[0m 🔧 Tool Call: python_code_sandbox \u001b[37m│\u001b[0m\n",
"\u001b[37m│\u001b[0m Args: { \u001b[37m│\u001b[0m\n",
"\u001b[37m│\u001b[0m \"code\": \"print(5 + 7)\" \u001b[37m│\u001b[0m\n",
"\u001b[37m│\u001b[0m } \u001b[37m│\u001b[0m\n",
"\u001b[37m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #808000; text-decoration-color: #808000\">╭──────────────────────────────────────────────── 🔧 Tool Output ─────────────────────────────────────────────────╮</span>\n",
"<span style=\"color: #808000; text-decoration-color: #808000\">│</span> Didn't find package packaging-24.2-py3-none-any.whl locally, attempting to load from <span style=\"color: #808000; text-decoration-color: #808000\">│</span>\n",
"<span style=\"color: #808000; text-decoration-color: #808000\">│</span> https://cdn.jsdelivr.net/pyodide/v0.27.7/full/Didn't find package micropip-0.9.0-py3-none-any.whl locally, <span style=\"color: #808000; text-decoration-color: #808000\">│</span>\n",
"<span style=\"color: #808000; text-decoration-color: #808000\">│</span> attempting to load from https://cdn.jsdelivr.net/pyodide/v0.27.7/full/Package packaging-24.2-py3-none-any.whl <span style=\"color: #808000; text-decoration-color: #808000\">│</span>\n",
"<span style=\"color: #808000; text-decoration-color: #808000\">│</span> loaded from https://cdn.jsdelivr.net/pyodide/v0.27.7/full/, caching the wheel in node_modules for future <span style=\"color: #808000; text-decoration-color: #808000\">│</span>\n",
"<span style=\"color: #808000; text-decoration-color: #808000\">│</span> use.Package micropip-0.9.0-py3-none-any.whl loaded from https://cdn.jsdelivr.net/pyodide/v0.27.7/full/, caching <span style=\"color: #808000; text-decoration-color: #808000\">│</span>\n",
"<span style=\"color: #808000; text-decoration-color: #808000\">│</span> the wheel in node_modules for future use.12 <span style=\"color: #808000; text-decoration-color: #808000\">│</span>\n",
"<span style=\"color: #808000; text-decoration-color: #808000\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[33m╭─\u001b[0m\u001b[33m───────────────────────────────────────────────\u001b[0m\u001b[33m 🔧 Tool Output \u001b[0m\u001b[33m────────────────────────────────────────────────\u001b[0m\u001b[33m─╮\u001b[0m\n",
"\u001b[33m│\u001b[0m Didn't find package packaging-24.2-py3-none-any.whl locally, attempting to load from \u001b[33m│\u001b[0m\n",
"\u001b[33m│\u001b[0m https://cdn.jsdelivr.net/pyodide/v0.27.7/full/Didn't find package micropip-0.9.0-py3-none-any.whl locally, \u001b[33m│\u001b[0m\n",
"\u001b[33m│\u001b[0m attempting to load from https://cdn.jsdelivr.net/pyodide/v0.27.7/full/Package packaging-24.2-py3-none-any.whl \u001b[33m│\u001b[0m\n",
"\u001b[33m│\u001b[0m loaded from https://cdn.jsdelivr.net/pyodide/v0.27.7/full/, caching the wheel in node_modules for future \u001b[33m│\u001b[0m\n",
"\u001b[33m│\u001b[0m use.Package micropip-0.9.0-py3-none-any.whl loaded from https://cdn.jsdelivr.net/pyodide/v0.27.7/full/, caching \u001b[33m│\u001b[0m\n",
"\u001b[33m│\u001b[0m the wheel in node_modules for future use.12 \u001b[33m│\u001b[0m\n",
"\u001b[33m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">╭───────────────────────────────────────────────────── 📝 AI ─────────────────────────────────────────────────────╮</span>\n",
"<span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span> The answer to 5 + 7 is 12. <span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">│</span>\n",
"<span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[37m╭─\u001b[0m\u001b[37m────────────────────────────────────────────────────\u001b[0m\u001b[37m 📝 AI \u001b[0m\u001b[37m────────────────────────────────────────────────────\u001b[0m\u001b[37m─╮\u001b[0m\n",
"\u001b[37m│\u001b[0m The answer to 5 + 7 is 12. \u001b[37m│\u001b[0m\n",
"\u001b[37m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from langgraph.prebuilt import create_react_agent\n",
"from langchain_sandbox import PyodideSandboxTool\n",
"\n",
"tool = PyodideSandboxTool(\n",
" # Allow Pyodide to install python packages that\n",
" # might be required.\n",
" allow_net=True\n",
")\n",
"agent = create_react_agent(\n",
" \"anthropic:claude-3-7-sonnet-latest\",\n",
" tools=[tool],\n",
")\n",
"result = await agent.ainvoke(\n",
" {\"messages\": [{\"role\": \"user\", \"content\": \"what's 5 + 7?\"}]},\n",
")\n",
"\n",
"format_messages(result['messages'])\n"
]
"outputs": [],
"source": "from langchain_sandbox import PyodideSandboxTool\nfrom langgraph.prebuilt import create_react_agent\n\n# Create sandbox tool with network access for package installation\ntool = PyodideSandboxTool(\n # Allow Pyodide to install Python packages that might be required\n allow_net=True\n)\n\n# Create a React agent with the sandbox tool\nagent = create_react_agent(\n \"anthropic:claude-3-7-sonnet-latest\",\n tools=[tool],\n)\n\n# Execute a mathematical query using the sandbox\nresult = await agent.ainvoke(\n {\"messages\": [{\"role\": \"user\", \"content\": \"what's 5 + 7?\"}]},\n)\n\n# Format and display the results\nformat_messages(result['messages'])"
},
{
"cell_type": "markdown",
@@ -485,4 +303,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}