{"id":21200,"date":"2025-02-15T22:45:53","date_gmt":"2025-02-15T14:45:53","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=21200"},"modified":"2025-02-15T22:46:10","modified_gmt":"2025-02-15T14:46:10","slug":"langgraph-supervisor","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/de\/langgraph-supervisor\/","title":{"rendered":"LangGraph Supervisor\uff1a\u5229\u7528\u76d1\u7763\u667a\u80fd\u4f53\u6765\u7ba1\u7406\u591a\u667a\u80fd\u4f53\u534f\u4f5c\u7684\u5de5\u5177"},"content":{"rendered":"<p><a href=\"https:\/\/www.kdjingpai.com\/de\/langgraph\/\">LangGraph<\/a> Supervisor\u662f\u4e00\u4e2a\u57fa\u4e8eLangGraph\u6846\u67b6\u7684Python\u5e93\uff0c\u4e13\u4e3a\u521b\u5efa\u548c\u7ba1\u7406\u591a\u667a\u80fd\u4f53\u7cfb\u7edf\u800c\u8bbe\u8ba1\u3002\u8be5\u5e93\u901a\u8fc7\u4e00\u4e2a\u4e2d\u592e\u76d1\u7763\u4ee3\u7406\u534f\u8c03\u591a\u4e2a\u4e13\u95e8\u4ee3\u7406\u7684\u5de5\u4f5c\uff0c\u786e\u4fdd\u901a\u4fe1\u6d41\u548c\u4efb\u52a1\u5206\u914d\u7684\u9ad8\u6548\u7ba1\u7406\u3002LangGraph Supervisor\u652f\u6301\u8def\u7531\u5668\u548c\u534f\u8c03\u5668\u6a21\u5f0f\uff0c\u63d0\u4f9b\u5de5\u5177\u5316\u7684\u4ee3\u7406\u4ea4\u63a5\u673a\u5236\uff0c\u7075\u6d3b\u7684\u6d88\u606f\u5386\u53f2\u7ba1\u7406\uff0c\u9002\u7528\u4e8e\u6d41\u5f0f\u5904\u7406\u3001\u77ed\u671f\u548c\u957f\u671f\u8bb0\u5fc6\u4ee5\u53ca\u4eba\u7c7b\u5728\u73af\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-21201\" title=\"LangGraph Supervisor\uff1a\u521b\u5efa\u548c\u7ba1\u7406\u591a\u667a\u80fd\u4f53\u7cfb\u7edf\u7684Python\u5e93-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/3fff8b4c39d60af.jpg\" alt=\"LangGraph Supervisor\uff1a\u521b\u5efa\u548c\u7ba1\u7406\u591a\u667a\u80fd\u4f53\u7cfb\u7edf\u7684Python\u5e93-1\" width=\"1213\" height=\"506\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/3fff8b4c39d60af.jpg 1213w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/02\/3fff8b4c39d60af-768x320.jpg 768w\" sizes=\"auto, (max-width: 1213px) 100vw, 1213px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>\u529f\u80fd\u5217\u8868<\/h2>\n<ul>\n<li>\u521b\u5efa\u76d1\u7763\u4ee3\u7406\u4ee5\u534f\u8c03\u591a\u4e2a\u4e13\u95e8\u4ee3\u7406<\/li>\n<li>\u652f\u6301\u8def\u7531\u5668\u548c\u534f\u8c03\u5668\u6a21\u5f0f<\/li>\n<li>\u5de5\u5177\u5316\u7684\u4ee3\u7406\u4ea4\u63a5\u673a\u5236<\/li>\n<li>\u7075\u6d3b\u7684\u6d88\u606f\u5386\u53f2\u7ba1\u7406<\/li>\n<li>\u652f\u6301\u6d41\u5f0f\u5904\u7406\u3001\u77ed\u671f\u548c\u957f\u671f\u8bb0\u5fc6<\/li>\n<li>\u4eba\u7c7b\u5728\u73af\u7684\u5e94\u7528\u573a\u666f\u652f\u6301<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4f7f\u7528\u5e2e\u52a9<\/h2>\n<h3>\u5b89\u88c5\u6d41\u7a0b<\/h3>\n<ol>\n<li>\u786e\u4fdd\u5df2\u5b89\u88c5Python\u73af\u5883\u3002<\/li>\n<li>\u4f7f\u7528pip\u5b89\u88c5LangGraph Supervisor\u5e93\uff1a<\/li>\n<\/ol>\n<pre><code>   pip install langgraph-supervisor\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5b89\u88c5LangChain\u548cOpenAI\u5e93\uff1a<\/li>\n<\/ol>\n<pre><code>   pip install langchain-openai\r\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u8bbe\u7f6eOpenAI API\u5bc6\u94a5\uff1a<\/li>\n<\/ol>\n<pre><code>   export OPENAI_API_KEY=&lt;your_api_key&gt;\r\n<\/code><\/pre>\n<h3>\u4f7f\u7528\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528LangGraph Supervisor\u7ba1\u7406\u4e24\u4e2a\u4e13\u95e8\u4ee3\u7406\uff1a<\/p>\n<ol>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u548c\u6a21\u5757\uff1a<\/li>\n<\/ol>\n<pre><code>   from langchain_openai import ChatOpenAI\r\nfrom langgraph_supervisor import create_supervisor\r\nfrom langgraph.prebuilt import create_react_agent\r\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u521b\u5efa\u4e13\u95e8\u4ee3\u7406\uff1a<\/li>\n<\/ol>\n<pre><code>   def add(a: float, b: float) -&gt; float:\r\n\"\"\"Add two numbers.\"\"\"\r\nreturn a + b\r\ndef multiply(a: float, b: float) -&gt; float:\r\n\"\"\"Multiply two numbers.\"\"\"\r\nreturn a * b\r\ndef web_search(query: str) -&gt; str:\r\n\"\"\"Search the web for information.\"\"\"\r\nreturn \"Here are the headcounts for each of the FAANG companies in 2024:...\"\r\nmodel = ChatOpenAI(model=\"gpt-4o\")\r\nmath_agent = create_react_agent(model=model, tools=[add, multiply], name=\"math_expert\", prompt=\"You are a math expert. Always use one tool at a time.\")\r\nresearch_agent = create_react_agent(model=model, tools=[web_search], name=\"research_expert\", prompt=\"You are a world class researcher with access to web search. Do not do any math.\")\r\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u521b\u5efa\u76d1\u7763\u4ee3\u7406\u5e76\u7f16\u8bd1\u8fd0\u884c\uff1a<\/li>\n<\/ol>\n<pre><code>   <a href=\"https:\/\/www.kdjingpai.com\/de\/workflow\/\">workflow<\/a> = create_supervisor([research_agent, math_agent], model=model, prompt=\"You are a team supervisor managing a research expert and a math expert.\")\r\napp = workflow.compile()\r\nresult = app.invoke({\r\n\"messages\": [\r\n{\"role\": \"user\", \"content\": \"what's the combined headcount of the FAANG companies in 2024?\"}\r\n]\r\n})\r\n<\/code><\/pre>\n<h3>\u8be6\u7ec6\u529f\u80fd\u64cd\u4f5c\u6d41\u7a0b<\/h3>\n<ol>\n<li><strong>\u521b\u5efa\u76d1\u7763\u4ee3\u7406<\/strong>\uff1a\u901a\u8fc7<code>create_supervisor<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u76d1\u7763\u4ee3\u7406\uff0c\u8d1f\u8d23\u534f\u8c03\u591a\u4e2a\u4e13\u95e8\u4ee3\u7406\u7684\u5de5\u4f5c\u3002<\/li>\n<li><strong>\u5b9a\u4e49\u4e13\u95e8\u4ee3\u7406<\/strong>\uff1a\u4f7f\u7528<code>create_react_agent<\/code>\u51fd\u6570\u5b9a\u4e49\u4e0d\u540c\u529f\u80fd\u7684\u4e13\u95e8\u4ee3\u7406\uff0c\u5982\u6570\u5b66\u8ba1\u7b97\u4ee3\u7406\u548c\u7f51\u7edc\u641c\u7d22\u4ee3\u7406\u3002<\/li>\n<li><strong>\u7f16\u8bd1\u548c\u8fd0\u884c\u5de5\u4f5c\u6d41<\/strong>\uff1a\u901a\u8fc7<code>workflow.compile()<\/code>\u7f16\u8bd1\u5de5\u4f5c\u6d41\uff0c\u5e76\u4f7f\u7528<code>app.invoke()<\/code>\u8fd0\u884c\u5de5\u4f5c\u6d41\uff0c\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u6d88\u606f\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<p>LangGraph Supervisor\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u591a\u667a\u80fd\u4f53\u7cfb\u7edf\u7ba1\u7406\u65b9\u6848\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u590d\u6742\u4efb\u52a1\u7684\u81ea\u52a8\u5316\u5904\u7406\u548c\u534f\u8c03\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LangGraph Supervisor\u662f\u4e00\u4e2a\u57fa\u4e8eLangGraph\u6846\u67b6\u7684Python\u5e93\uff0c\u4e13\u4e3a\u521b\u5efa\u548c\u7ba1\u7406\u591a\u667a\u80fd\u4f53\u7cfb\u7edf\u800c\u8bbe\u8ba1\u3002\u8be5\u5e93\u901a\u8fc7\u4e00\u4e2a\u4e2d\u592e\u76d1\u7763\u4ee3\u7406\u534f\u8c03\u591a\u4e2a\u4e13\u95e8\u4ee3\u7406\u7684\u5de5\u4f5c\uff0c\u786e\u4fdd\u901a\u4fe1\u6d41\u548c\u4efb\u52a1\u5206\u914d\u7684\u9ad8\u6548\u7ba1\u7406\u3002LangGraph Supervis&#8230;<\/p>\n","protected":false},"author":1,"featured_media":61842,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[230,201],"class_list":["post-21200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tool","tag-aikaiyuanxiangmu","tag-aizhinengti"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/21200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/comments?post=21200"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/21200\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/media\/61842"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/media?parent=21200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/categories?post=21200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/tags?post=21200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}