{"id":3872,"date":"2026-08-27T07:42:31","date_gmt":"2026-08-26T23:42:31","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=3872"},"modified":"2026-08-27T07:43:14","modified_gmt":"2026-08-26T23:43:14","slug":"langchain%e6%a1%86%e6%9e%b6%e4%b9%8bmiddleware-%e7%94%a8%e9%92%a9%e5%ad%90%e6%8e%a7%e5%88%b6agent%e8%bf%90%e8%a1%8c%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=3872","title":{"rendered":"LangChain\u6846\u67b6\u4e4bMiddleware-\u7528\u94a9\u5b50\u63a7\u5236Agent\u8fd0\u884c\u8fc7\u7a0b"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u4e0a\u7bc7\u4f60\u5206\u6e05\u4e86 State \/ Store \/ Context\u2014\u2014\u4e09\u79cd\u300c\u8bb0\u4f4f\u300d\u5404\u7ba1\u4e00\u6bb5\u3002\u53ef\u8fd8\u6709\u6a2a\u5207\u9700\u6c42\uff1a\u90ae\u7bb1\u522b\u8fdb\u6a21\u578b\u3001\u522b\u8ba9\u5de5\u5177\u6b7b\u5faa\u73af\u70e7\u94b1\u3001\u5386\u53f2\u592a\u957f\u8981\u6458\u8981\u3001\u53d1\u4fe1\u524d\u8981\u4eba\u5ba1\u3002\u8fd9\u4e9b\u4e0d\u8be5\u9760\u300c\u591a\u5199\u51e0\u4e2a tool\u300d\u78b0\u8fd0\u6c14\u3002\u8fd9\u7bc7\u8bb2 <strong>Middleware<\/strong>\uff1a\u5728 Agent \u5faa\u73af\u7684\u94a9\u5b50\u4e0a\u5d4c\u5165\u9884\u5b9a\u4e49\uff08\u4ee5\u53ca\u53ef\u81ea\u5b9a\u4e49\uff09\u903b\u8f91\u3002<\/p>\n<\/blockquote>\n<hr \/>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u5c0f\u660e\u60f3\u300c\u5077\u5077\u300d\u6539 Agent \u884c\u4e3a<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u79c1\u53a8\u4e0a\u7ebf\u540e\uff0c\u5408\u89c4\u540c\u5b66\u8bf4\uff1a<\/p>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\u7528\u6237\u6d88\u606f\u91cc\u7684\u90ae\u7bb1\u5fc5\u987b\u8131\u654f\uff0c\u4e0d\u80fd\u539f\u6837\u8fdb\u6a21\u578b\u3002\n<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u8fd0\u7ef4\u53c8\u8bf4\uff1a<\/p>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\u6709\u4eba\u6545\u610f\u8ba9\u00a0Agent\u00a0\u8fde\u8c03\u5de5\u5177\u00a050\u00a0\u6b21\uff0c\u8d26\u5355\u70b8\u4e86\u3002\n<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5c0f\u660e\u7b2c\u4e00\u53cd\u5e94\uff1a\u5728 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">chat()<\/code> \u91cc\u5199 if\u3001\u5728\u5de5\u5177\u91cc\u5199\u8ba1\u6570\u5668\u3002\u6539\u4e86\u4e09\u5904\uff0c\u8fd8\u6709\u6f0f\u7f51\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u4ed6\u53bb\u627e\u8001\u5f20\uff1a&#8221;\u6709\u6ca1\u6709\u6302\u5728\u6846\u67b6\u4e0a\u3001\u6bcf\u6b21\u8c03\u6a21\u578b\u524d\u540e\u90fd\u4f1a\u8dd1\u7684\u62e6\u622a\u5668\uff1f&#8221;<\/p>\n<p style=\"margin: 1em 0px;\">\u8001\u5f20\u8bf4\uff1a&#8221;\u6709\u3002Middleware \u5728\u667a\u80fd\u4f53\u8fd0\u884c\u5404\u8fc7\u7a0b\u9884\u7559 <strong>hook<\/strong>\uff0c\u65b9\u4fbf\u5d4c\u5165\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002\u53ef\u4ee5\u81ea\u5df1\u5199\uff0c\u4e5f\u53ef\u4ee5\u5148\u7528 LangChain <strong>Prebuilt<\/strong>\u3002&#8221;<\/p>\n<hr \/>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4ec0\u4e48\u662f Middleware<\/span><\/h2>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">agent.invoke\n\u00a0\u00a0\u2192\u00a0middleware\u00a0before_*\n\u00a0\u00a0\u2192\u00a0\u8c03\u7528\u6a21\u578b\n\u00a0\u00a0\u2192\u00a0middleware\u00a0after_*\n\u00a0\u00a0\u00a0\u00a0\u00a0\u251c\u2500\u00a0\u6709\u00a0tool_calls\u00a0\u2192\u00a0\u6267\u884c\u5de5\u5177\u00a0\u2192\u00a0\u518d\u56de\u00a0before_*\n\u00a0\u00a0\u00a0\u00a0\u00a0\u2514\u2500\u00a0\u65e0\u00a0tool_calls\u00a0\u2192\u00a0\u8fd4\u56de\n<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">&#8220;\u5b83\u4e0d\u662f\u53e6\u4e00\u5957 Runtime\uff0c\u800c\u662f\u63d2\u5728\u6807\u51c6\u5faa\u73af\u4e0a\u7684\u6a2a\u5207\u903b\u8f91\uff1a\u5b89\u5168\u3001\u9650\u6d41\u3001\u6458\u8981\u3001HITL\u2026\u2026<\/p>\n<p style=\"margin: 1em 0px;\">\u6302\u8f7d\u5c31\u4e00\u884c\u5217\u8868\uff1a&#8221;<\/p>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs language-python\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">agent\u00a0=\u00a0create_agent(\n\u00a0\u00a0model,\n\u00a0\u00a0tools=[...],\n\u00a0\u00a0middleware=[\n\u00a0\u00a0\u00a0\u00a0PIIMiddleware(<span class=\"hljs-string\" style=\"color: #859900;\">\"email\"<\/span>,\u00a0strategy=<span class=\"hljs-string\" style=\"color: #859900;\">\"redact\"<\/span>,\u00a0apply_to_input=<span class=\"hljs-literal\" style=\"color: #cb4b16;\">True<\/span>),\n\u00a0\u00a0],\n)\n<\/code><\/pre>\n<table style=\"border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; text-align: left; overflow: auto; display: table; overflow-wrap: break-word; word-break: break-all; height: 283px;\" width=\"713\">\n<thead>\n<tr>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u4e2d\u95f4\u4ef6\uff08\u8282\u9009\uff09<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u4f5c\u7528<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u672c\u8bfe<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\"><code>PIIMiddleware<\/code><\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u68c0\u6d4b\/\u8131\u654f PII<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">Demo A<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\"><code>ModelCallLimitMiddleware<\/code><\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u9650\u5236\u6a21\u578b\u8c03\u7528\u6b21\u6570<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">Demo B<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\"><code>SummarizationMiddleware<\/code><\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u5386\u53f2\u8fc7\u957f\u81ea\u52a8\u6458\u8981<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">Demo C<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\"><code>HumanInTheLoopMiddleware<\/code><\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u5de5\u5177\u524d\u4eba\u5de5\u5ba1\u6279<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">Day12 \u5bf9\u7167<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">ToolRetry \/ ModelFallback \u7b49<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u91cd\u8bd5\u3001\u964d\u7ea7<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u8ba4\u77e5\u7ea7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u5b98\u65b9\u6e05\u5355\uff1a<a style=\"text-decoration: none; overflow-wrap: break-word; font-weight: bold; color: #ef7060; border-bottom: 1px solid #ef7060;\" href=\"https:\/\/docs.langchain.com\/oss\/python\/langchain\/middleware\/built-in\">https:\/\/docs.langchain.com\/oss\/python\/langchain\/middleware\/built-in<\/a><\/p>\n<\/blockquote>\n<table style=\"border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; text-align: left; overflow: auto; display: table; overflow-wrap: break-word; word-break: break-all; height: 163px;\" width=\"708\">\n<thead>\n<tr>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\"><\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">Tool \u6539 State\/Store<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">Middleware<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u65f6\u673a<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u6a21\u578b\u9009\u4e2d\u5de5\u5177\u65f6<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u6bcf\u6b21\u6a21\u578b\/\u5de5\u5177\u6b65\u9aa4\u524d\u540e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u5178\u578b\u7528\u9014<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u8bfb\u5199\u8bb0\u5fc6<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u8131\u654f\u3001\u9650\u6d41\u3001\u6458\u8981\u3001\u4eba\u5ba1<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u662f\u5426\u4f9d\u8d56\u6a21\u578b\u9009\u578b<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u662f<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u5426\uff08\u6846\u67b6\u5f3a\u5236\u6267\u884c\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin: 1em 0px;\">&#8220;\u9ad8\u7ea7\u7528\u6cd5\u91cc\uff0c\u5bfc\u56fe\u70b9\u5230<strong>\u52a8\u6001\u4fee\u6539\u8bf7\u6c42\u53c2\u6570<\/strong>\uff08\u5982 RAG \u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">@dynamic_prompt<\/code> \u6bcf\u6b21\u68c0\u7d22\u540e\u6539 Prompt\uff09\u2014\u2014\u672c\u8d28\u4e5f\u662f\u94a9\u5b50\u91cc\u6539\u53d1\u7ed9\u6a21\u578b\u7684\u5185\u5bb9\u3002\u672c\u7bc7\u5148\u628a Prebuilt \u8dd1\u901a\uff1b\u81ea\u5b9a\u4e49 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">before_model<\/code> \u6df1\u6316\u53ef\u540e\u7f6e\u3002&#8221;<\/p>\n<hr \/>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4ee3\u7801\u62c6\u89e3\uff1a\u4e09\u4e2a Prebuilt + HITL \u5bf9\u7167<\/span><\/h2>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">1. Demo A\uff1aPII \u8131\u654f<\/h3>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs language-python\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"><span class=\"hljs-keyword\" style=\"color: #6c71c4;\">def<\/span>\u00a0<span class=\"hljs-title function_\" style=\"color: #268bd2;\">build_pii_agent<\/span>():\n\u00a0\u00a0<span class=\"hljs-keyword\" style=\"color: #6c71c4;\">return<\/span>\u00a0create_agent(\n\u00a0\u00a0\u00a0\u00a0get_chat_model(),\n\u00a0\u00a0\u00a0\u00a0tools=[],\n\u00a0\u00a0\u00a0\u00a0system_prompt=<span class=\"hljs-string\" style=\"color: #859900;\">\"\u4f60\u662f\u7b80\u6d01\u52a9\u6559\u3002\u7528\u6237\u8ba9\u4f60\u590d\u8ff0\u4fe1\u606f\u65f6\uff0c\u6309\u6536\u5230\u7684\u6587\u672c\u5982\u5b9e\u590d\u8ff0\u3002\"<\/span>,\n\u00a0\u00a0\u00a0\u00a0middleware=[\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PIIMiddleware(<span class=\"hljs-string\" style=\"color: #859900;\">\"email\"<\/span>,\u00a0strategy=<span class=\"hljs-string\" style=\"color: #859900;\">\"redact\"<\/span>,\u00a0apply_to_input=<span class=\"hljs-literal\" style=\"color: #cb4b16;\">True<\/span>),\n\u00a0\u00a0\u00a0\u00a0],\n\u00a0\u00a0)\n<\/code><\/pre>\n<p style=\"margin: 1em 0px;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">main.py<\/code> \u5bf9\u6bd4\u300c\u8fdb\u6a21\u578b\u524d\u300d\u4e0e\u56de\u590d\uff1a<\/p>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs language-python\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">raw\u00a0=\u00a0<span class=\"hljs-string\" style=\"color: #859900;\">\"\u6211\u7684\u90ae\u7bb1\u662f\u00a0alice@example.com\uff0c\u8bf7\u539f\u6837\u590d\u8ff0\u90ae\u7bb1\u3002\"<\/span>\nresult\u00a0=\u00a0agent.invoke({<span class=\"hljs-string\" style=\"color: #859900;\">\"messages\"<\/span>:\u00a0[{<span class=\"hljs-string\" style=\"color: #859900;\">\"role\"<\/span>:\u00a0<span class=\"hljs-string\" style=\"color: #859900;\">\"user\"<\/span>,\u00a0<span class=\"hljs-string\" style=\"color: #859900;\">\"content\"<\/span>:\u00a0raw}]})\nhuman\u00a0=\u00a0result[<span class=\"hljs-string\" style=\"color: #859900;\">\"messages\"<\/span>][<span class=\"hljs-number\" style=\"color: #cb4b16;\">0<\/span>]\n<span class=\"hljs-built_in\" style=\"color: #2aa198;\">print<\/span>(<span class=\"hljs-string\" style=\"color: #859900;\">\"\u8fdb\u5165\u6a21\u578b\u7684\u00a0HumanMessage:\"<\/span>,\u00a0human.content)\n<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">&#8220;\u671f\u671b\uff1a<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">HumanMessage<\/code> \u91cc\u51fa\u73b0 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">[REDACTED_EMAIL]<\/code> \u4e00\u7c7b\u5360\u4f4d\u3002\u8fd9\u5c31\u662f<strong>\u5f00\u542f\u4e2d\u95f4\u4ef6\u524d\u540e\u7684\u884c\u4e3a\u5dee<\/strong>\u2014\u2014\u9a8c\u6536\u70b9\u3002&#8221;<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">2. Demo B\uff1a\u6a21\u578b\u8c03\u7528\u4e0a\u9650<\/h3>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs language-python\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\"><span class=\"hljs-keyword\" style=\"color: #6c71c4;\">def<\/span>\u00a0<span class=\"hljs-title function_\" style=\"color: #268bd2;\">build_limit_agent<\/span>():\n\u00a0\u00a0<span class=\"hljs-keyword\" style=\"color: #6c71c4;\">return<\/span>\u00a0create_agent(\n\u00a0\u00a0\u00a0\u00a0get_chat_model(),\n\u00a0\u00a0\u00a0\u00a0tools=TOOLS,\n\u00a0\u00a0\u00a0\u00a0system_prompt=<span class=\"hljs-string\" style=\"color: #859900;\">\"\u4f60\u662f\u7b97\u672f\u52a9\u6559\u3002\u51e1\u52a0\u51cf\u5fc5\u987b\u8c03\u7528\u00a0add\u00a0\u5de5\u5177\uff0c\u4e0d\u8981\u5fc3\u7b97\u3002\"<\/span>,\n\u00a0\u00a0\u00a0\u00a0checkpointer=InMemorySaver(),\n\u00a0\u00a0\u00a0\u00a0middleware=[\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ModelCallLimitMiddleware(run_limit=<span class=\"hljs-number\" style=\"color: #cb4b16;\">1<\/span>,\u00a0exit_behavior=<span class=\"hljs-string\" style=\"color: #859900;\">\"end\"<\/span>),\n\u00a0\u00a0\u00a0\u00a0],\n\u00a0\u00a0)\n<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">&#8220;\u6545\u610f\u51fa\u9898\uff1a\u5fc5\u987b\u4e24\u6b21\u8c03\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">add<\/code>\u3002<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">run_limit=1<\/code> \u53ea\u5141\u8bb8\u5355\u6b21 invoke \u5185 <strong>1 \u6b21<\/strong>\u6a21\u578b\u8c03\u7528\uff0c\u591a\u6b65\u4efb\u52a1\u4f1a\u88ab\u622a\u65ad\u5e76\u4f18\u96c5 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">end<\/code>\uff0c\u907f\u514d\u6b7b\u5faa\u73af\u70e7\u94b1\u3002<br \/>\n<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">thread_limit<\/code> \u8de8\u591a\u6b21 invoke \u7d2f\u8ba1\uff0c\u901a\u5e38\u8981\u914d checkpointer\u3002&#8221;<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">3. Demo C\uff1a\u81ea\u52a8\u6458\u8981<\/h3>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs language-python\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">SummarizationMiddleware(\n\u00a0\u00a0model=model,\n\u00a0\u00a0trigger=(<span class=\"hljs-string\" style=\"color: #859900;\">\"messages\"<\/span>,\u00a0<span class=\"hljs-number\" style=\"color: #cb4b16;\">6<\/span>),\n\u00a0\u00a0keep=(<span class=\"hljs-string\" style=\"color: #859900;\">\"messages\"<\/span>,\u00a0<span class=\"hljs-number\" style=\"color: #cb4b16;\">2<\/span>),\n)\n<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">&#8220;\u540c\u4e00 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">thread_id<\/code> \u591a\u8f6e\u540e\u89e6\u53d1\u6458\u8981\uff0c\u538b\u7f29\u65e7\u6d88\u606f\u3001\u4fdd\u7559\u6700\u8fd1\u82e5\u5e72\u6761\u3002\u6559\u5b66\u7528\u4f4e\u9608\u503c\uff1b\u751f\u4ea7\u5e38\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">(\\\"tokens\\\", N)<\/code>\u3002\u8fd9\u548c Memory \u7bc7\u7684\u300c\u603b\u7ed3\u7b56\u7565\u300d\u662f\u540c\u4e00\u5757\u808c\u8089\u3002&#8221;<\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">4. HITL \u5bf9\u7167\uff08\u8ba4\u77e5\u5373\u53ef\uff09<\/h3>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs language-python\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">HumanInTheLoopMiddleware(\n\u00a0\u00a0interrupt_on={\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-string\" style=\"color: #859900;\">\"send_email\"<\/span>:\u00a0{<span class=\"hljs-string\" style=\"color: #859900;\">\"allowed_decisions\"<\/span>:\u00a0[<span class=\"hljs-string\" style=\"color: #859900;\">\"approve\"<\/span>,\u00a0<span class=\"hljs-string\" style=\"color: #859900;\">\"edit\"<\/span>,\u00a0<span class=\"hljs-string\" style=\"color: #859900;\">\"reject\"<\/span>]},\n\u00a0\u00a0\u00a0\u00a0<span class=\"hljs-string\" style=\"color: #859900;\">\"read_email\"<\/span>:\u00a0<span class=\"hljs-literal\" style=\"color: #cb4b16;\">False<\/span>,\n\u00a0\u00a0}\n)\n<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">&#8220;\u8bfb\u4fe1\u81ea\u52a8\u653e\u884c\uff0c\u53d1\u4fe1\u5fc5\u987b\u4eba\u5ba1\u3002\u9700\u8981 checkpointer + <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">Command(resume=...)<\/code>\u3002\u672c\u7bc7\u77e5\u9053\uff1a<strong>\u5371\u9669\u5de5\u5177\u53ef\u4ee5\u6302 HITL \u4e2d\u95f4\u4ef6<\/strong>\u3002&#8221;<\/p>\n<p style=\"margin: 1em 0px;\">\u5c0f\u660e\u590d\u8ff0\uff1a&#8221;\u4e2d\u95f4\u4ef6\u662f\u94a9\u5b50\u5217\u8868\uff1bPII \u6539\u8f93\u5165\uff0cLimit \u5361\u6b21\u6570\uff0cSummarize \u538b\u5386\u53f2\uff1b\u53d1\u4fe1\u7528\u4eba\u5ba1\u3002&#8221;<\/p>\n<hr \/>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u603b\u7ed3<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u8001\u5f20\u8bf4\uff1a&#8221;\u7b2c\u5341\u7bc7\u53ea\u529e\u4e00\u4ef6\u4e8b\u2014\u2014\u7ed9 Agent \u5faa\u73af\u88c5\u4e0a\u53ef\u63d2\u62d4\u94a9\u5b50\u3002&#8221;<\/p>\n<p style=\"margin: 1em 0px;\">&#8220;\u4e09\u4e2a\u6838\u5fc3\u7406\u89e3\uff1a<\/p>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: decimal;\">\n<li>\n<section><strong>Middleware = \u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684 hook<\/strong> \u2014\u2014 \u4e0d\u662f\u65b0 Runtime\uff0c\u662f\u6a2a\u5207\u589e\u5f3a<\/section>\n<\/li>\n<li>\n<section><strong>\u5148\u7528 Prebuilt<\/strong> \u2014\u2014 PII \/ ModelCallLimit \/ Summarization \u8986\u76d6\u5b89\u5168\u3001\u6210\u672c\u3001\u4e0a\u4e0b\u6587<\/section>\n<\/li>\n<li>\n<section><strong>\u53ef\u7ec4\u5408\u3001\u53ef\u8fdb\u9636<\/strong> \u2014\u2014 \u5217\u8868\u53e0\u52a0\uff1bHITL\u3001\u52a8\u6001\u6539 Prompt \u5728\u540e\u7eed\u573a\u666f\u5c55\u5f00&#8221;<\/section>\n<\/li>\n<\/ol>\n<p style=\"margin: 1em 0px;\">LangChain \u652f\u7ebf\u8fdb\u5ea6\uff1a<\/p>\n<section style=\"display: block; background-repeat: no-repeat; width: 100%; height: 16px;\"><\/section>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\u2026\u00a0\u2192\u00a0Runtime\n\u00a0\u00a0\u00a0\u00a0\u2193\nMiddleware\uff08\u672c\u7bc7\uff1aPrebuilt\u00a0\u94a9\u5b50\uff09\n\u00a0\u00a0\u00a0\u00a0\u2193\n\u591a\u00a0Agent\u00a0\u2192\u00a0Agentic\u00a0RAG\u00a0\u2026\n<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u5c0f\u660e\u8bf4\uff1a&#8221;\u6a2a\u5207\u80fd\u529b\u6709\u4e86\u3002\u590d\u6742\u6d41\u7a0b\u82e5\u8981\u7814\u7a76\u5458+\u5199\u624b+\u7f16\u8f91\uff0c\u5c31\u8be5\u591a Agent \u4e86\u3002&#8221;<\/p>\n<p style=\"margin: 1em 0px;\">&#8220;\u5bf9\u3002\u4e0b\u4e00\u7bc7\uff1a<strong>\u591a Agent \u534f\u4f5c<\/strong>\u3002&#8221;<\/p>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>\u80fd\u6539\u5de5\u5177\u7ed3\u679c\uff0c\u4e0d\u5982\u5728\u6846\u67b6\u94a9\u5b50\u4e0a\u7edf\u4e00\u6539\u884c\u4e3a\u3002Middleware \u8ba9\u5b89\u5168\u4e0e\u6210\u672c\u7b56\u7565\u53d8\u6210\u914d\u7f6e\uff0c\u800c\u4e0d\u662f\u6563\u843d\u7684 if\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u7bc7\u4f60\u5206\u6e05\u4e86 State \/ Store \/ Context\u2014\u2014\u4e09\u79cd\u300c\u8bb0\u4f4f\u300d\u5404\u7ba1\u4e00\u6bb5\u3002\u53ef\u8fd8\u6709\u6a2a\u5207\u9700\u6c42\uff1a\u90ae\u7bb1\u522b\u8fdb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3873,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[163,165],"tags":[167,168,170],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3872"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3872"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3872\/revisions"}],"predecessor-version":[{"id":3875,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3872\/revisions\/3875"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/media\/3873"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}