{"id":81131,"date":"2026-07-24T23:50:34","date_gmt":"2026-07-24T15:50:34","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/81131.html"},"modified":"2026-07-24T23:50:34","modified_gmt":"2026-07-24T15:50:34","slug":"%e5%91%8a%e5%88%ab%e9%87%91%e9%b1%bc%e8%ae%b0%e5%bf%86%ef%bc%9alangchain-%e4%ba%94%e5%a4%a7%e6%a0%b8%e5%bf%83%e8%ae%b0%e5%bf%86%e6%9c%ba%e5%88%b6%e4%b8%8e-lcel-%e6%8c%81%e4%b9%85","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/81131.html","title":{"rendered":"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d\uff1aLangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357"},"content":{"rendered":"<h2>\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d&#xff1a;LangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357<\/h2>\n<p>\u5728\u505a\u5927\u6a21\u578b\u5e94\u7528\u5f00\u53d1\u65f6&#xff0c;\u5927\u5bb6\u80af\u5b9a\u90fd\u7ecf\u5386\u8fc7\u4e00\u4e2a\u4ee4\u4eba\u6293\u72c2\u7684\u9636\u6bb5&#xff1a;\u4f60\u6ee1\u5fc3\u6b22\u559c\u5730\u63a5\u5165\u4e86 OpenAI \u6216\u8005\u56fd\u5185\u7684\u5927\u6a21\u578b API&#xff0c;\u505a\u4e86\u4e00\u4e2a\u804a\u5929\u673a\u5668\u4eba&#xff0c;\u7ed3\u679c\u53d1\u73b0\u5b83\u662f\u4e2a\u201c\u6e23\u7537\u201d\u2014\u2014\u4f60\u8bf4\u8fc7\u7684\u8bdd&#xff0c;\u5b83\u8f6c\u5934\u5c31\u5fd8\u3002\u524d\u4e00\u79d2\u4f60\u521a\u544a\u8bc9\u5b83\u201c\u6211\u53eb\u5f20\u4e09\u201d&#xff0c;\u4e0b\u4e00\u79d2\u95ee\u5b83\u201c\u6211\u53eb\u4ec0\u4e48\u201d&#xff0c;\u5b83\u4f1a\u4e00\u8138\u65e0\u8f9c\u5730\u56de\u7b54\u201c\u62b1\u6b49&#xff0c;\u6211\u4e0d\u77e5\u9053\u60a8\u7684\u540d\u5b57\u201d\u3002<\/p>\n<p>\u5176\u5b9e&#xff0c;\u8fd9\u4e0d\u602a\u5927\u6a21\u578b\u3002\u4eca\u5929\u6211\u4eec\u5c31\u6765\u6df1\u5ea6\u6252\u4e00\u6252\u5927\u6a21\u578b\u7684\u8bb0\u5fc6\u539f\u7406&#xff0c;\u5e76\u8be6\u7ec6\u62c6\u89e3 LangChain \u4e2d\u6781\u5176\u91cd\u8981\u7684 Memory&#xff08;\u7f13\u51b2\u8bb0\u5fc6&#xff09; \u6a21\u5757\u3002\u672c\u6587\u4e0d\u4ec5\u4f1a\u68b3\u7406\u4f20\u7edf\u7684\u4e94\u5927\u8bb0\u5fc6\u7ec4\u4ef6&#xff0c;\u8fd8\u4f1a\u989d\u5916\u8865\u5145\u76ee\u524d LangChain \u5b98\u65b9\u4e3b\u63a8\u7684 LCEL&#xff08;\u8868\u8fbe\u5f0f\u8bed\u8a00&#xff09;\u67b6\u6784\u4e0b\u7684\u6301\u4e45\u5316\u8bb0\u5fc6\u65b9\u6848&#xff0c;\u5e26\u4f60\u5728 Windows \u73af\u5883\u4e0b\u4ece\u96f6\u624b\u5199\u4e00\u4e2a\u201c\u62e5\u6709\u672c\u5730\u957f\u671f\u8bb0\u5fc6\u7684 AI \u4f34\u4fa3\u201d\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u3001 \u6838\u5fc3\u77e5\u8bc6\u8bb2\u89e3&#xff1a;\u4e3a\u4ec0\u4e48\u5927\u6a21\u578b\u6ca1\u6709\u8bb0\u5fc6&#xff1f;<\/h3>\n<p>\u5728\u52a8\u624b\u5199\u4ee3\u7801\u4e4b\u524d&#xff0c;\u6211\u4eec\u8981\u5148\u641e\u61c2\u5927\u6a21\u578b&#xff08;LLM&#xff09;\u7684\u5e95\u5c42\u4ea4\u4e92\u903b\u8f91&#xff0c;\u5426\u5219\u7528\u8d77 LangChain \u7684\u8bb0\u5fc6\u6a21\u5757\u5c31\u50cf\u662f\u5728\u80cc\u6a21\u677f&#xff0c;\u9047\u5230 Bug \u5c31\u4f1a\u6293\u778e\u3002<\/p>\n<h4>1.1 \u5927\u6a21\u578b\u7684\u201c\u65e0\u72b6\u6001\u201d\u672c\u8d28 (Stateless)<\/h4>\n<p>\u65e0\u8bba\u662f ChatGPT \u7684\u7f51\u9875\u7aef&#xff0c;\u8fd8\u662f\u6211\u4eec\u8c03\u7528\u7684 API&#xff0c;\u5927\u6a21\u578b\u672c\u8eab\u662f**\u65e0\u72b6\u6001&#xff08;Stateless&#xff09;**\u7684\u3002\u8fd9\u5c31\u597d\u6bd4 HTTP \u534f\u8bae\u4e00\u6837&#xff0c;\u6bcf\u4e00\u6b21\u8bf7\u6c42\u5bf9\u4e8e\u670d\u52a1\u5668\u6765\u8bf4\u90fd\u662f\u4e00\u6b21\u5168\u65b0\u7684\u76f8\u9047\u3002<\/p>\n<p>\u5927\u6a21\u578b\u5185\u90e8\u5e76\u6ca1\u6709\u4e00\u4e2a\u6570\u636e\u5e93\u53bb\u4e13\u95e8\u5b58\u653e\u201c\u7528\u6237 A \u521a\u624d\u8bf4\u4e86\u4ec0\u4e48\u201d\u3002<\/p>\n<p>\u90a3\u4e3a\u4ec0\u4e48 ChatGPT \u7f51\u9875\u7248\u80fd\u8bb0\u4f4f\u4e0a\u4e0b\u6587\u5462&#xff1f;<\/p>\n<p>\u7b54\u6848\u662f&#xff1a;\u524d\u7aef\u628a\u5386\u53f2\u5bf9\u8bdd\u5168\u90e8\u4f20\u8fc7\u53bb\u4e86\u3002 \u5982\u679c\u4f60\u548c AI \u804a\u4e86 10 \u4e2a\u56de\u5408&#xff0c;\u5f53\u4f60\u5728\u7b2c 11 \u4e2a\u56de\u5408\u63d0\u95ee\u65f6&#xff0c;\u7a0b\u5e8f\u5b9e\u9645\u4e0a\u5728\u540e\u53f0\u628a\u4f60\u524d 10 \u4e2a\u56de\u5408\u7684\u5bf9\u8bdd\u6253\u5305\u6210\u4e86\u4e00\u4e2a\u8d85\u957f\u7684\u5b57\u7b26\u4e32&#xff08;\u6216\u8005\u6d88\u606f\u5217\u8868&#xff09;&#xff0c;\u548c\u7b2c 11 \u4e2a\u95ee\u9898\u4e00\u8d77\u53d1\u7ed9\u4e86\u5927\u6a21\u578b\u3002<\/p>\n<h4>1.2 Token \u7206\u70b8\u4e0e\u4e0a\u4e0b\u6587\u7a97\u53e3\u9650\u5236 (Context Window)<\/h4>\n<p>\u65e2\u7136\u628a\u5386\u53f2\u8bb0\u5f55\u5168\u4f20\u8fc7\u53bb\u5c31\u80fd\u89e3\u51b3\u95ee\u9898&#xff0c;\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981 LangChain \u590d\u6742\u7684 Memory \u6a21\u5757\u5462&#xff1f;\u76f4\u63a5\u62fc\u63a5\u5b57\u7b26\u4e32\u4e0d\u5c31\u597d\u4e86&#xff1f;<\/p>\n<p>\u8fd9\u5c31\u5f15\u51fa\u4e86\u5927\u6a21\u578b\u7684\u4e24\u5927\u6838\u5fc3\u75db\u70b9&#xff1a;<\/p>\n<li>Token \u9650\u5236&#xff1a;\u6bcf\u4e2a\u6a21\u578b\u90fd\u6709\u6700\u5927\u4e0a\u4e0b\u6587\u957f\u5ea6&#xff08;\u6bd4\u5982 GPT-3.5 \u662f 16k&#xff0c;GPT-4o \u662f 128k&#xff09;\u3002\u5982\u679c\u4f60\u7684\u5386\u53f2\u5bf9\u8bdd\u65e0\u9650\u8ffd\u52a0&#xff0c;\u5f88\u5feb\u5c31\u4f1a\u8d85\u51fa Context Window \u5bfc\u81f4 API \u62a5\u9519\u5d29\u6e83\u3002<\/li>\n<li>\u70e7\u94b1&#xff1a;API \u662f\u6309 Token \u8ba1\u8d39\u7684\u3002\u6bcf\u4e00\u6b21\u63d0\u95ee\u90fd\u8981\u628a\u4e4b\u524d\u7684\u51e0\u4e07\u5b57\u5386\u53f2\u8bb0\u5f55\u53d1\u8fc7\u53bb&#xff0c;\u4f60\u7684\u8d26\u5355\u4f1a\u5448\u6307\u6570\u7ea7\u7206\u70b8&#xff0c;\u67e5\u8be2\u5ef6\u8fdf\u4e5f\u4f1a\u8d8a\u6765\u8d8a\u9ad8\u3002<\/li>\n<p>\u4e3a\u4e86\u4f18\u96c5\u5730\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898&#xff0c;LangChain \u62bd\u8c61\u51fa\u4e86\u5404\u79cd Memory \u7f13\u51b2\u7b56\u7565&#xff0c;\u5e2e\u6211\u4eec\u5728\u201c\u4fdd\u7559\u5173\u952e\u8bb0\u5fc6\u201d\u548c\u201c\u8282\u7701 Token\u201d\u4e4b\u95f4\u627e\u5230\u5b8c\u7f8e\u7684\u5e73\u8861\u70b9\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001 \u6982\u5ff5\u8bb2\u89e3&#xff1a;LangChain \u4e94\u5927\u7f13\u51b2\u8bb0\u5fc6\u6df1\u5ea6\u5256\u6790<\/h3>\n<p>\u5728 LangChain \u4f20\u7edf\u7684\u67b6\u6784\u4e2d&#xff0c;Memory \u7ec4\u4ef6\u7684\u6838\u5fc3\u4f5c\u7528\u5c31\u662f\u5728\u6bcf\u6b21\u8c03\u7528\u5927\u6a21\u578b\u4e4b\u524d&#xff0c;\u81ea\u52a8\u4ece\u5b58\u50a8\u4e2d\u201c\u52a0\u8f7d&#xff08;Load&#xff09;\u201d\u5386\u53f2\u8bb0\u5fc6\u6ce8\u5165\u5230 Prompt \u4e2d&#xff1b;\u5728\u8c03\u7528\u7ed3\u675f\u540e&#xff0c;\u81ea\u52a8\u5c06\u672c\u6b21\u7684\u95ee\u7b54\u201c\u4fdd\u5b58&#xff08;Save&#xff09;\u201d\u5230\u5b58\u50a8\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e94\u79cd\u6700\u6838\u5fc3\u7684\u7f13\u51b2\u7b56\u7565&#xff0c;\u8fd9\u4e5f\u662f\u9762\u8bd5\u4e2d\u7ecf\u5e38\u88ab\u95ee\u5230\u7684\u67b6\u6784\u9009\u578b\u70b9&#xff1a;<\/p>\n<h4>2.1 ConversationBufferMemory (\u5168\u91cf\u5bf9\u8bdd\u7f13\u51b2)<\/h4>\n<p>\u6700\u7b80\u5355\u7c97\u66b4\u7684\u4e00\u79cd\u3002\u5b83\u539f\u5c01\u4e0d\u52a8\u5730\u628a\u6240\u6709\u7684 HumanMessage \u548c AIMessage \u5b58\u5728\u4e00\u4e2a\u5217\u8868\u91cc\u3002<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u4fe1\u606f 100% \u5b8c\u6574&#xff0c;\u6ca1\u6709\u4efb\u4f55\u4e22\u5931\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u5bf9\u8bdd\u8f6e\u6570\u4e00\u591a&#xff0c;\u9a6c\u4e0a\u89e6\u53d1 Token \u9650\u5236&#xff0c;API \u8c03\u7528\u6210\u672c\u98d9\u5347\u3002<\/li>\n<li>\u9002\u7528\u573a\u666f&#xff1a;\u6781\u77ed\u7684\u5355\u6b21\u4efb\u52a1\u5bf9\u8bdd&#xff0c;\u6216\u8005\u4f60\u4f7f\u7528\u7684\u662f\u672c\u5730\u514d\u8d39\u90e8\u7f72\u4e14\u652f\u6301\u8d85\u957f\u4e0a\u4e0b\u6587\u7684\u6a21\u578b\u3002<\/li>\n<\/ul>\n<h4>2.2 ConversationBufferWindowMemory (\u6ed1\u52a8\u7a97\u53e3\u7f13\u51b2)<\/h4>\n<p>\u5b83\u5f15\u5165\u4e86\u201c\u7a97\u53e3&#xff08;Window&#xff09;\u201d\u7684\u6982\u5ff5&#xff0c;\u4e5f\u5c31\u662f\u53c2\u6570 k\u3002\u5982\u679c\u8bbe\u5b9a k&#061;5&#xff0c;\u5b83\u6c38\u8fdc\u53ea\u8bb0\u4f4f\u6700\u8fd1\u7684 5 \u8f6e\u5bf9\u8bdd&#xff0c;\u66f4\u65e9\u7684\u5bf9\u8bdd\u4f1a\u88ab\u76f4\u63a5\u4e22\u5f03\u3002<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u5185\u5b58\u5360\u7528\u548c Token \u6d88\u8017\u88ab\u6b7b\u6b7b\u5361\u4f4f&#xff0c;\u7edd\u5bf9\u4e0d\u4f1a\u8d85\u9650\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u5927\u6a21\u578b\u4f1a\u60a3\u4e0a\u201c\u8001\u5e74\u75f4\u5446\u201d&#xff0c;\u524d\u534a\u4e2a\u5c0f\u65f6\u804a\u8fc7\u7684\u5173\u952e\u4fe1\u606f\u76f4\u63a5\u4e22\u5931\u3002<\/li>\n<li>\u9002\u7528\u573a\u666f&#xff1a;\u53ea\u5173\u6ce8\u5f53\u524d\u4e0a\u4e0b\u6587\u7684\u95f2\u804a\u673a\u5668\u4eba\u3001\u5ba2\u670d\u57fa\u7840\u6392\u969c\u3002<\/li>\n<\/ul>\n<h4>2.3 ConversationSummaryMemory (\u6458\u8981\u7f13\u51b2)<\/h4>\n<p>\u8fd9\u662f\u975e\u5e38\u806a\u660e\u7684\u4e00\u79cd\u505a\u6cd5\u3002\u5b83\u4e0d\u518d\u76f4\u63a5\u4fdd\u5b58\u539f\u59cb\u5bf9\u8bdd&#xff0c;\u800c\u662f\u5728\u540e\u53f0\u6084\u6084\u5f00\u542f\u4e00\u4e2a\u5927\u6a21\u578b\u7ebf\u7a0b&#xff0c;\u6bcf\u6b21\u5bf9\u8bdd\u7ed3\u675f\u540e&#xff0c;\u8ba9 AI \u628a\u4e4b\u524d\u7684\u5bf9\u8bdd\u603b\u7ed3\u6210\u4e00\u6bb5\u7b80\u77ed\u7684\u6458\u8981&#xff08;Summary&#xff09;&#xff0c;\u4e0b\u6b21\u63d0\u95ee\u65f6\u53ea\u628a\u6458\u8981\u53d1\u8fc7\u53bb\u3002<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u6781\u5927\u538b\u7f29\u4e86\u957f\u6587\u672c&#xff0c;\u4fdd\u7559\u4e86\u65f6\u95f4\u8de8\u5ea6\u5f88\u957f\u7684\u6838\u5fc3\u4e8b\u4ef6\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u6458\u8981\u672c\u8eab\u4f1a\u635f\u5931\u7ec6\u8282&#xff1b;\u4e14\u6bcf\u6b21\u5bf9\u8bdd\u90fd\u8981\u989d\u5916\u8c03\u7528\u4e00\u6b21 LLM \u6765\u751f\u6210\u6458\u8981&#xff0c;\u589e\u52a0\u4e86\u8bf7\u6c42\u8017\u65f6\u3002<\/li>\n<li>\u9002\u7528\u573a\u666f&#xff1a;\u9700\u8981\u804a\u5f88\u4e45\u3001\u4f46\u7ec6\u8282\u6570\u636e&#xff08;\u5982\u5177\u4f53\u7684\u4ee3\u7801\u3001\u6570\u5b57&#xff09;\u4e0d\u90a3\u4e48\u91cd\u8981\u7684\u957f\u7bc7\u89d2\u8272\u626e\u6f14\u3002<\/li>\n<\/ul>\n<h4>2.4 ConversationSummaryBufferMemory (\u6458\u8981&#043;\u6ed1\u52a8\u7a97\u53e3\u6df7\u5408\u7f13\u51b2)<\/h4>\n<p>\u8fd9\u662f\u4f01\u4e1a\u7ea7\u5e94\u7528\u4e2d\u6700\u63a8\u8350\u3001\u6700\u738b\u724c\u7684\u5185\u7f6e\u7ec4\u4ef6\u3002<\/p>\n<p>\u5b83\u7ed3\u5408\u4e86\u524d\u4e24\u8005\u7684\u4f18\u70b9&#xff1a;\u8bbe\u5b9a\u4e00\u4e2a\u6700\u5927 Token \u6570&#xff08;\u6bd4\u5982 max_token_limit&#061;1000&#xff09;\u3002\u5f53\u603b Token \u5c0f\u4e8e 1000 \u65f6&#xff0c;\u5b83\u5c31\u50cf BufferMemory \u4e00\u6837\u539f\u5c01\u4e0d\u52a8\u4fdd\u5b58&#xff1b;\u4e00\u65e6\u8d85\u8fc7 1000&#xff0c;\u5b83\u4e0d\u4f1a\u76f4\u63a5\u4e22\u5f03\u8001\u5bf9\u8bdd&#xff0c;\u800c\u662f\u628a\u6700\u8001\u7684\u51e0\u8f6e\u5bf9\u8bdd\u62ff\u53bb\u751f\u6210\u6458\u8981&#xff0c;\u6700\u8fd1\u7684\u5bf9\u8bdd\u4f9d\u7136\u4fdd\u6301\u539f\u6837\u3002<\/p>\n<ul>\n<li>\u67b6\u6784\u7f8e\u5b66&#xff1a;\u6700\u8fd1\u7684\u7ec6\u8282\u6781\u5176\u6e05\u6670&#xff0c;\u4e45\u8fdc\u7684\u8bb0\u5fc6\u53d8\u4e3a\u6838\u5fc3\u6458\u8981\u3002\u5b8c\u7f8e\u6a21\u62df\u4e86\u4eba\u7c7b\u7684\u8bb0\u5fc6\u673a\u5236&#xff01;<\/li>\n<\/ul>\n<h4>2.5 ConversationTokenBufferMemory (\u7cbe\u51c6 Token \u622a\u65ad\u7f13\u51b2)<\/h4>\n<p>\u548c\u6ed1\u52a8\u7a97\u53e3\u7c7b\u4f3c&#xff0c;\u4f46\u5b83\u7684\u6807\u5c3a\u4e0d\u662f\u201c\u5bf9\u8bdd\u8f6e\u6570 k\u201d&#xff0c;\u800c\u662f\u201c\u7cbe\u786e\u7684 Token \u6570\u91cf\u201d\u3002\u56e0\u4e3a\u4e00\u8f6e\u5bf9\u8bdd\u53ef\u80fd\u5305\u542b 10 \u4e2a\u5b57&#xff0c;\u4e5f\u53ef\u80fd\u5305\u542b 10000 \u4e2a\u5b57&#xff0c;\u7528 Token \u6765\u622a\u65ad\u5bf9\u4e8e\u63a7\u5236 API \u6210\u672c\u66f4\u52a0\u7cbe\u51c6\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001 \u5e38\u7528\u7684\u4f7f\u7528\u6280\u5de7\u4e0e\u6392\u5751\u6307\u5357<\/h3>\n<p>\u73af\u5883\u51c6\u5907&#xff1a;<\/p>\n<p>\u64cd\u4f5c\u7cfb\u7edf&#xff1a;Windows 10\/11<\/p>\n<p>Python \u7248\u672c&#xff1a;3.9&#043;<\/p>\n<p>\u57fa\u7840\u4f9d\u8d56&#xff1a;pip install langchain langchain-openai tiktoken<\/p>\n<h4>3.1 \u7b80\u5355\u5165\u95e8&#xff1a;\u624b\u6495 Memory \u5e95\u5c42\u8bfb\u5199<\/h4>\n<p>\u522b\u4e00\u5f00\u59cb\u5c31\u7528\u590d\u6742\u7684 Chain \u53bb\u5305\u5b83&#xff0c;\u6211\u4eec\u5148\u76f4\u63a5\u770b\u770b Memory \u5e95\u5c42\u5728\u5e72\u561b\u3002<\/p>\n<p>Python<\/p>\n<p><span class=\"token keyword\">from<\/span> langchain<span class=\"token punctuation\">.<\/span>memory <span class=\"token keyword\">import<\/span> ConversationBufferMemory<\/p>\n<p><span class=\"token comment\"># 1. \u521d\u59cb\u5316\u8bb0\u5fc6<\/span><br \/>\nmemory <span class=\"token operator\">&#061;<\/span> ConversationBufferMemory<span class=\"token punctuation\">(<\/span>return_messages<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># 2. \u624b\u52a8\u4fdd\u5b58\u4e0a\u4e0b\u6587 (\u8fd9\u901a\u5e38\u662f\u6846\u67b6\u5e95\u5c42\u5e2e\u4f60\u505a\u7684)<\/span><br \/>\nmemory<span class=\"token punctuation\">.<\/span>save_context<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;input&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;\u4f60\u597d&#xff0c;\u6211\u53eb\u67b6\u6784\u5e08\u5c0f\u674e&#034;<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;output&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;\u4f60\u597d\u5c0f\u674e&#xff0c;\u8bf7\u95ee\u6709\u4ec0\u4e48\u53ef\u4ee5\u5e2e\u60a8&#xff1f;&#034;<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\nmemory<span class=\"token punctuation\">.<\/span>save_context<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;input&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;\u6211\u64c5\u957fPython&#034;<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;output&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;\u592a\u68d2\u4e86&#xff0c;Python\u975e\u5e38\u5f3a\u5927\u3002&#034;<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># 3. \u8bfb\u53d6\u8bb0\u5fc6<\/span><br \/>\nvariables <span class=\"token operator\">&#061;<\/span> memory<span class=\"token punctuation\">.<\/span>load_memory_variables<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>variables<span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u9884\u671f\u8f93\u51fa (\u8fd4\u56de\u4e86\u7ed3\u6784\u5316\u7684\u6d88\u606f\u5bf9\u8c61\u5217\u8868):<\/span><br \/>\n<span class=\"token comment\"># {&#039;history&#039;: [HumanMessage(content&#061;&#039;\u4f60\u597d&#xff0c;\u6211\u53eb\u67b6\u6784\u5e08\u5c0f\u674e&#039;), AIMessage(content&#061;&#039;\u4f60\u597d\u5c0f\u674e&#xff0c;\u8bf7\u95ee\u6709\u4ec0\u4e48\u53ef\u4ee5\u5e2e\u60a8&#xff1f;&#039;), HumanMessage(content&#061;&#039;\u6211\u64c5\u957fPython&#039;), AIMessage(content&#061;&#039;\u592a\u68d2\u4e86&#xff0c;Python\u975e\u5e38\u5f3a\u5927\u3002&#039;)]}<\/span><\/p>\n<h4>3.2 \u9ad8\u7ea7\u6280\u5de7&#xff1a;\u6df7\u5408\u6458\u8981\u7f13\u51b2\u7684\u5b9e\u9645\u5e94\u7528<\/h4>\n<p>\u6211\u4eec\u6765\u770b ConversationSummaryBufferMemory \u662f\u5982\u4f55\u81ea\u52a8\u6298\u53e0\u5386\u53f2\u8bb0\u5f55\u7684\u3002<\/p>\n<p>Python<\/p>\n<p><span class=\"token keyword\">import<\/span> os<br \/>\n<span class=\"token keyword\">from<\/span> langchain_openai <span class=\"token keyword\">import<\/span> ChatOpenAI<br \/>\n<span class=\"token keyword\">from<\/span> langchain<span class=\"token punctuation\">.<\/span>memory <span class=\"token keyword\">import<\/span> ConversationSummaryBufferMemory<\/p>\n<p>os<span class=\"token punctuation\">.<\/span>environ<span class=\"token punctuation\">[<\/span><span class=\"token string\">&#034;OPENAI_API_KEY&#034;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;sk-xxx&#034;<\/span><\/p>\n<p>llm <span class=\"token operator\">&#061;<\/span> ChatOpenAI<span class=\"token punctuation\">(<\/span>model<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;gpt-3.5-turbo&#034;<\/span><span class=\"token punctuation\">,<\/span> temperature<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># max_token_limit&#061;40 \u6545\u610f\u8bbe\u5f97\u6781\u5c0f&#xff0c;\u903c\u8feb\u5b83\u7acb\u9a6c\u89e6\u53d1\u6458\u8981\u903b\u8f91<\/span><br \/>\nmemory <span class=\"token operator\">&#061;<\/span> ConversationSummaryBufferMemory<span class=\"token punctuation\">(<\/span><br \/>\n    llm<span class=\"token operator\">&#061;<\/span>llm<span class=\"token punctuation\">,<\/span><br \/>\n    max_token_limit<span class=\"token operator\">&#061;<\/span><span class=\"token number\">40<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    return_messages<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><\/p>\n<p>memory<span class=\"token punctuation\">.<\/span>save_context<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;input&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;\u5e2e\u6211\u5b9a\u4e00\u5f20\u660e\u5929\u53bb\u5317\u4eac\u7684\u673a\u7968&#034;<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;output&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;\u597d\u7684&#xff0c;\u6b63\u5728\u4e3a\u60a8\u67e5\u8be2\u822a\u73ed\u3002&#034;<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><br \/>\nmemory<span class=\"token punctuation\">.<\/span>save_context<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;input&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;\u987a\u4fbf\u5e2e\u6211\u5b9a\u4e00\u5bb6\u738b\u5e9c\u4e95\u9644\u8fd1\u7684\u9152\u5e97&#034;<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;output&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;\u5df2\u4e3a\u60a8\u627e\u5230\u51e0\u5bb6\u5408\u9002\u7684\u9152\u5e97\u3002&#034;<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\"># \u52a0\u8f7d\u8bb0\u5fc6\u67e5\u770b\u7ed3\u679c<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>memory<span class=\"token punctuation\">.<\/span>load_memory_variables<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\"># \u9884\u671f\u8f93\u51fa&#xff1a;\u6700\u65e9\u7684\u673a\u7968\u5bf9\u8bdd\u88ab\u538b\u7f29\u6210\u4e86\u4e00\u6bb5 Summary \u5b57\u7b26\u4e32&#xff0c;\u800c\u6700\u8fd1\u7684\u9152\u5e97\u5bf9\u8bdd\u53ef\u80fd\u8fd8\u4fdd\u7559\u539f\u6837\u3002<\/span><\/p>\n<h4>3.3 \u5e38\u89c1\u9519\u8bef&#xff1a;ValueError: Missing some input keys<\/h4>\n<p>\u8fd9\u662f\u65b0\u624b\u6700\u5bb9\u6613\u8e29\u7684\u5751\u3002<\/p>\n<p>\u9519\u8bef\u539f\u56e0&#xff1a;\u5927\u6a21\u578b\u7684 PromptTemplate \u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u540d&#xff0c;\u548c Memory \u4e2d\u6307\u5b9a\u7684\u53d8\u91cf\u540d\u6ca1\u5bf9\u4e0a\u3002\u6bd4\u5982&#xff0c;Memory \u9ed8\u8ba4\u628a\u5386\u53f2\u8bb0\u5f55\u653e\u5728 history \u53d8\u91cf\u91cc&#xff0c;\u4f46\u4f60\u7684 Prompt \u91cc\u5199\u7684\u662f {chat_history}\u3002<\/p>\n<p>\u6539\u6b63\u65b9\u6cd5&#xff1a;\u521d\u59cb\u5316 Memory \u65f6\u660e\u786e\u6307\u5b9a memory_key\u3002<\/p>\n<p>Python<\/p>\n<p>memory <span class=\"token operator\">&#061;<\/span> ConversationBufferMemory<span class=\"token punctuation\">(<\/span>memory_key<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;chat_history&#034;<\/span><span class=\"token punctuation\">,<\/span> return_messages<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h4>3.4 \u8c03\u8bd5\u6280\u5de7&#xff1a;\u5982\u4f55\u77e5\u9053\u5230\u5e95\u6d88\u8017\u4e86\u591a\u5c11 Token&#xff1f;<\/h4>\n<p>\u5982\u679c\u4e0d\u60f3\u4f9d\u9760 API \u540e\u53f0\u7684\u8d26\u5355&#xff0c;\u53ef\u4ee5\u5728 Windows \u672c\u5730\u5f00\u542f LangChain \u7684\u8c03\u8bd5\u6a21\u5f0f\u6216\u8005\u4f7f\u7528 get_num_tokens \u65b9\u6cd5&#xff1a;<\/p>\n<p>Python<\/p>\n<p><span class=\"token keyword\">import<\/span> langchain<br \/>\nlangchain<span class=\"token punctuation\">.<\/span>debug <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">True<\/span> <span class=\"token comment\"># \u5f00\u542f\u540e&#xff0c;\u63a7\u5236\u53f0\u4f1a\u6253\u5370\u51fa\u6bcf\u4e00\u6b65\u62fc\u88c5\u597d\u7684 Prompt<\/span><\/p>\n<hr \/>\n<h3>\u56db\u3001 \u67b6\u6784\u5347\u7ea7\u8865\u5145&#xff1a;\u4ece Legacy Memory \u5230 LCEL MessageHistory (\u52a0\u9910\u5185\u5bb9)<\/h3>\n<p>\u4f5c\u4e3a\u4e00\u540d\u67b6\u6784\u5e08&#xff0c;\u6211\u5fc5\u987b\u5411\u5927\u5bb6\u6307\u51fa LangChain \u7684\u6f14\u8fdb\u65b9\u5411&#xff08;\u8fd9\u90e8\u5206\u5b98\u65b9\u6587\u6863\u5199\u5f97\u5f88\u6563&#xff0c;\u4f46\u6781\u5176\u91cd\u8981&#xff09;\u3002<\/p>\n<p>\u4f20\u7edf\u7684 Memory \u7ec4\u4ef6&#xff08;\u5982\u4e0a\u6587\u6240\u8ff0&#xff09;\u901a\u5e38\u9700\u8981\u914d\u5408\u8001\u5f0f\u7684 LLMChain \u6216 ConversationChain \u4f7f\u7528\u3002\u4f46\u5728\u6700\u65b0\u7684 LangChain \u67b6\u6784\u4e2d&#xff0c;\u5b98\u65b9\u5168\u9762\u62e5\u62b1\u4e86 LCEL&#xff08;\u8868\u8fbe\u5f0f\u8bed\u8a00&#xff09;\u3002<\/p>\n<p>\u5728 LCEL \u4f53\u7cfb\u4e0b&#xff0c;\u6211\u4eec\u4e0d\u518d\u4f7f\u7528\u8001\u5f0f\u7684 Memory \u7c7b&#xff0c;\u800c\u662f\u4f7f\u7528\u66f4\u5e95\u5c42\u3001\u66f4\u5bb9\u6613\u62d3\u5c55\u7684 BaseChatMessageHistory \u914d\u5408 RunnableWithMessageHistory \u6765\u5b9e\u73b0\u6301\u4e45\u5316\u8bb0\u5fc6\u3002\u8fd9\u8ba9\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u8f7b\u677e\u5730\u628a\u8bb0\u5fc6\u5b58\u5165 SQLite\u3001Redis \u751a\u81f3 MongoDB \u4e2d&#xff0c;\u5b9e\u73b0\u591a\u7528\u6237\u7684\u957f\u671f\u8bb0\u5fc6\u9694\u79bb\u3002<\/p>\n<p>\u4e0b\u9762&#xff0c;\u6211\u4eec\u5c06\u7528\u8fd9\u5957\u6700\u65b0\u7684\u4f01\u4e1a\u7ea7\u67b6\u6784&#xff0c;\u6765\u5b8c\u6210\u6211\u4eec\u7684\u5b9e\u6218\u9879\u76ee&#xff01;<\/p>\n<hr \/>\n<h3>\u4e94\u3001 \u5b9e\u6218\u9879\u76ee\u6f14\u7ec3&#xff1a;\u6253\u9020\u201c\u672c\u5730 SQLite \u6301\u4e45\u5316\u591a\u7528\u6237 AI \u4f34\u4fa3\u201d<\/h3>\n<p>\u9879\u76ee\u9700\u6c42&#xff1a;<\/p>\n<p>\u5199\u4e00\u4e2a\u63a7\u5236\u53f0\u804a\u5929\u673a\u5668\u4eba\u3002\u652f\u6301\u591a\u7528\u6237&#xff08;\u901a\u8fc7 session_id \u533a\u5206&#xff09;\u3002\u5c31\u7b97\u4f60\u5173\u95ed\u4e86 Python \u7a0b\u5e8f&#xff08;\u5173\u673a\u91cd\u542f&#xff09;&#xff0c;\u53ea\u8981\u8f93\u5165\u76f8\u540c\u7684 session_id&#xff0c;AI \u4f9d\u7136\u80fd\u8bb0\u4f4f\u4f60\u4eec\u4e4b\u524d\u7684\u5bf9\u8bdd&#xff01;\u8fd9\u5728\u5b9e\u9645\u7684\u5fae\u4fe1\u673a\u5668\u4eba\u6216 Web \u5ba2\u670d\u7cfb\u7edf\u4e2d\u662f\u521a\u9700\u3002<\/p>\n<p>\u6280\u672f\u6808&#xff1a;LangChain LCEL &#043; OpenAI &#043; SQLite (Windows \u81ea\u5e26\u652f\u6301&#xff0c;\u65e0\u9700\u989d\u5916\u5b89\u88c5\u6570\u636e\u5e93\u670d\u52a1\u7aef)\u3002<\/p>\n<h4>Step 1: \u73af\u5883\u4f9d\u8d56\u51c6\u5907<\/h4>\n<p>\u6253\u5f00 Windows CMD \u6216 PowerShell&#xff0c;\u6267\u884c&#xff1a;<\/p>\n<p>Bash<\/p>\n<p>pip install langchain langchain<span class=\"token operator\">&#8211;<\/span>openai langchain<span class=\"token operator\">&#8211;<\/span>community sqlalchemy<\/p>\n<h4>Step 2: \u7f16\u5199\u6838\u5fc3\u4ee3\u7801<\/h4>\n<p>\u65b0\u5efa\u4e00\u4e2a Python \u6587\u4ef6 persistent_chat.py&#xff0c;\u7c98\u8d34\u4ee5\u4e0b\u4ee3\u7801\u5e76\u4ed4\u7ec6\u9605\u8bfb\u6ce8\u91ca&#xff1a;<\/p>\n<p>Python<\/p>\n<p><span class=\"token keyword\">import<\/span> os<br \/>\n<span class=\"token keyword\">from<\/span> langchain_openai <span class=\"token keyword\">import<\/span> ChatOpenAI<br \/>\n<span class=\"token keyword\">from<\/span> langchain_core<span class=\"token punctuation\">.<\/span>prompts <span class=\"token keyword\">import<\/span> ChatPromptTemplate<span class=\"token punctuation\">,<\/span> MessagesPlaceholder<br \/>\n<span class=\"token keyword\">from<\/span> langchain_community<span class=\"token punctuation\">.<\/span>chat_message_histories <span class=\"token keyword\">import<\/span> SQLChatMessageHistory<br \/>\n<span class=\"token keyword\">from<\/span> langchain_core<span class=\"token punctuation\">.<\/span>runnables<span class=\"token punctuation\">.<\/span>history <span class=\"token keyword\">import<\/span> RunnableWithMessageHistory<\/p>\n<p><span class=\"token comment\"># 1. \u73af\u5883\u53d8\u91cf\u914d\u7f6e (\u8bf7\u66ff\u6362\u4e3a\u4f60\u81ea\u5df1\u7684 API KEY)<\/span><br \/>\nos<span class=\"token punctuation\">.<\/span>environ<span class=\"token punctuation\">[<\/span><span class=\"token string\">&#034;OPENAI_API_KEY&#034;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;sk-xxx&#034;<\/span><br \/>\nos<span class=\"token punctuation\">.<\/span>environ<span class=\"token punctuation\">[<\/span><span class=\"token string\">&#034;OPENAI_API_BASE&#034;<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;https:\/\/api.openai.com\/v1&#034;<\/span> <span class=\"token comment\"># \u82e5\u6709\u4ee3\u7406\u53ef\u914d\u7f6e<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">get_session_history<\/span><span class=\"token punctuation\">(<\/span>session_id<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">str<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token triple-quoted-string string\">&#034;&#034;&#034;<br \/>\n    \u8fd9\u662f\u4e00\u4e2a\u83b7\u53d6\u5386\u53f2\u8bb0\u5f55\u7684\u5de5\u5382\u51fd\u6570\u3002<br \/>\n    \u5f53\u7528\u6237\u53d1\u6d88\u606f\u65f6&#xff0c;\u6846\u67b6\u4f1a\u81ea\u52a8\u8c03\u7528\u6b64\u51fd\u6570&#xff0c;\u4f20\u5165\u5f53\u524d\u7528\u6237\u7684 session_id\u3002<br \/>\n    \u6211\u4eec\u5c06\u5bf9\u8bdd\u6570\u636e\u5b58\u50a8\u5728\u5f53\u524d\u76ee\u5f55\u7684 sqlite.db \u6587\u4ef6\u4e2d\u3002<br \/>\n    &#034;&#034;&#034;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> SQLChatMessageHistory<span class=\"token punctuation\">(<\/span><br \/>\n        session_id<span class=\"token operator\">&#061;<\/span>session_id<span class=\"token punctuation\">,<\/span><br \/>\n        connection_string<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;sqlite:\/\/\/memory_chat.db&#034;<\/span> <span class=\"token comment\"># Windows\u4e0b\u4f1a\u5728\u540c\u7ea7\u76ee\u5f55\u751f\u6210 db \u6587\u4ef6<\/span><br \/>\n    <span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;&#x1f680; \u6b63\u5728\u521d\u59cb\u5316\u6301\u4e45\u5316 AI \u4f34\u4fa3&#8230;&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># 2. \u5b9e\u4f8b\u5316\u5e95\u5c42\u5927\u6a21\u578b<\/span><br \/>\n    llm <span class=\"token operator\">&#061;<\/span> ChatOpenAI<span class=\"token punctuation\">(<\/span>model<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;gpt-3.5-turbo&#034;<\/span><span class=\"token punctuation\">,<\/span> temperature<span class=\"token operator\">&#061;<\/span><span class=\"token number\">0.7<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># 3. \u6784\u5efa\u73b0\u4ee3\u5316\u7684 Prompt \u6a21\u677f<\/span><br \/>\n    prompt <span class=\"token operator\">&#061;<\/span> ChatPromptTemplate<span class=\"token punctuation\">.<\/span>from_messages<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><br \/>\n        <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;system&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;\u4f60\u662f\u4e00\u4e2a\u5e7d\u9ed8\u3001\u8d34\u5fc3\u7684\u4e2a\u4ebaAI\u52a9\u7406\u3002\u4f60\u9700\u8981\u50cf\u8001\u670b\u53cb\u4e00\u6837\u548c\u7528\u6237\u5bf9\u8bdd\u3002&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token comment\"># MessagesPlaceholder \u662f LCEL \u4e2d\u4e13\u95e8\u7528\u6765\u9884\u7559\u7ed9\u5386\u53f2\u6d88\u606f\u5217\u8868\u7684\u63d2\u69fd<\/span><br \/>\n        MessagesPlaceholder<span class=\"token punctuation\">(<\/span>variable_name<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;history&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;human&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;{question}&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># 4. LCEL \u7ec4\u88c5\u57fa\u7840 Chain<\/span><br \/>\n    chain <span class=\"token operator\">&#061;<\/span> prompt <span class=\"token operator\">|<\/span> llm<\/p>\n<p>    <span class=\"token comment\"># 5. \u6ce8\u5165\u6301\u4e45\u5316\u8bb0\u5fc6\u6a21\u5757 (\u8fd9\u6b65\u662f\u7cbe\u9ad3&#xff01;)<\/span><br \/>\n    <span class=\"token comment\"># \u5c06\u666e\u901a\u7684 chain \u5305\u88c5\u6210\u5e26\u8bb0\u5fc6\u7684 chain<\/span><br \/>\n    chain_with_history <span class=\"token operator\">&#061;<\/span> RunnableWithMessageHistory<span class=\"token punctuation\">(<\/span><br \/>\n        chain<span class=\"token punctuation\">,<\/span><br \/>\n        get_session_history<span class=\"token punctuation\">,<\/span>           <span class=\"token comment\"># \u521a\u624d\u5b9a\u4e49\u7684\u5de5\u5382\u51fd\u6570<\/span><br \/>\n        input_messages_key<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;question&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token comment\"># \u544a\u8bc9\u6846\u67b6&#xff0c;\u7528\u6237\u7684\u8f93\u5165\u5bf9\u5e94\u5b57\u5178\u91cc\u7684\u54ea\u4e2a key<\/span><br \/>\n        history_messages_key<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;history&#034;<\/span><span class=\"token punctuation\">,<\/span><span class=\"token comment\"># \u544a\u8bc9\u6846\u67b6&#xff0c;\u5386\u53f2\u8bb0\u5f55\u5e94\u8be5\u585e\u5230 Prompt \u7684\u54ea\u4e2a\u63d2\u69fd\u91cc<\/span><br \/>\n    <span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u2705 \u521d\u59cb\u5316\u5b8c\u6210&#xff01;\u7cfb\u7edf\u652f\u6301\u591a\u8d26\u53f7\u767b\u5f55\u3002&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;-&#034;<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">50<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># 6. \u4ea4\u4e92\u5f0f\u591a\u7528\u6237\u5bf9\u8bdd\u903b\u8f91<\/span><br \/>\n    current_user <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">input<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u8bf7\u8f93\u5165\u60a8\u7684\u767b\u5f55\u8d26\u53f7 (\u5982 user_a): &#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\\\\n\u4f60\u597d&#xff0c;<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>current_user<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#xff01;\u6211\u4eec\u5f00\u59cb\u804a\u5929\u5427 (\u8f93\u5165 q \u9000\u51fa, \u8f93\u5165 switch \u5207\u6362\u8d26\u53f7)&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token keyword\">while<\/span> <span class=\"token boolean\">True<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        user_input <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">input<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\\\\n[<\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>current_user<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">] \u8bf4: &#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token keyword\">if<\/span> user_input<span class=\"token punctuation\">.<\/span>lower<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#039;q&#039;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u518d\u89c1&#xff01;\u60a8\u7684\u8bb0\u5fc6\u5df2\u5b89\u5168\u4fdd\u5b58\u5728\u672c\u5730\u3002&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">break<\/span><br \/>\n        <span class=\"token keyword\">elif<\/span> user_input<span class=\"token punctuation\">.<\/span>lower<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#039;switch&#039;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n            current_user <span class=\"token operator\">&#061;<\/span> <span class=\"token builtin\">input<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u8bf7\u8f93\u5165\u5207\u6362\u540e\u7684\u767b\u5f55\u8d26\u53f7: &#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\u5df2\u5207\u6362\u81f3\u8d26\u53f7 <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>current_user<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">\u3002&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token keyword\">continue<\/span><\/p>\n<p>        <span class=\"token comment\"># 7. \u8c03\u7528\u5927\u6a21\u578b&#xff0c;\u5e76\u5728 config \u4e2d\u4f20\u5165 session_id<\/span><br \/>\n        <span class=\"token comment\"># \u6d41\u5f0f\u8f93\u51fa (stream) \u589e\u52a0\u4f53\u9a8c\u611f<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;AI \u56de\u590d: &#034;<\/span><span class=\"token punctuation\">,<\/span> end<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;&#034;<\/span><span class=\"token punctuation\">,<\/span> flush<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        response_stream <span class=\"token operator\">&#061;<\/span> chain_with_history<span class=\"token punctuation\">.<\/span>stream<span class=\"token punctuation\">(<\/span><br \/>\n            <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;question&#034;<\/span><span class=\"token punctuation\">:<\/span> user_input<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span><br \/>\n            config<span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;configurable&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;session_id&#034;<\/span><span class=\"token punctuation\">:<\/span> current_user<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">}<\/span> <span class=\"token comment\"># \u6838\u5fc3&#xff1a;\u901a\u8fc7 config \u6307\u5b9a\u9694\u79bb\u7684\u4f1a\u8bdd<\/span><br \/>\n        <span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token keyword\">for<\/span> chunk <span class=\"token keyword\">in<\/span> response_stream<span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>chunk<span class=\"token punctuation\">.<\/span>content<span class=\"token punctuation\">,<\/span> end<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;&#034;<\/span><span class=\"token punctuation\">,<\/span> flush<span class=\"token operator\">&#061;<\/span><span class=\"token boolean\">True<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\"># \u6362\u884c<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#034;__main__&#034;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    main<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h4>Step 3: \u6d4b\u8bd5\u9a8c\u8bc1\u4e0e\u9884\u671f\u6548\u679c<\/h4>\n<p>\u5728\u7ec8\u7aef\u8fd0\u884c python persistent_chat.py\u3002<\/p>\n<p>\u6211\u4eec\u4f1a\u505a\u8fd9\u6837\u4e00\u4e2a\u6d4b\u8bd5&#xff1a;\u7528 user_a \u767b\u5f55&#xff0c;\u544a\u8bc9 AI \u540d\u5b57\u3002\u7136\u540e\u5207\u6362\u5230 user_b \u767b\u5f55&#xff0c;\u518d\u5207\u56de user_a\u3002\u6700\u540e\u5173\u95ed\u7a0b\u5e8f\u91cd\u65b0\u6253\u5f00&#xff0c;\u9a8c\u8bc1\u8bb0\u5fc6\u662f\u5426\u8fd8\u5728&#xff01;<\/p>\n<p>\u9884\u671f\u4ea4\u4e92\u8fc7\u7a0b&#xff1a;<\/p>\n<p>Plaintext<\/p>\n<p>&#x1f680; \u6b63\u5728\u521d\u59cb\u5316\u6301\u4e45\u5316 AI \u4f34\u4fa3&#8230;<br \/>\n\u2705 \u521d\u59cb\u5316\u5b8c\u6210&#xff01;\u7cfb\u7edf\u652f\u6301\u591a\u8d26\u53f7\u767b\u5f55\u3002<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n\u8bf7\u8f93\u5165\u60a8\u7684\u767b\u5f55\u8d26\u53f7 (\u5982 user_a): user_a<\/p>\n<p>\u4f60\u597d&#xff0c;user_a&#xff01;\u6211\u4eec\u5f00\u59cb\u804a\u5929\u5427 (\u8f93\u5165 q \u9000\u51fa, \u8f93\u5165 switch \u5207\u6362\u8d26\u53f7)<\/p>\n<p>[user_a] \u8bf4: \u6211\u662f\u516c\u53f8\u65b0\u6765\u7684\u540e\u7aef\u5f00\u53d1&#xff0c;\u6211\u53eb\u674e\u56db\u3002<br \/>\nAI \u56de\u590d: \u6b22\u8fce\u52a0\u5165\u516c\u53f8&#xff0c;\u674e\u56db&#xff01;\u4f5c\u4e3a\u540e\u7aef\u5f00\u53d1&#xff0c;\u63a5\u4e0b\u6765\u80af\u5b9a\u4f1a\u6709\u4e0d\u5c11\u6311\u6218&#xff0c;\u6709\u4efb\u4f55\u4ee3\u7801\u95ee\u9898\u90fd\u53ef\u4ee5\u968f\u65f6\u627e\u6211\u54e6\u3002<\/p>\n<p>[user_a] \u8bf4: switch<br \/>\n\u8bf7\u8f93\u5165\u5207\u6362\u540e\u7684\u767b\u5f55\u8d26\u53f7: user_b<br \/>\n\u5df2\u5207\u6362\u81f3\u8d26\u53f7 user_b\u3002<\/p>\n<p>[user_b] \u8bf4: \u4f60\u8ba4\u8bc6\u674e\u56db\u5417&#xff1f;<br \/>\nAI \u56de\u590d: \u62b1\u6b49&#xff0c;\u6211\u4e0d\u8ba4\u8bc6\u53eb\u674e\u56db\u7684\u4eba\u54e6\u3002\u8bf7\u95ee\u4ed6\u662f\u60a8\u7684\u670b\u53cb\u5417&#xff1f; (\u9a8c\u8bc1\u9694\u79bb\u6210\u529f&#xff1a;user_b \u8bfb\u53d6\u4e0d\u5230 user_a \u7684\u8bb0\u5fc6)<\/p>\n<p>[user_b] \u8bf4: switch<br \/>\n\u8bf7\u8f93\u5165\u5207\u6362\u540e\u7684\u767b\u5f55\u8d26\u53f7: user_a<br \/>\n\u5df2\u5207\u6362\u81f3\u8d26\u53f7 user_a\u3002<\/p>\n<p>[user_a] \u8bf4: \u6211\u53eb\u4ec0\u4e48\u540d\u5b57&#xff1f;\u804c\u4f4d\u662f\u4ec0\u4e48&#xff1f;<br \/>\nAI \u56de\u590d: \u4f60\u662f\u516c\u53f8\u65b0\u6765\u7684\u540e\u7aef\u5f00\u53d1&#xff0c;\u540d\u53eb\u674e\u56db&#xff01;(\u9a8c\u8bc1\u8bb0\u5fc6\u6062\u590d\u6210\u529f)<\/p>\n<p>[user_a] \u8bf4: q<br \/>\n\u518d\u89c1&#xff01;\u60a8\u7684\u8bb0\u5fc6\u5df2\u5b89\u5168\u4fdd\u5b58\u5728\u672c\u5730\u3002<\/p>\n<p>\u6b64\u65f6&#xff0c;\u4f60\u770b\u4e00\u4e0b\u4f60 Windows \u9879\u76ee\u6587\u4ef6\u76ee\u5f55\u4e0b&#xff0c;\u4f1a\u591a\u51fa\u4e00\u4e2a memory_chat.db \u6587\u4ef6\u3002\u5373\u4f7f\u4f60\u5173\u673a\u91cd\u542f&#xff0c;\u660e\u5929\u518d\u6b21\u8fd0\u884c\u811a\u672c\u5e76\u8f93\u5165 user_a&#xff0c;AI \u4f9d\u7136\u8bb0\u5f97\u674e\u56db\u662f\u540e\u7aef\u5f00\u53d1&#xff01;<\/p>\n<hr \/>\n<h3>\u516d\u3001 \u603b\u7ed3\u4e0e\u5ef6\u4f38<\/h3>\n<p>\u5927\u6a21\u578b\u7684\u5f00\u53d1\u4e0d\u4ec5\u4ec5\u662f\u8c03\u7528 API&#xff0c;\u66f4\u662f\u5bf9\u6570\u636e\u6d41\u3001\u4e0a\u4e0b\u6587\u548c\u5b58\u50a8\u72b6\u6001\u7684\u7cbe\u5bc6\u7ba1\u7406\u3002<\/p>\n<p>\u4f20\u7edf\u7684 ConversationBufferMemory \u53ca\u5176\u53d8\u79cd\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u4e86\u5982\u4f55\u5728 Token \u9650\u5236\u548c\u4fe1\u606f\u5b8c\u6574\u5ea6\u4e4b\u95f4\u505a\u6743\u8861&#xff1b;\u800c\u73b0\u4ee3\u7684 RunnableWithMessageHistory \u7ed3\u5408\u5916\u90e8\u6570\u636e\u5e93&#xff08;SQLite\/Redis&#xff09;&#xff0c;\u624d\u771f\u6b63\u6253\u901a\u4e86\u4f01\u4e1a\u7ea7\u7cfb\u7edf\u843d\u5730\u7684\u6700\u540e\u4e00\u516c\u91cc\u3002<\/p>\n<p>\u5927\u6a21\u578b\u5e94\u7528\u7684\u67b6\u6784\u6f14\u8fdb\u6781\u5feb\u3002\u5982\u679c\u4f60\u5bf9\u9879\u76ee\u4e2d\u63d0\u5230\u7684\u591a\u7528\u6237\u9ad8\u5e76\u53d1\u6709\u8981\u6c42&#xff0c;\u6216\u8005\u4f60\u7684\u5386\u53f2\u6570\u636e\u91cf\u6781\u5927&#xff0c;\u9700\u8981\u7ed3\u5408 RAG&#xff08;\u68c0\u7d22\u589e\u5f3a\u751f\u6210&#xff09;\u6280\u672f\u5c06\u5386\u53f2\u5bf9\u8bdd\u5b58\u5165 Chroma \u7b49\u5411\u91cf\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u201c\u8bed\u4e49\u76f8\u4f3c\u5ea6\u63d0\u53d6\u8bb0\u5fc6\u201d&#xff0c;\u8fd9\u53c8\u662f\u4e00\u4e2a\u66f4\u9ad8\u9636\u7684\u8bdd\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d&#xff1a;LangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357<br \/>\n\u5728\u505a\u5927\u6a21\u578b\u5e94\u7528\u5f00\u53d1\u65f6&#xff0c;\u5927\u5bb6\u80af\u5b9a\u90fd\u7ecf\u5386\u8fc7\u4e00\u4e2a\u4ee4\u4eba\u6293\u72c2\u7684\u9636\u6bb5&#xff1a;\u4f60\u6ee1\u5fc3\u6b22\u559c\u5730\u63a5\u5165\u4e86 OpenAI \u6216\u8005\u56fd\u5185\u7684\u5927\u6a21\u578b API&#xff0c;\u505a\u4e86\u4e00\u4e2a\u804a\u5929\u673a\u5668\u4eba&#xff0c;\u7ed3\u679c\u53d1\u73b0\u5b83\u662f\u4e2a\u201c\u6e23\u7537\u201d\u2014\u2014\u4f60\u8bf4\u8fc7\u7684\u8bdd&#xff0c;\u5b83\u8f6c\u5934\u5c31\u5fd8\u3002\u524d\u4e00\u79d2\u4f60\u521a\u544a\u8bc9\u5b83\u201c\u6211\u53eb\u5f20\u4e09\u201d&#xff0c;\u4e0b\u4e00\u79d2\u95ee\u5b83\u201c\u6211\u53eb\u4ec0\u4e48\u201d&#xff0c;\u5b83\u4f1a\u4e00\u8138\u65e0\u8f9c\u5730\u56de\u7b54\u201c\u62b1\u6b49&#xff0c;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[997,81,50],"topic":[],"class_list":["post-81131","post","type-post","status-publish","format-standard","hentry","category-server","tag-langchain","tag-python","tag-50"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d\uff1aLangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357 - \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\/81131.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d\uff1aLangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d&#xff1a;LangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357 \u5728\u505a\u5927\u6a21\u578b\u5e94\u7528\u5f00\u53d1\u65f6&#xff0c;\u5927\u5bb6\u80af\u5b9a\u90fd\u7ecf\u5386\u8fc7\u4e00\u4e2a\u4ee4\u4eba\u6293\u72c2\u7684\u9636\u6bb5&#xff1a;\u4f60\u6ee1\u5fc3\u6b22\u559c\u5730\u63a5\u5165\u4e86 OpenAI \u6216\u8005\u56fd\u5185\u7684\u5927\u6a21\u578b API&#xff0c;\u505a\u4e86\u4e00\u4e2a\u804a\u5929\u673a\u5668\u4eba&#xff0c;\u7ed3\u679c\u53d1\u73b0\u5b83\u662f\u4e2a\u201c\u6e23\u7537\u201d\u2014\u2014\u4f60\u8bf4\u8fc7\u7684\u8bdd&#xff0c;\u5b83\u8f6c\u5934\u5c31\u5fd8\u3002\u524d\u4e00\u79d2\u4f60\u521a\u544a\u8bc9\u5b83\u201c\u6211\u53eb\u5f20\u4e09\u201d&#xff0c;\u4e0b\u4e00\u79d2\u95ee\u5b83\u201c\u6211\u53eb\u4ec0\u4e48\u201d&#xff0c;\u5b83\u4f1a\u4e00\u8138\u65e0\u8f9c\u5730\u56de\u7b54\u201c\u62b1\u6b49&#xff0c;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/81131.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-24T15:50:34+00:00\" \/>\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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/81131.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/81131.html\",\"name\":\"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d\uff1aLangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-24T15:50:34+00:00\",\"dateModified\":\"2026-07-24T15:50:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/81131.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/81131.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/81131.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d\uff1aLangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357\"}]},{\"@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":"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d\uff1aLangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357 - \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\/81131.html","og_locale":"zh_CN","og_type":"article","og_title":"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d\uff1aLangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d&#xff1a;LangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357 \u5728\u505a\u5927\u6a21\u578b\u5e94\u7528\u5f00\u53d1\u65f6&#xff0c;\u5927\u5bb6\u80af\u5b9a\u90fd\u7ecf\u5386\u8fc7\u4e00\u4e2a\u4ee4\u4eba\u6293\u72c2\u7684\u9636\u6bb5&#xff1a;\u4f60\u6ee1\u5fc3\u6b22\u559c\u5730\u63a5\u5165\u4e86 OpenAI \u6216\u8005\u56fd\u5185\u7684\u5927\u6a21\u578b API&#xff0c;\u505a\u4e86\u4e00\u4e2a\u804a\u5929\u673a\u5668\u4eba&#xff0c;\u7ed3\u679c\u53d1\u73b0\u5b83\u662f\u4e2a\u201c\u6e23\u7537\u201d\u2014\u2014\u4f60\u8bf4\u8fc7\u7684\u8bdd&#xff0c;\u5b83\u8f6c\u5934\u5c31\u5fd8\u3002\u524d\u4e00\u79d2\u4f60\u521a\u544a\u8bc9\u5b83\u201c\u6211\u53eb\u5f20\u4e09\u201d&#xff0c;\u4e0b\u4e00\u79d2\u95ee\u5b83\u201c\u6211\u53eb\u4ec0\u4e48\u201d&#xff0c;\u5b83\u4f1a\u4e00\u8138\u65e0\u8f9c\u5730\u56de\u7b54\u201c\u62b1\u6b49&#xff0c;","og_url":"https:\/\/www.wsisp.com\/helps\/81131.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-24T15:50:34+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/81131.html","url":"https:\/\/www.wsisp.com\/helps\/81131.html","name":"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d\uff1aLangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-24T15:50:34+00:00","dateModified":"2026-07-24T15:50:34+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/81131.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/81131.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/81131.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u544a\u522b\u201c\u91d1\u9c7c\u8bb0\u5fc6\u201d\uff1aLangChain \u4e94\u5927\u6838\u5fc3\u8bb0\u5fc6\u673a\u5236\u4e0e LCEL \u6301\u4e45\u5316\u5b9e\u6218\u5168\u6307\u5357"}]},{"@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\/81131","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=81131"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/81131\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=81131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=81131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=81131"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=81131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}