{"id":19768,"date":"2025-12-10T15:33:17","date_gmt":"2025-12-10T07:33:17","guid":{"rendered":"https:\/\/92it.top\/?p=19768"},"modified":"2025-12-12T14:02:22","modified_gmt":"2025-12-12T06:02:22","slug":"langchain-%e5%ae%89%e8%a3%85%e9%85%8d%e7%bd%ae-tips","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=19768","title":{"rendered":"LangChain \u5b89\u88c5\u914d\u7f6e Tips"},"content":{"rendered":"\n<p><strong>\u5b89\u88c5\u73af\u5883<\/strong>&nbsp;<strong>\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u672c\u5730\u5b89\u88c5 LangChain \u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u56e0\u4e3a LangChain \u662f\u4e00\u4e2a\u201c\u80f6\u6c34\u201d\u6846\u67b6\uff0c\u5b83\u901a\u5e38\u9700\u8981\u914d\u5408\u5927\u6a21\u578b\uff08LLM\uff09\u624d\u80fd\u5de5\u4f5c\u3002<\/p>\n\n\n\n<p>LangChain&nbsp;<strong>\u5e76\u975e\u5fc5\u987b\u4f9d\u8d56 Python \u73af\u5883<\/strong>\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u8bed\u8a00\u652f\u6301\uff0c\u6838\u5fc3\u529f\u80fd\u53ef\u5728\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00 \/ \u8fd0\u884c\u65f6\u4e2d\u4f7f\u7528\uff0cPython \u53ea\u662f\u5176\u6700\u4e3b\u6d41\u3001\u751f\u6001\u6700\u5b8c\u5584\u7684\u7248\u672c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u8bf4\u660e\uff0c\u5e2e\u4f60\u6e05\u6670\u4e86\u89e3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9009\u62e9\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u8bed\u8a00 \/ \u73af\u5883<\/th><th>\u652f\u6301\u7a0b\u5ea6<\/th><th>\u9002\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td>Python<\/td><td>\u5b8c\u5168\u652f\u6301\uff08\u5b98\u65b9\u6838\u5fc3\u7248\u672c\uff09<\/td><td>\u7edd\u5927\u591a\u6570\u573a\u666f\uff08RAG\u3001Agent\u3001\u672c\u5730\u5927\u6a21\u578b\u96c6\u6210\u3001\u5411\u91cf\u5e93\u8054\u52a8\uff09\uff0c\u751f\u6001\u6700\u4e30\u5bcc<\/td><\/tr><tr><td>JavaScript\/TypeScript<\/td><td>\u5b98\u65b9\u652f\u6301\uff08LangChain JS\uff09<\/td><td>\u524d\u7aef \/ Node.js \u540e\u7aef\u3001\u5168\u6808 AI \u5e94\u7528\u3001\u8f7b\u91cf\u7ea7\u68c0\u7d22\u573a\u666f<\/td><\/tr><tr><td>Go<\/td><td>\u793e\u533a\u7ef4\u62a4\uff08LangChain Go\uff09<\/td><td>\u9ad8\u6027\u80fd\u540e\u7aef\u3001Go \u8bed\u8a00\u9879\u76ee\u96c6\u6210\uff08\u529f\u80fd\u7565\u7cbe\u7b80\uff09<\/td><\/tr><tr><td>Java<\/td><td>\u793e\u533a\u7ef4\u62a4\uff08LangChain4j\uff09<\/td><td>\u4f01\u4e1a\u7ea7 Java \u9879\u76ee\u3001Spring \u751f\u6001\u96c6\u6210<\/td><\/tr><tr><td>C#\/.NET<\/td><td>\u793e\u533a\u7ef4\u62a4\uff08LangChain .NET\uff09<\/td><td>.NET \u5e73\u53f0\u5e94\u7528\u3001Windows \u5f00\u53d1\u573a\u666f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff1a\u53ea\u6709\u60f3\u4f7f\u7528 LangChain \u5168\u90e8\u529f\u80fd\uff08\u5c24\u5176\u662f\u6700\u65b0\u7279\u6027\u3001\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u96c6\u6210\uff09\uff0cPython \u662f\u6700\u4f18\u89e3\uff1b\u82e5\u4f60\u7684\u6280\u672f\u6808\u662f JS\/Go\/Java \u7b49\uff0c\u4e5f\u80fd\u627e\u5230\u9002\u914d\u7248\u672c\uff0c\u53ea\u662f\u529f\u80fd\u8986\u76d6\u5ea6\u548c\u751f\u6001\u5b8c\u5584\u5ea6\u7565\u4f4e\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>Python\u73af\u5883\u642d\u5efa\uff08Windows\uff09<\/strong>&nbsp;<strong>\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udd39\u83b7\u53d6\u7535\u8111\u5904\u7406\u5668\u578b\u53f7<\/p>\n\n\n\n<p>\u6309\u4e0bWin+R\u952e\uff0c\u8fd0\u884ccmd\u6216\u76f4\u63a5\u5728\u5f00\u59cb\u83dc\u5355\u641c\u7d22\u547d\u4ee4\u63d0\u793a\u7b26\u540e, \u8fdb\u5165\u547d\u4ee4\u63d0\u793a\u7b26\u754c\u9762\uff0c\u8f93\u5165\u4ee5\u4e0b\u6307\u4ee4\u83b7\u53d6\u5904\u7406\u5668\u578b\u53f7.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo %PROCESSOR_ARCHITECTURE%\n<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"362\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-15-1024x362.png\" alt=\"\" class=\"wp-image-19770\" style=\"width:578px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-15-1024x362.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-15-300x106.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-15-768x271.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-15-830x293.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-15-230x81.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-15-350x124.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-15-480x170.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-15.png 1438w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u4e0a\u56fe\u663e\u793a\u6b64\u7535\u8111\u662fAMD64\u5904\u7406\u5668<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\ud83d\udd39\u5b89\u88c5python<\/p>\n\n\n\n<p>python\u5b98\u7f51\u7f51\u5740:&nbsp;<a href=\"https:\/\/www.python.org\/\">Welcome to Python.org<\/a><\/p>\n\n\n\n<p>\u7531\u4e8e\u6b64\u7535\u8111\u662fAMD64\u5904\u7406\u5668\uff0c\u56e0\u6b64\u5728\u786e\u8ba4\u8981\u4e0b\u8f7d\u7684python\u7248\u672c\u540e\u9009\u62e9 Windows&nbsp;installer&nbsp;(64-bit) \u6216 Windows x86-64 executable installer \uff0c\u5982\u4e0b\u56fe\u6240\u793a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"450\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-16-1024x450.png\" alt=\"\" class=\"wp-image-19771\" style=\"width:528px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-16-1024x450.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-16-300x132.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-16-768x337.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-16-830x364.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-16-230x101.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-16-350x154.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-16-480x211.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-16.png 1446w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"609\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-17-1024x609.png\" alt=\"\" class=\"wp-image-19772\" style=\"width:528px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-17-1024x609.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-17-300x178.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-17-768x457.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-17-830x494.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-17-230x137.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-17-350x208.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-17-480x285.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-17.png 1456w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5b89\u88c5\u53ef\u6267\u884c\u7a0b\u5e8f\uff0c\u52fe\u9009\u4e0b\u9762\u7684\u9009\u9879\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"627\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-18-1024x627.png\" alt=\"\" class=\"wp-image-19773\" style=\"width:528px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-18-1024x627.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-18-300x184.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-18-768x470.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-18-830x508.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-18-230x141.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-18-350x214.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-18-480x294.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-18.png 1448w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<ul>\n<li><code>Use admin privileges when installing py.exe<\/code><\/li>\n\n\n\n<li><code>Add python.exe to PATH<\/code><\/li>\n<\/ul>\n\n\n\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\u6309\u4e0bWin+R\u952e\uff0c\u8fd0\u884ccmd\u6216\u76f4\u63a5\u5728\u5f00\u59cb\u83dc\u5355\u641c\u7d22\u547d\u4ee4\u63d0\u793a\u7b26\u540e\uff0c\u8fdb\u5165\u547d\u4ee4\u63d0\u793a\u7b26\u754c\u9762\uff0c\u8f93\u5165\u4ee5\u4e0b\u6307\u4ee4\uff0c\u4e0b\u56fe\u4e3a\u5b89\u88c5\u6210\u529f\u7684\u793a\u4f8b\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"101\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-19-1024x101.png\" alt=\"\" class=\"wp-image-19774\" style=\"width:646px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-19-1024x101.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-19-300x30.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-19-768x76.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-19-830x82.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-19-230x23.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-19-350x35.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-19-480x47.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-19.png 1459w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"290\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-20-1024x290.png\" alt=\"\" class=\"wp-image-19775\" style=\"width:648px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-20-1024x290.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-20-300x85.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-20-768x218.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-20-830x235.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-20-230x65.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-20-350x99.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-20-480x136.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-20.png 1450w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u521b\u5efa\u865a\u62df\u73af\u5883\uff08\u5f3a\u70c8\u63a8\u8350\uff09<\/strong>&nbsp;<strong>\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e3a\u4e86\u907f\u514d\u4f9d\u8d56\u51b2\u7a81\uff0c\u5efa\u8bae\u4e3a LangChain \u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n\n\n\n<p>\ud83d\udd39Windows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python -m venv langchain_env\nlangchain_env\\Scripts\\activate<\/pre>\n\n\n\n<p>\ud83d\udd39Mac \/ Linux:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">python3 -m venv langchain_env\nsource langchain_env\/bin\/activate<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u5b89\u88c5 LangChain \u6838\u5fc3\u5e93<\/strong>&nbsp;<strong>\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u6fc0\u6d3b\u7684\u865a\u62df\u73af\u5883\u4e2d\uff0c\u4f7f\u7528 pip \u5b89\u88c5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install langchain\n<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"496\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21-1024x496.png\" alt=\"\" class=\"wp-image-19779\" style=\"width:596px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21-1024x496.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21-300x145.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21-768x372.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21-1536x744.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21-830x402.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21-230x111.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21-350x169.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21-480x232.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-21.png 1599w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u4ece\u6700\u8fd1\u7684\u7248\u672c\u5f00\u59cb\uff0cLangChain \u5c06\u751f\u6001\u7cfb\u7edf\u62c6\u5206\u6210\u4e86\u4e0d\u540c\u7684\u5305\u3002<\/p>\n\n\n\n<ul>\n<li>\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528\u793e\u533a\u96c6\u6210\u7684\u529f\u80fd\uff08\u5927\u591a\u6570\u60c5\u51b5\u90fd\u9700\u8981\uff09\uff0c\u5efa\u8bae\u540c\u65f6\u5b89\u88c5\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install langchain-community\n<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\ud83d\udd39langchain-core \u548c langchain-community<\/p>\n\n\n\n<ul>\n<li>langchain-core &nbsp;(\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838)\uff1a<\/li>\n\n\n\n<li>\u8fd9\u662f LangChain \u7684\u5927\u8111\u3002\u5b83\u5b9a\u4e49\u4e86\u4ec0\u4e48\u662f\u201c\u94fe\uff08Chain\uff09\u201d\uff0c\u4ec0\u4e48\u662f\u201c\u63d0\u793a\u8bcd\u6a21\u677f\uff08Prompt\uff09\u201d\uff0c\u4ec0\u4e48\u662f\u201c\u6d88\u606f\uff08Message\uff09\u201d\u3002\u4f46\u5b83\u672c\u8eab\u4e0d\u5305\u542b\u8fde\u63a5\u5916\u90e8\u4e16\u754c\u7684\u4ee3\u7801\u3002\u5b83\u53ea\u662f\u4e00\u5957\u903b\u8f91\u89c4\u5219\uff0c\u5c31\u50cf Windows \u64cd\u4f5c\u7cfb\u7edf\uff0c\u5982\u679c\u6ca1\u6709\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5b83\u8fde\u4e0d\u4e0a\u6253\u5370\u673a\u3001\u663e\u5361\u6216\u7f51\u5361\u3002<\/li>\n\n\n\n<li>langchain-community&nbsp;(\u9a71\u52a8\u7a0b\u5e8f\u5305)\uff1a<\/li>\n\n\n\n<li>\u8fd9\u662f LangChain \u7684\u5916\u8bbe\u4ed3\u5e93\u3002\u5b83\u5305\u542b\u4e86\u8fde\u63a5\u6210\u5343\u4e0a\u4e07\u4e2a\u7b2c\u4e09\u65b9\u8f6f\u4ef6\u548c\u670d\u52a1\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u6bd4\u5982\uff1a\u4f60\u60f3\u8fde\u63a5&nbsp;Qdrant&nbsp;\u6570\u636e\u5e93\uff1f\u4ee3\u7801\u5728\u8fd9\u91cc\u3002\u4f60\u60f3\u8bfb\u53d6&nbsp;PDF&nbsp;\u6587\u4ef6\uff1f\u4ee3\u7801\u5728\u8fd9\u91cc\u3002\u4f60\u60f3\u8c03\u7528&nbsp;HuggingFace&nbsp;\u7684\u6a21\u578b\uff1f\u4ee3\u7801\u4e5f\u5728\u8fd9\u91cc\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709 langchain-community\uff0cLangChain \u5c31\u53ea\u662f\u4e00\u4e2a\u7a7a\u58f3\u903b\u8f91\uff0c\u65e0\u6cd5\u4e0e\u5916\u754c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u5b83\u5305\u542b\u7684\u56db\u5927\u6838\u5fc3\u677f\u5757\uff0c\u4e5f\u662f\u4f60\u505a\u9879\u76ee\u5fc5\u7528\u7684\uff1a<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\ud83d\udd39\u6587\u6863\u52a0\u8f7d\u5668 (Document Loaders) \u2014\u2014 RAG \u7684\u7b2c\u4e00\u6b65<br>\u8fd9\u662f\u6700\u5e38\u7528\u7684\u529f\u80fd\u3002\u5982\u679c\u4f60\u60f3\u505a\u201c\u77e5\u8bc6\u5e93\u201d\uff0c\u4f60\u9700\u8981\u628a\u672c\u5730\u7684\u6587\u4ef6\u8bfb\u8fdb\u7a0b\u5e8f\u91cc\u3002<br>langchain-core \u4e0d\u61c2\u600e\u4e48\u8bfb PDF \u6216 Word\uff0c\u8fd9\u4e9b\u529f\u80fd\u5168\u5728 community \u91cc\u3002<\/p>\n\n\n\n<ul>\n<li>\u8bfb\u53d6 PDF: PyPDFLoader<\/li>\n\n\n\n<li>\u8bfb\u53d6 Markdown: UnstructuredMarkdownLoader<\/li>\n\n\n\n<li>\u8bfb\u53d6 Word: Docx2txtLoader<\/li>\n\n\n\n<li>\u722c\u53d6\u7f51\u9875: WebBaseLoader<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u274c \u6838\u5fc3\u5305\u91cc\u6ca1\u6709\u8fd9\u4e2a\uff0c\u4f1a\u62a5\u9519\nfrom langchain.document_loaders import PyPDFLoader \n\n# \u2705 \u5fc5\u987b\u4ece\u793e\u533a\u5305\u5bfc\u5165\nfrom langchain_community.document_loaders import PyPDFLoader<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\ud83d\udd39\u5411\u91cf\u6570\u636e\u5e93\u96c6\u6210 (Vector Stores)<br>\u867d\u7136 Qdrant \u6700\u8fd1\u63a8\u51fa\u4e86\u72ec\u7acb\u7684 langchain-qdrant \u5305\uff0c\u4f46\u5728 LangChain \u7684\u751f\u6001\u4e2d\uff0c\u7edd\u5927\u591a\u6570\u6570\u636e\u5e93\u7684\u8fde\u63a5\u5668\u90fd\u4f4f\u5728 community \u91cc\u3002<\/p>\n\n\n\n<ul>\n<li>\u8fde\u63a5 Chroma: Chroma<\/li>\n\n\n\n<li>\u8fde\u63a5 FAISS: FAISS<\/li>\n\n\n\n<li>\u8fde\u63a5 Neo4j: Neo4jVector<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u679c\u4f60\u4e0d\u88c5\u8fd9\u4e2a\u5305\uff0cLangChain \u5c31\u4e0d\u77e5\u9053\u5982\u4f55\u628a\u5411\u91cf\u5b58\u8fdb\u8fd9\u4e9b\u6570\u636e\u5e93\uff0c\u4e5f\u65e0\u6cd5\u4ece\u91cc\u9762\u68c0\u7d22\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\ud83d\udd39\u6a21\u578b\u96c6\u6210 (LLMs &amp; Embeddings)<br>\u9664\u4e86 OpenAI\u3001Anthropic \u8fd9\u79cd\u5de8\u5934\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u5305\uff08\u5982 langchain-openai\uff09\u4e4b\u5916\uff0c\u6210\u767e\u4e0a\u5343\u7684\u5176\u4ed6\u5f00\u6e90\u6a21\u578b\u3001\u5c0f\u4f17\u6a21\u578b\u7684\u63a5\u53e3\u90fd\u5728\u8fd9\u91cc\u3002<\/p>\n\n\n\n<ul>\n<li>HuggingFace: HuggingFaceHub, HuggingFaceEmbeddings (\u8fd9\u662f\u4f60\u505a\u672c\u5730\u77e5\u8bc6\u5e93\u5fc5\u7528\u7684)<\/li>\n\n\n\n<li>LlamaCpp: \u7528\u4e8e\u672c\u5730\u8fd0\u884c\u91cf\u5316\u6a21\u578b<\/li>\n\n\n\n<li>Ollama: \u867d\u7136\u73b0\u5728\u6709 langchain-ollama\uff0c\u4f46\u5f88\u591a\u65e7\u6559\u7a0b\u548c\u8f85\u52a9\u529f\u80fd\u4f9d\u7136\u4f9d\u8d56 community \u4e2d\u7684\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\ud83d\udd39\u5de5\u5177 (Tools)\uff1a\u8ba9 AI \u62e5\u6709\u201c\u624b\u201d\u548c\u201c\u773c\u201d\u3002<\/p>\n\n\n\n<ul>\n<li>Google \u641c\u7d22: GoogleSearchAPIWrapper<\/li>\n\n\n\n<li>\u7ef4\u57fa\u767e\u79d1\u641c\u7d22: WikipediaAPIWrapper<\/li>\n\n\n\n<li>DuckDuckGo: DuckDuckGoSearchRun<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\ud83d\udd39\u4e3a\u4ec0\u4e48\u8981\u62c6\u5206\uff1f(\u80cc\u666f\u77e5\u8bc6)<\/p>\n\n\n\n<p>\u5728 LangChain \u7684\u65e9\u671f\u7248\u672c\uff080.1.0 \u4e4b\u524d\uff09\uff0c\u6240\u6709\u8fd9\u4e9b\u4e1c\u897f\u90fd\u585e\u5728\u4e00\u4e2a\u5305\u91cc\u3002<br>\u7ed3\u679c\u5c31\u662f\uff1a<\/p>\n\n\n\n<p>\u5305\u5de8\u5927\u65e0\u6bd4\uff1a\u4f60\u53ea\u60f3\u7528\u4e2a OpenAI\uff0c\u7ed3\u679c\u5b83\u8ba9\u4f60\u4e0b\u8f7d\u4e86\u51e0\u767e\u5146\u5173\u4e8e AWS\u3001Azure\u3001Google \u7684\u4f9d\u8d56\u5e93\u3002<br>\u7ef4\u62a4\u56f0\u96be\uff1a\u67d0\u4e2a\u51b7\u95e8\u7684\u6570\u636e\u5e93\u66f4\u65b0\u4e86 API\uff0c\u5bfc\u81f4\u6574\u4e2a LangChain \u90fd\u8981\u53d1\u65b0\u7248\u3002<br>\u6240\u4ee5\u73b0\u5728\u53d8\u6210\u4e86\uff1a<\/p>\n\n\n\n<ul>\n<li>langchain-core: \u8f7b\u91cf\uff0c\u7a33\u5b9a\uff0c\u5f88\u5c11\u53d8\u52a8\u3002<\/li>\n\n\n\n<li>langchain-community: \u5e9e\u5927\uff0c\u66f4\u65b0\u5feb\uff0c\u4f9d\u8d56\u591a\u3002<\/li>\n\n\n\n<li>langchain-openai \/ langchain-qdrant: \u5b98\u65b9\u8ba4\u8bc1\u7684\u9876\u7ea7\u5408\u4f5c\u4f19\u4f34\uff0c\u72ec\u7acb\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\ud83d\udd39\u603b\u7ed3: \u201c\u793e\u533a\u96c6\u6210\u7684\u529f\u80fd\u201d \u7b80\u5355\u6765\u8bf4\u5c31\u662f\uff1aLangChain \u7528\u6765\u8fde\u63a5\u201c\u522b\u4eba\u5bb6\u8f6f\u4ef6\u201d\u7684\u6240\u6709\u63a5\u53e3\u3002<\/p>\n\n\n\n<ul>\n<li>\u4f60\u60f3\u8bfb PDF \u6587\u4ef6\uff1f\u9700\u8981\u5b83\u3002<\/li>\n\n\n\n<li>\u4f60\u60f3\u7528 Ollama \u6216 HuggingFace\uff1f\u9700\u8981\u5b83\u3002<\/li>\n\n\n\n<li>\u4f60\u60f3\u5b58 \u5411\u91cf\u6570\u636e\u5e93\uff1f\u9700\u8981\u5b83\u3002<\/li>\n\n\n\n<li>\u6240\u4ee5\uff0c\u9664\u975e\u4f60\u53ea\u662f\u60f3\u7eaf\u7cb9\u5730\u7814\u7a76 LangChain \u7684\u6e90\u4ee3\u7801\u903b\u8f91\u800c\u4e0d\u505a\u4efb\u4f55\u5b9e\u9645\u5e94\u7528\uff0c\u5426\u5219 pip install langchain-community \u662f 100% \u5fc5\u987b\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u7b80\u5355\u7684Demo<\/strong>&nbsp;<strong>\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udd39RAG Chain Demo<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install langchain-openai\npip install langchain-qdrant qdrant-client\npip install langchain-huggingface\npip install sentence-transformers -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\n# \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u5207\u6362\u5230\u56fd\u5185\u955c\u50cf\u6e90\nos.environ['HF_ENDPOINT'] = 'https:\/\/hf-mirror.com'\n\nfrom langchain_openai import ChatOpenAI\nfrom langchain_huggingface import HuggingFaceEmbeddings\n\n# \u5bfc\u5165 Qdrant \u76f8\u5173\u7684\u5e93\nfrom langchain_qdrant import QdrantVectorStore\nfrom qdrant_client import QdrantClient\nfrom qdrant_client.http import models\n\nfrom langchain_core.prompts import ChatPromptTemplate\nfrom langchain_core.output_parsers import StrOutputParser\nfrom langchain_core.runnables import RunnablePassthrough\n\n# ==========================================\n# 0. \u914d\u7f6e\u4fe1\u606f (\u586b\u5165\u4f60\u7684\u771f\u5b9e\u4fe1\u606f)\n# ==========================================\n# Qdrant \u914d\u7f6e\nQDRANT_URL = \"http:\/\/xxxx:6333\/\" # \u4f60\u7684\u96c6\u7fa4\u5730\u5740\nQDRANT_API_KEY = \"xxx\" # \u4f60\u7684 API Key\nCOLLECTION_NAME = \"LangChainTest\" # \u96c6\u5408\u540d\u79f0\n\n# DeepSeek \u914d\u7f6e\nDEEPSEEK_API_KEY = \"xxx\"\nDEEPSEEK_BASE_URL = \"https:\/\/api.deepseek.com\"\n\n# ==========================================\n# 1. \u51c6\u5907\u6570\u636e &amp; Embedding\n# ==========================================\ndocuments = [\n    \"\u5e7b\u5f71\u79d1\u6280\u516c\u53f8\u6210\u7acb\u4e8e 2023 \u5e74\uff0c\u603b\u90e8\u4f4d\u4e8e\u6df1\u5733\u5357\u5c71\u533a\u3002\",\n    \"\u5e7b\u5f71\u79d1\u6280\u7684\u6838\u5fc3\u4ea7\u54c1\u662f 'ShadowAI'\uff0cAI \u52a9\u624b\u3002\",\n    \"\u516c\u53f8\u7684 CEO \u53eb\u674e\u56db\uff0cCTO \u53eb\u5f20\u4e09\u3002\",\n    \"\u516c\u53f8\u7684\u4f01\u4e1a\u6587\u5316\u662f\uff1a'\u4e0d\u52a0\u73ed\uff0c\u53ea\u6478\u9c7c'\u3002\"\n]\n\nprint(\"1. \u52a0\u8f7d Embedding \u6a21\u578b...\")\n# \u6ce8\u610f\uff1a\u5982\u679c\u4f60\u7528\u7684\u662f bge-small-zh\uff0c\u7ef4\u5ea6\u662f 512\n# \u5982\u679c\u4f60\u7528 bge-m3\uff0c\u7ef4\u5ea6\u662f 1024\u3002\u4e00\u5b9a\u8981\u8bb0\u4f4f\u8fd9\u4e2a\u6570\u5b57\uff01\nembeddings = HuggingFaceEmbeddings(\n    model_name=\"BAAI\/bge-small-zh-v1.5\",\n    model_kwargs={'device': 'cpu'}\n)\n\n# ==========================================\n# 2. \u8fde\u63a5 Qdrant (\u6838\u5fc3\u4fee\u6539\u90e8\u5206)\n# ==========================================\nprint(\"2. \u8fde\u63a5 Qdrant \u6570\u636e\u5e93...\")\n\n# \u6b65\u9aa4 A: \u521b\u5efa\u539f\u751f Qdrant \u5ba2\u6237\u7aef\nclient = QdrantClient(\n    url=QDRANT_URL,\n    api_key=QDRANT_API_KEY,\n)\n\n# \u6b65\u9aa4 B: \u68c0\u67e5\u96c6\u5408\u662f\u5426\u5b58\u5728\uff0c\u4e0d\u5b58\u5728\u5219\u521b\u5efa (\u8fd9\u4e00\u6b65\u5f88\u91cd\u8981\uff0c\u9632\u6b62\u62a5\u9519)\n# \u6ce8\u610f\uff1asize \u5fc5\u987b\u548c\u4f60\u7684 embedding \u6a21\u578b\u7ef4\u5ea6\u4e00\u81f4\uff01\n# bge-small-zh-v1.5 = 512\n# bge-m3 = 1024\n# nomic-embed-text = 768\nif not client.collection_exists(collection_name=COLLECTION_NAME):\n    print(f\"\u96c6\u5408 {COLLECTION_NAME} \u4e0d\u5b58\u5728\uff0c\u6b63\u5728\u521b\u5efa...\")\n    client.create_collection(\n        collection_name=COLLECTION_NAME,\n        vectors_config=models.VectorParams(size=512, distance=models.Distance.COSINE),\n    )\n\n# \u6b65\u9aa4 C: \u5b9e\u4f8b\u5316 LangChain \u7684 VectorStore\nvectorstore = QdrantVectorStore(\n    client=client,\n    collection_name=COLLECTION_NAME,\n    embedding=embeddings,\n)\n\n# \u6b65\u9aa4 D: \u6dfb\u52a0\u6570\u636e (\u5982\u679c\u6570\u636e\u5df2\u7ecf\u5728\u5e93\u91cc\u4e86\uff0c\u8fd9\u884c\u53ef\u4ee5\u6ce8\u91ca\u6389\uff0c\u4e0d\u7136\u4f1a\u91cd\u590d\u6dfb\u52a0)\nprint(\"\u6b63\u5728\u5199\u5165\u6570\u636e...\")\nvectorstore.add_texts(documents)\n\n# \u521b\u5efa\u68c0\u7d22\u5668\nretriever = vectorstore.as_retriever(search_kwargs={\"k\": 2})\n\n# ==========================================\n# 3. \u540e\u7eed RAG \u6d41\u7a0b (\u548c\u4e4b\u524d\u5b8c\u5168\u4e00\u6837)\n# ==========================================\nprint(\"3. \u521d\u59cb\u5316 DeepSeek...\")\nllm = ChatOpenAI(\n    model=\"deepseek-chat\",\n    openai_api_key=DEEPSEEK_API_KEY,\n    openai_api_base=DEEPSEEK_BASE_URL,\n    temperature=0.1\n)\n\ntemplate = \"\"\"\n\u4f7f\u7528\u4ee5\u4e0b\u4e0a\u4e0b\u6587\u56de\u7b54\u95ee\u9898\u3002\u5982\u679c\u4e0d\u6e05\u695a\uff0c\u8bf4\u4e0d\u77e5\u9053\u3002\n\u3010\u4e0a\u4e0b\u6587\u3011\uff1a\n{context}\n\n\u3010\u95ee\u9898\u3011\uff1a\n{question}\n\"\"\"\nprompt = ChatPromptTemplate.from_template(template)\n\ndef format_docs(docs):\n    return \"\\n\\n\".join([d.page_content for d in docs])\n\nrag_chain = (\n    {\"context\": retriever | format_docs, \"question\": RunnablePassthrough()}\n    | prompt\n    | llm\n    | StrOutputParser()\n)\n\n# ==========================================\n# 4. \u6d4b\u8bd5\n# ==========================================\nprint(\"\\n=== \u6d4b\u8bd5\u7ed3\u679c ===\")\nprint(rag_chain.invoke(\"\u8fd9\u5bb6\u516c\u53f8\u7684CEO\u662f\u8c01\uff1f\"))<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\ud83d\udd39RAG Server Demo<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install \"langserve[all]\" uvicorn sse_starlette<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\n# 1. \u89e3\u51b3\u4e0b\u8f7d\u62a5\u9519 (\u53ef\u9009)\nos.environ['HF_ENDPOINT'] = 'https:\/\/hf-mirror.com'\n\nfrom fastapi import FastAPI\nfrom langchain_openai import ChatOpenAI\nfrom langchain_huggingface import HuggingFaceEmbeddings\nfrom langchain_qdrant import QdrantVectorStore\nfrom qdrant_client import QdrantClient\nfrom langchain_core.prompts import ChatPromptTemplate\nfrom langchain_core.output_parsers import StrOutputParser\nfrom langchain_core.runnables import RunnablePassthrough\nfrom langserve import add_routes\n\n# ==========================================\n# 2. \u5168\u5c40\u521d\u59cb\u5316 (\u53ea\u6267\u884c\u4e00\u6b21\uff0c\u907f\u514d\u6bcf\u6b21\u8bf7\u6c42\u90fd\u91cd\u8fde)\n# ==========================================\n\n# \u914d\u7f6e\u4fe1\u606f\nQDRANT_URL = \"http:\/\/xxx:6333\/\" # \u66ff\u6362\u4f60\u7684\nQDRANT_API_KEY = \"xxx\" # \u66ff\u6362\u4f60\u7684\nCOLLECTION_NAME = \"LangChainTest\"\nDEEPSEEK_API_KEY = \"xxx\"\nDEEPSEEK_BASE_URL = \"https:\/\/api.deepseek.com\"\n\n# \u521d\u59cb\u5316 Embedding\nprint(\"\u6b63\u5728\u52a0\u8f7d\u6a21\u578b...\")\nembeddings = HuggingFaceEmbeddings(\n    model_name=\"BAAI\/bge-small-zh-v1.5\",\n    model_kwargs={'device': 'cpu'}\n)\n\n# \u8fde\u63a5 Qdrant\nclient = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)\nvectorstore = QdrantVectorStore(\n    client=client,\n    collection_name=COLLECTION_NAME,\n    embedding=embeddings,\n)\nretriever = vectorstore.as_retriever(search_kwargs={\"k\": 2})\n\n# \u521d\u59cb\u5316 DeepSeek\nllm = ChatOpenAI(\n    model=\"deepseek-chat\",\n    openai_api_key=DEEPSEEK_API_KEY,\n    openai_api_base=DEEPSEEK_BASE_URL,\n    temperature=0.1\n)\n\n# \u5b9a\u4e49 Prompt\ntemplate = \"\"\"\n\u4f7f\u7528\u4ee5\u4e0b\u4e0a\u4e0b\u6587\u56de\u7b54\u95ee\u9898\u3002\u5982\u679c\u4e0d\u6e05\u695a\uff0c\u8bf4\u4e0d\u77e5\u9053\u3002\n\u3010\u4e0a\u4e0b\u6587\u3011\uff1a\n{context}\n\n\u3010\u95ee\u9898\u3011\uff1a\n{question}\n\"\"\"\nprompt = ChatPromptTemplate.from_template(template)\n\ndef format_docs(docs):\n    return \"\\n\\n\".join([d.page_content for d in docs])\n\n# \u5b9a\u4e49\u94fe (Chain)\nrag_chain = (\n    {\"context\": retriever | format_docs, \"question\": RunnablePassthrough()}\n    | prompt\n    | llm\n    | StrOutputParser()\n)\n\n# ==========================================\n# 3. \u521b\u5efa FastAPI \u5e94\u7528 (LangServe \u6838\u5fc3)\n# ==========================================\napp = FastAPI(\n    title=\"LangChain RAG API\",\n    version=\"1.0\",\n    description=\"\u4e00\u4e2a\u57fa\u4e8e DeepSeek + Qdrant \u7684\u77e5\u8bc6\u5e93\u95ee\u7b54 API\",\n)\n\n# \u6838\u5fc3\u9b54\u6cd5\uff1a\u628a chain \u6302\u8f7d\u5230 \/rag \u8fd9\u4e2a\u8def\u5f84\u4e0b\nadd_routes(\n    app,\n    rag_chain,\n    path=\"\/rag\",\n)\n\nif __name__ == \"__main__\":\n    import uvicorn\n    # \u542f\u52a8\u670d\u52a1\uff0c\u76d1\u542c 8000 \u7aef\u53e3\n    print(\"\u670d\u52a1\u5df2\u542f\u52a8: http:\/\/localhost:8000\")\n    uvicorn.run(app, host=\"localhost\", port=8000)<\/pre>\n\n\n\n<p>\u5f53\u4f60\u8fd0\u884c python server.py \u65f6\uff1a<\/p>\n\n\n\n<ul>\n<li>uvicorn \u542f\u52a8\uff0c\u7ad9\u5728\u95e8\u53e3\uff08\u7aef\u53e3 8000\uff09\u5f00\u59cb\u63a5\u5ba2\u3002<\/li>\n\n\n\n<li>\u7528\u6237\u53d1\u6765\u4e00\u4e2a\u8bf7\u6c42\uff1a\u201cCEO \u662f\u8c01\uff1f\u201d<\/li>\n\n\n\n<li>uvicorn \u628a\u8bf7\u6c42\u4ea4\u7ed9 langserve\u3002<\/li>\n\n\n\n<li>langserve \u8fd0\u884c\u4f60\u7684 RAG \u94fe\uff0c\u751f\u6210\u7b54\u6848\u3002<\/li>\n\n\n\n<li>sse_starlette \u8d1f\u8d23\u628a\u7b54\u6848\u5efa\u7acb\u6210\u4e00\u6761\u7ba1\u9053\uff0c\u201c\u674e\u2026\u56db\u2026\u662f\u2026\u201d\uff0c\u4e00\u4e2a\u5b57\u4e00\u4e2a\u5b57\u63a8\u56de\u53bb\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6240\u4ee5\u8fd9\u4e09\u4e2a\u7f3a\u4e00\u4e0d\u53ef\uff1a\u4e00\u4e2a\u662f\u5927\u8111\uff08LangServe\uff09\uff0c\u4e00\u4e2a\u662f\u8eab\u4f53\uff08Uvicorn\uff09\uff0c\u4e00\u4e2a\u662f\u5634\u5df4\uff08SSE\uff09\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>LangChain \u548c \u00a0LangFuse \u7ed3\u5408 Demo\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udd39\u4ec0\u4e48\u662fLangfuse<\/p>\n\n\n\n<p>Langfuse\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u53ef\u89c2\u6d4b\u6027\u5e73\u53f0\uff0c\u4e13\u4e3a\u5927\u8bed\u8a00\u6a21\u578b\uff08LLM\uff09\u5e94\u7528\u800c\u8bbe\u8ba1\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u5957\u5168\u9762\u7684\u5de5\u5177\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u6df1\u5165\u4e86\u89e3LLM\u5e94\u7528\u7684\u8fd0\u884c\u72b6\u51b5\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9LLM\u8bf7\u6c42\u7684\u8ddf\u8e2a\u3001\u8c03\u8bd5\u548c\u4f18\u5316\u3002\u65e0\u8bba\u4f7f\u7528\u7684\u662fLangChain\u3001LlamaIndex\u7b49\u4e3b\u6d41\u6846\u67b6\uff0c\u8fd8\u662f\u81ea\u5b9a\u4e49\u7684RAG\uff08\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff09\u7ba1\u9053\uff0cLangfuse\u90fd\u80fd\u4ee5\u6700\u5c0f\u7684\u914d\u7f6e\u6210\u672c\u65e0\u7f1d\u96c6\u6210\uff0c\u5e76\u63ed\u793aLLM\u5e94\u7528\u5185\u90e8\u7684\u590d\u6742\u6027\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"723\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-36-1024x723.png\" alt=\"\" class=\"wp-image-19831\" style=\"width:468px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-36-1024x723.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-36-300x212.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-36-768x542.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-36-830x586.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-36-230x162.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-36-350x247.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-36-480x339.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-36.png 1501w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>Langfuse\u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e\u5176\u5bf9LLM\u5e94\u7528\u5168\u751f\u547d\u5468\u671f\u7684\u8be6\u7ec6\u53ef\u89c1\u6027\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u8ffd\u8e2a\uff08Traces\uff09\uff1a<\/strong> \u8bb0\u5f55LLM\u5e94\u7528\u4e2d\u5b8c\u6574\u7684\u8bf7\u6c42\u751f\u547d\u5468\u671f\uff0c\u4ece\u7528\u6237\u67e5\u8be2\u5230\u6700\u7ec8\u54cd\u5e94\u7684\u6bcf\u4e00\u4e2a\u6b65\u9aa4\uff0c\u5305\u62ecAPI\u8c03\u7528\u3001\u4e0a\u4e0b\u6587\u3001\u63d0\u793a\u3001\u5e76\u884c\u5904\u7406\u7b49\u3002\u8fd9\u5bf9\u4e8e\u7406\u89e3\u591a\u8df3\u63a8\u7406\u3001\u5de5\u5177\u4f7f\u7528\u548c\u68c0\u7d22\u8c03\u7528\u7684\u884c\u4e3a\u81f3\u5173\u91cd\u8981\u3002<\/li>\n\n\n\n<li><strong>\u6307\u6807\uff08Metrics\uff09\uff1a<\/strong> \u63d0\u4f9b\u5173\u952e\u7684\u6027\u80fd\u3001\u6210\u672c\u548c\u8d28\u91cf\u6307\u6807\u3002\u53ef\u4ee5\u76d1\u63a7\u6a21\u578b\u5ef6\u8fdf\u3001API\u8c03\u7528\u6210\u672c\uff08\u4f8b\u5982OpenAI\u7684token\u4f7f\u7528\uff09\u3001\u9519\u8bef\u7387\u548c\u91cd\u8bd5\u6b21\u6570\u7b49\uff0c\u4ece\u800c\u91cf\u5316\u5e94\u7528\u7684\u8fd0\u884c\u6548\u7387\u548c\u7ecf\u6d4e\u6548\u76ca\u3002<\/li>\n\n\n\n<li><strong>\u8bc4\u4f30\uff08Assessments\uff09\uff1a<\/strong> \u652f\u6301\u5bf9LLM\u54cd\u5e94\u8d28\u91cf\u8fdb\u884c\u81ea\u52a8\u5316\u8bc4\u4f30\u548c\u4eba\u5de5\u8bc4\u4f30\u3002Langfuse\u53ef\u4ee5\u4e0e\u201cLLM\u5373\u88c1\u5224\u201d\u7684\u8bc4\u4f30\u65b9\u6cd5\u96c6\u6210\uff0c\u5e76\u652f\u6301\u57fa\u4e8eLLM\u7684\u81ea\u52a8\u8bc4\u5206\uff0c\u8bc4\u4f30\u6307\u6807\u5305\u62ec\u51c6\u786e\u6027\u3001\u6709\u6bd2\u6027\u548c\u8fde\u8d2f\u6027\u7b49\u3002\u6b64\u5916\uff0c\u5b83\u4e5f\u63d0\u4f9b\u4e86\u624b\u52a8\u8bc4\u4f30\u754c\u9762\uff0c\u65b9\u4fbf\u4eba\u5de5\u5ba1\u6838\u548c\u53cd\u9988\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u9a8c\u8ddf\u8e2a\uff08Experiment Tracking\uff09\uff1a<\/strong> \u5141\u8bb8\u5f00\u53d1\u8005\u5bf9\u4e0d\u540c\u914d\u7f6e\u8fdb\u884cA\/B\u6d4b\u8bd5\uff0c\u5e76\u6bd4\u8f83\u5b83\u4eec\u5728\u6027\u80fd\u3001\u6210\u672c\u548c\u8d28\u91cf\u65b9\u9762\u7684\u8868\u73b0\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u9a71\u52a8\u7684\u4f18\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7b80\u800c\u8a00\u4e4b\uff0cLangfuse\u586b\u8865\u4e86\u4f20\u7edf\u76d1\u63a7\u5de5\u5177\u5728LLM\u5e94\u7528\u9886\u57df\u7559\u4e0b\u7684\u7a7a\u767d\uff0c\u5b83\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6e05\u6670\u7684\u201c\u7a97\u53e3\u201d\uff0c\u53ef\u4ee5\u900f\u89c6AI\u5de5\u4f5c\u6d41\u7684\u5185\u90e8\uff0c\u5e2e\u52a9\u6211\u4eec\u53d1\u73b0\u95ee\u9898\u3001\u8861\u91cf\u5173\u952e\u6307\u6807\uff0c\u5e76\u5145\u6ee1\u4fe1\u5fc3\u5730\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n\n\n\n<p>\u4f20\u7edf\u7684\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u4e60\u60ef\u4e8e\u4f7f\u7528\u65e5\u5fd7\u548c\u5e94\u7528\u6027\u80fd\u76d1\u63a7\uff08APM\uff09\u5de5\u5177\u6765\u8ffd\u8e2a\u7cfb\u7edf\u884c\u4e3a\u3002\u7136\u800c\uff0cLLM\u5e94\u7528\u7684\u7279\u6027\u4f7f\u5f97\u8fd9\u4e9b\u4f20\u7edf\u65b9\u6cd5\u663e\u5f97\u529b\u4e0d\u4ece\u5fc3\u3002LLM\u5e94\u7528\u5e76\u975e\u7b80\u5355\u7684\u786e\u5b9a\u6027\u903b\u8f91\uff0c\u5176\u5185\u90e8\u8fd0\u4f5c\u5145\u6ee1\u4e86\u590d\u6742\u6027\u548c\u4e0d\u786e\u5b9a\u6027\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u770b\u4f3c\u7b80\u5355\u7684\u5ba2\u6237\u652f\u6301\u804a\u5929\u673a\u5668\u4eba\uff0c\u5728\u5e55\u540e\u53ef\u80fd\u6d89\u53ca\uff1a<\/p>\n\n\n\n<ul>\n<li>\u4ece\u5411\u91cf\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u76f8\u5173\u6587\u6863<\/li>\n\n\n\n<li>\u6784\u5efa\u5305\u542b\u4e0a\u4e0b\u6587\u7684\u590d\u6742\u63d0\u793a<\/li>\n\n\n\n<li>\u5bf9\u4e0d\u540c\u63a8\u7406\u6b65\u9aa4\u8fdb\u884c\u591a\u6b21LLM\u8c03\u7528<\/li>\n\n\n\n<li>\u901a\u8fc7\u5404\u79cd\u8fc7\u6ee4\u5668\u5904\u7406\u54cd\u5e94<\/li>\n\n\n\n<li>\u751f\u6210\u6700\u7ec8\u7b54\u6848<\/li>\n<\/ul>\n\n\n\n<p>\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u90fd\u53ef\u80fd\u5f15\u5165\u4e0d\u53ef\u63a7\u7684\u53d8\u91cf\uff0c\u5982token\u4f7f\u7528\u91cf\u3001\u54cd\u5e94\u8d28\u91cf\u3001\u5ef6\u8fdf\u4ee5\u53ca\u56e0\u63d0\u793a\u590d\u6742\u5ea6\u548c\u6a21\u578b\u884c\u4e3a\u800c\u6ce2\u52a8\u7684\u6210\u672c\u3002\u5f53\u804a\u5929\u673a\u5668\u4eba\u7ed9\u51fa\u4e0d\u4f73\u7684\u54cd\u5e94\u65f6\uff0c\u6211\u4eec\u5f88\u96be\u76f4\u63a5\u5224\u65ad\u662f\u68c0\u7d22\u5230\u7684\u6587\u6863\u4e0d\u76f8\u5173\u3001\u63d0\u793a\u6784\u5efa\u4e0d\u5f53\uff0c\u8fd8\u662f\u6a21\u578b\u4ea7\u751f\u4e86\u5e7b\u89c9\u3002\u7f3a\u4e4f\u9002\u5f53\u7684\u53ef\u89c2\u6d4b\u6027\uff0c\u8c03\u8bd5LLM\u5e94\u7528\u5c31\u50cf\u5728\u76f2\u76ee\u6478\u7d22\uff0c\u4e0d\u4ec5\u6548\u7387\u4f4e\u4e0b\uff0c\u8fd8\u53ef\u80fd\u5bfc\u81f4\u6210\u672c\u5931\u63a7\u3002<\/p>\n\n\n\n<p>Langfuse\u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u6311\u6218\u800c\u751f\u3002\u5b83\u4f5c\u4e3a\u4e00\u4e2a\u53ef\u89c2\u6d4b\u6027\u5c42\uff0c\u80fd\u591f\u6355\u6349\u8bed\u8a00\u6a21\u578b\u5e94\u7528\u7684\u5b8c\u6574\u6267\u884c\u4e0a\u4e0b\u6587\uff0c\u4ece\u800c\u63d0\u4f9b\u4f20\u7edf\u5de5\u5177\u65e0\u6cd5\u6bd4\u62df\u7684\u6df1\u5ea6\u6d1e\u5bdf\uff1a<\/p>\n\n\n\n<ul>\n<li>1. \u6210\u672c\u900f\u660e\u5316\u4e0e\u4f18\u5316<\/li>\n<\/ul>\n\n\n\n<p>LLM\u7684API\u8c03\u7528\u6210\u672c\u662f\u8bb8\u591a\u5f00\u53d1\u8005\u9762\u4e34\u7684\u75db\u70b9\u3002Langfuse\u80fd\u591f\u81ea\u52a8\u8ba1\u7b97\u652f\u6301\u6a21\u578b\u7684\u6210\u672c\uff08\u5982OpenAI\u3001Anthropic\uff09\uff0c\u5e76\u5141\u8bb8\u6309\u7528\u6237\u3001\u4f1a\u8bdd\u3001\u7279\u5b9a\u6a21\u578b\u7248\u672c\u3001\u4e0d\u540c\u529f\u80fd\u6216\u5de5\u4f5c\u6d41\u7a0b\uff0c\u751a\u81f3\u5730\u7406\u533a\u57df\u6765\u7ec6\u5206\u6210\u672c\u3002\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u7cbe\u786e\u5730\u627e\u51fa\u54ea\u4e9b\u63d0\u793a\u3001\u7528\u6237\u6216\u529f\u80fd\u6b63\u5728\u9a71\u52a8\u4f60\u7684\u5f00\u652f\uff0c\u4ece\u800c\u6709\u9488\u5bf9\u6027\u5730\u8fdb\u884c\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u901a\u8fc7Langfuse\uff0c\u53ef\u4ee5\u53d1\u73b0\u67d0\u4e9b\u516c\u53f8\u62a5\u544a\u89e6\u53d1\u4e86\u6781\u957f\u7684\u63d0\u793a\uff0812,000+ token\uff09\uff0c\u5bfc\u81f4\u6210\u672c\u98d9\u5347\uff0c\u8fdb\u800c\u4f18\u5316\u5206\u5757\u7b56\u7565\uff0c\u5c06\u5e73\u5747\u63d0\u793a\u957f\u5ea6\u51cf\u5c1140%\uff0c\u5b9e\u73b0\u663e\u8457\u7684\u6210\u672c\u8282\u7ea6\u3002<\/p>\n\n\n\n<ul>\n<li>2. \u6027\u80fd\u74f6\u9888\u8bc6\u522b<\/li>\n<\/ul>\n\n\n\n<p>LLM\u5e94\u7528\u7684\u54cd\u5e94\u901f\u5ea6\u76f4\u63a5\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\u3002Langfuse\u6355\u83b7\u4e86\u4ece\u6570\u636e\u68c0\u7d22\u3001\u63d0\u793a\u521b\u5efa\u5230LLM\u5206\u6790\u7684\u6bcf\u4e00\u4e2a\u9636\u6bb5\uff0c\u4f7f\u5f97\u5ef6\u8fdf\u6d4b\u91cf\u548c\u7ec6\u7c92\u5ea6\u7684\u6027\u80fd\u8c03\u4f18\u6210\u4e3a\u53ef\u80fd\u3002\u5f53\u7684\u5e94\u7528\u611f\u89c9\u7f13\u6162\u65f6\uff0cLangfuse\u53ef\u4ee5\u51c6\u786e\u5730\u663e\u793a\u65f6\u95f4\u82b1\u8d39\u5728\u54ea\u91cc\uff0c\u662f\u5411\u91cf\u641c\u7d22\u3001\u63d0\u793a\u6784\u5efa\uff0c\u8fd8\u662fLLM\u63a8\u7406\u672c\u8eab\uff1f\u901a\u8fc7\u8bc6\u522b\u5e76\u4f18\u5316\u6700\u6162\u7684\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u5e94\u7528\u7684\u6574\u4f53\u6027\u80fd\u3002<\/p>\n\n\n\n<ul>\n<li>3. \u5927\u89c4\u6a21\u8d28\u91cf\u76d1\u63a7<\/li>\n<\/ul>\n\n\n\n<p>\u786e\u4fddLLM\u8f93\u51fa\u7684\u8d28\u91cf\u662f\u6784\u5efa\u53ef\u9760AI\u5e94\u7528\u7684\u5173\u952e\u3002Langfuse\u5141\u8bb8\u8fd0\u884c\u57fa\u4e8e\u6a21\u578b\u7684\u8bc4\u4f30\uff0cLLM\u53ef\u4ee5\u6839\u636e\u51c6\u786e\u6027\u3001\u6709\u6bd2\u6027\u6216\u5e7b\u89c9\u7b49\u56e0\u7d20\u5bf9\u7279\u5b9a\u4f1a\u8bdd\u3001\u8ffd\u8e2a\u6216LLM\u8c03\u7528\u8fdb\u884c\u8bc4\u5206\u3002\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u8bbe\u7f6e\u81ea\u52a8\u5316\u8d28\u91cf\u8bc4\u5206\uff0c\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u6301\u7eed\u8fd0\u884c\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u624b\u52a8\u5ba1\u67e5\u3002\u8fd9\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u6548\u7387\uff0c\u8fd8\u80fd\u5728\u7528\u6237\u53d1\u73b0\u95ee\u9898\u4e4b\u524d\u53ca\u65f6\u6355\u83b7\u5e76\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n\n\n\n<ul>\n<li>4. \u63d0\u793a\u8bcd\u5de5\u7a0b\u4f18\u5316<\/li>\n<\/ul>\n\n\n\n<p>\u63d0\u793a\u8bcd\uff08Prompt\uff09\u7684\u5fae\u5c0f\u6539\u52a8\u90fd\u53ef\u80fd\u5bf9LLM\u7684\u8f93\u51fa\u8d28\u91cf\u548c\u6210\u672c\u4ea7\u751f\u5de8\u5927\u5f71\u54cd\u3002Langfuse\u80fd\u591f\u8ffd\u8e2a\u54ea\u4e9b\u63d0\u793a\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u8868\u73b0\u6700\u4f73\uff0c\u5e76\u652f\u6301\u5bf9\u63d0\u793a\u53d8\u4f53\u8fdb\u884cA\/B\u6d4b\u8bd5\uff0c\u8861\u91cf\u5b83\u4eec\u5bf9\u8d28\u91cf\u548c\u6210\u672c\u7684\u5f71\u54cd\u3002\u901a\u8fc7\u7cfb\u7edf\u5730\u6d4b\u8bd5\u548c\u4f18\u5316\u63d0\u793a\uff0c\u53ef\u4ee5\u627e\u5230\u6bcf\u4e2a\u7528\u4f8b\u7684\u6700\u4f73\u7248\u672c\uff0c\u4ece\u800c\u63d0\u5347\u6a21\u578b\u8868\u73b0\u5e76\u63a7\u5236\u6210\u672c\u3002<\/p>\n\n\n\n<ul>\n<li>5. \u7528\u6237\u4f53\u9a8c\u6d1e\u5bdf<\/li>\n<\/ul>\n\n\n\n<p>Langfuse\u5141\u8bb8\u5b9e\u65f6\u76d1\u63a7\u6210\u672c\u548c\u5ef6\u8fdf\u6307\u6807\uff0c\u5e76\u6309\u7528\u6237\u3001\u4f1a\u8bdd\u3001\u5730\u7406\u4f4d\u7f6e\u548c\u6a21\u578b\u7248\u672c\u8fdb\u884c\u5206\u7c7b\u3002\u901a\u8fc7\u4e86\u89e3\u4e0d\u540c\u7528\u6237\u7fa4\u4f53\u5982\u4f55\u4e0eAI\u4e92\u52a8\uff0c\u53ef\u4ee5\u4f18\u5148\u8003\u8651\u6539\u8fdb\u65b9\u5411\uff0c\u5e76\u8bc6\u522b\u4e2a\u6027\u5316\u7684\u673a\u4f1a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4e3a\u9ad8\u9891\u7528\u6237\u548c\u666e\u901a\u7528\u6237\u5236\u5b9a\u4e0d\u540c\u7684\u4f18\u5316\u7b56\u7565\uff0c\u4ee5\u63d0\u4f9b\u66f4\u4f18\u8d28\u7684\u670d\u52a1\u3002<\/p>\n\n\n\n<p>\u603b\u4e4b\uff0cLangfuse\u4e3aLLM\u5e94\u7528\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u96c6\uff0c\u5c06AI\u5f00\u53d1\u4ece\u201c\u6210\u672c\u9ad8\u6602\u7684\u731c\u6d4b\u6e38\u620f\u201d\u8f6c\u53d8\u4e3a\u201c\u6570\u636e\u9a71\u52a8\u7684\u4f18\u5316\u673a\u5668\u201d\u3002\u5b83\u8ba9LLM\u5e94\u7528\u7684\u53ef\u89c2\u6d4b\u6027\u4e0d\u518d\u662f\u53ef\u9009\u9879\uff0c\u800c\u662f\u6784\u5efa\u53ef\u9760\u3001\u9ad8\u6548\u548c\u53ef\u89e3\u91caAI\u7cfb\u7edf\u7684\u5fc5\u9700\u54c1\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"500\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-38-1024x500.png\" alt=\"\" class=\"wp-image-19833\" style=\"width:548px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-38-1024x500.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-38-300x146.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-38-768x375.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-38-830x405.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-38-230x112.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-38-350x171.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-38-480x234.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-38.png 1397w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5b89\u88c5langfuse sdk\uff08\u7248\u672c>3.0\uff09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install --upgrade langfuse -i https:\/\/mirrors.aliyun.com\/pypi\/simple\/\n<\/pre>\n\n\n\n<p>Python\u4ee3\u7801 LangChain \u7ed3\u5408 Langfuse\uff0c \u8fde\u63a5Langfuse Cloud<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\n# \u89e3\u51b3\u4e0b\u8f7d\u62a5\u9519 (\u53ef\u9009)\nos.environ['HF_ENDPOINT'] = 'https:\/\/hf-mirror.com'\n\nfrom fastapi import FastAPI\nfrom langchain_openai import ChatOpenAI\nfrom langchain_huggingface import HuggingFaceEmbeddings\nfrom langchain_qdrant import QdrantVectorStore\nfrom qdrant_client import QdrantClient\nfrom langchain_core.prompts import ChatPromptTemplate\nfrom langchain_core.output_parsers import StrOutputParser\nfrom langchain_core.runnables import RunnablePassthrough\nfrom langserve import add_routes\nfrom langfuse import Langfuse\nfrom langfuse.langchain import CallbackHandler\n\n# ==========================================\n#  Langfuse \u914d\u7f6e\n# ==========================================\nos.environ[\"LANGFUSE_PUBLIC_KEY\"] = \"pk-xx\"   # \u66ff\u6362\u4f60\u7684\u516c\u94a5\nos.environ[\"LANGFUSE_SECRET_KEY\"] = \"sk-xx\"   # \u66ff\u6362\u4f60\u7684\u79c1\u94a5\nos.environ[\"LANGFUSE_HOST\"] = \"https:\/\/us.cloud.langfuse.com\" # \u5982\u679c\u662f\u4e91\u7aef\u7248\u4e0d\u7528\u6539\uff0c\u79c1\u6709\u5316\u90e8\u7f72\u6539\u8fd9\u91cc\n\n# ==========================================\n# \u5168\u5c40\u521d\u59cb\u5316\n# ==========================================\n\n# [\u65b0\u589e] \u521d\u59cb\u5316 Langfuse Handler\n# \u8fd9\u4e2a handler \u4f1a\u81ea\u52a8\u6355\u83b7 LangChain \u7684\u6240\u6709\u52a8\u4f5c\nlangfuse_handler = CallbackHandler()\n\n# \u914d\u7f6e\u4fe1\u606f\nQDRANT_URL = \"http:\/\/xxx:6333\/\" # \u66ff\u6362\u4f60\u7684\nQDRANT_API_KEY = \"xxx\" # \u66ff\u6362\u4f60\u7684\nCOLLECTION_NAME = \"Employee Handbook\"\nDEEPSEEK_API_KEY = \"sk-xxx\"\nDEEPSEEK_BASE_URL = \"https:\/\/api.deepseek.com\"\n\n# \u521d\u59cb\u5316 Embedding\nprint(\"\u6b63\u5728\u52a0\u8f7d\u6a21\u578b...\")\nembeddings = HuggingFaceEmbeddings(\n    model_name=\"BAAI\/bge-m3\",\n    model_kwargs={\"device\": \"cpu\"},\n    encode_kwargs={\"normalize_embeddings\": True}\n)\n\n# \u8fde\u63a5 Qdrant\nclient = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)\nvectorstore = QdrantVectorStore(\n    client=client,\n    collection_name=COLLECTION_NAME,\n    embedding=embeddings,\n)\nretriever = vectorstore.as_retriever(search_kwargs={\"k\": 2})\n\n# \u521d\u59cb\u5316 DeepSeek\nllm = ChatOpenAI(\n    model=\"deepseek-chat\",\n    openai_api_key=DEEPSEEK_API_KEY,\n    openai_api_base=DEEPSEEK_BASE_URL,\n    temperature=0.1\n)\n\n# \u5b9a\u4e49 Prompt\ntemplate = \"\"\"\n\u4f7f\u7528\u4ee5\u4e0b\u4e0a\u4e0b\u6587\u56de\u7b54\u95ee\u9898\u3002\u5982\u679c\u4e0d\u6e05\u695a\uff0c\u8bf4\u4e0d\u77e5\u9053\u3002\n\u3010\u4e0a\u4e0b\u6587\u3011\uff1a\n{context}\n\n\u3010\u95ee\u9898\u3011\uff1a\n{question}\n\"\"\"\nprompt = ChatPromptTemplate.from_template(template)\n\ndef format_docs(docs):\n    return \"\\n\\n\".join([d.page_content for d in docs])\n\n# \u5b9a\u4e49\u94fe (Chain)\nrag_chain = (\n    {\"context\": retriever | format_docs, \"question\": RunnablePassthrough()}\n    | prompt\n    | llm\n    | StrOutputParser()\n)\n\n# ==========================================\n# \u521b\u5efa FastAPI \u5e94\u7528 (LangServe \u6838\u5fc3)\n# ==========================================\napp = FastAPI(\n    title=\"LangChain RAG API\",\n    version=\"1.0\",\n    description=\"\u4e00\u4e2a\u57fa\u4e8e DeepSeek + Qdrant \u7684\u77e5\u8bc6\u5e93\u95ee\u7b54 API\",\n)\n\n# \u5728\u8fd9\u91cc\u6ce8\u5165 Langfuse\n# \u4f7f\u7528 .with_config() \u5c06 callbacks \u7ed1\u5b9a\u5230\u94fe\u4e0a\n# \u8fd9\u6837\u901a\u8fc7 API \u8bf7\u6c42\u7684\u6bcf\u4e00\u6b21\u8c03\u7528\u90fd\u4f1a\u81ea\u52a8\u53d1\u9001\u5230 Langfuse\nadd_routes(\n    app,\n    rag_chain.with_config({\"callbacks\": [langfuse_handler]}), # &lt;--- [\u91cd\u70b9\u4fee\u6539]\n    path=\"\/rag\",\n)\n\nif __name__ == \"__main__\":\n    import uvicorn\n    # \u542f\u52a8\u670d\u52a1\uff0c\u76d1\u542c 8000 \u7aef\u53e3\n    print(\"\u670d\u52a1\u5df2\u542f\u52a8: http:\/\/localhost:8000\")\n    uvicorn.run(app, host=\"localhost\", port=8000)<\/pre>\n\n\n\n<p>\u5728\u753b\u9762\u8c03\u7528RAG\u63d0\u95ee\u95ee\u9898<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"704\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-1024x704.png\" alt=\"\" class=\"wp-image-19835\" style=\"width:610px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-1024x704.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-300x206.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-768x528.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-1536x1056.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-2048x1408.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-830x571.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-230x158.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-350x241.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-40-480x330.png 480w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u767b\u5f55LangFuse\u7f51\u9875\u53ef\u4ee5\u770b\u5230\u7ec6\u8282\u88abLangFuse\u76d1\u63a7\u5230\u4e86\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">https:\/\/us.cloud.langfuse.com\/<\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"503\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-1024x503.png\" alt=\"\" class=\"wp-image-19834\" style=\"width:626px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-1024x503.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-300x147.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-768x377.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-1536x754.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-2048x1005.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-830x408.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-230x113.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-350x172.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-39-480x236.png 480w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"539\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-1024x539.png\" alt=\"\" class=\"wp-image-19837\" style=\"width:634px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-1024x539.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-300x158.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-768x404.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-1536x809.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-2048x1079.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-830x437.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-230x121.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-350x184.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/12\/image-42-480x253.png 480w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5\u73af\u5883&nbsp;\ud83d\udd16 \u5728\u672c\u5730\u5b89\u88c5 LangChain \u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u56e0\u4e3a LangChain \u662f\u4e00\u4e2a\u201c\u80f6\u6c34\u201d\u6846 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/19768"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19768"}],"version-history":[{"count":15,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/19768\/revisions"}],"predecessor-version":[{"id":19829,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/19768\/revisions\/19829"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}