{"id":172,"date":"2025-04-22T21:53:37","date_gmt":"2025-04-22T13:53:37","guid":{"rendered":"http:\/\/sspacee.com\/?p=172"},"modified":"2025-04-22T21:53:53","modified_gmt":"2025-04-22T13:53:53","slug":"langgraph-%e5%85%a5%e9%97%a8%ef%bc%881%ef%bc%89-%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"http:\/\/sspacee.com\/?p=172","title":{"rendered":"LangGraph \u5165\u95e8\uff081\uff09-\u4ecb\u7ecd"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5f15\u8a00<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fc7\u53bb\u4e00\u5e74\u4e2d\uff0cLangChain \u6210\u4e3a\u4e86\u5e02\u573a\u4e0a\u5907\u53d7\u6b22\u8fce\u7684 AI \u6846\u67b6\u4e4b\u4e00\u3002\u8fd9\u4e2a\u8f7b\u91cf\u7ea7\u7684\u534f\u8c03\u5de5\u5177\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u6253\u9020\u51fa\u4f9d\u6258\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff08LLMs\uff09\u7684\u5e94\u7528\u7a0b\u5e8f\u53ca\u5176\u6240\u6709\u76f8\u5173\u7ec4\u4ef6\uff0c\u5982\u5411\u91cf\u6570\u636e\u5e93\uff08vectorDB\uff09\u3001\u5185\u5b58\u3001\u63d0\u793a\u8bed\u3001\u5de5\u5177\u548c\u4ee3\u7406\u3002LangChain \u7684\u4eae\u70b9\u5728\u4e8e\u5b83\u80fd\u591f\u65b9\u4fbf\u5730\u6784\u5efa\u6240\u8c13\u7684\u201c\u94fe\u6761\u201d\u3002\u4e00\u4e2a\u201c\u94fe\u6761\u201d\u662f\u4e00\u7cfb\u5217\u534f\u4f5c\u7ec4\u4ef6\u7684\u5e8f\u5217\uff0c\u901a\u8fc7\u5927\u578b\u8bed\u8a00\u6a21\u578b\u5904\u7406\u7528\u6237\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002\u94fe\u6761\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u4ef6\uff0c\u6bd4\u5982\u63d0\u793a\u8bed\u3001\u68c0\u7d22\u5668\u3001\u5904\u7406\u5668\u548c\u5de5\u5177\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u4e00\u4e2a\u94fe\u6761\u5185\u5d4c\u5957\u53e6\u4e00\u4e2a\u94fe\u6761\uff0c\u4ee5\u6784\u5efa\u66f4\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u800c\uff0c\u8fd9\u4e9b\u94fe\u6761\u7f3a\u5c11\u5728\u8fd0\u884c\u65f6\u5f15\u5165\u5faa\u73af\u7684\u529f\u80fd\uff0c\u8fd9\u610f\u5473\u7740\u6ca1\u6709\u73b0\u6210\u7684\u6846\u67b6\u80fd\u8ba9\u5927\u578b\u8bed\u8a00\u6a21\u578b\u5728\u7c7b\u4f3c for-loop \u7684\u60c5\u5883\u4e2d\u63a8\u65ad\u51fa\u4e0b\u4e00\u4e2a\u6700\u4f73\u52a8\u4f5c\u3002\u968f\u7740\u591a\u4ee3\u7406\u5e94\u7528\u7a0b\u5e8f\u2014\u2014\u5c55\u73b0\u4e0d\u540c\u5177\u6709\u7279\u5b9a\u4e2a\u6027\u548c\u5de5\u5177\u7684\u4ee3\u7406\u2014\u2014\u53d8\u5f97\u8d8a\u6765\u8d8a\u5b9e\u7528\u548c\u4e3b\u6d41\uff08\u53c2\u89c1\u50cf AutoGen \u8fd9\u6837\u7684\u5e93\u9879\u76ee\u7684\u5174\u8d77\uff09\uff0cLangChain \u7684\u5f00\u53d1\u4eba\u5458\u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u5e93\uff0c\u4ee5\u7b80\u5316\u8fd9\u7c7b\u4ee3\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u7ba1\u7406\u3002\u8fd9\u4e2a\u5728 2024 \u5e74 1 \u6708\u63a8\u51fa\u7684\u65b0\u5e93\u540d\u4e3a LangGraph\uff0c\u6b63\u5982\u540d\u5b57\u6240\u793a\uff0c\u5b83\u57fa\u4e8e\u56fe\u8fd9\u4e00\u6570\u5b66\u6982\u5ff5\uff0c\u4f5c\u4e3a\u5927\u578b\u8bed\u8a00\u6a21\u578b\u9a71\u52a8\u5e94\u7528\u7684\u6846\u67b6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6df1\u5165\u4e86\u89e3\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u56fe\u7684\u6570\u5b66\u6982\u5ff5\uff0c\u4ee5\u53ca\u5b83\u4e3a\u4f55\u9002\u5408\u5faa\u73af\u94fe\u6761\u7684\u6846\u67b6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u56fe\u7684\u6982\u5ff5<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6a21\u62df\u548c\u5206\u6790\u590d\u6742\u7cfb\u7edf\u65f6\uff0c\u4f7f\u7528\u80fd\u591f\u6355\u6349\u4ee3\u7406\u95f4\u5173\u7cfb\u7684\u6a21\u578b\u81f3\u5173\u91cd\u8981\u3002\u8fd9\u4e9b\u5173\u7cfb\u53ca\u5176\u6743\u91cd\u548c\u5206\u5e03\u53ef\u4ee5\u901a\u8fc7\u56fe\u7684\u6570\u5b66\u8868\u793a\u8fdb\u884c\u5efa\u6a21\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56fe\u662f\u4e00\u7ec4\u5bf9\u8c61\u7684\u96c6\u5408\uff0c\u5176\u4e2d\u4e00\u4e9b\u5bf9\u8c61\u5bf9\u901a\u8fc7\u8fde\u63a5\u76f8\u8fde\u3002\u8fd9\u4e9b\u8fde\u63a5\u901a\u5e38\u88ab\u79f0\u4f5c\u8fb9\u6216\u5f27\uff0c\u5b83\u4eec\u6240\u8fde\u63a5\u7684\u5bf9\u8c61\u88ab\u79f0\u4e3a\u9876\u70b9\u6216\u8282\u70b9\u3002\u56fe\u7528\u4e8e\u5efa\u6a21\u5bf9\u8c61\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5982\u793e\u4ea4\u7f51\u7edc\u3001\u9053\u8def\u7f51\u7edc\u6216\u5316\u5b66\u4e2d\u7684\u5206\u5b50\u8fde\u63a5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u5e38\uff0c\u5f53\u4e00\u4e2a\u7cfb\u7edf\u80fd\u591f\u7528\u56fe\u6765\u5efa\u6a21\u65f6\uff0c\u6211\u4eec\u79f0\u8fd9\u4e2a\u7cfb\u7edf\u4e3a\u4e00\u4e2a\u7f51\u7edc\u3002\u7f51\u7edc\u79d1\u5b66\u662f\u4e00\u95e8\u65e8\u5728\u7406\u89e3\u5176\u57fa\u7840\u7ed3\u6784\u4e3a\u56fe\u7684\u73b0\u8c61\u7684\u5b66\u79d1\u3002\u4f8b\u5982\uff0c\u5728\u6d41\u884c\u75c5\u5b66\u9886\u57df\uff0c\u7f51\u7edc\u7528\u4e8e\u6a21\u62df\u75be\u75c5\u7684\u4f20\u64ad\uff0c\u5176\u4e2d\u8282\u70b9\u4ee3\u8868\u4e2a\u4f53\uff0c\u8fde\u63a5\u4ee3\u8868\u4e2a\u4f53\u95f4\u7684\u63a5\u89e6\uff08\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\u6211\u5173\u4e8e\u7f51\u7edc\u79d1\u5b66\u7684\u7cfb\u5217\u6587\u7ae0\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 LangGraph \u4e2d\uff0c\u56fe\u6846\u67b6\u7528\u4e8e\u8868\u793a\u6784\u5efa\u5927\u578b\u8bed\u8a00\u6a21\u578b\u9a71\u52a8\u5e94\u7528\u6240\u9700\u7684\u5178\u578b\u7ec4\u4ef6\u3002\u66f4\u5177\u4f53\u5730\u8bf4\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8282\u70b9 \u2192 \u8fd9\u4e9b\u662f\u6e38\u620f\u4e2d\u7684\u201c\u53c2\u4e0e\u8005\u201d\u6216\u4ee3\u7406\u3002\u6bcf\u4e2a\u4eba\u53ef\u4ee5\u4e13\u957f\u4e8e\u4e0d\u540c\u7684\u6d3b\u52a8\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u4e00\u4e2a\u56fe\uff0c\u5305\u542b\u4e09\u4e2a\u8282\u70b9\uff0c\u5206\u522b\u4ee3\u8868\u5fc3\u7406\u5b66\u5bb6\u3001\u793e\u4f1a\u5b66\u5bb6\u548c\u7ecf\u6d4e\u5b66\u5bb6\uff0c\u4ed6\u4eec\u6b63\u5728\u8fdb\u884c\u4e00\u9879\u5173\u4e8e\u793e\u4ea4\u5a92\u4f53\u5bf9\u5fc3\u7406\u5065\u5eb7\u5f71\u54cd\u7684\u5927\u89c4\u6a21\u8c03\u67e5\uff08\u6211\u4eec\u8fd8\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u4ee3\u8868\u8fd9\u4e09\u4f4d\u4e13\u5bb6\u7684\u76d1\u7763\u8005\u7684\u7b2c\u56db\u4e2a\u8282\u70b9\uff09\u3002<\/li>\n\n\n\n<li>\u8fb9 \u2192 \u8fd9\u4e9b\u4ee3\u8868\u8282\u70b9\u4e4b\u95f4\u7684\u8fde\u63a5\u548c\u5173\u7cfb\u3002\u5b83\u4eec\u53ef\u4ee5\u662f\u8282\u70b9\u95f4\u4f20\u9012\u4fe1\u606f\u7684\u8fde\u63a5\uff0c\u4e5f\u53ef\u4ee5\u662f\u786e\u5b9a\u6fc0\u6d3b\u8282\u70b9\u53ca\u5176\u6267\u884c\u987a\u5e8f\u7684\u6761\u4ef6\u8fb9\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u5173\u4e8e LangGraph \u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u4f60\u53ef\u4ee5\u9605\u8bfb\u5b98\u65b9\u535a\u5ba2\uff08<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/blog.langchain.dev\/langgraph\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/blog.langchain.dev\/langgraph\/<\/a>\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u770b\u5230\u5982\u4f55\u8bbe\u8ba1\u4f60\u7684\u7b2c\u4e00\u4e2a\u591a\u4ee3\u7406\u56fe\u7684\u793a\u4f8b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6784\u5efa\u591a\u4ee3\u7406\u56fe<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5c06\u6784\u5efa\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u89d2\u8272\u53ca\u5176\u7279\u6027\uff08\u4f7f\u7528\u5927\u578b\u8bed\u8a00\u6a21\u578b\u7684\u8bcd\u6c47\uff0c\u5373\u4ed6\u4eec\u7684\u201c\u7cfb\u7edf\u6d88\u606f\u201d\uff09\u7684\u591a\u4ee3\u7406\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5fc3\u7406\u5b66\u5bb6\uff1a\u201c\u4e00\u4e2a\u80fd\u591f\u65e0\u7f1d\u5c06\u795e\u7ecf\u79d1\u5b66\u7406\u8bba\u878d\u5165\u5bf9\u8bdd\u7684\u4e13\u5bb6\u5fc3\u7406\u5b66\u5bb6\uff0c\u63ed\u793a\u4eba\u7c7b\u884c\u4e3a\u548c\u60c5\u611f\u7684\u590d\u6742\u6027\u3002\u201d<\/li>\n\n\n\n<li>\u793e\u4f1a\u5b66\u5bb6\uff1a\u201c\u4e00\u4f4d\u654f\u9510\u7684\u793e\u4f1a\u5b66\u5bb6\uff0c\u64c5\u957f\u5256\u6790\u793e\u4f1a\u6a21\u5f0f\uff0c\u63a2\u7a76\u793e\u4ea4\u5a92\u4f53\u4e0e\u6211\u4eec\u7684\u96c6\u4f53\u5fc3\u7406\u4e4b\u95f4\u7684\u76f8\u4e92\u4f5c\u7528\u3002\u201d<\/li>\n\n\n\n<li>\u7ecf\u6d4e\u5b66\u5bb6\uff1a\u201c\u4e00\u4f4d\u52a1\u5b9e\u7684\u7ecf\u6d4e\u5b66\u5bb6\uff0c\u4ee5\u7cbe\u51c6\u7684\u65b9\u5f0f\u91cf\u5316\u65e0\u5f62\u8d44\u4ea7\uff0c\u5c06\u793e\u4ea4\u5a92\u4f53\u8d8b\u52bf\u4e0e\u7ecf\u6d4e\u5f71\u54cd\u8054\u7cfb\u8d77\u6765\u3002\u201d<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u7684\u7814\u7a76\u95ee\u9898\u5c06\u63a2\u8ba8\u793e\u4ea4\u5a92\u4f53\u4e0e\u5fc3\u7406\u5065\u5eb7\u4e4b\u95f4\u7684\u5173\u8054\u3002\u4e3a\u4e86\u6784\u5efa\u56fe\uff0c\u6211\u4f7f\u7528\u4e86\u6765\u81ea langgraph \u4ed3\u5e93\u7684\u793a\u4f8b\uff08<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/github.com\/langchain-ai\/langgraph\/tree\/main\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/langchain-ai\/langgraph\/tree\/main<\/a>\uff09\uff0c\u5e76\u8c03\u6574\u5b83\u6765\u8bbe\u8ba1\u4e0a\u8ff0\u4e09\u4f4d\u4ee3\u7406\u7684\u4e2a\u6027\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4ec5\u8986\u76d6\u4fee\u6539\u8fc7\u7684\u811a\u672c\uff1a\u5982\u679c\u4f60\u60f3\u81ea\u5df1\u5c1d\u8bd5\u7b14\u8bb0\u672c\uff0c\u8bf7\u53c2\u8003\u8fd9\u4e2a\u94fe\u63a5\uff08<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/github.com\/langchain-ai\/langgraph\/blob\/main\/examples\/multi_agent\/multi-agent-collaboration.ipynb\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/langchain-ai\/langgraph\/blob\/main\/examples\/multi_agent\/multi-agent-collaboration.ipynb<\/a>\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u6211\u4eec\u8bf4\u8fc7\uff0c\u8981\u6784\u5efa\u56fe\uff0c\u6211\u4eec\u5c06\u9700\u8981\u4e24\u4e2a\u4e3b\u8981\u6210\u5206\uff1a\u8282\u70b9\u548c\u8fb9\u3002\u8ba9\u6211\u4eec\u4e86\u89e3\u80cc\u540e\u7684\u903b\u8f91\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8282\u70b9<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8282\u70b9\u53ef\u4ee5\u662f\u5177\u6709\u7279\u5b9a\u4efb\u52a1\u6216\u4e2a\u6027\u7684\u4ee3\u7406\uff0c\u4e5f\u53ef\u4ee5\u662f\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u6267\u884c\u7684\u64cd\u4f5c\uff0c\u7ed9\u5b9a\u63d0\u4f9b\u7684\u5de5\u5177\u3002\u5728\u6211\u4eec\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6709 3 \u4e2a\u4ee3\u7406 \u2014 \u5fc3\u7406\u5b66\u5bb6\u3001\u793e\u4f1a\u5b66\u5bb6\u548c\u7ecf\u6d4e\u5b66\u5bb6 \u2014 \u548c\u4e00\u4e2a\u5de5\u5177 \u2014 \u641c\u7d22\u5f15\u64ce Tavily\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u521d\u59cb\u5316\u56fe\uff0c\u6211\u5c06\u9996\u5148\u8bbe\u7f6e\u6211\u7684\u6a21\u578b\uff08\u6211\u5c06\u5229\u7528 Azure OpenAI GPT-4\uff09\u548c\u6211\u7684\u5de5\u5177\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from langchain.schema import HumanMessage, AIMessage\nfrom langchain_openai import AzureChatOpenAI\n\n\nfrom dotenv import load_dotenv\n\n\nload_dotenv()\n\n\nos.environ&#91;\"AZURE_OPENAI_API_KEY\"]\nos.environ&#91;\"AZURE_OPENAI_ENDPOINT\"]\nos.environ&#91;\"TAVILY_API_KEY\"]\n\n\nllm = AzureChatOpenAI(\n    openai_api_version=\"2023-07-01-preview\",\n    azure_deployment=\"gpt-4\",\n)\n\n\nfrom langchain_community.tools.tavily_search import TavilySearchResults\n\n\ntavily_tool = TavilySearchResults(max_results=5)\n\n\ntools = &#91;tavily_tool]\ntool_executor = ToolExecutor(tools)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u521d\u59cb\u5316\u4ee3\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import functools\n\n\n# Helper function to create a node for a given agent\ndef agent_node(state, agent, name):\n    result = agent.invoke(state)\n    # We convert the agent output into a format that is suitable to append to the global state\n    if isinstance(result, FunctionMessage):\n        pass\n    else:\n        result = HumanMessage(**result.dict(exclude={\"type\", \"name\"}), name=name)\n    return {\n        \"messages\": &#91;result],\n        # Since we have a strict workflow, we can\n        # track the sender so we know who to pass to next.\n        \"sender\": name,\n    }\n\n\n# Psychologist agent and node\npsychologist_agent = create_agent(\n    llm,\n    &#91;tavily_tool],\n    system_message=\"An expert psychologist who seamlessly weaves neuroscientific theories into conversations, unraveling the complexities of human behavior and emotions.\",\n)\npsychologist_node = functools.partial(agent_node, agent=psychologist_agent, name=\"Psychologist\")\n\n\n# Socioligist\nsociologist_agent = create_agent(\n    llm,\n    &#91;tavily_tool],\n    system_message=\"A keen-eyed sociologist adept at dissecting societal patterns, investigating the collective psyche. Focus on group effects rather than individual effects.\",\n)\nsociologist_node = functools.partial(agent_node, agent=sociologist_agent, name=\"Sociologist\")\n\n\n# Socioligist\neconomist_agent = create_agent(\n    llm,\n    &#91;tavily_tool],\n    system_message=\"A pragmatic economist who quantifies intangibles, connecting trends to economic implications with precision.\",\n)\neconomist_node = functools.partial(agent_node, agent=economist_agent, name=\"Economist\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8fb9<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u903b\u8f91\u6765\u5e94\u7528\u4e8e\u6211\u4eec\u7684\u8fb9\uff0c\u4ee5\u786e\u5b9a\u5faa\u73af\u7684\u6267\u884c\u6a21\u5f0f\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u4ee5\u4e0b\u7ed3\u6784\u7684\u8def\u7531\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/v2-99bcdc8382737452750adcc071a170a6_1440w.webp?consumer=ZHI_MENG\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code># Either agent can decide to end\ndef router(state):\n    # This is the router\n    messages = state&#91;\"messages\"]\n    last_message = messages&#91;-1]\n    if \"function_call\" in last_message.additional_kwargs:\n        # The previus agent is invoking a tool\n        return \"call_tool\"\n    if \"FINAL ANSWER\" in last_message.content:\n        # Any agent decided the work is done\n        return \"end\"\n    return \"continue\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u8bf4\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5468\u671f\u4e2d\u7684\u72b6\u6001\uff0c\u6709\u4e09\u79cd\u53ef\u80fd\u7684\u4e0b\u4e00\u6b65\u52a8\u4f5c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u524d\u4e00\u4e2a\u52a8\u4f5c\uff08-1\uff09\u4ea7\u751f\u4e86\u8c03\u7528\u5de5\u5177\u7684\u9700\u8981\uff08\u8f93\u51fa\u4e3a\u201cfunction call\u201d\uff09\uff0c\u5219\u5c06\u8c03\u7528\u4e00\u4e2a\u53ef\u7528\u5de5\u5177\uff1b<\/li>\n\n\n\n<li>\u5982\u679c\u524d\u4e00\u4e2a\u52a8\u4f5c\u4ea7\u751f\u4e86\u6700\u7ec8\u8f93\u51fa\uff08\u201cFINAL ANSWER\u201d\uff09\uff0c\u5219\u5468\u671f\u7ed3\u675f\uff1b<\/li>\n\n\n\n<li>\u5426\u5219\uff0c\u5468\u671f\u5c06\u6309\u7167\u6211\u4eec\u5728\u521d\u59cb\u5316\u56fe\u65f6\u5b9a\u4e49\u7684\u987a\u5e8f\u7ee7\u7eed\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u6211\u4eec\u6709\u4e86\u8bbe\u7f6e\u56fe\u6846\u67b6\u7684\u6240\u6709\u8981\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u8def\u7531\u521d\u59cb\u5316\u8282\u70b9\uff083\u4e2a\u4ee3\u7406+1\u4e2a\u5de5\u5177\uff09\u548c\u8fb9\uff0c\u8bbe\u7f6e\u5982\u4e0b\u5faa\u73af\uff1a\u5fc3\u7406\u5b66\u5bb6 \u2192 \u793e\u4f1a\u5b66\u5bb6 \u2192 \u7ecf\u6d4e\u5b66\u5bb6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>workflow = StateGraph(AgentState)\n\n\nworkflow.add_node(\"Psychologist\", psychologist_node)\nworkflow.add_node(\"Sociologist\", sociologist_node)\nworkflow.add_node(\"Economist\", economist_node)\nworkflow.add_node(\"call_tool\", tool_node)\n\n\nworkflow.add_conditional_edges(\n    \"Psychologist\",\n    router,\n    {\"continue\": \"Sociologist\", \"call_tool\": \"call_tool\", \"end\": END},\n)\nworkflow.add_conditional_edges(\n    \"Sociologist\",\n    router,\n    {\"continue\": \"Economist\", \"call_tool\": \"call_tool\", \"end\": END},\n)\n\n\nworkflow.add_conditional_edges(\n    \"Economist\",\n    router,\n    {\"continue\": \"Psychologist\", \"call_tool\": \"call_tool\", \"end\": END},\n)\n\n\nworkflow.add_conditional_edges(\n    \"call_tool\",\n    lambda x: x&#91;\"sender\"],\n    {\n        \"Psychologist\": \"Psychologist\",\n        \"Sociologist\": \"Sociologist\",\n        \"Economist\": \"Economist\"\n    },\n)\nworkflow.set_entry_point(\"Psychologist\")\ngraph = workflow.compile()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u521d\u59cb\u5316\u56fe\u3002\u6211\u4eec\u5c06\u628a\u6574\u4e2a\u5bf9\u8bdd\u5b58\u50a8\u5230\u4e00\u4e2a\u540d\u4e3a outputs \u7684\u5217\u8868\u4e2d\uff0c\u7136\u540e\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u6253\u5370\u6bcf\u4e2a\u4ee3\u7406\u7684\u63a8\u7406\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>outputs = &#91;]\nfor s in graph.stream(\n    {\n        \"messages\": &#91;\n            HumanMessage(\n                content=\"Provide me with meaningful insights about the effects of social media on mental health, covering individual effects, collective effects and macroeconomics effects. Once you provided me with these three perpectives, finish.\"\n            )\n        ],\n    },\n    # Maximum number of steps to take in the graph\n    {\"recursion_limit\": 150},\n):\n    print(s)\n    outputs.append(s)\n    print(\"----\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{'\u5fc3\u7406\u5b66\u5bb6'\uff1a{'\u6d88\u606f'\uff1a&#91;HumanMessage\uff08content =''\uff0cadditional_kwargs = {\n----\n{'call_tool': {'messages': &#91;FunctionMessage(content=\n----\n{'\u793e\u4f1a\u5b66\u5bb6': {'\u6d88\u606f': &#91;HumanMessage(content=\"\u57fa\u4e8e\u5148\u524d\u7684\u4fe1\u606f\n{'__end__': {'messages': &#91;HumanMessage(content='\n.....<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u53ea\u6253\u5370\u6700\u7ec8\u7b54\u6848\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(outputs&#91;-1]&#91;'__end__']&#91;'messages']&#91;-1].content)\nFINAL ANSWER:\n\n\nBased on the search results, here are some insights about the effects of social media on mental health from three perspectives: individual, collective, and macroeconomic.\n\n\nIndividual effects: Social media platforms are popular venues for sharing personal experiences, seeking information, and offering peer-to-peer support among individuals living with mental illness. Many individuals living with mental disorders have expressed interest in using social media platforms for seeking mental health information, connecting with mental health providers, and accessing evidence-based mental health services delivered over social media specifically for coping with mental health symptoms or for promoting overall health and wellbeing. However, excessive use or problematic use of social media can have negative effects on mental health &#91;source](&lt;https:\/\/www.ncbi.nlm.nih.gov\/pmc\/articles\/PMC7785056\/&gt;).\n\n\nCollective effects: Some studies have explored the relationship between how individuals perceive themselves on social media versus offline across different generations. Such research can help us understand the collective mental health impact of social media on different demographic groups. The perception of oneself on social media versus offline can have implications for psychological well-being &#91;source](&lt;https:\/\/www.psypost.org\/2024\/01\/gen-zs-mental-health-and-authenticity-on-social-media-fascinating-insights-from-new-research-220725&gt;).\n\n\nMacroeconomic effects: Much of the research to date has focused on populations within individual countries, typically the high-income nations of the global north. Therefore, there's a need to further understand the impact of social media on mental health in diverse settings, including lower-income countries. As social media platforms have a global reach, understanding their impact on mental health across different economic contexts is crucial for developing effective mental health strategies and policies &#91;source](&lt;https:\/\/www.frontiersin.org\/articles\/10.3389\/fpsyt.2024.1367335&gt;). \n\n\nThe role of social media in mental health is complex, as it can both support and hinder mental health, depending on the usage patterns, individual factors, and the sociocultural and economic context. It's important for researchers, clinicians, policymakers, and social media users themselves to carefully weigh the potential benefits against the potential risks of social media use for mental health.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b63\u5982\u4f60\u6240\u89c1\uff0c\u6240\u6709\u4ee3\u7406\u7684\u8f93\u51fa\u90fd\u662f\u5bf9\u6700\u7ec8\u7b54\u6848\u7684\u5b9d\u8d35\u8f93\u5165\uff0c\u8be5\u7b54\u6848\u6db5\u76d6\u4e86\u6765\u81ea\u6240\u6709\u4e09\u4e2a\u89d2\u5ea6\uff08\u4e2a\u4f53\u3001\u96c6\u4f53\u548c\u5b8f\u89c2\u7ecf\u6d4e\uff09\u7684\u7814\u7a76\u95ee\u9898\uff0c\u5e76\u63d0\u4f9b\u4e86\u901a\u8fc7\u5de5\u5177\u68c0\u7d22\u7684 3 \u4e2a\u6765\u6e90\u94fe\u63a5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/www.ncbi.nlm.nih.gov\/pmc\/articles\/PMC7785056\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.ncbi.nlm.nih.gov\/pmc\/articles\/PMC7785056\/<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/www.psypost.org\/2024\/01\/gen-zs-mental-health-and-authenticity-on-social-media-fascinating-insights-from-new-research-220725\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.psypost.org\/2024\/01\/gen-zs-mental-health-and-authenticity-on-social-media-fascinating-insights-from-new-research-220725<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/www.frontiersin.org\/articles\/10.3389\/fpsyt.2024.1367335\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.frontiersin.org\/articles\/10.3389\/fpsyt.2024.1367335<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ed3\u8bba<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">LangGraph \u4f5c\u4e3a\u5f00\u53d1\u591a\u4ee3\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u5f3a\u5927\u5de5\u5177\u800c\u5d2d\u9732\u5934\u89d2\uff0c\u5176\u57fa\u4e8e\u56fe\u7684\u65b9\u6cd5\u63d0\u4f9b\u4e86\u72ec\u7279\u7684\u4f18\u52bf\uff0c\u4f7f\u5f97\u53ef\u4ee5\u521b\u5efa\u52a8\u6001\u5de5\u4f5c\u6d41\u7a0b\uff0c\u5176\u4e2d\u591a\u4e2a\u72ec\u7acb\u4ee3\u7406\u65e0\u7f1d\u534f\u4f5c\u3002\u8fd9\u4e9b\u7531\u5927\u8bed\u8a00\u6a21\u578b\u9a71\u52a8\u7684\u4ee3\u7406\u53ef\u4ee5\u5171\u4eab\u72b6\u6001\u6216\u72ec\u7acb\u5de5\u4f5c\uff0c\u5c06\u6700\u7ec8\u54cd\u5e94\u4f20\u9012\u7ed9\u5f7c\u6b64\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u5916\uff0c\u901a\u8fc7\u5206\u7ec4\u5de5\u5177\u548c\u804c\u8d23\uff0cLangGraph \u63d0\u9ad8\u4e86\u6548\u7387\u3002\u6bcf\u4e2a\u4ee3\u7406\u4e13\u6ce8\u4e8e\u7279\u5b9a\u4efb\u52a1\uff0c\u4ece\u800c\u83b7\u5f97\u66f4\u597d\u7684\u7ed3\u679c\u3002\u5141\u8bb8\u5b9a\u5236\u5355\u72ec\u7684\u63d0\u793a\u8bcd\uff0c\u4ee3\u7406\u751a\u81f3\u53ef\u4ee5\u7531\u5355\u72ec\u7684\u5fae\u8c03\u8bed\u8a00\u6a21\u578b\u9a71\u52a8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u5728\u8fc7\u53bb\u4e00\u5e74\u4e2d\uff0cLangChain \u6210\u4e3a\u4e86\u5e02\u573a\u4e0a\u5907\u53d7\u6b22\u8fce\u7684 AI \u6846\u67b6\u4e4b\u4e00\u3002\u8fd9\u4e2a\u8f7b\u91cf\u7ea7\u7684\u534f\u8c03\u5de5\u5177\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u6253\u9020\u51fa\u4f9d\u6258\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff08LLMs\uff09\u7684\u5e94\u7528\u7a0b\u5e8f\u53ca\u5176\u6240\u6709\u76f8\u5173\u7ec4\u4ef6\uff0c\u5982\u5411\u91cf\u6570\u636e\u5e93\uff08vectorDB\uff09\u3001\u5185\u5b58\u3001\u63d0\u793a\u8bed\u3001\u5de5\u5177\u548c\u4ee3\u7406\u3002LangChain \u7684\u4eae\u70b9\u5728\u4e8e\u5b83\u80fd\u591f\u65b9\u4fbf\u5730\u6784\u5efa\u6240\u8c13\u7684\u201c\u94fe\u6761\u201d\u3002\u4e00\u4e2a\u201c\u94fe\u6761\u201d\u662f\u4e00\u7cfb\u5217\u534f\u4f5c\u7ec4\u4ef6\u7684\u5e8f\u5217\uff0c\u901a\u8fc7\u5927\u578b\u8bed\u8a00\u6a21\u578b\u5904\u7406\u7528\u6237\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002\u94fe\u6761\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u7ec4\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/sspacee.com\/index.php?rest_route=\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sspacee.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sspacee.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sspacee.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sspacee.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=172"}],"version-history":[{"count":1,"href":"http:\/\/sspacee.com\/index.php?rest_route=\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":173,"href":"http:\/\/sspacee.com\/index.php?rest_route=\/wp\/v2\/posts\/172\/revisions\/173"}],"wp:attachment":[{"href":"http:\/\/sspacee.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sspacee.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sspacee.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}