{"id":124702,"date":"2026-11-19T10:32:59","date_gmt":"2026-11-19T02:32:59","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/124702.html"},"modified":"2026-11-19T10:32:59","modified_gmt":"2026-11-19T02:32:59","slug":"langchain-agent-%e4%b8%89%e5%a4%a7%e6%95%b0%e6%8d%ae%e7%ae%a1%e7%90%86%e6%9c%ba%e5%88%b6%ef%bc%9acontext%e3%80%81state-%e4%b8%8e-store-%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/124702.html","title":{"rendered":"LangChain Agent \u4e09\u5927\u6570\u636e\u7ba1\u7406\u673a\u5236\uff1aContext\u3001State \u4e0e Store \u8be6\u89e3"},"content":{"rendered":"<p>        \/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/<br \/>\n        #content_views .toc,<br \/>\n        \/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/<br \/>\n        #content_views.markdown_views &gt; p:empty:has(+ .toc),<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty,<br \/>\n        \/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/<br \/>\n        #content_views.htmledit_views #main-toc,<br \/>\n        #content_views.htmledit_views #hr-toc,<br \/>\n        #content_views.htmledit_views p[id*=\\&#8221;-toc\\&#8221;] {<br \/>\n          display: none !important;<br \/>\n        }<br \/>\n        \/* \u76ee\u5f55\u53bb\u6389\u540e\uff0c\u7d27\u8ddf\u7684\u9996\u4e2a\u6807\u9898\u4e0d\u518d\u591a\u51fa\u4e00\u5757\u4e0a\u8fb9\u8ddd *\/<br \/>\n        #content_views.markdown_views &gt; .toc + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + h4,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h4 {<br \/>\n          margin-top: 0 !important;<br \/>\n        }<\/p>\n<p>LangChain \u7684 create_agent \u5e95\u5c42\u8fd0\u884c\u5728 LangGraph Runtime \u4e4b\u4e0a\u3002Runtime \u53ef\u4ee5\u7406\u89e3\u4e3a Agent \u6bcf\u6b21\u6267\u884c\u65f6\u62ff\u5230\u7684\u201c\u8fd0\u884c\u73af\u5883\u201d&#xff0c;\u5176\u4e2d\u4e0e\u6570\u636e\u7ba1\u7406\u6700\u76f8\u5173\u7684\u4e09\u4e2a\u90e8\u5206\u662f&#xff1a;<\/p>\n<ul>\n<li>\n<p>Context&#xff1a;\u672c\u6b21\u8c03\u7528\u9700\u8981\u7684\u9759\u6001\u8eab\u4efd\u548c\u4f9d\u8d56\u3002<\/p>\n<\/li>\n<li>\n<p>State&#xff1a;\u6267\u884c\u8fc7\u7a0b\u4e2d\u4e0d\u65ad\u53d8\u5316\u7684\u77ed\u671f\u72b6\u6001\u3002<\/p>\n<\/li>\n<li>\n<p>Store&#xff1a;\u8de8\u4f1a\u8bdd\u4fdd\u5b58\u7684\u957f\u671f\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>1.\u8ba4\u8bc6Runtime<\/h2>\n<table>\n<tr>\n<p>\u6982\u5ff5<\/p>\n<p>\u4f5c\u7528<\/p>\n<p>\u8bfb\u5199<\/p>\n<p>\u5178\u578b\u751f\u547d\u5468\u671f<\/p>\n<p>\u5178\u578b\u5185\u5bb9<\/p>\n<p>\u8bbf\u95ee\u65b9\u5f0f<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Context<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u6ce8\u5165\u672c\u6b21\u8fd0\u884c\u6240\u9700\u7684\u8eab\u4efd\u3001\u914d\u7f6e\u548c\u4f9d\u8d56<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u53ea\u8bfb<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u5355\u6b21\u8c03\u7528<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>user_id\u3001tenant_id\u3001\u6743\u9650\u3001\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>runtime.context<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>State<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u4fdd\u5b58 Agent \u6267\u884c\u4e2d\u4ea7\u751f\u7684\u52a8\u6001\u6570\u636e<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u53ef\u8bfb\u5199<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u540c\u4e00\u4f1a\u8bdd&#xff1a;\u540c\u4e00thread_id\u7684\u591a\u6b21\u8c03\u7528<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u6d88\u606f\u3001\u6b65\u9aa4\u7ed3\u679c\u3001\u8ba1\u6570\u5668\u3001\u4e1a\u52a1\u6d41\u7a0b\u72b6\u6001<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>runtime.state<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Store<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u4fdd\u5b58\u8de8\u4f1a\u8bdd\u9700\u8981\u590d\u7528\u7684\u6570\u636e<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u53ef\u8bfb\u5199<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u8de8\u4f1a\u8bdd&#xff1b;<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>\u7528\u6237\u504f\u597d\u3001\u957f\u671f\u8bb0\u5fc6\u3001\u5386\u53f2\u8d44\u6599<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>runtime.store<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5224\u65ad\u6570\u636e\u653e\u5728\u54ea\u91cc&#xff0c;\u53ef\u4ee5\u8fde\u7eed\u95ee\u4e24\u4e2a\u95ee\u9898&#xff1a;<\/p>\n<li>\n<p>\u8fd9\u4efd\u6570\u636e\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u4f1a\u4e0d\u4f1a\u53d8\u5316&#xff1f;\u4e0d\u53d8\u5316&#xff0c;\u4f18\u5148\u8003\u8651 Context\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u4f1a\u53d8\u5316&#xff0c;\u4e0b\u4e00\u6b21\u4f1a\u8bdd\u8fd8\u8981\u4e0d\u8981\u4f7f\u7528&#xff1f;\u53ea\u5728\u5f53\u524d\u4f1a\u8bdd\u4f7f\u7528\u8c03\u7528 State&#xff0c;\u8de8\u4f1a\u8bdd\u590d\u7528\u8c03\u7528 Store\u3002<\/p>\n<\/li>\n<p>\u66f4\u591aRuntime\u4fe1\u606f\u53ef\u4ee5\u67e5\u770b\u5b98\u65b9\u6587\u6863&#xff1a;Runtime &#8211; Docs by LangChain<\/p>\n<h2>2.Context&#xff08;\u8fd0\u884c\u4e0a\u4e0b\u6587&#xff09;<\/h2>\n<p>Runtime Context \u662f\u4e00\u79cd\u4f9d\u8d56\u6ce8\u5165\u673a\u5236&#xff1a;\u8c03\u7528\u8005\u5728\u8fd0\u884c\u5f00\u59cb\u65f6\u628a\u7528\u6237\u8eab\u4efd\u3001\u79df\u6237\u4fe1\u606f\u3001\u6743\u9650\u6216\u5916\u90e8\u670d\u52a1\u8fde\u63a5\u4f20\u7ed9 Agent&#xff0c;\u8282\u70b9\u3001\u4e2d\u95f4\u4ef6\u548c\u5de5\u5177\u5728\u6267\u884c\u65f6\u518d\u4ece Runtime \u8bfb\u53d6\u3002<\/p>\n<p>\u5b83\u89e3\u51b3\u7684\u662f\u4e09\u4e2a\u5de5\u7a0b\u95ee\u9898&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u907f\u514d\u628a\u7528\u6237 ID\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u4fe1\u606f\u786c\u7f16\u7801\u8fdb\u5de5\u5177\u3002<\/p>\n<\/li>\n<li>\n<p>\u540c\u4e00\u4e2a Agent \u53ef\u4ee5\u5b89\u5168\u5730\u670d\u52a1\u4e0d\u540c\u7528\u6237\u548c\u79df\u6237\u3002<\/p>\n<\/li>\n<li>\n<p>\u5de5\u5177\u7684\u4e1a\u52a1\u53c2\u6570\u53ea\u4fdd\u7559\u771f\u6b63\u9700\u8981\u6a21\u578b\u586b\u5199\u7684\u5185\u5bb9\u3002<\/p>\n<\/li>\n<\/ul>\n<p>Context \u4e2d\u7684\u6570\u636e\u9ed8\u8ba4\u4e0d\u4f1a\u81ea\u52a8\u53d1\u7ed9\u5927\u6a21\u578b&#xff1b;\u53ea\u6709\u4ee3\u7801\u4e3b\u52a8\u8bfb\u53d6\u5e76\u628a\u5185\u5bb9\u52a0\u5165\u63d0\u793a\u8bcd\u6216\u5de5\u5177\u7ed3\u679c\u65f6&#xff0c;\u6a21\u578b\u624d\u4f1a\u770b\u5230\u5b83\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u5728\u7cfb\u7edf\u4e2d\u7528\u6237\u901a\u5e38\u4f1a\u5148\u767b\u5f55&#xff0c;\u7136\u540e\u8bbf\u95eeagent&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u628a\u767b\u5f55\u7528\u6237\u4fe1\u606f\u5b58\u5165Context&#xff0c;\u5728Agent\u5185\u90e8\u7684Middleware\u6216Tool\u65e7\u80fd\u5f88\u65b9\u4fbf\u7684\u901a\u8fc7runtime.context\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u3002\u800c\u8fd9\u4e2a\u7528\u6237\u4fe1\u606f\u5e76\u4e0d\u4f1a\u8ba9\u6a21\u578b\u770b\u5230&#xff0c;\u4e5f\u4e0d\u9700\u8981\u4f5c\u4e3a\u53c2\u6570\u8ba9\u6a21\u578b\u4f20\u9012&#xff0c;\u7eaf\u7cb9\u662fAgent\u5185\u90e8\u4fe1\u606f\u3002<\/p>\n<h3>2.1\u51c6\u5907\u6570\u636e<\/h3>\n<p>\u4e0b\u9762&#xff0c;\u6211\u4eec\u5c31\u4ee5\u4e00\u4e2a\u591a\u79df\u6237\u7684\u5728\u7ebf\u6559\u80b2Agent\u7cfb\u7edf\u4e3a\u4f8b&#xff0c;\u6765\u6f14\u793aContext\u7684\u7528\u6cd5\u3002<\/p>\n<p>\u6211\u4eec\u5148\u51c6\u5907\u4e00\u4e2a\u5b57\u5178&#xff0c;\u6a21\u62df\u4e00\u4e2a\u591a\u79df\u6237\u7684\u5728\u7ebf\u6559\u80b2\u9879\u76ee\u6570\u636e\u5e93&#xff0c;\u5176\u4e2d\u7684\u79df\u6237\u5c31\u662f\u5b66\u6821&#xff0c;user\u5c31\u662f\u5b66\u5458&#xff1a;<\/p>\n<p>USER_DATABASE &#061; {<br \/>\n    &#034;school1&#034;:{&#034;id_01&#034;:{&#034;name&#034;:&#034;\u5f20\u4e09&#034;,&#034;course&#034; :&#034;java\u5b9e\u8df5&#034;},<br \/>\n               &#034;id_02&#034;:{&#034;name&#034;:&#034;\u674e\u56db&#034;,&#034;course&#034; :&#034;python\u5b9e\u8df5&#034;}<br \/>\n               },<br \/>\n    &#034;school2&#034;:{&#034;id_01&#034;:{&#034;name&#034;:&#034;\u738b\u4e94&#034;,&#034;course&#034; :&#034;c&#043;&#043;\u5b9e\u8df5&#034;},<br \/>\n               &#034;id_02&#034;:{&#034;name&#034;:&#034;\u8d75\u516d&#034;,&#034;course&#034; :&#034;go\u5b9e\u8df5&#034;}<br \/>\n               }<br \/>\n} <\/p>\n<p>\u4e0b\u9762\u7528\u201c\u67e5\u8be2\u5f53\u524d\u7528\u6237\u8d44\u6599\u201d\u6f14\u793a Context\u3002\u6a21\u578b\u53ea\u8d1f\u8d23\u51b3\u5b9a\u4f55\u65f6\u8c03\u7528\u5de5\u5177&#xff0c;\u4e0d\u8d1f\u8d23\u586b\u5199 user_id \u548cschool_id&#xff1b;\u5de5\u5177\u4ece Runtime Context \u4e2d\u8bfb\u53d6\u5b83\u4eec\u3002<\/p>\n<h3>2.2\u5b9a\u4e49Context Schema<\/h3>\n<p>\u6211\u4eec\u5728\u5b9a\u4e49\u7c7b\u7684\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528&#064;dataclass\u88c5\u9970\u5668&#xff0c;dataclass\u662fPython3.7\u5f15\u5165\u7684\u4e00\u4e2a\u88c5\u9970\u5668&#xff0c;\u7528\u4e8e\u7b80\u5316\u7c7b\u7684\u5b9a\u4e49&#xff0c;\u81ea\u52a8\u751f\u6210\u4e00\u4e9b\u5e38\u7528\u65b9\u6cd5&#xff0c;\u5982__init__()\u3001__repr__()\u3001__eq__()\u7b49\u3002frozen&#061;True\u8868\u793a\u8be5\u6570\u636e\u7c7b\u662f\u4e0d\u53ef\u53d8\u7684&#xff0c;\u4e00\u65e6\u521b\u5efa\u5b9e\u4f8b\u540e&#xff0c;\u5176\u5c5e\u6027\u503c\u4e0d\u80fd\u88ab\u4fee\u6539\u3002<\/p>\n<p>&#064;dataclass(frozen&#061;True) #\u5b9a\u4e49\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b&#xff0c;<br \/>\nclass USERCONTEXT():<br \/>\n    &#034;&#034;&#034;agent\u7684\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b&#034;&#034;&#034;<br \/>\n    user_id: str<br \/>\n    school_id: str <\/p>\n<p>\u5b9a\u4e49USERCONTEXT\u7c7b\u5c31\u662f\u4e00\u4e2acontext_schema &#xff0c;\u4f5c\u7528\u662f\u7ed9 Context \u63d0\u4f9b\u660e\u786e\u7684\u6570\u636e\u7ed3\u6784\u3002frozen&#061;True\u4e0d\u662f LangChain \u7684\u786c\u6027\u8981\u6c42&#xff0c;\u4f46\u80fd\u8868\u8fbe\u201c\u8fd0\u884c\u5185\u4e0d\u53ef\u4fee\u6539\u201d\u7684\u8bbe\u8ba1\u610f\u56fe\u3002<\/p>\n<h3>2.3Tool\u4e2d\u4f7f\u7528Context<\/h3>\n<p>\u5728tool\u4e2d&#xff0c;\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0runtime\u53c2\u6570&#xff0c;\u7136\u540e\u5229\u7528runtime.context\u6765\u8bbf\u95eeContext&#xff1a;<\/p>\n<p>&#064;tool<br \/>\n#\u8fd0\u884c\u65f6\u5de5\u5177&#xff0c;tool\u662flangchain\u4e2d\u7684\u4e00\u4e2a\u88c5\u9970\u5668&#xff0c;\u7528\u4e8e\u5c06\u4e00\u4e2a\u51fd\u6570\u5305\u88c5\u6210\u4e00\u4e2a\u5de5\u5177&#xff0c;\u4f7f\u5176\u53ef\u4ee5\u5728agent\u4e2d\u88ab\u8c03\u7528&#xff0c;\u4f46\u662fagent\u4e0d\u80fd\u8c03\u7528\u5b83\u4e5f\u4e0d\u4f1a\u770b\u5230\u5b83&#xff0c;\u5b83\u662f\u7531langchain\u8fd0\u884c\u65f6\u6765\u8c03\u7528\u7684\u3002<br \/>\ndef get_user_profile(runtime : ToolRuntime[USERCONTEXT]):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u901a\u8fc7\u7528\u6237ID\u548c\u5b66\u6821ID\u83b7\u53d6\u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f<br \/>\n    :param runtime: \u5de5\u5177\u8fd0\u884c\u65f6\u5bf9\u8c61<br \/>\n    :return: \u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f<br \/>\n    &#034;&#034;&#034;<br \/>\n    cnt &#061; runtime.context  #\u83b7\u53d6\u4e0a\u4e0b\u6587\u4e2d\u7684\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b\u5b9e\u4f8b<br \/>\n    if cnt is None:<br \/>\n        raise ValueError(&#034;\u4e0a\u4e0b\u6587\u6570\u636e\u7c7b\u5b9e\u4f8b\u4e3a\u7a7a&#034;)<br \/>\n    profile &#061; USER_DATABASE.get(cnt.school_id).get(cnt.user_id)<br \/>\n    if profile is None:<br \/>\n        raise ValueError(f&#034;\u672a\u627e\u5230\u7528\u6237\u4fe1\u606f&#xff0c;school_id: {cnt.school_id}, user_id: {cnt.user_id}&#034;)<br \/>\n    return {<br \/>\n        &#034;name&#034;: profile[&#034;name&#034;],<br \/>\n        &#034;course&#034;: profile[&#034;course&#034;]<br \/>\n    }<\/p>\n<h3>2.4\u7ed9Agent\u6dfb\u52a0Context<\/h3>\n<p>\u5728\u5b9a\u4e49Agent\u65f6&#xff0c;\u9700\u8981\u6307\u5b9a\u6211\u4eec\u521b\u5efa\u597d\u7684Context\u3002<\/p>\n<p>context_schema&#061;USERCONTEXT\u8868\u793a\u4e0a\u4e0b\u6587\u6570\u636e\u7c7b\u4e3aUSERCONTEXT&#xff0c;agent\u5728\u8c03\u7528\u5de5\u5177\u65f6\u4f1a\u5c06\u4e0a\u4e0b\u6587\u6570\u636e\u7c7b\u5b9e\u4f8b\u4f20\u5165\u5de5\u5177\u7684runtime\u4e2d\u3002\u5e76\u4e14\u5728\u8c03\u7528\u4e2d&#xff0c;\u9700\u8981\u5199\u5165context\u7684\u4fe1\u606f\u3002<\/p>\n<p>model &#061; init_chat_model(<br \/>\n    model &#061; &#034;deepseek-flash&#034;,<br \/>\n    extra_body &#061; {<br \/>\n        &#034;thinking&#034;: {&#034;type&#034;: &#034;disabled&#034;}  #\u63a7\u5236\u6a21\u578b\u601d\u8003\u5f00\u5173<br \/>\n    }<br \/>\n)<br \/>\nagent &#061; create_agent(model &#061; model, tools&#061;[get_user_profile],context_schema&#061;USERCONTEXT) <\/p>\n<h3>2.5\u8c03\u7528agent<\/h3>\n<p>context \u5c5e\u4e8e\u672c\u6b21 agent \u8fd0\u884c\u7684\u914d\u7f6e\u4fe1\u606f&#xff0c;\u4e0d\u4f1a\u53d1\u7ed9\u5927\u6a21\u578b&#xff1b; \u5982\u679c\u5de5\u5177\u9700\u8981\u7528\u5230\u7528\u6237\u8eab\u4efd\u6807\u8bc6&#xff08;user_id\u3001school_id&#xff09;&#xff0c;\u5c31\u628a\u5b83\u653e\u5230 context \u91cc\u6ce8\u5165\u7ed9\u5de5\u5177&#xff1b; \u4e0d\u662f\u8c03\u7528\u6a21\u578b\u5c31\u4e00\u5b9a\u8981\u4f20&#xff0c;\u53ea\u6709\u540e\u7eed\u8981\u8c03\u7528\u4f9d\u8d56\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u7684\u5de5\u5177\u65f6&#xff0c;\u624d\u9700\u8981\u4f20\u3002\u8fd9\u4e2a context \u662frun \u7ea7\u522b\u7684\u4e0a\u4e0b\u6587&#xff0c;\u4e0d\u4f1a\u81ea\u52a8\u8de8\u8f6e\u6b21\u4fdd\u5b58&#xff0c;\u591a\u8f6e\u5bf9\u8bdd\u6bcf\u4e00\u6b21 invoke \u90fd\u8981\u7ee7\u7eed\u5e26\u4e0a context&#061;\u3002<\/p>\n<p>response &#061; agent.invoke(<br \/>\n    {&#034;messages&#034;: [HumanMessage(&#034;\u5e2e\u6211\u67e5\u8be2\u6b63\u5728\u5904\u7406\u7684\u8bfe\u7a0b&#034;)]},<br \/>\n    context&#061;USERCONTEXT(user_id&#061;&#034;id_01&#034;, school_id&#061;&#034;school1&#034;) , #\u4f20\u5165\u4e0a\u4e0b\u6587\u6570\u636e\u7c7b\u5b9e\u4f8b<br \/>\n)<br \/>\nfor message in response[&#034;messages&#034;]:<br \/>\nmessage.pretty_print() <\/p>\n<p>\u6253\u5370\u7ed3\u679c&#xff1a;<\/p>\n<p>D:\\\\python\\\\python.exe D:\\\\pythonProject\\\\langchain\\\\\u8fdb\u9636\\\\01.context\u7684\u4f7f\u7528.py<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; Human Message &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u5e2e\u6211\u67e5\u8be2\u6b63\u5728\u5904\u7406\u7684\u8bfe\u7a0b<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; Ai Message &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u6211\u6765\u5e2e\u60a8\u67e5\u8be2\u3002\u4e0d\u8fc7&#xff0c;\u8981\u83b7\u53d6\u60a8\u7684\u4e2a\u4eba\u4fe1\u606f&#xff08;\u5305\u62ec\u6b63\u5728\u5904\u7406\u7684\u8bfe\u7a0b&#xff09;&#xff0c;\u6211\u9700\u8981\u5148\u8c03\u7528\u5de5\u5177\u83b7\u53d6\u60a8\u7684\u7528\u6237\u8d44\u6599\u3002<br \/>\nTool Calls:<br \/>\nget_user_profile (call_00_JZWI8rQ3zMYT6FHHaG585857)<br \/>\nCall ID: call_00_JZWI8rQ3zMYT6FHHaG585857<br \/>\nArgs:<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; Tool Message &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\nName: get_user_profile<br \/>\n{&#034;name&#034;: &#034;\u5f20\u4e09&#034;, &#034;course&#034;: &#034;java\u5b9e\u8df5&#034;}<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; Ai Message &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u60a8\u597d&#xff0c;\u5f20\u4e09&#xff01;\u6839\u636e\u60a8\u7684\u7528\u6237\u8d44\u6599&#xff0c;\u60a8\u76ee\u524d\u6b63\u5728\u5904\u7406\u7684\u8bfe\u7a0b\u662f&#xff1a;<br \/>\nJava\u5b9e\u8df5<br \/>\n\u795d\u60a8\u5b66\u4e60\u987a\u5229&#xff01;\u5982\u9700\u67e5\u8be2\u5176\u4ed6\u4fe1\u606f&#xff0c;\u968f\u65f6\u544a\u8bc9\u6211\u3002<br \/>\n\u8fdb\u7a0b\u5df2\u7ed3\u675f&#xff0c;\u9000\u51fa\u4ee3\u7801\u4e3a 0 <\/p>\n<li>\u7528\u6237\u6d88\u606f&#xff1a;\u5e2e\u6211\u67e5\u8be2\u6b63\u5728\u5904\u7406\u7684\u8bfe\u7a0b&#xff08;\u8fd9\u90e8\u5206\u662f\u7ed9 LLM \u770b\u7684&#xff09;<\/li>\n<li>LLM \u5224\u65ad\u9700\u8981\u8c03\u7528\u5de5\u5177 get_user_profile<\/li>\n<li>\u6846\u67b6\u81ea\u52a8\u4ece\u4f60\u4f20\u5165\u7684 context&#061;USERCONTEXT(&#8230;) \u7ed9\u5230 ToolRuntime&#xff0c;\u5de5\u5177\u62ff\u5230 runtime.context \u91cc\u9762\u7684 user_id\u3001school_id&#xff0c;\u6a21\u578b\u5168\u7a0b\u770b\u4e0d\u5230\u8fd9\u4e24\u4e2a id&#xff0c;\u4e5f\u4e0d\u9700\u8981\u751f\u6210\u8fd9\u4e24\u4e2a\u53c2\u6570<\/li>\n<li>\u5de5\u5177\u67e5\u5230\u6570\u636e\u8fd4\u56de&#xff1a;{&#034;name&#034;: &#034;\u5f20\u4e09&#034;, &#034;course&#034;: &#034;java\u5b9e\u8df5&#034;}<\/li>\n<li>LLM \u57fa\u4e8e\u5de5\u5177\u8fd4\u56de\u7ed3\u679c\u6574\u7406\u81ea\u7136\u8bed\u8a00\u56de\u7b54\u8f93\u51fa<\/li>\n<p>\u5b8c\u6574\u4ee3\u7801&#xff1a;<\/p>\n<p>from dataclasses import dataclass<br \/>\nfrom langchain.tools import tool<br \/>\nfrom dotenv import load_dotenv<br \/>\nfrom langchain.chat_models import init_chat_model<br \/>\nfrom langchain_core.messages import HumanMessage<br \/>\nfrom langgraph.prebuilt import ToolRuntime<br \/>\nfrom langchain.agents import create_agent<br \/>\nload_dotenv(encoding&#061;&#034;utf-8&#034;)<br \/>\nUSER_DATABASE &#061; {<br \/>\n&#034;school1&#034;:{&#034;id_01&#034;:{&#034;name&#034;:&#034;\u5f20\u4e09&#034;,&#034;course&#034; :&#034;java\u5b9e\u8df5&#034;},<br \/>\n&#034;id_02&#034;:{&#034;name&#034;:&#034;\u674e\u56db&#034;,&#034;course&#034; :&#034;python\u5b9e\u8df5&#034;}<br \/>\n},<br \/>\n&#034;school2&#034;:{&#034;id_01&#034;:{&#034;name&#034;:&#034;\u738b\u4e94&#034;,&#034;course&#034; :&#034;c&#043;&#043;\u5b9e\u8df5&#034;},<br \/>\n&#034;id_02&#034;:{&#034;name&#034;:&#034;\u8d75\u516d&#034;,&#034;course&#034; :&#034;go\u5b9e\u8df5&#034;}<br \/>\n}<br \/>\n}<br \/>\n&#064;dataclass(frozen&#061;True)<br \/>\nclass USERCONTEXT():<br \/>\n&#034;&#034;&#034;agent\u7684\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b&#034;&#034;&#034;<br \/>\nuser_id: str<br \/>\nschool_id: str<br \/>\n#\u5b9a\u4e49\u83b7\u53d6\u4fe1\u606f\u5de5\u5177<br \/>\n&#064;tool<br \/>\ndef get_user_profile(runtime : ToolRuntime[USERCONTEXT]):<br \/>\n&#034;&#034;&#034;<br \/>\n\u901a\u8fc7\u7528\u6237ID\u548c\u5b66\u6821ID\u83b7\u53d6\u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f<br \/>\n:param runtime: \u5de5\u5177\u8fd0\u884c\u65f6\u5bf9\u8c61<br \/>\n:return: \u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f<br \/>\n&#034;&#034;&#034;<br \/>\ncnt &#061; runtime.context  #\u83b7\u53d6\u4e0a\u4e0b\u6587\u4e2d\u7684\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b\u5b9e\u4f8b<br \/>\nif cnt is None:<br \/>\nraise ValueError(&#034;\u4e0a\u4e0b\u6587\u6570\u636e\u7c7b\u5b9e\u4f8b\u4e3a\u7a7a&#034;)<br \/>\nprofile &#061; USER_DATABASE.get(cnt.school_id).get(cnt.user_id)<br \/>\nif profile is None:<br \/>\nraise ValueError(f&#034;\u672a\u627e\u5230\u7528\u6237\u4fe1\u606f&#xff0c;school_id: {cnt.school_id}, user_id: {cnt.user_id}&#034;)<br \/>\nreturn {<br \/>\n&#034;name&#034;: profile[&#034;name&#034;],<br \/>\n&#034;course&#034;: profile[&#034;course&#034;]<br \/>\n}<br \/>\nmodel &#061; init_chat_model(<br \/>\nmodel &#061; &#034;deepseek-flash&#034;,<br \/>\nextra_body &#061; {<br \/>\n&#034;thinking&#034;: {&#034;type&#034;: &#034;disabled&#034;}  #\u63a7\u5236\u6a21\u578b\u5f00\u5173<br \/>\n}<br \/>\n)<br \/>\nagent &#061; create_agent(model &#061; model,<br \/>\ntools&#061;[get_user_profile],<br \/>\ncontext_schema&#061;USERCONTEXT,<br \/>\nsystem_prompt&#061;&#034;\u4f60\u662f\u8bfe\u7a0b\u5e73\u53f0\u52a9\u624b\u3002\u9700\u8981\u7528\u6237\u8d44\u6599\u65f6\u8c03\u7528\u5de5\u5177\u3002&#034;)<br \/>\n&#034;&#034;&#034;<br \/>\ncontext_schema&#061;USERCONTEXT\u8868\u793a\u4e0a\u4e0b\u6587\u6570\u636e\u7c7b\u4e3aUSERCONTEXT&#xff0c;agent\u5728\u8c03\u7528\u5de5\u5177\u65f6\u4f1a\u5c06\u4e0a\u4e0b\u6587\u6570\u636e\u7c7b\u5b9e\u4f8b\u4f20\u5165\u5de5\u5177\u7684runtime\u4e2d\u3002<br \/>\n&#034;&#034;&#034;<br \/>\n##\u6d41\u5f0f\u8f93\u51fa<br \/>\nresponse &#061; agent.stream_events(<br \/>\n{&#034;messages&#034;: [HumanMessage(&#034;\u5e2e\u6211\u67e5\u8be2\u6b63\u5728\u5904\u7406\u7684\u8bfe\u7a0b&#034;)]},<br \/>\ncontext&#061;USERCONTEXT(user_id&#061;&#034;id_01&#034;, school_id&#061;&#034;school1&#034;) , #\u4f20\u5165\u4e0a\u4e0b\u6587\u6570\u636e\u7c7b\u5b9e\u4f8b<br \/>\nversion &#061; &#034;v3&#034;<br \/>\n)<br \/>\nfor message in response.messages:<br \/>\nfor delta in message.text:<br \/>\nprint(delta, end&#061;&#034;&#034;, flush&#061;True)<br \/>\nresponse &#061; agent.invoke(<br \/>\n{&#034;messages&#034;: [HumanMessage(&#034;\u5e2e\u6211\u67e5\u8be2\u6b63\u5728\u5904\u7406\u7684\u8bfe\u7a0b&#034;)]},<br \/>\ncontext&#061;USERCONTEXT(user_id&#061;&#034;id_01&#034;, school_id&#061;&#034;school1&#034;) , #\u4f20\u5165\u4e0a\u4e0b\u6587\u6570\u636e\u7c7b\u5b9e\u4f8b<br \/>\n)<br \/>\nfor message in response[&#034;messages&#034;]:<br \/>\nmessage.pretty_print() <\/p>\n<h2>3.State&#xff08;\u77ed\u671f\u8bb0\u5fc6&#xff09;<\/h2>\n<p>State\u5b83\u662fAgent\u7684\u77ed\u671f\u8bb0\u5fc6&#xff0c;\u5b58\u50a8\u5f53\u524d\u4f1a\u8bdd\u7684\u5386\u53f2\u6d88\u606f\u3001\u4efb\u52a1\u72b6\u6001\u7b49\u4fe1\u606f\u3002<\/p>\n<p>\u6211\u4eec\u4e4b\u524d\u6240\u8bf4\u7684\u77ed\u671f\u8bb0\u5fc6\u65f6\u4f7f\u7528\u7684\u662f\u9ed8\u8ba4\u7684AgentState&#xff0c;\u5176\u4e2d\u53ea\u5305\u542b\u4f1a\u8bdd\u7684\u5386\u53f2\u6d88\u606f&#xff08;messages&#xff09;&#xff0c;\u672c\u8282\u5f00\u59cb\u6211\u4eec\u5b66\u4e60\u5982\u4f55\u81ea\u5b9a\u4e49AgentState&#xff0c;\u8bb0\u5f55\u9664\u4e86\u5386\u53f2\u6d88\u606f\u4ee5\u5916\u7684\u5176\u4ed6\u4fe1\u606f\u3002<\/p>\n<h3>3.1\u81ea\u5b9a\u4e49State<\/h3>\n<p>\u8981\u81ea\u5b9a\u4e49AgentState&#xff0c;\u5176\u5b9e\u5c31\u662f\u5b9a\u4e49\u4e00\u4e2a\u7c7b&#xff0c;\u7136\u540e\u7ee7\u627fAgentState&#xff0c;\u5728\u5176\u4e2d\u6dfb\u52a0\u60f3\u8981\u8bb0\u5f55\u7684\u5c5e\u6027\u4fe1\u606f\u5373\u53ef\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u6211\u4eec\u8981\u5b9e\u73b0\u7edf\u8ba1\u7528\u6237\u7684\u6a21\u578b\u8c03\u7528\u6b21\u6570\u3001\u8c03\u7528\u65f6\u95f4\u7684\u529f\u80fd&#xff0c;\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49state&#xff1a;<\/p>\n<p># \u5b9a\u4e49\u81ea\u5b9a\u4e49State\u7ed3\u6784<br \/>\nclass State(AgentState):<br \/>\n    chat_count : int # \u6a21\u578b\u5bf9\u8bdd\u6b21\u6570 <\/p>\n<p>\u7531\u4e8e\u5728AgentState\u4e2d\u5df2\u7ecf\u5177\u5907messages\u5c5e\u6027&#xff0c;\u4e5f\u5c31\u662f\u5386\u53f2\u6d88\u606f\u5217\u8868&#xff0c;\u56e0\u6b64\u6211\u4eec\u7684CustomState\u7ee7\u627f\u4e86AgentState\u4ee5\u540e&#xff0c;\u4e0d\u4ec5\u53ef\u4ee5\u8bb0\u5f55\u4f1a\u8bdd\u7684\u5386\u53f2\u6d88\u606f&#xff0c;\u4e5f\u80fd\u8bb0\u5f55Agent\u8fd0\u884c\u7684\u4efb\u52a1\u72b6\u6001\u4fe1\u606f\u4e86\u3002<\/p>\n<p>\u6807\u9898\u4e00\u90a3\u4e48&#xff0c;\u6211\u4eec\u8be5\u5982\u4f55\u64cd\u4f5cstate\u4e2d\u7684\u81ea\u5b9a\u4e49\u5c5e\u6027\u5462&#xff1f;<\/p>\n<p>Lang Chain\u4e2d\u901a\u5e38\u6709\u4e24\u4e2a\u5730\u65b9\u53ef\u4ee5\u64cd\u4f5cstate&#xff1a;<\/p>\n<ul>\n<li>\n<p>Tool<\/p>\n<\/li>\n<li>\n<p>Middleware<\/p>\n<\/li>\n<\/ul>\n<p>\u7531\u4e8eMiddleware\u8fd8\u6ca1\u6709\u63a5\u89e6&#xff0c;\u6211\u4eec\u5148\u6765\u770b\u5728Tool\u4e2d\u8bbf\u95eestate<\/p>\n<h3>3.2\u4f7f\u7528\u5de5\u5177\u8bbf\u95eestate<\/h3>\n<p>\u5728 Tool \u4e2d\u8bbf\u95ee State&#xff0c;\u9760\u7684\u662f\u5de5\u5177\u51fd\u6570\u91cc\u7684 runtime \u53c2\u6570\u3002\u53ea\u8981\u7ed9\u5de5\u5177\u58f0\u660e runtime: ToolRuntime&#xff0c;LangGraph \u5728\u8c03\u7528\u8fd9\u4e2a\u5de5\u5177\u65f6\u5c31\u4f1a\u628a\u5f53\u524d\u8fd0\u884c\u73af\u5883\u6ce8\u5165\u8fdb\u6765&#xff0c;\u6211\u4eec\u901a\u8fc7 runtime.state \u5c31\u80fd\u8bfb\u5230\u5f53\u524d\u4f1a\u8bdd\u7684 State\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u4e24\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>runtime.state \u662f\u53ea\u8bfb\u7684&#xff0c;\u76f4\u63a5\u4fee\u6539\u5b83\u4e0d\u4f1a\u751f\u6548&#xff0c;\u5fc5\u987b\u901a\u8fc7\u8fd4\u56de\u503c\u628a\u66f4\u65b0\u63d0\u4ea4\u7ed9\u6846\u67b6\u3002<\/p>\n<\/li>\n<li>\n<p>\u5de5\u5177\u8981\u66f4\u65b0 State&#xff0c;\u9700\u8981\u8fd4\u56de\u4e00\u4e2a Command \u5bf9\u8c61&#xff0c;\u628a\u8981\u5199\u5165\u7684\u5b57\u6bb5\u653e\u5728 update \u5b57\u5178\u91cc\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u5de5\u5177\u7684\u4f5c\u7528\u662f&#xff1a;\u6bcf\u6b21\u88ab\u8c03\u7528\u65f6&#xff0c;\u628a State \u4e2d\u7684 chat_count \u52a0 1&#xff0c;\u5e76\u8fd4\u56de\u4e00\u6761\u5de5\u5177\u6d88\u606f&#xff0c;\u544a\u8bc9\u6a21\u578b\u8fd9\u6b21\u66f4\u65b0\u5df2\u7ecf\u5b8c\u6210\u3002<\/p>\n<p>&#064;tool<br \/>\ndef get_increse_chat_count(runtime: ToolRuntime):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u5728\u6bcf\u4e00\u6b21\u8c03\u7528\u6a21\u578b\u7684\u65f6\u5019&#xff0c;\u90fd\u4f1a\u8c03\u7528\u8fd9\u4e2a\u5de5\u5177\u6765\u589e\u52a0\u804a\u5929\u8ba1\u6570\u5668\u7684\u503c<br \/>\n    :param runtime: \u5de5\u5177\u8fd0\u884c\u65f6\u5bf9\u8c61<br \/>\n    :return: \u4e00\u4e2aState\u5bf9\u8c61&#xff0c;\u8868\u793a\u5f53\u524d\u7684\u804a\u5929\u8ba1\u6570\u5668\u503c &#xff0c;\u4f46\u662f\u56e0\u4e3aAgentState\u7c7b\u7ee7\u627f\u7684\u662fTypeDict&#xff0c;\u6240\u4ee5\u8fd4\u56de\u7684State\u5bf9\u8c61\u4f1a\u88ab\u81ea\u52a8\u8f6c\u6362\u4e3a\u5b57\u5178\u7c7b\u578b<br \/>\n    &#034;&#034;&#034;<br \/>\n    state &#061; runtime.state.get(&#034;chat_count&#034;,0) #\u83b7\u53d6\u5f53\u524d\u7684\u804a\u5929\u8ba1\u6570\u5668\u503c&#xff0c;\u5982\u679c\u6ca1\u6709\u5219\u9ed8\u8ba4\u4e3a0<br \/>\n    new_count &#061; state &#043; 1<br \/>\n    update &#061; {<br \/>\n        &#034;messages&#034; :[<br \/>\n            ToolMessage(content&#061;&#034;Successfully updated agent state&#034;,tool_call_id &#061; runtime.tool_call_id)<br \/>\n        ],<br \/>\n        &#034;chat_count&#034;: new_count<br \/>\n    }<br \/>\n    return Command(update &#061; update) <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u91cc\u6709\u51e0\u4e2a\u5173\u952e\u70b9\u503c\u5f97\u5c55\u5f00\u8bf4\u660e&#xff1a;<\/p>\n<li>\n<p>runtime.state.get(&#034;chat_count&#034;, 0)&#xff1a;\u4ece\u5f53\u524d State \u4e2d\u8bfb\u53d6\u8ba1\u6570\u5668&#xff0c;\u7b2c\u4e00\u6b21\u8fd0\u884c\u65f6\u8be5\u5b57\u6bb5\u8fd8\u4e0d\u5b58\u5728&#xff0c;\u6240\u4ee5\u7528 0 \u4f5c\u4e3a\u9ed8\u8ba4\u503c&#xff0c;\u907f\u514d KeyError\u3002<\/p>\n<\/li>\n<li>\n<p>runtime.tool_call_id&#xff1a;\u672c\u6b21\u5de5\u5177\u8c03\u7528\u7684\u552f\u4e00\u6807\u8bc6\u3002\u8fd4\u56de ToolMessage \u65f6\u5fc5\u987b\u5e26\u4e0a\u5b83&#xff0c;\u6846\u67b6\u624d\u80fd\u628a\u8fd9\u6761\u5de5\u5177\u7ed3\u679c\u548c\u6a21\u578b\u53d1\u8d77\u7684\u90a3\u6b21\u5de5\u5177\u8c03\u7528\u5bf9\u5e94\u8d77\u6765\u3002<\/p>\n<\/li>\n<li>\n<p>Command(update&#061;update)&#xff1a;\u8fd9\u662f\u5411 State \u5199\u6570\u636e\u7684\u6807\u51c6\u65b9\u5f0f\u3002update \u4e2d\u7684 messages \u4f1a\u88ab\u8ffd\u52a0\u5230\u5386\u53f2\u6d88\u606f\u5217\u8868&#xff0c;chat_count \u5219\u4f1a\u8986\u76d6 State \u4e2d\u540c\u540d\u5b57\u6bb5\u3002<\/p>\n<\/li>\n<p>\u8fd8\u6709\u4e00\u70b9\u5bb9\u6613\u8e29\u5751&#xff1a;\u5de5\u5177\u5fc5\u987b\u771f\u7684\u88ab\u8c03\u7528&#xff0c;State \u624d\u4f1a\u66f4\u65b0\u3002\u5982\u679c\u6a21\u578b\u5224\u65ad\u4e0d\u9700\u8981\u8c03\u7528\u5de5\u5177&#xff0c;\u90a3\u4e48 chat_count \u5c31\u4e0d\u4f1a\u53d8\u5316\u3002\u56e0\u6b64\u8fd9\u7c7b\u8ba1\u6570\u903b\u8f91\u901a\u5e38\u914d\u5408\u7cfb\u7edf\u63d0\u793a\u8bcd\u4f7f\u7528&#xff0c;\u660e\u786e\u544a\u8bc9\u6a21\u578b\u5728\u4ec0\u4e48\u573a\u666f\u4e0b\u5e94\u8be5\u8c03\u7528\u8be5\u5de5\u5177\u3002<\/p>\n<h3>3.3\u8bbe\u7f6estate schema<\/h3>\n<p>\u628a\u5de5\u5177\u6ce8\u518c\u5230 Agent \u65f6&#xff0c;\u8fd8\u9700\u8981\u628a\u81ea\u5b9a\u4e49\u7684 State \u901a\u8fc7 state_schema \u4f20\u7ed9 create_agent&#xff0c;\u5426\u5219\u6846\u67b6\u4ecd\u7136\u4f7f\u7528\u9ed8\u8ba4\u7684 AgentState&#xff0c;chat_count \u8fd9\u4e2a\u5b57\u6bb5\u4e0d\u4f1a\u88ab\u4fdd\u5b58&#xff0c;state_schema&#061;State\u8868\u793a\u72b6\u6001\u6570\u636e\u7c7b\u4e3aState&#xff0c;agent\u5728\u8c03\u7528\u5de5\u5177\u65f6\u4f1a\u5c06\u72b6\u6001\u6570\u636e\u7c7b\u5b9e\u4f8b\u4f20\u5165\u5de5\u5177\u7684runtime\u4e2d\u3002<\/p>\n<p>agent &#061; create_agent(model,<br \/>\n                     tools&#061;[get_increse_chat_count],<br \/>\n                     state_schema&#061;State,<br \/>\n                     checkpointer&#061; InMemorySaver())<\/p>\n<p>\u8c03\u7528\u4e4b\u540e&#xff0c;\u53ef\u4ee5\u5728\u8fd4\u56de\u7ed3\u679c\u7684 State \u4e2d\u770b\u5230\u7d2f\u52a0\u540e\u7684\u8ba1\u6570&#xff1a;<\/p>\n<p>config &#061; {&#034;configurable&#034;:{&#034;thread_id&#034;:&#034;1&#034;}}<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e00\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke({&#034;messages&#034;:&#034;\u4f60\u597d&#xff0c;\u6211\u662f\u5f20\u626c&#xff0c;\u6211\u559c\u6b22\u859b\u4e4b\u8c26\u7684\u6b4c&#xff0c;\u6211\u60f3\u548c\u4f60\u804a\u804a\u859b\u4e4b\u8c26\u7684\u6b4c\u3002&#034;},config )<br \/>\nprint(response)<br \/>\nagent.get_state(config)<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e8c\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061;agent.invoke({&#034;messages&#034;:&#034;\u4f60\u77e5\u9053\u6f14\u5458\u8fd9\u9996\u6b4c\u66f2\u5417&#xff1f;&#034;}, config)<br \/>\nprint(response)<br \/>\nagent.get_state(config)<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e09\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke({&#034;messages&#034;:&#034;\u4f60\u77e5\u9053\u6211\u548c\u4f60\u5df2\u7ecf\u804a\u4e86\u591a\u5c11\u6b21\u4e86\u5417&#xff1f;&#034;}, config)<br \/>\nprint(response)<br \/>\nagent.get_state(config)<br \/>\nconfig2 &#061; {&#034;configurable&#034;:{&#034;thread_id&#034;:&#034;2&#034;}}<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u56db\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke({&#034;messages&#034;:&#034;\u4f60\u77e5\u9053\u6211\u548c\u4f60\u5df2\u7ecf\u804a\u4e86\u591a\u5c11\u6b21\u4e86\u5417&#xff1f;&#034;}, config2)<br \/>\nprint(response)<br \/>\nagent.get_state(config2) <\/p>\n<p>\u8fd9\u91cc thread_id \u662f State \u7684\u751f\u547d\u5468\u671f\u8fb9\u754c&#xff1a;\u540c\u4e00\u4e2a thread_id \u7684\u591a\u6b21\u8c03\u7528\u5171\u4eab\u540c\u4e00\u4efd State&#xff0c;chat_count \u4f1a\u6301\u7eed\u7d2f\u52a0&#xff1b;\u6362\u4e00\u4e2a thread_id \u5c31\u662f\u4e00\u6bb5\u5168\u65b0\u7684\u4f1a\u8bdd&#xff0c;\u8ba1\u6570\u4f1a\u4ece 0 \u91cd\u65b0\u5f00\u59cb\u3002\u8fd9\u4e5f\u6b63\u597d\u547c\u5e94\u4e86\u524d\u9762\u8868\u683c\u91cc State \u7684\u5b9a\u4f4d\u2014\u2014\u53ea\u5728\u5f53\u524d\u4f1a\u8bdd\u5185\u6709\u6548&#xff0c;\u4e0d\u4f1a\u8de8\u4f1a\u8bdd\u4fdd\u5b58\u3002<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b&#xff0c;\u53ef\u4ee5\u770b\u51fa\u524d\u4e09\u6b21\u7684\u8fd0\u884c&#xff0c;\u5b83\u7684chat_count\u5b57\u6bb5\u662f1&#xff0c;2&#xff0c;3&#xff1b;\u4f46\u662f\u5230\u4e86\u7b2c\u56db\u6b21&#xff0c;\u6211\u5c06thread_id\u8bbe\u7f6e\u4e3a2&#xff0c;\u5b83\u7684chat_count\u5b57\u6bb5\u53d8\u6210\u4e86&#xff0c;\u8fd9\u4e5f\u8bf4\u660estate\u662f\u5728\u540c\u4e00\u4f1a\u8bdd&#xff0c;\u540c\u4e00thread_id\u7684\u591a\u6b21\u8c03\u7528<\/p>\n<p>D:\\\\python\\\\python.exe D:\\\\pythonProject\\\\langchain\\\\\u8fdb\u9636\\\\02.state\u7684\u4f7f\u7528.py<br \/>\n&#8212;&#8212;-\u7b2c\u4e00\u8f6e&#8212;&#8212;&#8211;<br \/>\n{&#039;messages&#039;: [HumanMessage(content&#061;&#039;\u4f60\u597d&#xff0c;\u6211\u662f\u5f20\u4e09&#xff0c;\u6211\u559c\u6b22\u859b\u4e4b\u8c26\u7684\u6b4c&#xff0c;\u6211\u60f3\u548c\u4f60\u804a\u804a\u859b\u4e4b\u8c26\u7684\u6b4c\u3002&#039;, additional_kwargs&#061;{}, response_metadata&#061;{}, id&#061;&#039;7ce39e1d-08e6-4e5f-af49-fdd3c83c7417&#039;), AIMessage(content&#061;&#039;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 26, &#039;prompt_tokens&#039;: 327, &#039;total_tokens&#039;: 353, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 128, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 128, &#039;prompt_cache_miss_tokens&#039;: 199}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;c81c4674-b1c4-4b81-9be1-f9826c577a8b&#039;, &#039;finish_reason&#039;: &#039;tool_calls&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-14a5-7962-a640-68fbecc54068-0&#039;, tool_calls&#061;[{&#039;name&#039;: &#039;get_increse_chat_count&#039;, &#039;args&#039;: {}, &#039;id&#039;: &#039;call_00_twvU5KWVIc7Oc8THi3wl4841&#039;, &#039;type&#039;: &#039;tool_call&#039;}], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 327, &#039;output_tokens&#039;: 26, &#039;total_tokens&#039;: 353, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 128}, &#039;output_token_details&#039;: {}}), ToolMessage(content&#061;&#039;Successfully updated agent state&#039;, name&#061;&#039;get_increse_chat_count&#039;, id&#061;&#039;11a08cac-f85e-4c13-94e3-21e039df7f50&#039;, tool_call_id&#061;&#039;call_00_twvU5KWVIc7Oc8THi3wl4841&#039;), AIMessage(content&#061;&#039;\u4f60\u597d&#xff0c;\u5f20\u4e09&#xff01;\u5f88\u9ad8\u5174\u8ba4\u8bc6\u4f60 &#x1f60a; \u859b\u4e4b\u8c26\u7684\u6b4c\u786e\u5b9e\u5f88\u6709\u8fa8\u8bc6\u5ea6\u2014\u2014\u65cb\u5f8b\u6293\u8033\u3001\u6b4c\u8bcd\u5e38\u5e38\u5e26\u7740\u6545\u4e8b\u611f\u548c\u4e00\u70b9\u201c\u81ea\u5632\u5f0f\u201d\u7684\u6df1\u60c5&#xff0c;\u5f88\u591a\u4eba\u542c\u7740\u4f1a\u5f88\u6709\u5171\u9e23\u3002\\\\n\\\\n\u6211\u4eec\u53ef\u4ee5\u4ece\u51e0\u4e2a\u89d2\u5ea6\u804a&#xff1a;\\\\n\\\\n1. **\u4f60\u6700\u559c\u6b22\u7684\u859b\u4e4b\u8c26\u6b4c\u66f2\u662f\u54ea\u4e00\u9996&#xff1f;** \u662f\u300a\u6f14\u5458\u300b\u300a\u4e11\u516b\u602a\u300b\u300a\u8ba4\u771f\u7684\u96ea\u300b\u8fd9\u7c7b\u5927\u70ed\u6b4c&#xff0c;\u8fd8\u662f\u504f\u51b7\u95e8\u4e00\u70b9\u7684&#xff1f;\\\\n2. **\u4f60\u66f4\u559c\u6b22\u4ed6\u7684\u201c\u82e6\u60c5\u6b4c\u201d\u8fd8\u662f\u201c\u5e7d\u9ed8\/\u53cd\u5dee\u611f\u201d\u5f3a\u7684\u4f5c\u54c1&#xff1f;**\\\\n3. **\u6709\u6ca1\u6709\u54ea\u53e5\u6b4c\u8bcd\u7279\u522b\u6233\u4f60&#xff1f;** \u6bd4\u5982\u300a\u6f14\u5458\u300b\u91cc\u90a3\u79cd\u201c\u611f\u60c5\u91cc\u7684\u8868\u6f14\u611f\u201d&#xff0c;\u6216\u8005\u300a\u521a\u521a\u597d\u300b\u91cc\u7684\u514b\u5236\u4e0e\u9057\u61be\u3002\\\\n\\\\n\u4f60\u53ef\u4ee5\u5148\u8bf4\u8bf4\u4f60\u6700\u8fd1\u5faa\u73af\u7684\u662f\u54ea\u9996\u3001\u4e3a\u4ec0\u4e48\u559c\u6b22&#xff0c;\u6211\u53ef\u4ee5\u966a\u4f60\u4e00\u8d77\u62c6\u6b4c\u8bcd\u3001\u804a\u65cb\u5f8b&#xff0c;\u4e5f\u53ef\u4ee5\u5e2e\u4f60\u63a8\u8350\u540c\u7c7b\u578b\u7684\u6b4c\u3002&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 171, &#039;prompt_tokens&#039;: 370, &#039;total_tokens&#039;: 541, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 128, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 128, &#039;prompt_cache_miss_tokens&#039;: 242}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;12aa1dbe-215b-4b0b-9898-c38d116051bb&#039;, &#039;finish_reason&#039;: &#039;stop&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-1795-7633-9a78-bcbe560868b2-0&#039;, tool_calls&#061;[], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 370, &#039;output_tokens&#039;: 171, &#039;total_tokens&#039;: 541, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 128}, &#039;output_token_details&#039;: {}})], &#039;chat_count&#039;: 1}<br \/>\n&#8212;&#8212;-\u7b2c\u4e8c\u8f6e&#8212;&#8212;&#8211;<br \/>\n{&#039;messages&#039;: [HumanMessage(content&#061;&#039;\u4f60\u597d&#xff0c;\u6211\u662f\u5f20\u4e09&#xff0c;\u6211\u559c\u6b22\u859b\u4e4b\u8c26\u7684\u6b4c&#xff0c;\u6211\u60f3\u548c\u4f60\u804a\u804a\u859b\u4e4b\u8c26\u7684\u6b4c\u3002&#039;, additional_kwargs&#061;{}, response_metadata&#061;{}, id&#061;&#039;7ce39e1d-08e6-4e5f-af49-fdd3c83c7417&#039;), AIMessage(content&#061;&#039;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 26, &#039;prompt_tokens&#039;: 327, &#039;total_tokens&#039;: 353, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 128, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 128, &#039;prompt_cache_miss_tokens&#039;: 199}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;c81c4674-b1c4-4b81-9be1-f9826c577a8b&#039;, &#039;finish_reason&#039;: &#039;tool_calls&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-14a5-7962-a640-68fbecc54068-0&#039;, tool_calls&#061;[{&#039;name&#039;: &#039;get_increse_chat_count&#039;, &#039;args&#039;: {}, &#039;id&#039;: &#039;call_00_twvU5KWVIc7Oc8THi3wl4841&#039;, &#039;type&#039;: &#039;tool_call&#039;}], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 327, &#039;output_tokens&#039;: 26, &#039;total_tokens&#039;: 353, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 128}, &#039;output_token_details&#039;: {}}), ToolMessage(content&#061;&#039;Successfully updated agent state&#039;, name&#061;&#039;get_increse_chat_count&#039;, id&#061;&#039;11a08cac-f85e-4c13-94e3-21e039df7f50&#039;, tool_call_id&#061;&#039;call_00_twvU5KWVIc7Oc8THi3wl4841&#039;), AIMessage(content&#061;&#039;\u4f60\u597d&#xff0c;\u5f20\u4e09&#xff01;\u5f88\u9ad8\u5174\u8ba4\u8bc6\u4f60 &#x1f60a; \u859b\u4e4b\u8c26\u7684\u6b4c\u786e\u5b9e\u5f88\u6709\u8fa8\u8bc6\u5ea6\u2014\u2014\u65cb\u5f8b\u6293\u8033\u3001\u6b4c\u8bcd\u5e38\u5e38\u5e26\u7740\u6545\u4e8b\u611f\u548c\u4e00\u70b9\u201c\u81ea\u5632\u5f0f\u201d\u7684\u6df1\u60c5&#xff0c;\u5f88\u591a\u4eba\u542c\u7740\u4f1a\u5f88\u6709\u5171\u9e23\u3002\\\\n\\\\n\u6211\u4eec\u53ef\u4ee5\u4ece\u51e0\u4e2a\u89d2\u5ea6\u804a&#xff1a;\\\\n\\\\n1. **\u4f60\u6700\u559c\u6b22\u7684\u859b\u4e4b\u8c26\u6b4c\u66f2\u662f\u54ea\u4e00\u9996&#xff1f;** \u662f\u300a\u6f14\u5458\u300b\u300a\u4e11\u516b\u602a\u300b\u300a\u8ba4\u771f\u7684\u96ea\u300b\u8fd9\u7c7b\u5927\u70ed\u6b4c&#xff0c;\u8fd8\u662f\u504f\u51b7\u95e8\u4e00\u70b9\u7684&#xff1f;\\\\n2. **\u4f60\u66f4\u559c\u6b22\u4ed6\u7684\u201c\u82e6\u60c5\u6b4c\u201d\u8fd8\u662f\u201c\u5e7d\u9ed8\/\u53cd\u5dee\u611f\u201d\u5f3a\u7684\u4f5c\u54c1&#xff1f;**\\\\n3. **\u6709\u6ca1\u6709\u54ea\u53e5\u6b4c\u8bcd\u7279\u522b\u6233\u4f60&#xff1f;** \u6bd4\u5982\u300a\u6f14\u5458\u300b\u91cc\u90a3\u79cd\u201c\u611f\u60c5\u91cc\u7684\u8868\u6f14\u611f\u201d&#xff0c;\u6216\u8005\u300a\u521a\u521a\u597d\u300b\u91cc\u7684\u514b\u5236\u4e0e\u9057\u61be\u3002\\\\n\\\\n\u4f60\u53ef\u4ee5\u5148\u8bf4\u8bf4\u4f60\u6700\u8fd1\u5faa\u73af\u7684\u662f\u54ea\u9996\u3001\u4e3a\u4ec0\u4e48\u559c\u6b22&#xff0c;\u6211\u53ef\u4ee5\u966a\u4f60\u4e00\u8d77\u62c6\u6b4c\u8bcd\u3001\u804a\u65cb\u5f8b&#xff0c;\u4e5f\u53ef\u4ee5\u5e2e\u4f60\u63a8\u8350\u540c\u7c7b\u578b\u7684\u6b4c\u3002&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 171, &#039;prompt_tokens&#039;: 370, &#039;total_tokens&#039;: 541, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 128, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 128, &#039;prompt_cache_miss_tokens&#039;: 242}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;12aa1dbe-215b-4b0b-9898-c38d116051bb&#039;, &#039;finish_reason&#039;: &#039;stop&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-1795-7633-9a78-bcbe560868b2-0&#039;, tool_calls&#061;[], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 370, &#039;output_tokens&#039;: 171, &#039;total_tokens&#039;: 541, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 128}, &#039;output_token_details&#039;: {}}), HumanMessage(content&#061;&#039;\u4f60\u77e5\u9053\u6f14\u5458\u8fd9\u9996\u6b4c\u66f2\u5417&#xff1f;&#039;, additional_kwargs&#061;{}, response_metadata&#061;{}, id&#061;&#039;4f242c4e-6c86-4c7d-b298-34d0bbf58d67&#039;), AIMessage(content&#061;&#039;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 26, &#039;prompt_tokens&#039;: 551, &#039;total_tokens&#039;: 577, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 384, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 384, &#039;prompt_cache_miss_tokens&#039;: 167}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;e4c265e5-1871-45c3-af40-6284844cafb0&#039;, &#039;finish_reason&#039;: &#039;tool_calls&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-1ff1-7a21-b014-e078fa7479b5-0&#039;, tool_calls&#061;[{&#039;name&#039;: &#039;get_increse_chat_count&#039;, &#039;args&#039;: {}, &#039;id&#039;: &#039;call_00_edeXLEVjOAVR7pcBRBHj4021&#039;, &#039;type&#039;: &#039;tool_call&#039;}], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 551, &#039;output_tokens&#039;: 26, &#039;total_tokens&#039;: 577, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 384}, &#039;output_token_details&#039;: {}}), ToolMessage(content&#061;&#039;Successfully updated agent state&#039;, name&#061;&#039;get_increse_chat_count&#039;, id&#061;&#039;90cde541-689c-4212-b4fb-7ecab7c214d6&#039;, tool_call_id&#061;&#039;call_00_edeXLEVjOAVR7pcBRBHj4021&#039;), AIMessage(content&#061;&#039;\u5f53\u7136\u77e5\u9053&#xff0c;\u300a\u6f14\u5458\u300b\u7b97\u662f\u859b\u4e4b\u8c26\u6700\u6709\u4ee3\u8868\u6027\u7684\u4f5c\u54c1\u4e4b\u4e00\u4e86 &#x1f3b5;\\\\n\\\\n\u7b80\u5355\u8bf4\u51e0\u53e5\u6211\u5bf9\u8fd9\u9996\u6b4c\u7684\u7406\u89e3&#xff1a;\\\\n\\\\n- **\u53d1\u884c\u80cc\u666f**&#xff1a;\u6536\u5f55\u5728\u859b\u4e4b\u8c26 2015 \u5e74\u7684\u4e13\u8f91\u300a\u7ec5\u58eb\u300b\u91cc&#xff0c;\u4e5f\u662f\u4ed6\u201c\u7b2c\u4e8c\u6b21\u8d70\u7ea2\u201d\u540e\u4f20\u5531\u5ea6\u6781\u9ad8\u7684\u4e00\u9996\u6b4c&#xff0c;KTV \u70b9\u5531\u7387\u5e38\u5e74\u5c45\u9ad8\u3002\\\\n- **\u6838\u5fc3\u4e3b\u9898**&#xff1a;\u7528\u201c\u6f14\u620f\u201d\u6765\u6bd4\u55bb\u4e00\u6bb5\u5df2\u7ecf\u53d8\u8d28\u7684\u611f\u60c5\u2014\u2014\u4e00\u65b9\u5728\u611f\u60c5\u91cc\u201c\u6f14\u201d&#xff0c;\u4e00\u65b9\u660e\u77e5\u5374\u914d\u5408\u7740\u201c\u6f14\u201d\u3002\u201c\u5176\u5b9e\u53f0\u4e0b\u7684\u89c2\u4f17\u5c31\u6211\u4e00\u4e2a\u201d\u8fd9\u53e5\u5c31\u5f88\u6709\u753b\u9762\u611f&#xff0c;\u628a\u4e24\u4e2a\u4eba\u7684\u5173\u7cfb\u5199\u6210\u4e86\u72ec\u89d2\u620f\u3002\\\\n- **\u6b4c\u8bcd\u4eae\u70b9**&#xff1a;\\\\n  &#8211; \u201c\u7b80\u5355\u70b9&#xff0c;\u8bf4\u8bdd\u7684\u65b9\u5f0f\u7b80\u5355\u70b9\u201d\u2014\u2014\u76f4\u767d\u5730\u8981\u6c42\u5bf9\u65b9\u522b\u518d\u7ed5\u5f2f\u5b50\u3001\u522b\u518d\u6f14\u3002\\\\n  &#8211; \u201c\u4f60\u96be\u8fc7\u7684\u592a\u8868\u9762&#xff0c;\u50cf\u6ca1\u5929\u8d4b\u7684\u6f14\u5458\u201d\u2014\u2014\u53c8\u5fc3\u75bc\u53c8\u8bbd\u523a&#xff0c;\u60c5\u7eea\u5f88\u590d\u6742\u3002\\\\n  &#8211; \u6574\u9996\u6b4c\u5176\u5b9e\u662f\u201c\u770b\u7a7f\u5374\u4e0d\u8bf4\u7834\u201d\u7684\u59d4\u5c48\u611f\u3002\\\\n- **\u65cb\u5f8b\/\u7f16\u66f2**&#xff1a;\u4e3b\u6b4c\u538b\u6291\u514b\u5236&#xff0c;\u526f\u6b4c\u60c5\u7eea\u91ca\u653e&#xff0c;\u859b\u4e4b\u8c26\u90a3\u79cd\u5e26\u70b9\u6c99\u54d1\u3001\u7565\u5e26\u9f3b\u97f3\u7684\u5531\u8154\u628a\u201c\u9690\u5fcd\u7684\u7206\u53d1\u201d\u5904\u7406\u5f97\u5f88\u6709\u5473\u9053\u3002\\\\n\\\\n\u5f20\u4e09&#xff0c;\u4f60\u542c\u300a\u6f14\u5458\u300b\u7684\u65f6\u5019&#xff0c;\u6700\u6233\u4f60\u7684\u662f\u54ea\u4e00\u53e5&#xff1f;\u662f\u201c\u7b80\u5355\u70b9\u201d\u7684\u90a3\u79cd\u76f4\u63a5&#xff0c;\u8fd8\u662f\u201c\u53f0\u4e0b\u7684\u89c2\u4f17\u5c31\u6211\u4e00\u4e2a\u201d\u7684\u90a3\u79cd\u5b64\u72ec\u611f&#xff1f;\u6211\u53ef\u4ee5\u987a\u7740\u4f60\u559c\u6b22\u7684\u70b9&#xff0c;\u518d\u5e2e\u4f60\u804a\u804a\u4ed6\u7684\u5176\u4ed6\u6b4c &#x1f60a;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 290, &#039;prompt_tokens&#039;: 594, &#039;total_tokens&#039;: 884, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 384, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 384, &#039;prompt_cache_miss_tokens&#039;: 210}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;7849fbd9-1ac4-467a-bc57-f5d395185b1c&#039;, &#039;finish_reason&#039;: &#039;stop&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-225a-7cd1-be01-2310d84f1326-0&#039;, tool_calls&#061;[], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 594, &#039;output_tokens&#039;: 290, &#039;total_tokens&#039;: 884, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 384}, &#039;output_token_details&#039;: {}})], &#039;chat_count&#039;: 2}<br \/>\n&#8212;&#8212;-\u7b2c\u4e09\u8f6e&#8212;&#8212;&#8211;<br \/>\n{&#039;messages&#039;: [HumanMessage(content&#061;&#039;\u4f60\u597d&#xff0c;\u6211\u662f\u5f20\u4e09&#xff0c;\u6211\u559c\u6b22\u859b\u4e4b\u8c26\u7684\u6b4c&#xff0c;\u6211\u60f3\u548c\u4f60\u804a\u804a\u859b\u4e4b\u8c26\u7684\u6b4c\u3002&#039;, additional_kwargs&#061;{}, response_metadata&#061;{}, id&#061;&#039;7ce39e1d-08e6-4e5f-af49-fdd3c83c7417&#039;), AIMessage(content&#061;&#039;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 26, &#039;prompt_tokens&#039;: 327, &#039;total_tokens&#039;: 353, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 128, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 128, &#039;prompt_cache_miss_tokens&#039;: 199}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;c81c4674-b1c4-4b81-9be1-f9826c577a8b&#039;, &#039;finish_reason&#039;: &#039;tool_calls&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-14a5-7962-a640-68fbecc54068-0&#039;, tool_calls&#061;[{&#039;name&#039;: &#039;get_increse_chat_count&#039;, &#039;args&#039;: {}, &#039;id&#039;: &#039;call_00_twvU5KWVIc7Oc8THi3wl4841&#039;, &#039;type&#039;: &#039;tool_call&#039;}], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 327, &#039;output_tokens&#039;: 26, &#039;total_tokens&#039;: 353, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 128}, &#039;output_token_details&#039;: {}}), ToolMessage(content&#061;&#039;Successfully updated agent state&#039;, name&#061;&#039;get_increse_chat_count&#039;, id&#061;&#039;11a08cac-f85e-4c13-94e3-21e039df7f50&#039;, tool_call_id&#061;&#039;call_00_twvU5KWVIc7Oc8THi3wl4841&#039;), AIMessage(content&#061;&#039;\u4f60\u597d&#xff0c;\u5f20\u4e09&#xff01;\u5f88\u9ad8\u5174\u8ba4\u8bc6\u4f60 &#x1f60a; \u859b\u4e4b\u8c26\u7684\u6b4c\u786e\u5b9e\u5f88\u6709\u8fa8\u8bc6\u5ea6\u2014\u2014\u65cb\u5f8b\u6293\u8033\u3001\u6b4c\u8bcd\u5e38\u5e38\u5e26\u7740\u6545\u4e8b\u611f\u548c\u4e00\u70b9\u201c\u81ea\u5632\u5f0f\u201d\u7684\u6df1\u60c5&#xff0c;\u5f88\u591a\u4eba\u542c\u7740\u4f1a\u5f88\u6709\u5171\u9e23\u3002\\\\n\\\\n\u6211\u4eec\u53ef\u4ee5\u4ece\u51e0\u4e2a\u89d2\u5ea6\u804a&#xff1a;\\\\n\\\\n1. **\u4f60\u6700\u559c\u6b22\u7684\u859b\u4e4b\u8c26\u6b4c\u66f2\u662f\u54ea\u4e00\u9996&#xff1f;** \u662f\u300a\u6f14\u5458\u300b\u300a\u4e11\u516b\u602a\u300b\u300a\u8ba4\u771f\u7684\u96ea\u300b\u8fd9\u7c7b\u5927\u70ed\u6b4c&#xff0c;\u8fd8\u662f\u504f\u51b7\u95e8\u4e00\u70b9\u7684&#xff1f;\\\\n2. **\u4f60\u66f4\u559c\u6b22\u4ed6\u7684\u201c\u82e6\u60c5\u6b4c\u201d\u8fd8\u662f\u201c\u5e7d\u9ed8\/\u53cd\u5dee\u611f\u201d\u5f3a\u7684\u4f5c\u54c1&#xff1f;**\\\\n3. **\u6709\u6ca1\u6709\u54ea\u53e5\u6b4c\u8bcd\u7279\u522b\u6233\u4f60&#xff1f;** \u6bd4\u5982\u300a\u6f14\u5458\u300b\u91cc\u90a3\u79cd\u201c\u611f\u60c5\u91cc\u7684\u8868\u6f14\u611f\u201d&#xff0c;\u6216\u8005\u300a\u521a\u521a\u597d\u300b\u91cc\u7684\u514b\u5236\u4e0e\u9057\u61be\u3002\\\\n\\\\n\u4f60\u53ef\u4ee5\u5148\u8bf4\u8bf4\u4f60\u6700\u8fd1\u5faa\u73af\u7684\u662f\u54ea\u9996\u3001\u4e3a\u4ec0\u4e48\u559c\u6b22&#xff0c;\u6211\u53ef\u4ee5\u966a\u4f60\u4e00\u8d77\u62c6\u6b4c\u8bcd\u3001\u804a\u65cb\u5f8b&#xff0c;\u4e5f\u53ef\u4ee5\u5e2e\u4f60\u63a8\u8350\u540c\u7c7b\u578b\u7684\u6b4c\u3002&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 171, &#039;prompt_tokens&#039;: 370, &#039;total_tokens&#039;: 541, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 128, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 128, &#039;prompt_cache_miss_tokens&#039;: 242}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;12aa1dbe-215b-4b0b-9898-c38d116051bb&#039;, &#039;finish_reason&#039;: &#039;stop&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-1795-7633-9a78-bcbe560868b2-0&#039;, tool_calls&#061;[], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 370, &#039;output_tokens&#039;: 171, &#039;total_tokens&#039;: 541, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 128}, &#039;output_token_details&#039;: {}}), HumanMessage(content&#061;&#039;\u4f60\u77e5\u9053\u6f14\u5458\u8fd9\u9996\u6b4c\u66f2\u5417&#xff1f;&#039;, additional_kwargs&#061;{}, response_metadata&#061;{}, id&#061;&#039;4f242c4e-6c86-4c7d-b298-34d0bbf58d67&#039;), AIMessage(content&#061;&#039;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 26, &#039;prompt_tokens&#039;: 551, &#039;total_tokens&#039;: 577, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 384, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 384, &#039;prompt_cache_miss_tokens&#039;: 167}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;e4c265e5-1871-45c3-af40-6284844cafb0&#039;, &#039;finish_reason&#039;: &#039;tool_calls&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-1ff1-7a21-b014-e078fa7479b5-0&#039;, tool_calls&#061;[{&#039;name&#039;: &#039;get_increse_chat_count&#039;, &#039;args&#039;: {}, &#039;id&#039;: &#039;call_00_edeXLEVjOAVR7pcBRBHj4021&#039;, &#039;type&#039;: &#039;tool_call&#039;}], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 551, &#039;output_tokens&#039;: 26, &#039;total_tokens&#039;: 577, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 384}, &#039;output_token_details&#039;: {}}), ToolMessage(content&#061;&#039;Successfully updated agent state&#039;, name&#061;&#039;get_increse_chat_count&#039;, id&#061;&#039;90cde541-689c-4212-b4fb-7ecab7c214d6&#039;, tool_call_id&#061;&#039;call_00_edeXLEVjOAVR7pcBRBHj4021&#039;), AIMessage(content&#061;&#039;\u5f53\u7136\u77e5\u9053&#xff0c;\u300a\u6f14\u5458\u300b\u7b97\u662f\u859b\u4e4b\u8c26\u6700\u6709\u4ee3\u8868\u6027\u7684\u4f5c\u54c1\u4e4b\u4e00\u4e86 &#x1f3b5;\\\\n\\\\n\u7b80\u5355\u8bf4\u51e0\u53e5\u6211\u5bf9\u8fd9\u9996\u6b4c\u7684\u7406\u89e3&#xff1a;\\\\n\\\\n- **\u53d1\u884c\u80cc\u666f**&#xff1a;\u6536\u5f55\u5728\u859b\u4e4b\u8c26 2015 \u5e74\u7684\u4e13\u8f91\u300a\u7ec5\u58eb\u300b\u91cc&#xff0c;\u4e5f\u662f\u4ed6\u201c\u7b2c\u4e8c\u6b21\u8d70\u7ea2\u201d\u540e\u4f20\u5531\u5ea6\u6781\u9ad8\u7684\u4e00\u9996\u6b4c&#xff0c;KTV \u70b9\u5531\u7387\u5e38\u5e74\u5c45\u9ad8\u3002\\\\n- **\u6838\u5fc3\u4e3b\u9898**&#xff1a;\u7528\u201c\u6f14\u620f\u201d\u6765\u6bd4\u55bb\u4e00\u6bb5\u5df2\u7ecf\u53d8\u8d28\u7684\u611f\u60c5\u2014\u2014\u4e00\u65b9\u5728\u611f\u60c5\u91cc\u201c\u6f14\u201d&#xff0c;\u4e00\u65b9\u660e\u77e5\u5374\u914d\u5408\u7740\u201c\u6f14\u201d\u3002\u201c\u5176\u5b9e\u53f0\u4e0b\u7684\u89c2\u4f17\u5c31\u6211\u4e00\u4e2a\u201d\u8fd9\u53e5\u5c31\u5f88\u6709\u753b\u9762\u611f&#xff0c;\u628a\u4e24\u4e2a\u4eba\u7684\u5173\u7cfb\u5199\u6210\u4e86\u72ec\u89d2\u620f\u3002\\\\n- **\u6b4c\u8bcd\u4eae\u70b9**&#xff1a;\\\\n  &#8211; \u201c\u7b80\u5355\u70b9&#xff0c;\u8bf4\u8bdd\u7684\u65b9\u5f0f\u7b80\u5355\u70b9\u201d\u2014\u2014\u76f4\u767d\u5730\u8981\u6c42\u5bf9\u65b9\u522b\u518d\u7ed5\u5f2f\u5b50\u3001\u522b\u518d\u6f14\u3002\\\\n  &#8211; \u201c\u4f60\u96be\u8fc7\u7684\u592a\u8868\u9762&#xff0c;\u50cf\u6ca1\u5929\u8d4b\u7684\u6f14\u5458\u201d\u2014\u2014\u53c8\u5fc3\u75bc\u53c8\u8bbd\u523a&#xff0c;\u60c5\u7eea\u5f88\u590d\u6742\u3002\\\\n  &#8211; \u6574\u9996\u6b4c\u5176\u5b9e\u662f\u201c\u770b\u7a7f\u5374\u4e0d\u8bf4\u7834\u201d\u7684\u59d4\u5c48\u611f\u3002\\\\n- **\u65cb\u5f8b\/\u7f16\u66f2**&#xff1a;\u4e3b\u6b4c\u538b\u6291\u514b\u5236&#xff0c;\u526f\u6b4c\u60c5\u7eea\u91ca\u653e&#xff0c;\u859b\u4e4b\u8c26\u90a3\u79cd\u5e26\u70b9\u6c99\u54d1\u3001\u7565\u5e26\u9f3b\u97f3\u7684\u5531\u8154\u628a\u201c\u9690\u5fcd\u7684\u7206\u53d1\u201d\u5904\u7406\u5f97\u5f88\u6709\u5473\u9053\u3002\\\\n\\\\n\u5f20\u4e09&#xff0c;\u4f60\u542c\u300a\u6f14\u5458\u300b\u7684\u65f6\u5019&#xff0c;\u6700\u6233\u4f60\u7684\u662f\u54ea\u4e00\u53e5&#xff1f;\u662f\u201c\u7b80\u5355\u70b9\u201d\u7684\u90a3\u79cd\u76f4\u63a5&#xff0c;\u8fd8\u662f\u201c\u53f0\u4e0b\u7684\u89c2\u4f17\u5c31\u6211\u4e00\u4e2a\u201d\u7684\u90a3\u79cd\u5b64\u72ec\u611f&#xff1f;\u6211\u53ef\u4ee5\u987a\u7740\u4f60\u559c\u6b22\u7684\u70b9&#xff0c;\u518d\u5e2e\u4f60\u804a\u804a\u4ed6\u7684\u5176\u4ed6\u6b4c &#x1f60a;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 290, &#039;prompt_tokens&#039;: 594, &#039;total_tokens&#039;: 884, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 384, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 384, &#039;prompt_cache_miss_tokens&#039;: 210}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;7849fbd9-1ac4-467a-bc57-f5d395185b1c&#039;, &#039;finish_reason&#039;: &#039;stop&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-225a-7cd1-be01-2310d84f1326-0&#039;, tool_calls&#061;[], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 594, &#039;output_tokens&#039;: 290, &#039;total_tokens&#039;: 884, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 384}, &#039;output_token_details&#039;: {}}), HumanMessage(content&#061;&#039;\u4f60\u77e5\u9053\u6211\u548c\u4f60\u5df2\u7ecf\u804a\u4e86\u591a\u5c11\u6b21\u4e86\u5417&#xff1f;&#039;, additional_kwargs&#061;{}, response_metadata&#061;{}, id&#061;&#039;a7cf961c-387f-4433-ae45-40c73946677a&#039;), AIMessage(content&#061;&#039;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 26, &#039;prompt_tokens&#039;: 896, &#039;total_tokens&#039;: 922, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 640, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 640, &#039;prompt_cache_miss_tokens&#039;: 256}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;64732ff0-28e3-4c69-8e19-84eddb22e1af&#039;, &#039;finish_reason&#039;: &#039;tool_calls&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-2dc2-7e43-b033-a75f7cb51f02-0&#039;, tool_calls&#061;[{&#039;name&#039;: &#039;get_increse_chat_count&#039;, &#039;args&#039;: {}, &#039;id&#039;: &#039;call_00_ilSJvgexmKxPeJ0ZwYVn6770&#039;, &#039;type&#039;: &#039;tool_call&#039;}], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 896, &#039;output_tokens&#039;: 26, &#039;total_tokens&#039;: 922, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 640}, &#039;output_token_details&#039;: {}}), ToolMessage(content&#061;&#039;Successfully updated agent state&#039;, name&#061;&#039;get_increse_chat_count&#039;, id&#061;&#039;31411b56-2ae8-4275-8097-b8228d83cb7b&#039;, tool_call_id&#061;&#039;call_00_ilSJvgexmKxPeJ0ZwYVn6770&#039;), AIMessage(content&#061;&#039;\u4ece\u6211\u8fd9\u8fb9\u7684\u8bb0\u5f55\u6765\u770b&#xff0c;\u54b1\u4eec\u76ee\u524d\u4e00\u5171\u804a\u4e86 **3 \u6b21** \u5566 &#x1f60a;\\\\n\\\\n- \u7b2c 1 \u6b21&#xff1a;\u4f60\u81ea\u6211\u4ecb\u7ecd&#xff0c;\u8bf4\u559c\u6b22\u859b\u4e4b\u8c26\u7684\u6b4c\\\\n- \u7b2c 2 \u6b21&#xff1a;\u4f60\u95ee\u6211\u77e5\u4e0d\u77e5\u9053\u300a\u6f14\u5458\u300b\\\\n- \u7b2c 3 \u6b21&#xff1a;\u5c31\u662f\u73b0\u5728\u8fd9\u6b21&#xff5e;\\\\n\\\\n\u4e0d\u8fc7\u6211\u8981\u8bf4\u660e\u4e00\u4e0b&#xff1a;\u6211\u662f\u901a\u8fc7\u4e00\u4e2a\u804a\u5929\u8ba1\u6570\u5de5\u5177\u6765\u8bb0\u5f55\u8f6e\u6b21\u7684&#xff0c;\u6240\u4ee5\u8fd9\u4e2a\u201c3 \u6b21\u201d\u662f\u4ece\u6211\u8fd9\u8fb9\u7edf\u8ba1\u5230\u7684\u6b21\u6570\u3002\u5982\u679c\u4f60\u6307\u7684\u662f\u522b\u7684\u4e1c\u897f&#xff08;\u6bd4\u5982\u67d0\u4e2a\u5e73\u53f0\u7684\u8bb0\u5f55&#xff09;&#xff0c;\u90a3\u6211\u53ef\u80fd\u5c31\u4e0d\u4e00\u5b9a\u5bf9\u5f97\u4e0a\u5566\u3002\\\\n\\\\n\u804a\u859b\u4e4b\u8c26\u804a\u5f97\u8fd8\u5f00\u5fc3\u5417&#xff1f;\u8981\u4e0d\u8981\u7ee7\u7eed\u804a\u804a\u300a\u6f14\u5458\u300b&#xff0c;\u6216\u8005\u6362\u4e00\u9996\u4ed6\u7684\u6b4c&#xff1f; &#x1f3b6;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 138, &#039;prompt_tokens&#039;: 939, &#039;total_tokens&#039;: 1077, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 768, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 768, &#039;prompt_cache_miss_tokens&#039;: 171}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;43ccb997-4a08-4eae-8543-4fc1d8c5caa2&#039;, &#039;finish_reason&#039;: &#039;stop&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-30be-73f1-bde1-2056d411117a-0&#039;, tool_calls&#061;[], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 939, &#039;output_tokens&#039;: 138, &#039;total_tokens&#039;: 1077, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 768}, &#039;output_token_details&#039;: {}})], &#039;chat_count&#039;: 3}<br \/>\n&#8212;&#8212;-\u7b2c\u56db\u8f6e&#8212;&#8212;&#8211;<br \/>\n{&#039;messages&#039;: [HumanMessage(content&#061;&#039;\u4f60\u77e5\u9053\u6211\u548c\u4f60\u5df2\u7ecf\u804a\u4e86\u591a\u5c11\u6b21\u4e86\u5417&#xff1f;&#039;, additional_kwargs&#061;{}, response_metadata&#061;{}, id&#061;&#039;5122cbc1-75d7-4452-bd17-ab96f1537028&#039;), AIMessage(content&#061;&#039;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 26, &#039;prompt_tokens&#039;: 316, &#039;total_tokens&#039;: 342, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 128, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 128, &#039;prompt_cache_miss_tokens&#039;: 188}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;c12df79b-f35d-4547-9e3c-b3aa62289eaf&#039;, &#039;finish_reason&#039;: &#039;tool_calls&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-35bc-7650-83fc-3bdfdd1cc3c1-0&#039;, tool_calls&#061;[{&#039;name&#039;: &#039;get_increse_chat_count&#039;, &#039;args&#039;: {}, &#039;id&#039;: &#039;call_00_bsBEJunWdJX2wn4pYmo28282&#039;, &#039;type&#039;: &#039;tool_call&#039;}], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 316, &#039;output_tokens&#039;: 26, &#039;total_tokens&#039;: 342, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 128}, &#039;output_token_details&#039;: {}}), ToolMessage(content&#061;&#039;Successfully updated agent state&#039;, name&#061;&#039;get_increse_chat_count&#039;, id&#061;&#039;53199b8d-c271-4526-82ac-161440317e1e&#039;, tool_call_id&#061;&#039;call_00_bsBEJunWdJX2wn4pYmo28282&#039;), AIMessage(content&#061;&#039;\u6211\u521a\u521a\u67e5\u4e86\u4e00\u4e0b\u8ba1\u6570\u5668\u2014\u2014\u4e0d\u8fc7\u8fd9\u4e2a\u5de5\u5177\u53ea\u8fd4\u56de\u4e86\u201c\u72b6\u6001\u5df2\u66f4\u65b0\u201d\u7684\u786e\u8ba4\u4fe1\u606f&#xff0c;\u5e76\u6ca1\u6709\u628a\u5177\u4f53\u7684\u8ba1\u6570\u6570\u5b57\u4f20\u56de\u6765&#xff0c;\u6240\u4ee5\u6211\u8fd9\u8fb9\u770b\u4e0d\u5230\u786e\u5207\u7684\u6b21\u6570\u3002\\\\n\\\\n\u5982\u679c\u4f60\u5e0c\u671b\u6211\u77e5\u9053\u51c6\u786e\u6570\u5b57&#xff0c;\u53ef\u4ee5\u544a\u8bc9\u6211&#xff0c;\u6216\u8005\u8ba9\u6211\u7528\u522b\u7684\u65b9\u5f0f\u5e2e\u4f60\u8bb0\u5f55\/\u67e5\u8be2\u804a\u5929\u6b21\u6570&#xff5e;&#039;, additional_kwargs&#061;{&#039;refusal&#039;: None}, response_metadata&#061;{&#039;token_usage&#039;: {&#039;completion_tokens&#039;: 57, &#039;prompt_tokens&#039;: 359, &#039;total_tokens&#039;: 416, &#039;completion_tokens_details&#039;: None, &#039;prompt_tokens_details&#039;: {&#039;audio_tokens&#039;: None, &#039;cache_write_tokens&#039;: None, &#039;cached_tokens&#039;: 128, &#039;image_tokens&#039;: None, &#039;text_tokens&#039;: None}, &#039;prompt_cache_hit_tokens&#039;: 128, &#039;prompt_cache_miss_tokens&#039;: 231}, &#039;model_provider&#039;: &#039;deepseek&#039;, &#039;model_name&#039;: &#039;deepseek-flash&#039;, &#039;system_fingerprint&#039;: &#039;bc19ba97cbe016479903222bb1578db6&#039;, &#039;id&#039;: &#039;3cf7c28b-22c4-404b-b01e-9109001bbbbb&#039;, &#039;finish_reason&#039;: &#039;stop&#039;, &#039;logprobs&#039;: None}, id&#061;&#039;lc_run&#8211;01a1e879-37d1-7e41-985e-df586bda8582-0&#039;, tool_calls&#061;[], invalid_tool_calls&#061;[], usage_metadata&#061;{&#039;input_tokens&#039;: 359, &#039;output_tokens&#039;: 57, &#039;total_tokens&#039;: 416, &#039;input_token_details&#039;: {&#039;cache_read&#039;: 128}, &#039;output_token_details&#039;: {}})], &#039;chat_count&#039;: 1}<br \/>\n\u8fdb\u7a0b\u5df2\u7ed3\u675f&#xff0c;\u9000\u51fa\u4ee3\u7801\u4e3a 0 <\/p>\n<p>\u5b8c\u6574\u4ee3\u7801:<\/p>\n<p>import os<br \/>\nfrom dotenv import load_dotenv<br \/>\nfrom langchain.chat_models import init_chat_model<br \/>\nfrom langchain.agents import AgentState<br \/>\nfrom langchain_core.messages import ToolMessage<br \/>\nfrom langgraph.prebuilt import ToolRuntime<br \/>\nfrom langchain.tools import tool<br \/>\nfrom langchain.agents import create_agent<br \/>\nfrom langgraph.checkpoint.memory import InMemorySaver<br \/>\nfrom langgraph.types import Command<br \/>\nload_dotenv(encoding&#061;&#034;utf-8&#034;)<br \/>\n#\u521d\u59cb\u5316\u6a21\u578b<br \/>\nmodel &#061; init_chat_model(model &#061; &#034;deepseek-flash&#034;,extra_body &#061; {&#034;thinking&#034;: {&#034;type&#034;: &#034;disabled&#034;}})<br \/>\n#\u81ea\u5b9a\u4e49State<br \/>\nclass State(AgentState):<br \/>\nchat_count : int<br \/>\n#\u901a\u8fc7\u5de5\u5177\u8c03\u7528&#xff0c;\u589e\u52a0\u6a21\u578b\u8ba1\u6570<br \/>\n&#064;tool<br \/>\ndef get_increse_chat_count(runtime: ToolRuntime):<br \/>\n&#034;&#034;&#034;<br \/>\n\u5728\u6bcf\u4e00\u6b21\u8c03\u7528\u6a21\u578b\u7684\u65f6\u5019&#xff0c;\u90fd\u4f1a\u8c03\u7528\u8fd9\u4e2a\u5de5\u5177\u6765\u589e\u52a0\u804a\u5929\u8ba1\u6570\u5668\u7684\u503c<br \/>\n:param runtime: \u5de5\u5177\u8fd0\u884c\u65f6\u5bf9\u8c61<br \/>\n:return: \u4e00\u4e2aState\u5bf9\u8c61&#xff0c;\u8868\u793a\u5f53\u524d\u7684\u804a\u5929\u8ba1\u6570\u5668\u503c &#xff0c;\u4f46\u662f\u56e0\u4e3aAgentState\u7c7b\u7ee7\u627f\u7684\u662fTypeDict&#xff0c;\u6240\u4ee5\u8fd4\u56de\u7684State\u5bf9\u8c61\u4f1a\u88ab\u81ea\u52a8\u8f6c\u6362\u4e3a\u5b57\u5178\u7c7b\u578b<br \/>\n&#034;&#034;&#034;<br \/>\nstate &#061; runtime.state.get(&#034;chat_count&#034;,0) #\u83b7\u53d6\u5f53\u524d\u7684\u804a\u5929\u8ba1\u6570\u5668\u503c&#xff0c;\u5982\u679c\u6ca1\u6709\u5219\u9ed8\u8ba4\u4e3a0<br \/>\nnew_count &#061; state &#043; 1<br \/>\nupdate &#061; {<br \/>\n&#034;messages&#034; :[<br \/>\nToolMessage(content&#061;&#034;Successfully updated agent state&#034;,tool_call_id &#061; runtime.tool_call_id)<br \/>\n],<br \/>\n&#034;chat_count&#034;: new_count<br \/>\n}<br \/>\nreturn Command(update &#061; update)<br \/>\n#\u521b\u5efaagent<br \/>\nagent &#061; create_agent(model,<br \/>\ntools&#061;[get_increse_chat_count],<br \/>\nstate_schema&#061;State,<br \/>\ncheckpointer&#061; InMemorySaver())<br \/>\n#state_schema&#061;State\u8868\u793a\u72b6\u6001\u6570\u636e\u7c7b\u4e3aState&#xff0c;agent\u5728\u8c03\u7528\u5de5\u5177\u65f6\u4f1a\u5c06\u72b6\u6001\u6570\u636e\u7c7b\u5b9e\u4f8b\u4f20\u5165\u5de5\u5177\u7684runtime\u4e2d\u3002<br \/>\n#\u4f1a\u8bdd\u6807\u8bc6<br \/>\nconfig &#061; {&#034;configurable&#034;:{&#034;thread_id&#034;:&#034;1&#034;}}<br \/>\n#\u6a21\u578b\u8c03\u7528<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e00\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke({&#034;messages&#034;:&#034;\u4f60\u597d&#xff0c;\u6211\u662f\u5f20\u4e09&#xff0c;\u6211\u559c\u6b22\u859b\u4e4b\u8c26\u7684\u6b4c&#xff0c;\u6211\u60f3\u548c\u4f60\u804a\u804a\u859b\u4e4b\u8c26\u7684\u6b4c\u3002&#034;},<br \/>\nconfig )<br \/>\nprint(response)<br \/>\nagent.get_state(config)<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e8c\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061;agent.invoke({&#034;messages&#034;:&#034;\u4f60\u77e5\u9053\u6f14\u5458\u8fd9\u9996\u6b4c\u66f2\u5417&#xff1f;&#034;}, config)<br \/>\nprint(response)<br \/>\nagent.get_state(config)<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e09\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke({&#034;messages&#034;:&#034;\u4f60\u77e5\u9053\u6211\u548c\u4f60\u5df2\u7ecf\u804a\u4e86\u591a\u5c11\u6b21\u4e86\u5417&#xff1f;&#034;}, config)<br \/>\nprint(response)<br \/>\nagent.get_state(config)<br \/>\nconfig2 &#061; {&#034;configurable&#034;:{&#034;thread_id&#034;:&#034;2&#034;}}<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u56db\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke({&#034;messages&#034;:&#034;\u4f60\u77e5\u9053\u6211\u548c\u4f60\u5df2\u7ecf\u804a\u4e86\u591a\u5c11\u6b21\u4e86\u5417&#xff1f;&#034;}, config2)<br \/>\nprint(response)<br \/>\nagent.get_state(config2) <\/p>\n<h2>4.Store&#xff08;\u957f\u671f\u8bb0\u5fc6&#xff09;<\/h2>\n<p>store\u662fLangChain\u63d0\u4f9b\u7684\u957f\u671f\u8bb0\u5fc6\u673a\u5236&#xff0c;\u7528\u4e8e\u5728\u4e0d\u540c\u4f1a\u8bdd\u95f4\u5171\u4eab\u6570\u636e\u3002\u4f8b\u5982&#xff1a;\u6a21\u578b\u4ee5\u5916\u7684\u6570\u636e\u3001\u7528\u6237\u504f\u597d\u7b49\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u540c\u4e00\u4f4d\u7528\u6237\u53ef\u80fd\u5148\u5728 thread-a \u4e2d\u544a\u8bc9 Agent\u201c\u6211\u559c\u6b22\u4ee3\u7801\u793a\u4f8b\u4f18\u5148\u201d&#xff0c;\u51e0\u5929\u540e\u53c8\u5728 thread-b \u4e2d\u5f00\u59cb\u65b0\u7684\u8bfe\u7a0b\u54a8\u8be2\u3002<\/p>\n<p>\u5982\u679c\u8fd9\u6761\u504f\u597d\u53ea\u653e\u5728 State \u4e2d&#xff0c;\u65b0 thread \u65e0\u6cd5\u81ea\u52a8\u8bfb\u53d6&#xff1b;\u628a\u5b83\u5199\u5165 Store \u540e&#xff0c;\u4e24\u4e2a thread \u90fd\u53ef\u4ee5\u8bbf\u95ee\u3002<\/p>\n<p>LangChain\u63d0\u4f9b\u4e86\u591a\u79cdStore\u7684\u5b9e\u73b0\u65b9\u5f0f&#xff0c;\u4f8b\u5982&#xff1a;<\/p>\n<ul>\n<li>\n<p>InMemoryStore<\/p>\n<\/li>\n<li>\n<p>PostgresStore<\/p>\n<\/li>\n<li>\n<p>RedisStore<\/p>\n<\/li>\n<li>\n<p>&#8230;<\/p>\n<\/li>\n<\/ul>\n<p>\u8bfe\u7a0b\u4e2d\u6211\u4eec\u4ee5InMemoryStore\u4e3a\u4f8b\u6765\u5b66\u4e60\u3002<\/p>\n<h3>4.1Store\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<p>Store\u7684\u6570\u636e\u683c\u5f0f\u662fJSON\u6587\u6863&#xff0c;JSON\u6587\u6863\u91c7\u7528\u5206\u7ea7\u7ba1\u7406&#xff1a;<\/p>\n<ul>\n<li>\n<p>Namespace&#xff08;\u547d\u540d\u7a7a\u95f4&#xff09;&#xff1a;\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u6587\u4ef6\u5939<\/p>\n<ul>\n<li>\n<p>Key&#xff08;\u952e&#xff09;&#xff1a;\u53ef\u4ee5\u7406\u89e3\u4e3a\u6587\u4ef6\u540d&#xff0c;\u5fc5\u987b\u552f\u4e00<\/p>\n<\/li>\n<li>\n<p>Value&#xff08;\u503c&#xff09;&#xff1a;\u8981\u5b58\u50a8\u7684JSON\u6587\u6863<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u6240\u4ee5&#xff0c;\u5b9a\u4f4dStore\u4e2d\u7684\u4e00\u6761\u6570\u636e&#xff0c;\u9700\u8981\u4e24\u90e8\u5206\u4fe1\u606f&#xff1a;namespace &#043; key&#xff0c;\u5176\u4e2dnamespace\u662f\u4e00\u4e2a\u5143\u7ec4&#xff0c;\u800ckey\u5219\u662f\u666e\u901a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u5728\u591a\u79df\u6237\u7684\u5728\u7ebf\u6559\u80b2Agent&#xff0c;\u6211\u4eec\u8981\u8bb0\u5f55\u6bcf\u4e2a\u7528\u6237\u7684\u5b66\u4e60\u504f\u597d&#xff0c;namespace\u548ckey\u5206\u522b\u53ef\u4ee5\u91c7\u7528\u4e0b\u9762\u7684\u7ec4\u7ec7\u65b9\u5f0f\u3002<\/p>\n<table>\n<tr>\n<p>namespace<\/p>\n<p>key<\/p>\n<p>value<\/p>\n<\/tr>\n<tbody>\n<tr>\n<td colspan=\"1\" rowspan=\"2\">\n<p>(&#034;school_a&#034;, &#034;u001&#034; )<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>&#034;learning_preferences&#034;<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>{&#034;\u5185\u5bb9\u987a\u5e8f&#034;:&#034;\u5148\u5199\u4ee3\u7801&#xff0c;\u518d\u89e3\u91ca\u539f\u7406&#034;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>&#034;coding_preferences&#034;<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>{&#034;\u4ee3\u7801\u98ce\u683c&#034;:&#034;\u7b80\u6d01\u4f46\u8981\u6709\u6ce8\u91ca&#034;}<\/p>\n<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"2\">\n<p>(&#034;school_a&#034;, &#034;u002&#034; )<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>&#034;learning_preferences&#034;<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>{&#034;&#8230;&#034;:&#034;&#8230;&#034;}<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>&#034;coding_preferences&#034;<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>{&#034;&#8230;&#034;:&#034;&#8230;&#034;}<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>USER_DATABASE &#061; {<br \/>\n    &#034;school1&#034;:{&#034;id_01&#034;:{&#034;name&#034;:&#034;\u5f20\u4e09&#034;,&#034;course&#034; :&#034;java\u5b9e\u8df5&#034;},<br \/>\n               &#034;id_02&#034;:{&#034;name&#034;:&#034;\u674e\u56db&#034;,&#034;course&#034; :&#034;python\u5b9e\u8df5&#034;}<br \/>\n               },<br \/>\n    &#034;school2&#034;:{&#034;id_01&#034;:{&#034;name&#034;:&#034;\u738b\u4e94&#034;,&#034;course&#034; :&#034;c&#043;&#043;\u5b9e\u8df5&#034;},<br \/>\n               &#034;id_02&#034;:{&#034;name&#034;:&#034;\u8d75\u516d&#034;,&#034;course&#034; :&#034;go\u5b9e\u8df5&#034;}<br \/>\n               }<br \/>\n}<br \/>\n&#064;dataclass(frozen&#061;True) #\u5b9a\u4e49\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b<br \/>\nclass USERCONTEXT():<br \/>\n    &#034;&#034;&#034;agent\u7684\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b&#034;&#034;&#034;<br \/>\n    school_id: str<br \/>\n    user_id: str <\/p>\n<p>Store \u7684\u8bbe\u8ba1\u662f\u4e09\u5c42\u7ed3\u6784,\u4f46\u662f\u5b83\u5b58\u50a8\u65b9\u5f0f\u662f\u53ef\u4ee5\u6709\u591a\u79cd\u7684\u3002<\/p>\n<p>\u4e00\u79cd\u662f\u4e00\u6761\u8bb0\u5f55 \u2192 \u552f\u4e00\u7ed1\u5b9a\u4e00\u4e2a value\u3002<\/p>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<li>\u53ef\u4ee5\u5355\u72ec\u589e\u3001\u5220\u3001\u6539\u67d0\u4e00\u6761\u8bb0\u5fc6&#xff0c;\u4e92\u4e0d\u5f71\u54cd<\/li>\n<li>\u9002\u5408\u96f6\u6563\u3001\u72ec\u7acb\u7684\u8bb0\u5fc6\u7247\u6bb5&#xff08;\u6bd4\u5982\u77e5\u8bc6\u5e93\u7247\u6bb5\u3001\u591a\u6761\u7528\u6237\u4e8b\u5b9e&#xff09;<\/li>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<li>\u60f3\u8981\u4e00\u6b21\u6027\u62ff\u5230\u5168\u90e8\u8bb0\u5fc6&#xff0c;\u5fc5\u987b\u63d0\u524d\u77e5\u9053\u6240\u6709 key \u7684\u540d\u5b57&#xff0c;\u4e0d\u65b9\u4fbf<\/li>\n<li>\u7ba1\u7406\u5927\u91cf\u8bb0\u5fc6\u65f6&#xff0c;key \u4f1a\u8d8a\u6765\u8d8a\u591a<\/li>\n<p>namespace &#061; (&#034;school_a&#034;, &#034;u001&#034;)<br \/>\n\u251c\u2500 key &#061; &#034;fav_singer&#034; \u2192 value &#061; &#034;\u859b\u4e4b\u8c26&#034;<br \/>\n\u251c\u2500 key &#061; &#034;fav_song&#034; \u2192 value &#061; &#034;\u9a86\u9a7c&#034;<br \/>\n\u2514\u2500 key &#061; &#034;hobby&#034; \u2192 value &#061; &#034;\u6253\u7bee\u7403&#034; <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1112\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/11\/20261119023257-6afe5fd92cfdc.png\" width=\"1301\" \/><\/p>\n<p>\u5b9e\u73b0\u4ee3\u7801&#xff1a;<\/p>\n<p>#\u5199\u5165\u5de5\u5177\u4ee3\u7801<br \/>\n&#064;tool<br \/>\ndef save_memory_separate(<br \/>\n    memory_key: str,<br \/>\n    memory_value: str,<br \/>\n    runtime: ToolRuntime[USERCONTEXT]<br \/>\n):<br \/>\n    &#034;&#034;&#034;\u65b9\u6848A&#xff1a;\u6bcf\u6761\u8bb0\u5fc6\u5355\u72ec\u4f5c\u4e3astore\u7684\u4e00\u4e2akey&#034;&#034;&#034;<br \/>\n    namespace &#061; get_user_id(runtime.context)  # (&#034;school_a&#034;, &#034;u001&#034;)<br \/>\n    # \u76f4\u63a5\u7528\u8bb0\u5fc6\u540d\u79f0\u4f5c\u4e3astore\u7684key<br \/>\n    runtime.store.put(namespace, memory_key, memory_value)<br \/>\n    return f&#034;\u4fdd\u5b58\u6210\u529f&#xff1a;{memory_key} &#061; {memory_value}&#034;<br \/>\n#\u8bfb\u53d6\u4ee3\u7801<br \/>\n&#064;tool<br \/>\ndef get_all_memory_separate(runtime: ToolRuntime[USERCONTEXT]):<br \/>\nnamespace &#061; get_user_id(runtime.context)<br \/>\n# get\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662fkey\u5217\u8868&#xff0c;\u6279\u91cf\u8bfb\u53d6\u591a\u6761\u72ec\u7acb\u8bb0\u5f55<br \/>\nitems &#061; runtime.store.get(namespace, [&#034;fav_singer&#034;, &#034;fav_song&#034;, &#034;hobby&#034;])<br \/>\nres &#061; {}<br \/>\nfor item in items:<br \/>\nres[item.key] &#061; item.value<br \/>\nreturn f&#034;\u7528\u6237\u8bb0\u5fc6&#xff1a;{res}&#034;<br \/>\n\u76f4\u63a5\u5220\u9664\u4e00\u6761&#xff0c;\u4e0d\u5f71\u54cd\u522b\u7684\u8bb0\u5fc6<br \/>\nruntime.store.delete(namespace, &#034;hobby&#034;) <\/p>\n<p>\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\u662f\u5355\u4e2a Store Key&#xff0c;value \u5305\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<li>\u8bfb\u53d6\u8d85\u7ea7\u65b9\u4fbf&#xff0c;\u4e00\u6b21 get \u62ff\u5230\u7528\u6237\u5168\u90e8\u504f\u597d&#xff0c;\u9002\u5408\u7528\u6237\u753b\u50cf\u3001\u504f\u597d\u914d\u7f6e<\/li>\n<li>\u4e0d\u7528\u7ef4\u62a4\u4e00\u5806\u96f6\u6563\u7684 store key<\/li>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<li>\u4fee\u6539\u3001\u5220\u9664\u5355\u6761\u504f\u597d&#xff0c;\u5fc5\u987b\u5168\u91cf\u8bfb\u51fa\u5b57\u5178\u3001\u6539\u5b8c\u518d\u6574\u4f53\u5199\u56de<\/li>\n<li>\u5e76\u53d1\u573a\u666f\u4e0b\u5bb9\u6613\u51fa\u73b0\u8986\u76d6\u4e22\u5931&#xff08;\u591a\u4eba\u540c\u65f6\u4fee\u6539\u540c\u4e00\u4efd\u5b57\u5178&#xff09;<\/li>\n<p>namespace &#061; (&#034;school_a&#034;, &#034;u001&#034;)<br \/>\n\u2514\u2500 key &#061; &#034;user_preference&#034; \u2192 value &#061; {<br \/>\n    &#034;fav_singer&#034;: &#034;\u859b\u4e4b\u8c26&#034;,<br \/>\n    &#034;fav_song&#034;: &#034;\u6f14\u5458&#034;,<br \/>\n    &#034;hobby&#034;: &#034;\u6253\u6e38\u620f&#034;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"639\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/11\/20261119023257-6afe5fd9a6238.png\" width=\"1200\" \/><\/p>\n<p>\u5199\u5165\u5de5\u5177\u4ee3\u7801<br \/>\n&#064;tool<br \/>\ndef save_memory_in_dict(<br \/>\n    pref_name: str,<br \/>\n    pref_value: str,<br \/>\n    runtime: ToolRuntime[USERCONTEXT]<br \/>\n):<br \/>\n    &#034;&#034;&#034;\u65b9\u6848B&#xff1a;\u5168\u90e8\u504f\u597d\u653e\u5728\u540c\u4e00\u4e2astore-key\u7684\u5b57\u5178\u91cc\u9762&#034;&#034;&#034;<br \/>\n    namespace &#061; get_user_id(runtime.context)<br \/>\n    STORE_KEY &#061; &#034;user_preference&#034;  # store\u5c42\u9762\u56fa\u5b9a\u552f\u4e00key<br \/>\n# \u8bfb\u53d6\u8fd9\u4e2akey\u5bf9\u5e94\u7684\u5b57\u5178<br \/>\nitems &#061; runtime.store.get(namespace, [STORE_KEY])<br \/>\nif items:<br \/>\n    pref_dict &#061; items[0].value<br \/>\nelse:<br \/>\n    pref_dict &#061; {}<br \/>\n\u5728\u5b57\u5178\u5185\u90e8\u65b0\u589e\/\u4fee\u6539\u5b57\u6bb5<br \/>\npref_dict[pref_name] &#061; pref_value<br \/>\n\u628a\u6574\u4e2a\u5b57\u5178\u91cd\u65b0\u5199\u56destore&#xff08;\u8986\u76d6\u65e7value&#xff09;<br \/>\nruntime.store.put(namespace, STORE_KEY, pref_dict)<br \/>\nreturn f&#034;\u5df2\u4fdd\u5b58\u504f\u597d&#xff1a;{pref_name}&#061;{pref_value}&#xff0c;\u5f53\u524d\u5168\u90e8\u504f\u597d&#xff1a;{pref_dict}&#034;<br \/>\n#d\u8bfb\u53d6\u4ee3\u7801<br \/>\n&#064;tool<br \/>\ndef get_all_memory_in_dict(runtime: ToolRuntime[USERCONTEXT]):<br \/>\nnamespace &#061; get_user_id(runtime.context)<br \/>\nSTORE_KEY &#061; &#034;user_preference&#034;<br \/>\nitems &#061; runtime.store.get(namespace, [STORE_KEY])<br \/>\nif items:<br \/>\nreturn f&#034;\u7528\u6237\u504f\u597d&#xff1a;{items[0].value}&#034;<br \/>\nreturn &#034;\u6682\u65e0\u504f\u597d\u8bb0\u5fc6&#034;<br \/>\n#\u5220\u9664\u4ee3\u7801&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u5220 store \u91cc\u7684\u4e00\u6761 key&#xff0c;\u8981\u5148\u8bfb\u5b57\u5178 \u2192 \u5220\u9664\u5b57\u5178\u5185\u5b57\u6bb5 \u2192 \u6574\u5b57\u5178\u5199\u56de\u53bb<br \/>\nnamespace &#061; get_user_id(runtime.context)<br \/>\nSTORE_KEY &#061; &#034;user_preference&#034;<br \/>\nitems &#061; runtime.store.get(namespace, [STORE_KEY])<br \/>\npref_dict &#061; items[0].value<br \/>\ndel pref_dict[&#034;hobby&#034;]   # \u5728\u5b57\u5178\u91cc\u9762\u5220\u6389\u8fd9\u4e2a\u5b50key<br \/>\nruntime.store.put(namespace, STORE_KEY, pref_dict) # \u6574\u4f53\u8986\u76d6\u4fdd\u5b58 <\/p>\n<p>\u4e0b\u9762\u6211\u4f7f\u7528\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u8fdb\u884c\u6f14\u793a\u3002<\/p>\n<h3>4.2\u521d\u59cb\u5316Store<\/h3>\n<p>\u9996\u5148\u6211\u4eec\u9700\u8981\u521d\u59cb\u5316Store<\/p>\n<p>model &#061; init_chat_model(model &#061; &#034;deepseek-flash&#034;,extra_body &#061; {&#034;thinking&#034;: {&#034;type&#034;: &#034;disabled&#034;}})<br \/>\nstore &#061; InMemoryStore()<br \/>\ncheckpointer &#061; InMemorySaver() <\/p>\n<p>store\u4e2d\u6709\u4e24\u4e2a\u65b9\u6cd5&#xff0c;\u5206\u522b\u7528\u6765\u8bfb\u53d6\u548c\u5199\u5165Store&#xff1a;<\/p>\n<ul>\n<li>\n<p>store.get(namespace, key)&#xff1a;\u8bfb\u53d6Store<\/p>\n<\/li>\n<li>\n<p>store.put(namespace, key, value)&#xff1a;\u5b58\u5165Store&#xff0c;\u8fd9\u91cc\u7684value\u5fc5\u987b\u662fdict[str,Any]\u7ed3\u6784&#xff0c;\u800c\u4e14dict\u4e2d\u7684\u503c\u5fc5\u987b\u662f\u53ef\u4ee5\u8f6cJSON\u7684&#xff0c;LangChain\u4f1a\u5c06\u5176\u8f6cJSON\u5b58\u50a8<\/p>\n<\/li>\n<\/ul>\n<h3 style=\"background-color:transparent\">4.3\u5728tool\u4e2d\u8bbf\u95eestore<\/h3>\n<p>\u4e0e State \u7c7b\u4f3c&#xff0c;\u8bbf\u95ee Store \u4e5f\u6709\u4e24\u79cd\u573a\u666f&#xff1a;Tool \u548c\u4e2d\u95f4\u4ef6\u3002\u8fd9\u91cc\u5148\u770b Tool&#xff0c;\u540c\u6837\u662f\u901a\u8fc7 runtime \u62ff\u5230 runtime.store\u3002<\/p>\n<p>\u5728 Tool \u4e2d\u8bbf\u95ee Store \u7684\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>runtime.store \u63d0\u4f9b get \/ put \/ delete \u4e09\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p>namespace \u662f\u5143\u7ec4&#xff0c;\u901a\u5e38\u7531 runtime.context \u4e2d\u7684 school_id\u3001user_id \u62fc\u51fa\u6765&#xff0c;\u5b9e\u73b0\u6309\u7528\u6237\u9694\u79bb\u3002<\/p>\n<\/li>\n<li>\n<p>put \u7684 value \u5fc5\u987b\u662f\u53ef\u8f6c JSON \u7684 dict\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u5de5\u5177\u628a\u7528\u6237\u504f\u597d\u5199\u5165 Store&#xff1a;<\/p>\n<p>1.\u9996\u5148\u9700\u8981\u83b7\u53d6namespace<\/p>\n<p>2.\u8bfb\u53d6\u8fd9\u4e2akey\u5bf9\u5e94\u7684\u5b57\u5178<\/p>\n<p>3.\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u5b57\u6bb5<\/p>\n<p>4.\u5c06\u5b57\u5178\u91cd\u65b0\u5199\u5165Store\u4e2d<\/p>\n<p>&#064;tool<br \/>\ndef save_long_memory(<br \/>\n    pref_name: str,<br \/>\n    pref_value: str,<br \/>\n    runtime: ToolRuntime[USERCONTEXT]<br \/>\n):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u4fdd\u5b58\u957f\u671f\u8bb0\u5fc6<br \/>\n    :param pref_name: \u504f\u597d\u540d\u79f0<br \/>\n    :param pref_value: \u504f\u597d\u503c<br \/>\n    :param runtime: \u5de5\u5177\u8fd0\u884c\u65f6\u5bf9\u8c61<br \/>\n    :return: \u4fdd\u5b58\u6210\u529f\u7684\u5185\u5bb9<br \/>\n    &#034;&#034;&#034;<br \/>\n    &#034;&#034;&#034;\u65b9\u6848B&#xff1a;\u5168\u90e8\u504f\u597d\u653e\u5728\u540c\u4e00\u4e2astore-key\u7684\u5b57\u5178\u91cc\u9762&#034;&#034;&#034;<br \/>\n    namespace &#061; get_user_id(runtime.context)<br \/>\n# \u8bfb\u53d6\u8fd9\u4e2akey\u5bf9\u5e94\u7684\u5b57\u5178<br \/>\nitems &#061; runtime.store.get(namespace, STORE_KEY)<br \/>\nif items:<br \/>\n    pref_dict &#061; items.value<br \/>\nelse:<br \/>\n    pref_dict &#061; {}<\/p>\n<p># \u5728\u5b57\u5178\u5185\u90e8\u65b0\u589e\/\u4fee\u6539\u5b57\u6bb5<br \/>\npref_dict[pref_name] &#061; pref_value<br \/>\n# \u628a\u6574\u4e2a\u5b57\u5178\u91cd\u65b0\u5199\u56destore&#xff08;\u8986\u76d6\u65e7value&#xff09;<br \/>\nruntime.store.put(namespace, STORE_KEY, pref_dict)<br \/>\nreturn f&#034;\u5df2\u4fdd\u5b58\u504f\u597d&#xff1a;{pref_name}&#061;{pref_value}&#xff0c;\u5f53\u524d\u5168\u90e8\u504f\u597d&#xff1a;{pref_dict}&#034; <\/p>\n<p>\u83b7\u53d6\u957f\u671f\u8bb0\u5fc6&#xff1a;<\/p>\n<p>\u83b7\u53d6\u957f\u671f\u8bb0\u5fc6&#xff1a;<br \/>\n&#064;tool<br \/>\ndef get_long_memory(runtime: ToolRuntime[USERCONTEXT]):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u83b7\u53d6\u957f\u671f\u8bb0\u5fc6<br \/>\n    :param runtime: \u5de5\u5177\u8fd0\u884c\u65f6\u5bf9\u8c61<br \/>\n    :return: \u957f\u671f\u8bb0\u5fc6<br \/>\n    &#034;&#034;&#034;<br \/>\n    namespace &#061; get_user_id(runtime.context) # \u83b7\u901a\u8fc7\u4e0a\u4e0b\u6587\u4e2d\u7684\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b\u5b9e\u4f8b\u83b7\u53d6\u6216\u8005\u6784\u5efa\u547d\u540d\u7a7a\u95f4<br \/>\n#\u901a\u8fc7namespace\u548ckey\u83b7\u53d6\u7528\u6237\u7684\u957f\u671f\u8bb0\u5fc6,\u8fd4\u56de\u7ed3\u679c\u662f\u4e00\u4e2a\u5b57\u5178\u7c7b\u578b&#xff0c;\u5982\u679c\u6ca1\u6709\u5219\u8fd4\u56deNone<br \/>\nitem &#061; runtime.store.get(namespace, STORE_KEY)<br \/>\npreference &#061; item.value if item else {}<\/p>\n<p>return f&#034;\u5f53\u524d\u504f\u597d\u5217\u8868\u4e3a&#xff1a;{preference}&#034; <\/p>\n<h3>4.4\u521b\u5efaagent\u5e76\u6d4b\u8bd5<\/h3>\n<p>\u521b\u5efa Agent&#xff1a;\u8c03\u7528 create_agent&#xff0c;\u628a\u6a21\u578b\u3001\u5de5\u5177\u3001context_schema\u3001checkpointer\u3001store \u548c system_prompt \u4e00\u6b21\u6027\u6ce8\u518c\u8fdb\u53bb\u3002<\/p>\n<p>\u51c6\u5907\u8fd0\u884c\u53c2\u6570&#xff1a;\u6784\u9020 context&#xff0c;\u4ee5\u53ca\u4e24\u4e2a\u4e0d\u540c thread_id \u7684 config&#xff0c;\u7528\u6765\u6a21\u62df\u540c\u4e00\u4f1a\u8bdd\u548c\u65b0\u4f1a\u8bdd\u3002<\/p>\n<p>\u524d\u4e09\u8f6e\u5199\u5165\u8bb0\u5fc6&#xff1a;\u5728 thread_id&#061;1 \u7684\u4f1a\u8bdd\u91cc\u8fde\u7eed\u544a\u8bc9 Agent \u4e09\u6761\u5b66\u4e60\u504f\u597d&#xff0c;\u6a21\u578b\u5224\u65ad\u9700\u8981\u4fdd\u5b58\u65f6\u8c03\u7528 save_long_memory&#xff0c;\u628a\u504f\u597d\u5199\u8fdb Store\u3002<\/p>\n<p>\u7b2c\u56db\u8f6e\u8de8\u4f1a\u8bdd\u8bfb\u53d6&#xff1a;\u6362\u5230 thread_id&#061;2 \u8fd9\u4e2a\u5168\u65b0\u4f1a\u8bdd&#xff0c;\u95ee\u201c\u6211\u7684\u5b66\u4e60\u4e60\u60ef\u662f\u4ec0\u4e48&#xff1f;\u201d&#xff0c;\u6a21\u578b\u8c03\u7528 get_long_memory \u4ece Store \u4e2d\u8bfb\u51fa\u4e4b\u524d\u4fdd\u5b58\u7684\u504f\u597d&#xff0c;\u9a8c\u8bc1\u957f\u671f\u8bb0\u5fc6\u8de8\u4f1a\u8bdd\u53ef\u7528\u3002<\/p>\n<p>\u7b2c\u4e94\u8f6e\u7efc\u5408\u5e94\u7528&#xff1a;\u7ee7\u7eed\u5728\u65b0\u4f1a\u8bdd\u91cc\u8ba9 Agent \u8bb2\u89e3\u4e8c\u5206\u67e5\u627e&#xff0c;\u89c2\u5bdf\u5b83\u662f\u5426\u4f1a\u7ed3\u5408 Store \u4e2d\u7684\u504f\u597d&#xff08;\u5148\u770b\u4ee3\u7801\u3001\u901a\u4fd7\u6613\u61c2\u3001\u7ed3\u5408\u4ee3\u7801&#xff09;\u6765\u7ec4\u7ec7\u56de\u7b54\u3002<\/p>\n<p>agent &#061; create_agent(<br \/>\n\u00a0 \u00a0 model &#061; model,<br \/>\n\u00a0 \u00a0 tools&#061;[get_user_profile,save_long_memory,get_long_memory],<br \/>\n\u00a0 \u00a0 context_schema&#061;USERCONTEXT,<br \/>\n\u00a0 \u00a0 checkpointer &#061; checkpointer,<br \/>\n\u00a0 \u00a0 store &#061; store,<br \/>\n\u00a0 \u00a0 system_prompt&#061; &#034;\u4f60\u662f\u8bfe\u7a0b\u5e2e\u52a9\u52a9\u624b&#xff0c;\u5728\u9700\u8981\u7528\u6237\u8d44\u6599\u5ba4\u8c03\u7528\u8d44\u6599\u5de5\u5177&#xff0c;\u6d89\u53ca\u957f\u671f\u5b66\u4e60\u504f\u597d\u6216\u8005\u4ee3\u7801\u89e3\u91ca\u65f6\u8c03\u7528\u957f\u671f\u8bb0\u5fc6\u5de5\u5177\u3002&#034;<br \/>\n)<br \/>\ncontext &#061; USERCONTEXT(user_id&#061;&#034;id_02&#034;, school_id&#061;&#034;school1&#034;)<br \/>\nconfig1 &#061; {&#034;configurable&#034;:{&#034;thread_id&#034;:&#034;1&#034;}}<br \/>\nconfig2 &#061; {&#034;configurable&#034;:{&#034;thread_id&#034;:&#034;2&#034;}}<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e00\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n{<br \/>\n&#034;messages&#034; : [HumanMessage(&#034;\u6211\u559c\u6b22\u5148\u770b\u4ee3\u7801\u518d\u770b\u539f\u7406&#034;)]<br \/>\n},<br \/>\nconfig &#061; config1,<br \/>\ncontext &#061; context<br \/>\n)<br \/>\nfor message in response[&#034;messages&#034;]:<br \/>\nmessage.pretty_print()<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e8c\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n{<br \/>\n&#034;messages&#034; : [HumanMessage(&#034;\u6211\u559c\u6b22\u8bb2\u89e3\u539f\u7406\u80fd\u591f\u901a\u4fd7\u6613\u61c2\u4e9b&#034;)]<br \/>\n},<br \/>\nconfig &#061; config1,<br \/>\ncontext &#061; context<br \/>\n)<br \/>\nfor message in response[&#034;messages&#034;]:<br \/>\nmessage.pretty_print()<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e09\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n{<br \/>\n&#034;messages&#034; : [HumanMessage(&#034;\u6211\u559c\u6b22\u8bb2\u89e3\u539f\u7406\u80fd\u591f\u7ed3\u5408\u4ee3\u7801&#034;)]<br \/>\n},<br \/>\nconfig &#061; config1,<br \/>\ncontext &#061; context<br \/>\n)<br \/>\nfor message in response[&#034;messages&#034;]:<br \/>\nmessage.pretty_print()<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u56db\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n{<br \/>\n&#034;messages&#034; : [HumanMessage(&#034;\u6211\u7684\u5b66\u4e60\u4e60\u60ef\u662f\u4ec0\u4e48&#xff1f;&#034;)]<br \/>\n},<br \/>\nconfig &#061; config2,<br \/>\ncontext &#061; context<br \/>\n)<br \/>\nfor message in response[&#034;messages&#034;]:<br \/>\nmessage.pretty_print()<br \/>\nprint(&#034;&#8212;&#8212;-\u7b2c\u4e94\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n{&#034;messages&#034; : [HumanMessage(&#034;\u7ed9\u6211\u8bb2\u89e3\u4e00\u4e0b\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u539f\u7406\u548c\u4ee3\u7801\u5b9e\u73b0&#034;)]} ,<br \/>\nconfig &#061; config2,<br \/>\ncontext &#061; context<br \/>\n) <\/p>\n<p>\u901a\u8fc7\u7b2c\u56db\u8f6e\u7684\u5bf9\u8bdd\u5c31\u53ef\u4ee5\u770b\u51fa&#xff0c;\u6211\u4eec\u7684\u6a21\u578b\u5177\u6709\u4e86\u8de8\u5bf9\u8bdd\u7684\u80fd\u529b&#xff0c;\u4e5f\u5c31\u5177\u5907\u957f\u671f\u8bb0\u5fc6\u7684\u529f\u80fd\u3002<\/p>\n<p>&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; Human Message &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u6211\u7684\u5b66\u4e60\u4e60\u60ef\u662f\u4ec0\u4e48&#xff1f;<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; Ai Message &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\nTool Calls:<br \/>\nget_long_memory (call_00_L4UpIP4sW73Nj7GnMLzP9493)<br \/>\nCall ID: call_00_L4UpIP4sW73Nj7GnMLzP9493<br \/>\nArgs:<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; Tool Message &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\nName: get_long_memory<br \/>\n\u5f53\u524d\u504f\u597d\u5217\u8868\u4e3a&#xff1a;{&#039;\u5b66\u4e60\u987a\u5e8f\u504f\u597d&#039;: &#039;\u559c\u6b22\u5148\u770b\u4ee3\u7801\u518d\u770b\u539f\u7406&#xff08;\u4ee3\u7801\u4f18\u5148&#xff0c;\u7136\u540e\u8bb2\u89e3\u80cc\u540e\u7684\u539f\u7406&#xff09;&#039;, &#039;\u539f\u7406\u8bb2\u89e3\u98ce\u683c\u504f\u597d&#039;: &#039;\u5e0c\u671b\u539f\u7406\u8bb2\u89e3\u901a\u4fd7\u6613\u61c2&#xff1a;\u591a\u7528\u751f\u6d3b\u5316\u7c7b\u6bd4\u3001\u5c11\u7528\u62bd\u8c61\u672f\u8bed\u5806\u780c&#xff0c;\u5148\u8bb2\u76f4\u89c2\u7406\u89e3\u518d\u8bb2\u7cbe\u786e\u8868\u8ff0&#xff1b;\u4e14\u539f\u7406\u5fc5\u987b\u7ed3\u5408\u4ee3\u7801\u6765\u8bb2&#xff08;\u7528\u4ee3\u7801\u7247\u6bb5\/\u8fd0\u884c\u7ed3\u679c\u5370\u8bc1\u539f\u7406&#xff0c;\u907f\u514d\u7eaf\u7406\u8bba\u7a7a\u8bb2&#xff09;&#039;}<br \/>\n&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; Ai Message &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n\u6839\u636e\u957f\u671f\u8bb0\u5fc6&#xff0c;\u4f60\u7684\u5b66\u4e60\u4e60\u60ef\u6709\u4e24\u6761&#xff1a;<br \/>\n1. \u5b66\u4e60\u987a\u5e8f&#xff1a;\u4ee3\u7801\u4f18\u5148<br \/>\n&gt; \u559c\u6b22\u5148\u770b\u4ee3\u7801&#xff0c;\u518d\u770b\u539f\u7406\u3002\u4e5f\u5c31\u662f\u5148\u62ff\u5230\u80fd\u8dd1\u7684\u4e1c\u897f&#xff0c;\u518d\u56de\u5934\u7406\u89e3\u5b83\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u5de5\u4f5c\u3002<br \/>\n2. \u539f\u7406\u8bb2\u89e3\u98ce\u683c&#xff1a;\u901a\u4fd7 &#043; \u7ed3\u5408\u4ee3\u7801<br \/>\n&gt; &#8211; \u901a\u4fd7\u6613\u61c2&#xff1a;\u591a\u7528\u751f\u6d3b\u5316\u7c7b\u6bd4&#xff0c;\u5c11\u5806\u62bd\u8c61\u672f\u8bed&#xff0c;\u5148\u8bb2\u76f4\u89c2\u7406\u89e3\u518d\u7ed9\u7cbe\u786e\u8868\u8ff0<br \/>\n&gt; &#8211; \u539f\u7406\u5fc5\u987b\u7ed3\u5408\u4ee3\u7801\u5370\u8bc1&#xff1a;\u7528\u4ee3\u7801\u7247\u6bb5\u548c\u8fd0\u884c\u7ed3\u679c\u6765\u8bf4\u660e\u539f\u7406&#xff0c;\u4e0d\u63a5\u53d7\u7eaf\u7406\u8bba\u7a7a\u8bb2<br \/>\n\u5408\u8d77\u6765\u5c31\u662f\u4f60\u7684\u7406\u60f3\u8bb2\u89e3\u8282\u594f&#xff1a;\u4ee3\u7801\u793a\u4f8b \u2192 \u7c7b\u6bd4\u8bb2\u6e05\u673a\u5236 \u2192 \u6bcf\u4e00\u70b9\u90fd\u56de\u6263\u5230\u4ee3\u7801 \u2192 \u6700\u540e\u4e25\u8c28\u8865\u5145\u3002<br \/>\n\u8981\u4e0d\u8981\u6211\u6309\u8fd9\u4e2a\u8282\u594f\u7ed9\u4f60\u8bb2\u4e00\u4e2a\u5177\u4f53\u4e3b\u9898&#xff1f; <\/p>\n<p>\u5b8c\u6574\u4ee3\u7801&#xff1a;<\/p>\n<p>from dataclasses import dataclass<br \/>\nfrom dotenv import load_dotenv<br \/>\nfrom langchain.chat_models import init_chat_model<br \/>\nfrom langchain.tools import tool<br \/>\nfrom langgraph.store.memory import InMemoryStore<br \/>\nfrom langgraph.prebuilt import ToolRuntime<br \/>\nfrom langgraph.checkpoint.memory import InMemorySaver<br \/>\nfrom langchain.agents import create_agent<br \/>\nfrom langchain_core.messages import SystemMessage, HumanMessage,AIMessage,ToolMessage<br \/>\nload_dotenv(encoding&#061;&#034;utf-8&#034;)<\/p>\n<p>model &#061; init_chat_model(model &#061; &#034;deepseek-flash&#034;,extra_body &#061; {&#034;thinking&#034;: {&#034;type&#034;: &#034;disabled&#034;}})<br \/>\nstore &#061; InMemoryStore()<br \/>\ncheckpointer &#061; InMemorySaver()<\/p>\n<p>USER_DATABASE &#061; {<br \/>\n    &#034;school1&#034;:{&#034;id_01&#034;:{&#034;name&#034;:&#034;\u5f20\u4e09&#034;,&#034;course&#034; :&#034;java\u5b9e\u8df5&#034;},<br \/>\n               &#034;id_02&#034;:{&#034;name&#034;:&#034;\u674e\u56db&#034;,&#034;course&#034; :&#034;python\u5b9e\u8df5&#034;}<br \/>\n               },<br \/>\n    &#034;school2&#034;:{&#034;id_01&#034;:{&#034;name&#034;:&#034;\u738b\u4e94&#034;,&#034;course&#034; :&#034;c&#043;&#043;\u5b9e\u8df5&#034;},<br \/>\n               &#034;id_02&#034;:{&#034;name&#034;:&#034;\u8d75\u516d&#034;,&#034;course&#034; :&#034;go\u5b9e\u8df5&#034;}<br \/>\n               }<br \/>\n}<br \/>\n&#064;dataclass(frozen&#061;True) #\u5b9a\u4e49\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b<br \/>\nclass USERCONTEXT():<br \/>\n    &#034;&#034;&#034;agent\u7684\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b&#034;&#034;&#034;<br \/>\n    school_id: str<br \/>\n    user_id: str<\/p>\n<p>#\u6839\u636econtext\u4e2d\u7684user_id\u548cschool_id\u83b7\u53d6\u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f<br \/>\ndef get_user_id(ctx: USERCONTEXT):<br \/>\n    return (ctx.school_id, ctx.user_id)<\/p>\n<p>STORE_KEY &#061; &#034;user_preference&#034;  # store\u5c42\u9762\u56fa\u5b9a\u552f\u4e00key<\/p>\n<p>&#064;tool<br \/>\ndef get_user_profile(runtime : ToolRuntime[USERCONTEXT]):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u901a\u8fc7\u7528\u6237ID\u548c\u5b66\u6821ID\u83b7\u53d6\u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f<br \/>\n    :param runtime: \u5de5\u5177\u8fd0\u884c\u65f6\u5bf9\u8c61<br \/>\n    :return: \u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f<br \/>\n    &#034;&#034;&#034;<br \/>\n    cnt &#061; runtime.context  #\u83b7\u53d6\u4e0a\u4e0b\u6587\u4e2d\u7684\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b\u5b9e\u4f8b<\/p>\n<p>    profile &#061; USER_DATABASE.get(cnt.school_id).get(cnt.user_id)<br \/>\n    return {<br \/>\n        f&#034;name: {profile[&#034;name&#034;]}&#034;,<br \/>\n        f&#034;course: {profile[&#034;course&#034;]}&#034;<br \/>\n    }<\/p>\n<p>&#064;tool<br \/>\ndef save_long_memory(<br \/>\n    pref_name: str,<br \/>\n    pref_value: str,<br \/>\n    runtime: ToolRuntime[USERCONTEXT]<br \/>\n):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u4fdd\u5b58\u957f\u671f\u8bb0\u5fc6<br \/>\n    :param pref_name: \u504f\u597d\u540d\u79f0<br \/>\n    :param pref_value: \u504f\u597d\u503c<br \/>\n    :param runtime: \u5de5\u5177\u8fd0\u884c\u65f6\u5bf9\u8c61<br \/>\n    :return: \u4fdd\u5b58\u6210\u529f\u7684\u5185\u5bb9<br \/>\n    &#034;&#034;&#034;<br \/>\n    &#034;&#034;&#034;\u65b9\u6848B&#xff1a;\u5168\u90e8\u504f\u597d\u653e\u5728\u540c\u4e00\u4e2astore-key\u7684\u5b57\u5178\u91cc\u9762&#034;&#034;&#034;<br \/>\n    namespace &#061; get_user_id(runtime.context)<\/p>\n<p>    # \u8bfb\u53d6\u8fd9\u4e2akey\u5bf9\u5e94\u7684\u5b57\u5178<br \/>\n    items &#061; runtime.store.get(namespace, STORE_KEY)<br \/>\n    if items:<br \/>\n        pref_dict &#061; items.value<br \/>\n    else:<br \/>\n        pref_dict &#061; {}<\/p>\n<p>    # \u5728\u5b57\u5178\u5185\u90e8\u65b0\u589e\/\u4fee\u6539\u5b57\u6bb5<br \/>\n    pref_dict[pref_name] &#061; pref_value<br \/>\n    # \u628a\u6574\u4e2a\u5b57\u5178\u91cd\u65b0\u5199\u56destore&#xff08;\u8986\u76d6\u65e7value&#xff09;<br \/>\n    runtime.store.put(namespace, STORE_KEY, pref_dict)<br \/>\n    return f&#034;\u5df2\u4fdd\u5b58\u504f\u597d&#xff1a;{pref_name}&#061;{pref_value}&#xff0c;\u5f53\u524d\u5168\u90e8\u504f\u597d&#xff1a;{pref_dict}&#034;<\/p>\n<p>&#064;tool<br \/>\ndef get_long_memory(runtime: ToolRuntime[USERCONTEXT]):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u83b7\u53d6\u957f\u671f\u8bb0\u5fc6<br \/>\n    :param runtime: \u5de5\u5177\u8fd0\u884c\u65f6\u5bf9\u8c61<br \/>\n    :return: \u957f\u671f\u8bb0\u5fc6<br \/>\n    &#034;&#034;&#034;<br \/>\n    namespace &#061; get_user_id(runtime.context) # \u83b7\u901a\u8fc7\u4e0a\u4e0b\u6587\u4e2d\u7684\u7528\u6237\u5185\u5bb9\u6570\u636e\u7c7b\u5b9e\u4f8b\u83b7\u53d6\u6216\u8005\u6784\u5efa\u547d\u540d\u7a7a\u95f4<\/p>\n<p>    #\u901a\u8fc7namespace\u548ckey\u83b7\u53d6\u7528\u6237\u7684\u957f\u671f\u8bb0\u5fc6,\u8fd4\u56de\u7ed3\u679c\u662f\u4e00\u4e2a\u5b57\u5178\u7c7b\u578b&#xff0c;\u5982\u679c\u6ca1\u6709\u5219\u8fd4\u56deNone<br \/>\n    item &#061; runtime.store.get(namespace, STORE_KEY)<br \/>\n    preference &#061; item.value if item else {}<\/p>\n<p>    return f&#034;\u5f53\u524d\u504f\u597d\u5217\u8868\u4e3a&#xff1a;{preference}&#034;<\/p>\n<p>agent &#061; create_agent(<br \/>\n    model &#061; model,<br \/>\n    tools&#061;[get_user_profile,save_long_memory,get_long_memory],<br \/>\n    context_schema&#061;USERCONTEXT,<br \/>\n    checkpointer &#061; checkpointer,<br \/>\n    store &#061; store,<br \/>\n    system_prompt&#061; &#034;\u4f60\u662f\u8bfe\u7a0b\u5e2e\u52a9\u52a9\u624b&#xff0c;\u5728\u9700\u8981\u7528\u6237\u8d44\u6599\u5ba4\u8c03\u7528\u8d44\u6599\u5de5\u5177&#xff0c;\u6d89\u53ca\u957f\u671f\u5b66\u4e60\u504f\u597d\u6216\u8005\u4ee3\u7801\u89e3\u91ca\u65f6\u8c03\u7528\u957f\u671f\u8bb0\u5fc6\u5de5\u5177\u3002&#034;<br \/>\n)<\/p>\n<p>context &#061; USERCONTEXT(user_id&#061;&#034;id_02&#034;, school_id&#061;&#034;school1&#034;)<\/p>\n<p>config1 &#061; {&#034;configurable&#034;:{&#034;thread_id&#034;:&#034;1&#034;}}<br \/>\nconfig2 &#061; {&#034;configurable&#034;:{&#034;thread_id&#034;:&#034;2&#034;}}<\/p>\n<p>print(&#034;&#8212;&#8212;-\u7b2c\u4e00\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n    {<br \/>\n        &#034;messages&#034; : [HumanMessage(&#034;\u6211\u559c\u6b22\u5148\u770b\u4ee3\u7801\u518d\u770b\u539f\u7406&#034;)]<br \/>\n    },<br \/>\n    config &#061; config1,<br \/>\n    context &#061; context<br \/>\n)<\/p>\n<p>for message in response[&#034;messages&#034;]:<br \/>\n    message.pretty_print()<\/p>\n<p>print(&#034;&#8212;&#8212;-\u7b2c\u4e8c\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n    {<br \/>\n        &#034;messages&#034; : [HumanMessage(&#034;\u6211\u559c\u6b22\u8bb2\u89e3\u539f\u7406\u80fd\u591f\u901a\u4fd7\u6613\u61c2\u4e9b&#034;)]<br \/>\n    },<br \/>\n    config &#061; config1,<br \/>\n    context &#061; context<br \/>\n)<\/p>\n<p>for message in response[&#034;messages&#034;]:<br \/>\n    message.pretty_print()<\/p>\n<p>print(&#034;&#8212;&#8212;-\u7b2c\u4e09\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n    {<br \/>\n        &#034;messages&#034; : [HumanMessage(&#034;\u6211\u559c\u6b22\u8bb2\u89e3\u539f\u7406\u80fd\u591f\u7ed3\u5408\u4ee3\u7801&#034;)]<br \/>\n    },<br \/>\n    config &#061; config1,<br \/>\n    context &#061; context<br \/>\n)<\/p>\n<p>for message in response[&#034;messages&#034;]:<br \/>\n    message.pretty_print()<\/p>\n<p>print(&#034;&#8212;&#8212;-\u7b2c\u56db\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n    {<br \/>\n        &#034;messages&#034; : [HumanMessage(&#034;\u6211\u7684\u5b66\u4e60\u4e60\u60ef\u662f\u4ec0\u4e48&#xff1f;&#034;)]<br \/>\n    },<br \/>\n    config &#061; config2,<br \/>\n    context &#061; context<br \/>\n)<\/p>\n<p>for message in response[&#034;messages&#034;]:<br \/>\n    message.pretty_print()<\/p>\n<p>print(&#034;&#8212;&#8212;-\u7b2c\u4e94\u8f6e&#8212;&#8212;&#8211;&#034;)<br \/>\nresponse &#061; agent.invoke(<br \/>\n    {&#034;messages&#034; : [HumanMessage(&#034;\u7ed9\u6211\u8bb2\u89e3\u4e00\u4e0b\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u539f\u7406\u548c\u4ee3\u7801\u5b9e\u73b0&#034;)]} ,<br \/>\n    config &#061; config2,<br \/>\n    context &#061; context<br \/>\n)<\/p>\n<p>for message in response[&#034;messages&#034;]:<br \/>\n    message.pretty_print() <\/p>\n<p>      <span id=\"quality-guide-article-end\" aria-hidden=\"true\" style=\"display:block;height:1px;margin-bottom:-1px\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content<\/p>\n","protected":false},"author":2,"featured_media":124700,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[66,997,50],"topic":[],"class_list":["post-124702","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-ai","tag-langchain","tag-50"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>LangChain Agent \u4e09\u5927\u6570\u636e\u7ba1\u7406\u673a\u5236\uff1aContext\u3001State \u4e0e Store \u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/124702.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LangChain Agent \u4e09\u5927\u6570\u636e\u7ba1\u7406\u673a\u5236\uff1aContext\u3001State \u4e0e Store \u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/124702.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-19T02:32:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/11\/20261119023257-6afe5fd92cfdc.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"26 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/124702.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/124702.html\",\"name\":\"LangChain Agent \u4e09\u5927\u6570\u636e\u7ba1\u7406\u673a\u5236\uff1aContext\u3001State \u4e0e Store \u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-11-19T02:32:59+00:00\",\"dateModified\":\"2026-11-19T02:32:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/124702.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/124702.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/124702.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LangChain Agent \u4e09\u5927\u6570\u636e\u7ba1\u7406\u673a\u5236\uff1aContext\u3001State \u4e0e Store \u8be6\u89e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LangChain Agent \u4e09\u5927\u6570\u636e\u7ba1\u7406\u673a\u5236\uff1aContext\u3001State \u4e0e Store \u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/124702.html","og_locale":"zh_CN","og_type":"article","og_title":"LangChain Agent \u4e09\u5927\u6570\u636e\u7ba1\u7406\u673a\u5236\uff1aContext\u3001State \u4e0e Store \u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content","og_url":"https:\/\/www.wsisp.com\/helps\/124702.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-11-19T02:32:59+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/11\/20261119023257-6afe5fd92cfdc.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"26 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/124702.html","url":"https:\/\/www.wsisp.com\/helps\/124702.html","name":"LangChain Agent \u4e09\u5927\u6570\u636e\u7ba1\u7406\u673a\u5236\uff1aContext\u3001State \u4e0e Store \u8be6\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-11-19T02:32:59+00:00","dateModified":"2026-11-19T02:32:59+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/124702.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/124702.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/124702.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"LangChain Agent \u4e09\u5927\u6570\u636e\u7ba1\u7406\u673a\u5236\uff1aContext\u3001State \u4e0e Store \u8be6\u89e3"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/124702","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=124702"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/124702\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/124700"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=124702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=124702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=124702"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=124702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}