{"id":742,"date":"2026-07-16T18:00:00","date_gmt":"2026-07-16T10:00:00","guid":{"rendered":"https:\/\/larkdrop.cafe\/?p=742"},"modified":"2026-07-16T14:22:07","modified_gmt":"2026-07-16T06:22:07","slug":"langgraph","status":"publish","type":"post","link":"https:\/\/larkdrop.cafe\/index.php\/2026\/07\/16\/langgraph\/","title":{"rendered":"LangGraph"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728Python\u4e2d\uff0c\u8fd9\u79cd\u590d\u6742\u7684workflow\u7f16\u6392\u53ef\u4ee5\u4f7f\u7528&nbsp;<strong>LangGraph<\/strong>&nbsp;\u6846\u67b6\u6765\u5b9e\u73b0\u3002LangGraph \u662f LangChain \u56e2\u961f\u63a8\u51fa\u7684\u5de5\u4f5c\u6d41\u7f16\u6392\u6846\u67b6\uff0c\u6838\u5fc3\u601d\u60f3\u662f<strong>\u5c06\u4e1a\u52a1\u6d41\u7a0b\u5efa\u6a21\u4e3a\u4e00\u5f20\u6709\u5411\u56fe<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8282\u70b9\uff08Node\uff09<\/strong>\uff1a\u6bcf\u4e2a\u8282\u70b9\u662f\u4e00\u4e2a\u5904\u7406\u6b65\u9aa4\uff08\u4e00\u4e2a\u51fd\u6570\uff09<\/li>\n\n\n\n<li><strong>\u8fb9\uff08Edge\uff09<\/strong>\uff1a\u5b9a\u4e49\u8282\u70b9\u4e4b\u95f4\u7684\u6267\u884c\u987a\u5e8f<\/li>\n\n\n\n<li><strong>\u72b6\u6001\uff08State\uff09<\/strong>\uff1a\u5728\u8282\u70b9\u4e4b\u95f4\u4f20\u9012\u7684\u5171\u4eab\u6570\u636e<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">LangGraph \u4e4b\u4e8e Python\uff0c\u5c31\u50cf&nbsp;<strong>Spring AI Alibaba<\/strong>&nbsp;\u4e4b\u4e8e Java\u3002\u4e24\u8005\u7684\u6838\u5fc3\u6982\u5ff5\u5bf9\u5e94\u5173\u7cfb\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u662f\u7528Langgraph\u5fc5\u987b\u9996\u5148\u5b89\u88c5\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\npip install langgraph\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a5\u4e0b\u6765\u5206\u6b65\u5b66\u4e60LangGraph\u7684\u57fa\u672c\u4f7f\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49\u72b6\u6001<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u72b6\u6001\u662f\u6240\u6709\u8282\u70b9\u5171\u4eab\u7684\u6570\u636e\u5bb9\u5668\uff0c\u4f7f\u7528&nbsp;<code>TypedDict<\/code>&nbsp;\u5b9a\u4e49\uff0cTypedDict\u5c31\u7b49\u4ef7\u4e8eSpring AI Alibaba\u4e2d\u7684OverAllState\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\nfrom typing import TypedDict, NotRequired\n\nclass MyState(TypedDict):\n    \"\"\"\u5de5\u4f5c\u6d41\u72b6\u6001\uff0c\u6240\u6709\u8282\u70b9\u901a\u8fc7\u8bfb\u5199\u72b6\u6001\u6765\u4f20\u9012\u6570\u636e\"\"\"\n    user_input: str                    # \u7528\u6237\u8f93\u5165\n    result: str           # \u5904\u7406\u7ed3\u679c\n    score: int            # \u8bc4\u5206\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\uff1a\u8fd9\u91cc\u548c\u666e\u901a\u7684Python\u7c7b\u4e0d\u540c\uff01<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b66\u8fc7Python\u770b\u5230\u00a0<code>class MyState:<\/code>\u00a0\u91cc\u9762\u5199\u00a0<code>user_input: str<\/code>\u00a0\u53ef\u80fd\u4f1a\u6761\u4ef6\u53cd\u5c04\u5730\u8ba4\u4e3a\u8fd9\u662f<strong>\u7c7b\u53d8\u91cf<\/strong>\uff08\u7c7b\u4f3cJava\u91cc\u7684\u00a0<code>static<\/code>\u00a0\u5b57\u6bb5\uff0c\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u540c\u4e00\u4e2a\u503c\uff09\u3002<strong>\u8fd9\u662f\u9519\u89c9\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>TypedDict<\/code>&nbsp;\u662fPython\u4e13\u95e8\u7528\u6765&#8221;\u63cf\u8ff0\u5b57\u5178\u7ed3\u6784&#8221;\u7684\u8bed\u6cd5\u7cd6\u3002\u5b83\u91cc\u9762\u5199\u7684\u6240\u6709&nbsp;<code>\u5b57\u6bb5\u540d: \u7c7b\u578b<\/code>&nbsp;\u90fd\u4e0d\u662f\u771f\u6b63\u7684\u53d8\u91cf\u8d4b\u503c\uff0c<strong>\u53ea\u662f\u7ed9\u5b57\u5178\u5b57\u6bb5\u505a\u7684\u7c7b\u578b\u58f0\u660e<\/strong>\u2014\u2014\u544a\u8bc9\u7f16\u8f91\u5668\u548c\u7c7b\u578b\u68c0\u67e5\u5668&#8221;\u8fd9\u79cd\u5b57\u5178\u91cc\u5e94\u8be5\u6709\u8fd9\u4e9b\u5b57\u6bb5\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u662f\u4ec0\u4e48\u7c7b\u578b&#8221;\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4e24\u4e2a\u5bf9\u6bd4\u5c31\u6e05\u695a\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\n# \u274c \u8fd9\u624d\u662f\u771f\u6b63\u7684Python\u7c7b\uff0c\u91cc\u9762\u662f\"\u7c7b\u53d8\u91cf\"\nclass NormalClass:\n    user_input: str = \"\u9ed8\u8ba4\u503c\"     # \u6709 = \u8d4b\u503c\uff0c\u8fd9\u662f\u7c7b\u5c5e\u6027\n\n# \u2705 TypedDict \u91cc\u6ca1\u6709 = \u8d4b\u503c\uff0c\u53ea\u662f\"\u5b57\u6bb5\u58f0\u660e\"\nclass MyState(TypedDict):\n    user_input: str                # \u6ca1\u6709 =\uff0c\u53ea\u662f\u8bf4\"\u8fd9\u79cd\u5b57\u5178\u91cc\u6709\u4e2a\u53ebuser_input\u7684key\uff0c\u503c\u662fstr\u7c7b\u578b\"\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7528\u6cd5\u4e0a&nbsp;<code>MyState<\/code>&nbsp;\u5c31\u662f\u4e00\u4e2a\u5b57\u5178\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\n# \u521b\u5efa\u4e00\u4e2aMyState\"\u5b9e\u4f8b\"\u2014\u2014\u5176\u5b9e\u5c31\u662f\u521b\u5efa\u4e00\u4e2a\u666e\u901a\u5b57\u5178\nstate: MyState = {\"user_input\": \"\u4f60\u597d\"}\n\n# \u8bfb\u53d6\uff1a\u7528\u5b57\u5178\u7684\u4e2d\u62ec\u53f7\u8bed\u6cd5\uff0c\u4e0d\u662f . \u8bed\u6cd5\nprint(state[\"user_input\"])      # \u2705 \u6b63\u786e\n# print(state.user_input)       # \u274c \u9519\u8bef\uff01TypedDict\u4e0d\u662f\u7c7b\uff0c\u6ca1\u6709\u5c5e\u6027\u8bbf\u95ee\n\n# \u5199\u5165\uff1a\u548c\u5b57\u5178\u5b8c\u5168\u4e00\u6837\nstate[\"result\"] = \"\u5df2\u5904\u7406\"\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u5728\u8282\u70b9\u51fd\u6570\u91cc\u4f60\u4f1a\u770b\u5230\u7684\u90fd\u662f&nbsp;<code>state[\"xxx\"]<\/code>&nbsp;\u8fd9\u79cd\u5b57\u5178\u8bbf\u95ee\u5199\u6cd5\uff0c\u4e0d\u662f&nbsp;<code>state.xxx<\/code>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49\u8282\u70b9<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u8282\u70b9\u5c31\u662f\u4e00\u4e2a\u666e\u901a\u7684Python\u51fd\u6570\uff0c\u63a5\u6536\u5f53\u524d\u72b6\u6001\uff0c\u8fd4\u56de\u8981\u66f4\u65b0\u7684\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\ndef process_node(state: MyState) -&gt; dict:\n    \"\"\"\u8282\u70b9\u51fd\u6570\uff1a\u8bfb\u53d6\u72b6\u6001\u4e2d\u7684\u6570\u636e\uff0c\u5904\u7406\u540e\u8fd4\u56de\u8981\u66f4\u65b0\u7684\u5b57\u6bb5\"\"\"\n    user_input = state[\"user_input\"]       # \u4ece\u72b6\u6001\u4e2d\u8bfb\u53d6\n    result = f\"\u5df2\u5904\u7406\uff1a{user_input}\"\n    return {\"result\": result, \"score\": 85}  # \u8fd4\u56de\u8981\u5199\u5165\u72b6\u6001\u7684\u5b57\u6bb5\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8282\u70b9\u51fd\u6570\u7684\u89c4\u5219\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53c2\u6570\uff1a\u63a5\u6536\u6574\u4e2a\u72b6\u6001\u5b57\u5178<\/li>\n\n\n\n<li>\u8fd4\u56de\u503c\uff1a\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u8981\u66f4\u65b0\u7684\u72b6\u6001\u5b57\u6bb5\uff08\u4e0d\u9700\u8981\u8fd4\u56de\u6240\u6709\u5b57\u6bb5\uff0c\u53ea\u8fd4\u56de\u6709\u53d8\u5316\u7684\uff09<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49\u56fe<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528&nbsp;<code>StateGraph<\/code>&nbsp;\u521b\u5efa\u56fe\uff0c\u6dfb\u52a0\u8282\u70b9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\nfrom langgraph.graph import StateGraph, START, END\n\n# \u521b\u5efa\u56fe\uff0c\u6307\u5b9a\u72b6\u6001\u7c7b\u578b\ngraph_builder = StateGraph(MyState)\n\n# \u6dfb\u52a0\u8282\u70b9\uff1a(\u8282\u70b9\u540d\u79f0, \u8282\u70b9\u51fd\u6570)\ngraph_builder.add_node(\"process\", process_node)\ngraph_builder.add_node(\"evaluate\", evaluate_node)\n```<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6dfb\u52a0\u8fb9<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u65e0\u6761\u4ef6\u8fb9\uff1a<\/strong>&nbsp;\u8282\u70b9A\u6267\u884c\u5b8c\u540e\uff0c\u4e00\u5b9a\u6267\u884c\u8282\u70b9B<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\n# START \u2192 process \u2192 evaluate \u2192 END\ngraph_builder.add_edge(START, \"process\")\ngraph_builder.add_edge(\"process\", \"evaluate\")\ngraph_builder.add_edge(\"evaluate\", END)\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6761\u4ef6\u8fb9\uff1a<\/strong>&nbsp;\u6839\u636e\u72b6\u6001\u4e2d\u7684\u6570\u636e\u51b3\u5b9a\u4e0b\u4e00\u6b65\u8d70\u54ea\u4e2a\u8282\u70b9<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\ndef route_function(state: MyState) -&gt; str:\n    \"\"\"\u8def\u7531\u51fd\u6570\uff1a\u6839\u636e\u72b6\u6001\u8fd4\u56de\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u540d\u79f0\"\"\"\n    if state.get(\"score\", 0) &gt;= 80:\n        return \"pass\"\n    return \"retry\"\n\n# \u4eceevaluate\u8282\u70b9\u51fa\u53d1\uff0c\u6839\u636eroute_function\u7684\u8fd4\u56de\u503c\u51b3\u5b9a\u8d70\u54ea\u6761\u8fb9\ngraph_builder.add_conditional_edges(\n    \"evaluate\",              # \u6e90\u8282\u70b9\n    route_function,          # \u8def\u7531\u51fd\u6570\n    {\n        \"pass\": \"finish\",    # \u8def\u7531\u51fd\u6570\u8fd4\u56de\"pass\" \u2192 \u8d70finish\u8282\u70b9\n        \"retry\": \"process\",  # \u8def\u7531\u51fd\u6570\u8fd4\u56de\"retry\" \u2192 \u8d70process\u8282\u70b9\n    }\n)\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e76\u884c\u8fb9\uff1a<\/strong>&nbsp;\u4e00\u4e2a\u8282\u70b9\u6267\u884c\u5b8c\u540e\uff0c\u540c\u65f6\u542f\u52a8\u591a\u4e2a\u8282\u70b9<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\n# geocode\u6267\u884c\u5b8c\u540e\uff0c\u540c\u65f6\u542f\u52a8\u4e09\u4e2a\u8282\u70b9\ngraph_builder.add_edge(\"geocode\", \"search_activities\")\ngraph_builder.add_edge(\"geocode\", \"search_restaurants\")\ngraph_builder.add_edge(\"geocode\", \"search_hotels\")\n\n# \u4e09\u4e2a\u5e76\u884c\u8282\u70b9\u90fd\u5b8c\u6210\u540e\uff0c\u6c47\u805a\u5230assemble\u8282\u70b9\ngraph_builder.add_edge(\n    [\"search_activities\", \"search_restaurants\", \"search_hotels\"],\n    \"assemble\",\n)\n```<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6267\u884c\u56fe<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">```\n# \u521b\u5efa\u56fe\uff0c\u6307\u5b9a\u72b6\u6001\u7c7b\u578b\ngraph_builder = StateGraph(MyState)\n\n# \u6dfb\u52a0\u8282\u70b9\uff1a(\u8282\u70b9\u540d\u79f0, \u8282\u70b9\u51fd\u6570)\ngraph_builder.add_node(\"process\", process_node)\ngraph_builder.add_node(\"evaluate\", evaluate_node)\n\n\n# \u7f16\u8bd1\u56fe\ngraph = graph_builder.compile()\n\n# \u975e\u6d41\u5f0f\u6267\u884c\uff1a\u4e00\u6b21\u6027\u8fd4\u56de\u6574\u4e2a\u5b57\u5178\nresult = graph.invoke({\"user_input\": \"\u4f60\u597d\"})\nprint(result)  # {\"user_input\": \"\u4f60\u597d\", \"result\": \"\u5df2\u5904\u7406\uff1a\u4f60\u597d\", \"score\": 85}\n\n# \u6d41\u5f0f\u6267\u884c\uff1a\u9010\u4e2a\u8282\u70b9\u8fd4\u56de\u4e2d\u95f4\u7ed3\u679c\nfor chunk in graph.stream({\"user_input\": \"\u4f60\u597d\"}, stream_mode=\"updates\"):\n    print(chunk)\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>graph.stream<\/code>&nbsp;\u8fd4\u56de\u7684&nbsp;<code>chunk<\/code>&nbsp;\u5230\u5e95\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f88\u591a\u540c\u5b66\u7b2c\u4e00\u6b21\u770b\u5230&nbsp;<code>for chunk in graph.stream(...)<\/code>&nbsp;\u4f1a\u4ee5\u4e3a\u6bcf\u4e2a&nbsp;<code>chunk<\/code>&nbsp;\u662f\u4e00\u6bb5\u6587\u672c\uff08\u7c7b\u4f3cLLM\u6d41\u5f0f\u8f93\u51fa\u90a3\u79cd\u9010\u5b57\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\uff09\u3002<strong>\u4e0d\u662f<\/strong>\u2014\u2014\u8fd9\u91cc\u7684&nbsp;<code>chunk<\/code>&nbsp;\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5b83\u8bb0\u5f55\u4e86<strong>\u521a\u521a\u6267\u884c\u5b8c\u7684\u90a3\u4e2a\u8282\u70b9\u7684\u66f4\u65b0<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u4f53\u7ed3\u6784\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\n{\n    \"\u8282\u70b9\u540d\": {\n        # \u8fd9\u4e2a\u8282\u70b9\u672c\u6b21\u6267\u884c\u65f6 return \u7684\u5185\u5bb9\n        \"\u5b57\u6bb51\": \u503c1,\n        \"\u5b57\u6bb52\": \u503c2,\n        ...\n    }\n}\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4e2a\u5177\u4f53\u4f8b\u5b50\u3002\u5047\u8bbe\u56fe\u91cc\u6709&nbsp;<code>process<\/code>&nbsp;\u548c&nbsp;<code>evaluate<\/code>&nbsp;\u4e24\u4e2a\u8282\u70b9\uff0c\u4f9d\u6b21\u6267\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\nfor chunk in graph.stream({\"user_input\": \"\u4f60\u597d\"}, stream_mode=\"updates\"):\n    print(chunk)\n\n# \u7b2c1\u6b21\u5faa\u73af\uff08process \u8282\u70b9\u6267\u884c\u5b8c\uff09\uff1a\n# {\"process\": {\"result\": \"\u5df2\u5904\u7406\uff1a\u4f60\u597d\", \"score\": 85}}\n#  \u2191 key\u662f\u8282\u70b9\u540d     \u2191 value\u662f\u8fd9\u4e2a\u8282\u70b9 return \u7684\u5b57\u5178\n\n# \u7b2c2\u6b21\u5faa\u73af\uff08evaluate \u8282\u70b9\u6267\u884c\u5b8c\uff09\uff1a\n# {\"evaluate\": {\"score\": 90}}\n#  \u2191 key\u662f\u8282\u70b9\u540d     \u2191 evaluate\u8282\u70b9\u53ea\u66f4\u65b0\u4e86score\u5b57\u6bb5\n```<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Human-in-the-Loop<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e9b\u6b65\u9aa4\u9700\u8981\u6682\u505c\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\u540e\u518d\u7ee7\u7eed\u3002LangGraph\u901a\u8fc7&nbsp;<code>interrupt()<\/code>&nbsp;\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">```\nfrom langgraph.types import interrupt, Command\nfrom langgraph.checkpoint.memory import MemorySaver\n\ndef ask_user_node(state: MyState) -&gt; dict:\n    \"\"\"\u9700\u8981\u7528\u6237\u8f93\u5165\u7684\u8282\u70b9\"\"\"\n    # interrupt() \u4f1a\u6682\u505c\u56fe\u7684\u6267\u884c\uff0c\u5c06\u53c2\u6570\u4f5c\u4e3a\u4e2d\u65ad\u503c\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\n    user_answer = interrupt(\"\u8bf7\u8f93\u5165\u4f60\u7684\u504f\u597d\uff1a\")\n    # \u7528\u6237\u6062\u590d\u6267\u884c\u540e\uff0cinterrupt() \u8fd4\u56de\u7528\u6237\u7684\u8f93\u5165\n    return {\"user_input\": user_answer}\n\n# \u4f7f\u7528human-in-the-loop\u65f6\uff0c\u5fc5\u987b\u914d\u7f6echeckpointer\uff08\u7528\u4e8e\u4fdd\u5b58\u4e2d\u65ad\u65f6\u7684\u72b6\u6001\uff09\ngraph = graph_builder.compile(checkpointer=MemorySaver())\n\n# \u7b2c\u4e00\u6b21\u6267\u884c\uff1a\u9047\u5230interrupt()\u4f1a\u6682\u505c\nconfig = {\"configurable\": {\"thread_id\": \"user-123\"}}\nfor chunk in graph.stream({\"user_input\": \"\u5f00\u59cb\"}, config=config):\n    print(chunk)  # \u6267\u884c\u5230ask_user_node\u65f6\u6682\u505c\uff0c\u8fd4\u56de\u4e2d\u65ad\u503c\n\n# \u7528\u6237\u8f93\u5165\u540e\uff0c\u6062\u590d\u6267\u884c\nfor chunk in graph.stream(\n    Command(resume=\"\u6211\u559c\u6b22\u5b89\u9759\u7684\u5730\u65b9\"),  # \u4f20\u5165\u7528\u6237\u7684\u56de\u7b54\n    config=config,                        # \u5fc5\u987b\u7528\u540c\u4e00\u4e2athread_id\n):\n    print(chunk)  # \u4ece\u4e2d\u65ad\u5904\u7ee7\u7eed\u6267\u884c\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>MemorySaver<\/code>\u00a0\u662f\u5185\u5b58\u4e2d\u7684\u72b6\u6001\u4fdd\u5b58\u5668\uff0c\u7528\u4e8e\u5728\u4e2d\u65ad\u671f\u95f4\u4fdd\u5b58\u56fe\u7684\u6267\u884c\u72b6\u6001\u3002<code>thread_id<\/code>\u00a0\u7528\u4e8e\u9694\u79bb\u4e0d\u540c\u7528\u6237\u7684\u4f1a\u8bdd\u3002<\/li>\n\n\n\n<li>\u5728\u4f7f\u7528\u4e86HIL\u4e4b\u540e\uff0c\u5728graph\u6267\u884c\u65f6\u9700\u8981\u4f20\u9012config\u53c2\u6570\uff0c\u56e0\u4e3a\u9700\u8981\u901a\u8fc7thread\u2014\u2014id\u6765\u56de\u590d\u4e2d\u65ad\u524d\u7684\u72b6\u6001<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u7efc\u5408\u6848\u4f8b\uff1a\u5916\u5356\u8ba2\u5355\u5904\u7406<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u6848\u4f8b\u628a\u4ee5\u4e0a\u6240\u6709\u8bed\u6cd5\u70b9\u4e32\u8d77\u6765\u3002\u573a\u666f\uff1a\u4e00\u4e2a\u5916\u5356\u8ba2\u5355\u5904\u7406\u7cfb\u7edf\u7684\u540e\u7aef\u63a5\u53e3\uff0c\u5305\u542b3\u4e2a\u8282\u70b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e1a\u52a1\u6d41\u7a0b\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>receive_order<\/code>\uff1a\u63a5\u6536\u8ba2\u5355\uff0c\u63d0\u53d6\u83dc\u54c1\u548c\u5730\u5740<\/li>\n\n\n\n<li><code>confirm_order<\/code>\uff08HITL\uff09\uff1a\u5411\u7528\u6237\u786e\u8ba4\u8ba2\u5355\u4fe1\u606f\uff0c\u7b49\u5f85\u7528\u6237\u786e\u8ba4\u6216\u4fee\u6539<\/li>\n\n\n\n<li><code>dispatch_order<\/code>\uff1a\u6839\u636e\u786e\u8ba4\u7ed3\u679c\u51b3\u5b9a\u6d3e\u5355\u6216\u53d6\u6d88<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/larkdrop.cafe\/wp-content\/uploads\/2026\/07\/image-21.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"931\" height=\"129\" data-original=\"https:\/\/larkdrop.cafe\/wp-content\/uploads\/2026\/07\/image-21.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-743\"  sizes=\"auto, (max-width: 931px) 100vw, 931px\" \/><\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u56fe\u7684\u5b9a\u4e49<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">```\nfrom typing import TypedDict, NotRequired\nfrom langgraph.graph import StateGraph, START, END\nfrom langgraph.types import interrupt\nfrom langgraph.checkpoint.memory import MemorySaver\n\n\n# 1. \u5b9a\u4e49\u72b6\u6001\nclass OrderState(TypedDict):\n    raw_message: str                      # \u7528\u6237\u539f\u59cb\u6d88\u606f\n    dish: str                # \u83dc\u54c1\u540d\u79f0\n    address: str             # \u914d\u9001\u5730\u5740\n    confirmed: bool          # \u7528\u6237\u662f\u5426\u786e\u8ba4\n    dispatch_result: str     # \u6d3e\u5355\u7ed3\u679c\n\n\n# 2. \u5b9a\u4e49\u8282\u70b9\ndef receive_order(state: OrderState) -&gt; dict:\n    \"\"\"\u89e3\u6790\u7528\u6237\u6d88\u606f\uff0c\u63d0\u53d6\u83dc\u54c1\u548c\u5730\u5740\"\"\"\n    message = state[\"raw_message\"]\n    # \u7b80\u5355\u6a21\u62df\u89e3\u6790\uff08\u5b9e\u9645\u9879\u76ee\u4e2d\u4f1a\u7528LLM\uff09\n    return {\"dish\": \"\u5bab\u4fdd\u9e21\u4e01\", \"address\": \"\u671b\u4eacSOHO\"}\n\n\ndef confirm_order(state: OrderState) -&gt; dict:\n    \"\"\"\u6682\u505c\u6267\u884c\uff0c\u7b49\u5f85\u7528\u6237\u786e\u8ba4\u8ba2\u5355\"\"\"\n    prompt = f\"\u60a8\u7684\u8ba2\u5355\uff1a{state['dish']}\uff0c\u914d\u9001\u5230\uff1a{state['address']}\u3002\u786e\u8ba4\u4e0b\u5355\u5417\uff1f(yes\/no)\"\n    answer = interrupt(prompt)  # \u6682\u505c\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\n    return {\"confirmed\": answer.strip().lower() in (\"yes\", \"\u662f\", \"\u786e\u8ba4\")}\n\n\ndef dispatch_order(state: OrderState) -&gt; dict:\n    \"\"\"\u6d3e\u5355\"\"\"\n    return {\"dispatch_result\": f\"{state['dish']} \u5df2\u6d3e\u5355\u5230 {state['address']}\"}\n\n\n# 3. \u8def\u7531\u51fd\u6570\uff1a\u6839\u636e\u786e\u8ba4\u7ed3\u679c\u51b3\u5b9a\u4e0b\u4e00\u6b65\ndef route_after_confirm(state: OrderState) -&gt; str:\n    if state.get(\"confirmed\"):\n        return \"dispatch\"\n    return \"cancel\"\n\n\n# 4. \u6784\u5efa\u56fe\nbuilder = StateGraph(OrderState)\nbuilder.add_node(\"receive_order\", receive_order)\nbuilder.add_node(\"confirm_order\", confirm_order)\nbuilder.add_node(\"dispatch_order\", dispatch_order)\n\nbuilder.add_edge(START, \"receive_order\")           # \u65e0\u6761\u4ef6\u8fb9\nbuilder.add_edge(\"receive_order\", \"confirm_order\") # \u65e0\u6761\u4ef6\u8fb9\nbuilder.add_conditional_edges(                      # \u6761\u4ef6\u8fb9\n    \"confirm_order\",\n    route_after_confirm,\n    {\"dispatch\": \"dispatch_order\", \"cancel\": END},\n)\nbuilder.add_edge(\"dispatch_order\", END)\n\n# 5. \u7f16\u8bd1\uff08\u5e26checkpointer\uff0c\u652f\u6301HITL\u4e2d\u65ad\u6062\u590d\uff09\ngraph = builder.compile(checkpointer=MemorySaver())\n```<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Web\u63a5\u53e3\u5b9e\u73b0<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">```\nimport uuid\nimport json\nfrom fastapi import FastAPI\nfrom fastapi.responses import StreamingResponse\nfrom pydantic import BaseModel\n\napp = FastAPI()\n\n\nclass RunRequest(BaseModel):\n    threadId: str\n    input: dict | None = None       # \u9996\u6b21\u6267\u884c\u65f6\u4f20\u5165\u521d\u59cb\u6570\u636e\n    resume: str | None = None       # \u6062\u590d\u6267\u884c\u65f6\u4f20\u5165\u7528\u6237\u7684\u56de\u7b54\n\n\n@app.post(\"\/api\/threads\")\ndef create_thread():\n    \"\"\"\u521b\u5efa\u65b0\u7684\u4f1a\u8bdd\u7ebf\u7a0b\"\"\"\n    return {\"threadId\": str(uuid.uuid4())}\n\n\n@app.post(\"\/api\/orders\/stream\")\ndef stream_order(req: RunRequest):\n    \"\"\"\u6d41\u5f0f\u6267\u884c\u56fe\uff0c\u901a\u8fc7SSE\u63a8\u9001\u4e8b\u4ef6\"\"\"\n    config = {\"configurable\": {\"thread_id\": req.threadId}}\n\n    def event_stream():\n        # \u5224\u65ad\u662f\u9996\u6b21\u6267\u884c\u8fd8\u662f\u6062\u590d\u6267\u884c\n        if req.input:\n            graph_input = req.input                    # \u9996\u6b21\uff1a\u4f20\u5165\u521d\u59cb\u6570\u636e\n        else:\n            graph_input = Command(resume=req.resume)   # \u6062\u590d\uff1a\u4f20\u5165\u7528\u6237\u56de\u7b54\n\n        # stream_mode=\"updates\" \u6bcf\u4e2a\u8282\u70b9\u5b8c\u6210\u540e\u63a8\u9001\u4e00\u6b21\n        for chunk in graph.stream(graph_input, config=config, stream_mode=\"updates\"):\n            # \u68c0\u67e5\u662f\u5426\u9047\u5230\u4e2d\u65ad\uff08HITL\u8282\u70b9\u6682\u505c\uff09\n            if \"__interrupt\" in chunk:\n                interrupt_data = chunk[\"__interrupt\"][0]\n                yield f\"data: {json.dumps({'type': 'interrupt', 'value': interrupt_data['value']})}\\n\\n\"\n                return  # \u4e2d\u65ad\u540e\u505c\u6b62\uff0c\u7b49\u5f85\u524d\u7aef\u4e0b\u6b21\u8bf7\u6c42\u6062\u590d\n\n            # \u63a8\u9001\u8282\u70b9\u5b8c\u6210\u4e8b\u4ef6\n            for node_name in chunk:\n                if not node_name.startswith(\"__\"):\n                    yield f\"data: {json.dumps({'type': 'node', 'name': node_name})}\\n\\n\"\n\n        yield f\"data: {json.dumps({'type': 'done'})}\\n\\n\"\n\n    return StreamingResponse(event_stream(), media_type=\"text\/event-stream\")\n```<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u524d\u7aef\u4ea4\u4e92\u6d41\u7a0b\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u8c03\u7528\u00a0<code>POST \/api\/threads<\/code>\u00a0\u2192 \u83b7\u5f97\u00a0<code>threadId<\/code><\/li>\n\n\n\n<li>\u8c03\u7528\u00a0<code>POST \/api\/orders\/stream<\/code>\uff0cbody:\u00a0<code>{threadId, input: {raw_message: \"\u4e00\u4efd\u5bab\u4fdd\u9e21\u4e01\u9001\u5230\u671b\u4eacSOHO\"}}<\/code><\/li>\n\n\n\n<li>\u6536\u5230SSE\u4e8b\u4ef6\uff1a<code>{type: \"node\", name: \"receive_order\"}<\/code>\u00a0\u2192 \u8ba2\u5355\u5df2\u89e3\u6790<\/li>\n\n\n\n<li>\u6536\u5230SSE\u4e8b\u4ef6\uff1a<code>{type: \"interrupt\", value: \"\u60a8\u7684\u8ba2\u5355\uff1a\u5bab\u4fdd\u9e21\u4e01...\u786e\u8ba4\u4e0b\u5355\u5417\uff1f\"}<\/code>\u00a0\u2192 \u56fe\u6682\u505c\u4e86<\/li>\n\n\n\n<li>\u524d\u7aef\u5c55\u793a\u786e\u8ba4\u63d0\u793a\uff0c\u7528\u6237\u70b9\u51fb&#8221;\u786e\u8ba4&#8221;<\/li>\n\n\n\n<li>\u8c03\u7528\u00a0<code>POST \/api\/orders\/stream<\/code>\uff0cbody:\u00a0<code>{threadId, resume: \"yes\"}<\/code>\u00a0\u2192 \u6062\u590d\u6267\u884c<\/li>\n\n\n\n<li>\u6536\u5230SSE\u4e8b\u4ef6\uff1a<code>{type: \"node\", name: \"dispatch_order\"}<\/code>\u00a0\u2192 \u6d3e\u5355\u5b8c\u6210<\/li>\n\n\n\n<li>\u6536\u5230SSE\u4e8b\u4ef6\uff1a<code>{type: \"done\"}<\/code>\u00a0\u2192 \u56fe\u6267\u884c\u7ed3\u675f<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Python\u4e2d\uff0c\u8fd9\u79cd\u590d\u6742\u7684workflow\u7f16\u6392\u53ef\u4ee5\u4f7f\u7528&nbsp;LangGraph&nbsp;\u6846\u67b6\u6765\u5b9e\u73b0\u3002 [&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":[10],"class_list":["post-742","post","type-post","status-publish","format-standard","hentry","category-learn","tag-ai"],"_links":{"self":[{"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/posts\/742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/comments?post=742"}],"version-history":[{"count":1,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/posts\/742\/revisions"}],"predecessor-version":[{"id":744,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/posts\/742\/revisions\/744"}],"wp:attachment":[{"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/media?parent=742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/categories?post=742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/tags?post=742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}