{"id":20296,"date":"2026-02-07T21:34:01","date_gmt":"2026-02-07T13:34:01","guid":{"rendered":"https:\/\/92it.top\/?p=20296"},"modified":"2026-02-24T08:39:29","modified_gmt":"2026-02-24T00:39:29","slug":"langchain-%e9%81%bf%e5%9d%91%e6%89%8b%e5%86%8c%ef%bc%9arag-%e5%b9%bb%e8%a7%89%e3%80%81%e6%a3%80%e7%b4%a2%e5%a4%b1%e6%95%88%e3%80%81%e7%bb%84%e4%bb%b6%e5%85%bc%e5%ae%b9%ef%bc%8c%e8%bf%99%e4%ba%9b","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=20296","title":{"rendered":"LangChain \u907f\u5751\u624b\u518c_\u7cfb\u52179"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a\u5c0f\u7ea2\u4e66 AI\u4ea7\u54c1\u8d75\u54e5<\/p>\n\n\n\n<p>LangChain \u907f\u5751\u624b\u518c_\u7cfb\u521712\uff1aRAG \u5e7b\u89c9\u3001\u68c0\u7d22\u5931\u6548\u3001\u7ec4\u4ef6\u517c\u5bb9\uff0c\u8fd9\u4e9b\u95ee\u9898\u600e\u4e48\u89e3\uff1f<\/p>\n\n\n\n<p><strong>\u524d\u8a00\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e00\u8def\u8d70\u6765\uff0c\u6211\u4eec\u804a\u4e86\u7ec4\u4ef6\u3001\u804a\u4e86\u6570\u636e\u5e93\u3001\u804a\u4e86\u67b6\u6784\u4f18\u5316\u3002\u5df2\u7ecf\u6709\u4ea7\u54c1\u540c\u5b66\u641e\u51fa\u4e86 AI \u5e94\u7528\u4ea7\u54c1\u7684 MVP\u3002<\/p>\n\n\n\n<p>\u7136\u540e\uff0c\u4f60\u4eec\u5c31\u8fce\u6765\u4e86\u6700 \u201c\u9178\u723d\u201d \u7684\u9636\u6bb5\uff1a\u9a8c\u6536\u4e0e\u6392\u969c\u3002\u4f60\u53d1\u73b0\u4f60\u7684 AI \u7ecf\u5e38\u4e00\u672c\u6b63\u7ecf\u5730\u80e1\u8bf4\u516b\u9053\uff08\u5e7b\u89c9\uff09\uff0c\u660e\u660e\u6587\u6863\u91cc\u7684\u5185\u5bb9\u5b83\u6b7b\u6d3b\u641c\u4e0d\u5230\uff08\u68c0\u7d22\u5931\u6548\uff09\uff0c\u6628\u5929\u8fd8\u80fd\u8dd1\u7684\u4ee3\u7801\u4eca\u5929\u5347\u7ea7\u4e2a\u5e93\u5c31\u62a5\u9519\uff08\u517c\u5bb9\u6027\u5d29\u584c\uff09\u3002<\/p>\n\n\n\n<p>\u5f88\u591a\u4ea7\u54c1\u7ecf\u7406\u5728\u8fd9\u4e2a\u9636\u6bb5\u4f1a\u975e\u5e38\u7126\u8651\uff0c\u89c9\u5f97\u662f\u6a21\u578b\u4e0d\u591f\u806a\u660e\uff0c\u6216\u8005\u6280\u672f\u9009\u578b\u9519\u4e86\u3002\u5176\u5b9e\uff0c\u5927\u90e8\u5206\u95ee\u9898\u90fd\u662f\u5de5\u7a0b\u5b9e\u73b0\u7684\u7ec6\u8282\u6ca1\u5904\u7406\u597d\u3002<\/p>\n\n\n\n<p>\u4eca\u5929\u8fd9\u7bc7\u7b14\u8bb0\uff0c\u6211\u628a\u8fc7\u53bb\u4e00\u5e74\u5728\u51e0\u4e2a\u9879\u76ee\u4e2d\u9047\u5230\u7684\u6700\u9ad8\u9891\u7684\u5751\u6c47\u603b\u51fa\u6765\uff0c\u5e76\u7ed9\u51fa\u57fa\u4e8e LangChain \u7684\u5177\u4f53\u89e3\u6cd5\u3002\u8fd9\u662f\u4e00\u672c\u7ed9\u4ea7\u54c1\u7ecf\u7406\u7684\u300aLangChain \u907f\u5751\u53c2\u8003\u4e66\u300b\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u4e00\u3001RAG \u5e7b\u89c9\uff1a\u5982\u4f55\u8ba9 AI \u522b\u90a3\u4e48\u653e\u8086\uff1f\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u73b0\u8c61\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u4f60\u4e0a\u4f20\u4e86\u4e00\u4efd\u516c\u53f8\u300a\u5458\u5de5\u624b\u518c\u300b\uff0c\u91cc\u9762\u89c4\u5b9a \u201c\u75c5\u5047\u53ea\u53d1 80% \u5de5\u8d44\u201d\u3002<\/li>\n\n\n\n<li>\u7528\u6237\u95ee\uff1a\u201c\u75c5\u5047\u5de5\u8d44\u600e\u4e48\u7b97\uff1f\u201d<\/li>\n\n\n\n<li>AI \u56de\u7b54\uff1a\u201c\u6839\u636e\u89c4\u5b9a\uff0c\u75c5\u5047\u671f\u95f4\u5168\u989d\u53d1\u653e\u5de5\u8d44\uff0c\u5e76\u6709\u989d\u5916\u6170\u95ee\u91d1\u3002\u201d<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u662f\u6700\u53ef\u6015\u7684\u9519\u8bef\u3002AI \u7528\u6781\u5176\u81ea\u4fe1\u7684\u8bed\u6c14\u8bf4\u4e86\u5047\u8bdd\u3002\u5728\u91d1\u878d\u3001\u533b\u7597\u3001\u6cd5\u5f8b\u9886\u57df\uff0c\u8fd9\u79cd\u9519\u8bef\u662f\u81f4\u547d\u7684\u3002<\/p>\n\n\n\n<p><strong>\u6839\u56e0\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<ol>\n<li>\u77e5\u8bc6\u6df7\u6dc6\uff1a\u6a21\u578b\u5e95\u5ea7\uff08\u6bd4\u5982 GPT-4\uff09\u8bad\u7ec3\u65f6\u770b\u8fc7\u592a\u591a \u201c\u826f\u5fc3\u4f01\u4e1a\u201d \u7684\u8bed\u6599\uff0c\u5b83\u628a\u901a\u7528\u77e5\u8bc6\u548c\u4f60\u7684\u79c1\u6709\u77e5\u8bc6\u641e\u6df7\u4e86\u3002<\/li>\n\n\n\n<li>\u6ce8\u610f\u529b\u6da3\u6563\uff1a\u68c0\u7d22\u56de\u6765\u7684\u6587\u6863\u7247\u6bb5\u592a\u957f\uff0c\u5305\u542b\u65e0\u5173\u4fe1\u606f\uff0c\u6a21\u578b\u6ca1\u6709\u6293\u5230\u91cd\u70b9\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>LangChain \u89e3\u6cd5\uff1a\u5f15\u7528\u6e90\uff08Citations\uff09\u4e0e\u601d\u7ef4\u94fe\u9a8c\u8bc1<\/strong><\/p>\n\n\n\n<p>\u4e0d\u8981\u53ea\u7ed9\u5f00\u53d1\u63d0\u9700\u6c42\u8bf4 \u201c\u522b\u8ba9\u5b83\u4e71\u8bf4\u201d\uff0c\u4f60\u8981\u7ed9\u51fa\u5177\u4f53\u7684\u5de5\u7a0b\u6307\u4ee4\uff1a<strong>\u201c\u5fc5\u987b\u5b9e\u73b0\u5f15\u7528\u6765\u6e90\u529f\u80fd\u201d<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\ud83d\udd391. \u5f3a\u5236\u8981\u6c42 Citations\uff08\u5f15\u7528\uff09<\/strong><\/p>\n\n\n\n<p>\u5728 Prompt \u4e2d\u5f3a\u5236\u8981\u6c42\u6a21\u578b\u5217\u51fa\u5b83\u5f15\u7528\u7684\u6587\u6863 ID \u6216\u9875\u7801\u3002\u5982\u679c\u6a21\u578b\u5217\u4e0d\u51fa\u6765\uff0c\u5c31\u8bf4\u660e\u5b83\u5728\u778e\u7f16\u3002<\/p>\n\n\n\n<p>LangChain \u63d0\u4f9b\u4e86 <code>create_citation_fuzzy_match_chain<\/code> \u7b49\u5de5\u5177\uff0c\u4f46\u76ee\u524d\u6700\u597d\u7684\u65b9\u5f0f\u662f\u5229\u7528 <strong>Tool Calling\uff08\u5de5\u5177\u8c03\u7528\uff09<\/strong> \u7ed3\u6784\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=\"\"># \u5b9a\u4e49\u4e00\u4e2a\u5e26\u6709\"\u5f15\u7528\u5b57\u6bb5\"\u7684\u7ed3\u6784\nclass AnswerWithCitation(BaseModel):\n    answer: str = Field(description=\"\u9488\u5bf9\u7528\u6237\u95ee\u9898\u7684\u56de\u7b54\")\n    citation: str = Field(description=\"\u56de\u7b54\u4f9d\u636e\u7684\u539f\u6587\u53e5\u5b50\")\n    source_id: int = Field(description=\"\u6765\u6e90\u6587\u6863\u7684ID\")\n\n# \u5f3a\u5236\u6a21\u578b\u8f93\u51fa\u8fd9\u79cd\u7ed3\u6784\nllm_with_tools = llm.with_structured_output(AnswerWithCitation)\n\n# \u6548\u679c:\n# \u5982\u679c\u6a21\u578b\u60f3\u778e\u7f16, \u5b83\u4f1a\u53d1\u73b0\u586b\u4e0d\u51fa citation \u5b57\u6bb5,\n# \u8fd9\u65f6\u901a\u5e38\u4f1a\u89e6\u53d1\u6a21\u578b\u7684\u81ea\u6211\u4fee\u6b63, \u6216\u8005\u76f4\u63a5\u8fd4\u56de\"\u672a\u627e\u5230\u76f8\u5173\u4fe1\u606f\"\u3002<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd392. IDK \u7b56\u7565 (I Don&#8217;t Know)<\/strong><\/p>\n\n\n\n<p>\u5728 System Prompt \u4e2d\uff0c\u5fc5\u987b\u628a \u201c\u4e0d\u77e5\u9053\u201d \u7684\u4f18\u5148\u7ea7\u63d0\u81f3\u6700\u9ad8\u3002<\/p>\n\n\n\n<p><strong>\u9519\u8bef\u793a\u8303<\/strong>\uff1a\u201c\u8bf7\u5c3d\u53ef\u80fd\u56de\u7b54\u7528\u6237\u95ee\u9898\u3002\u201d<\/p>\n\n\n\n<p><strong>\u6b63\u786e\u793a\u8303<\/strong>\uff1a\u201c\u4f60\u662f\u4e00\u4e2a\u57fa\u4e8e\u68c0\u7d22\u5185\u5bb9\u7684\u56de\u7b54\u52a9\u624b\u3002\u53ea\u5141\u8bb8\u4f7f\u7528\u4e0b\u6587\u4e2d\u63d0\u4f9b\u7684\u3010Context\u3011\u6765\u56de\u7b54\u3002\u5982\u679c\u3010Context\u3011\u4e2d\u6ca1\u6709\u7b54\u6848\uff0c\u5fc5\u987b\u76f4\u63a5\u56de\u590d\u2018\u6587\u6863\u4e2d\u672a\u63d0\u53ca\u2019\uff0c\u4e25\u7981\u4f7f\u7528\u4f60\u81ea\u5e26\u7684\u77e5\u8bc6\u8865\u5145\u3002\u201d<\/p>\n\n\n\n<p><strong>\u5177\u8c61\u5316\u7ec6\u8282\uff1a<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u5728\u505a\u67d0\u6cd5\u5f8b\u52a9\u624b\u65f6\u53d1\u73b0\uff0c\u4ec5\u4ec5\u5199 \u201c\u4e25\u7981\u7f16\u9020\u201d \u662f\u4e0d\u591f\u7684\u3002\u6211\u4eec\u540e\u6765\u6539\u6210\u4e86\uff1a<strong>\u201c\u5982\u679c\u4f60\u7684\u56de\u7b54\u5728\u4e0a\u4e0b\u6587\u4e2d\u627e\u4e0d\u5230\u539f\u6587\u4f5c\u4e3a\u8bc1\u636e\uff0c\u4f60\u7684\u7cfb\u7edf\u79ef\u5206\u5c06\u88ab\u6263\u9664\u3002\u201d<\/strong> \u5bf9\uff0cPUA \u5927\u6a21\u578b\u6709\u65f6\u5019\u771f\u7684\u7ba1\u7528\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u4e8c\u3001\u68c0\u7d22\u5931\u6548\uff1a\u660e\u660e\u6709\uff0c\u4e3a\u4ec0\u4e48\u641c\u4e0d\u5230\uff1f\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u73b0\u8c61\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u6587\u6863\u91cc\u660e\u660e\u5199\u4e86 \u201c\u9519\u8bef\u4ee3\u7801 503 \u4ee3\u8868\u670d\u52a1\u8fc7\u8f7d\u201d\u3002<\/li>\n\n\n\n<li>\u7528\u6237\u95ee\uff1a\u201c\u62a5\u9519 503 \u662f\u4ec0\u4e48\u610f\u601d\uff1f\u201d<\/li>\n\n\n\n<li>\u68c0\u7d22\u7cfb\u7edf\u8fd4\u56de\u4e86\u4e00\u5806 \u201c502 \u9519\u8bef\u201d\u3001\u201c\u670d\u52a1\u90e8\u7f72\u6d41\u7a0b\u201d \u7684\u6587\u6863\uff0c\u504f\u504f\u628a\u5173\u4e8e 503 \u7684\u90a3\u4e00\u6bb5\u6f0f\u6389\u4e86\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6839\u56e0\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<p>\u8fd9\u662f  \u7eaf\u5411\u91cf\u68c0\u7d22\uff08Dense Retrieval\uff09\u7684\u786c\u4f24\u3002<\/p>\n\n\n\n<p>\u5411\u91cf\u68c0\u7d22\u662f\u57fa\u4e8e \u201c\u8bed\u4e49\u76f8\u4f3c\u5ea6\u201d \u7684\u3002\u5728\u5411\u91cf\u7a7a\u95f4\u91cc\uff0c\u201c503\u201d \u548c \u201c502\u201d \u53ef\u80fd\u6328\u5f97\u5f88\u8fd1\uff08\u90fd\u662f\u9519\u8bef\u7801\uff09\uff0c\u6216\u8005 \u201c503\u201d \u8fd9\u4e2a\u6570\u5b57\u88ab Embedding \u6a21\u578b\u5ffd\u7565\u4e86\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u4e13\u6709\u540d\u8bcd\u3001\u7cbe\u786e\u6570\u5b57\u3001\u7f29\u5199\u8bcd\uff0c\u5411\u91cf\u68c0\u7d22\u7684\u6548\u679c\u5f80\u5f80\u4e0d\u5982\u4f20\u7edf\u7684\u5173\u952e\u8bcd\u641c\u7d22\u3002<\/p>\n\n\n\n<p><strong>LangChain \u89e3\u6cd5\uff1a\u6df7\u5408\u68c0\u7d22\uff08Hybrid Search\uff09<\/strong><\/p>\n\n\n\n<p>\u8fd9\u662f\u76ee\u524d\u751f\u4ea7\u73af\u5883\u7684\u6807\u914d\u3002\u4e0d\u8981\u53ea\u7528\u5411\u91cf\u641c\u7d22\u3002<\/p>\n\n\n\n<p><strong>\ud83d\udd391. EnsembleRetriever\uff08\u96c6\u6210\u68c0\u7d22\u5668\uff09<\/strong><\/p>\n\n\n\n<p>LangChain \u63d0\u4f9b\u4e86\u4e00\u4e2a\u795e\u5668\u53eb <code>EnsembleRetriever<\/code>\u3002\u5b83\u7684\u4f5c\u7528\u662f\u628a ** \u5173\u952e\u8bcd\u641c\u7d22\uff08BM25\uff09<strong>\u548c<\/strong>\u5411\u91cf\u641c\u7d22\uff08Vector Store\uff09** \u7684\u7ed3\u679c\u52a0\u6743\u5408\u5e76\u3002<\/p>\n\n\n\n<ul>\n<li>BM25\uff1a\u5c31\u50cf\u4ee5\u524d\u7684 Google\uff0c\u64c5\u957f\u7cbe\u786e\u5339\u914d\u5173\u952e\u5b57\uff08503\u3001X-2000 \u578b\u53f7\uff09\u3002<\/li>\n\n\n\n<li>Vector\uff1a\u64c5\u957f\u7406\u89e3\u8bed\u4e49\uff08\u201c\u670d\u52a1\u5668\u5d29\u4e86\u201d=\u201c\u670d\u52a1\u4e0d\u53ef\u7528\u201d\uff09\u3002<\/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=\"\">from langchain.retrievers import BM25Retriever, EnsembleRetriever\n\n# 1. \u5efa\u7acb\u5173\u952e\u8bcd\u68c0\u7d22\u5668\nbm25_retriever = BM25Retriever.from_documents(docs)\nbm25_retriever.k = 5  # \u53d6\u524d5\u4e2a\n\n# 2. \u5efa\u7acb\u5411\u91cf\u68c0\u7d22\u5668\uff08\u5047\u8bbe vector_store \u5df2\u7ecf\u5efa\u597d\uff09\nvector_retriever = vector_store.as_retriever(search_kwargs={\"k\": 5})\n\n# 3. \u6df7\u5408\uff01\n# weights=[0.5, 0.5] \u8868\u793a\u4e24\u8005\u6743\u91cd\u76f8\u7b49\nensemble_retriever = EnsembleRetriever(\n    retrievers=[bm25_retriever, vector_retriever],\n    weights=[0.5, 0.5]\n)\n\n# \u73b0\u5728\u7684 ensemble_retriever \u65e2\u80fd\u641c\u5230\u8bed\u4e49\u76f8\u5173\u7684\uff0c\u4e5f\u80fd\u641c\u5230\u7cbe\u786e\u5339\u914d\u7684<\/pre>\n\n\n\n<p><strong>\u4ea7\u54c1\u7ecf\u7406 Insight:<\/strong><\/p>\n\n\n\n<p>\u5f53\u6d4b\u8bd5\u53cd\u9988 \u201c\u641c\u4e0d\u5230\u7cbe\u51c6\u8bcd\u201d \u65f6\uff0c\u4e0d\u8981\u8ba9\u5f00\u53d1\u53bb\u6362 Embedding \u6a21\u578b\uff0c\u90a3\u4e2a\u6210\u672c\u592a\u9ad8\u3002<strong>\u76f4\u63a5\u8981\u6c42\u52a0\u4e0a BM25 \u6df7\u5408\u68c0\u7d22\uff0c\u7acb\u7aff\u89c1\u5f71\u3002<\/strong><\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd392. \u4e0a\u4e0b\u6587\u91cd\u6392\u5e8f\uff08Re-ranking\uff09<\/strong><\/p>\n\n\n\n<p>\u6709\u65f6\u5019\u4e0d\u662f\u6ca1\u641c\u5230\uff0c\u800c\u662f\u641c\u5230\u4e86\u6392\u5728\u7b2c 10 \u4f4d\u3002\u6a21\u578b\u901a\u5e38\u53ea\u770b\u524d 5 \u4e2a\u6587\u6863\u3002<\/p>\n\n\n\n<p>\u8fd9\u65f6\u5019\u9700\u8981\u5f15\u5165 Reranker\uff08\u91cd\u6392\u5e8f\u6a21\u578b\uff09\u3002\u5b83\u50cf\u4e00\u4e2a\u4e25\u683c\u7684\u8003\u5b98\uff0c\u628a\u68c0\u7d22\u56de\u6765\u7684 10 \u4e2a\u7c97\u7cd9\u7ed3\u679c\uff0c\u7cbe\u7ec6\u5730\u6253\u5206\u91cd\u6392\u3002<\/p>\n\n\n\n<p>\u5728 LangChain \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ContextualCompressionRetriever<\/code> \u914d\u5408 <code>CohereRerank<\/code> \u6216 <code>BGE-Reranker<\/code>\u3002\u8fd9\u80fd<strong>\u8ba9\u68c0\u7d22\u51c6\u786e\u7387\u63d0\u5347 20% \u4ee5\u4e0a<\/strong>\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u4e09\u3001Lost in the Middle\uff1a\u957f\u4e0a\u4e0b\u6587\u7684\u9677\u9631\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u73b0\u8c61\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u4e3a\u4e86\u8ba9 AI \u638c\u63e1\u66f4\u591a\u4fe1\u606f\uff0c\u4f60\u628a\u68c0\u7d22\u5230\u7684 20 \u6bb5\u6587\u6863\uff08Total 10k Tokens\uff09\u5168\u585e\u7ed9\u4e86 GPT-4\u3002<\/li>\n\n\n\n<li>\u7ed3\u679c\u53d1\u73b0\uff0cAI \u53ea\u8bb0\u5f97\u7b2c\u4e00\u6bb5\u548c\u6700\u540e\u4e00\u6bb5\u7684\u5185\u5bb9\uff0c\u4e2d\u95f4\u6587\u6863\u91cc\u63d0\u5230\u7684\u5173\u952e\u4fe1\u606f\u88ab\u5b83\u65e0\u89c6\u4e86\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6839\u56e0\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<p>\u8fd9\u662f LLM \u7684\u56fa\u6709\u7f3a\u9677\u3002\u7814\u7a76\u8868\u660e\uff0c\u5927\u6a21\u578b\u5bf9 Context \u7684\u9996\u5c3e\u90e8\u5206\u5173\u6ce8\u5ea6\u6700\u9ad8\uff0c\u4e2d\u95f4\u90e8\u5206\u5bb9\u6613 \u201c\u6ce8\u610f\u529b\u574d\u584c\u201d\u3002<\/p>\n\n\n\n<p><strong>LangChain \u89e3\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p>LangChain \u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e13\u95e8\u7684\u5de5\u5177\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<strong>\u957f\u4e0a\u4e0b\u6587\u91cd\u6392\u5e8f\uff08LongContextReorder\uff09<\/strong>\u3002<\/p>\n\n\n\n<p>\u5b83\u7684\u903b\u8f91\u5f88\u7b80\u5355\u4f46\u5f88\u6709\u6548\uff1a\u628a\u68c0\u7d22\u5230\u7684\u6700\u76f8\u5173\u7684\u6587\u6863\uff0c\u7269\u7406\u79fb\u52a8\u5230 Context \u7684\u5934\u90e8\u548c\u5c3e\u90e8\uff0c\u628a\u4e0d\u592a\u76f8\u5173\u7684\u6587\u6863\u653e\u5728\u4e2d\u95f4\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=\"\">from langchain_community.document_transformers import LongContextReorder\n\n# \u5047\u8bbe docs \u662f\u68c0\u7d22\u56de\u6765\u7684 10 \u4e2a\u6587\u6863\uff0c\u6309\u76f8\u5173\u6027\u4ece\u9ad8\u5230\u4f4e\u6392\u5e8f\nreordering = LongContextReorder()\n\n# \u6267\u884c\u91cd\u6392\u5e8f\n# \u7ed3\u679c\u4f1a\u53d8\u6210: [\u7b2c1\u76f8\u5173, \u7b2c3\u76f8\u5173, ... \u7b2c10\u76f8\u5173 ..., \u7b2c4\u76f8\u5173,\nreordered_docs = reordering.transform_documents(docs)\n\n# \u8fd9\u6837,\u6700\u91cd\u8981\u7684\u7b2c1\u548c\u7b2c2\u4e2a\u6587\u6863,\u5206\u522b\u5360\u636e\u4e86 Prompt \u7684\u5f00\u5934\u548c\u7ed3\u5c3e<\/pre>\n\n\n\n<p><strong>\u4ea7\u54c1\u7ecf\u7406 Insight:<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u7684\u5e94\u7528\u573a\u666f\u6d89\u53ca<strong>\u957f\u6587\u6863\u7efc\u8ff0<\/strong>\u6216\u8005<strong>\u5927\u91cf\u788e\u7247\u4fe1\u606f\u6574\u5408<\/strong>\uff0c\u5fc5\u987b\u5728\u8fd9\u4e2a\u73af\u8282\u505a\u4f18\u5316\uff0c\u5426\u5219 Token \u8d39\u82b1\u4e86\uff0c\u6548\u679c\u5374\u51fa\u4e0d\u6765\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u56db\u3001\u7ec4\u4ef6\u517c\u5bb9\u6027\uff1a\u4ee3\u7801\u6628\u665a\u8fd8\u80fd\u8dd1\u3002\u3002\u3002\u6211\u771f\u662f\u75af\u4e86\u6211\u3002\u3002\u3002\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u73b0\u8c61\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u5468\u4e94\u4e0b\u73ed\u524d Demo \u8dd1\u901a\u4e86\uff0c\u5468\u4e00\u65e9\u4e0a\u6f14\u793a\u65f6\u62a5\u9519 <code>ImportError<\/code> \u6216\u8005 <code>Method deprecated<\/code>\u3002<\/li>\n\n\n\n<li>\u5f00\u53d1\u4e00\u67e5\uff0c\u8bf4\u662f LangChain \u5468\u672b\u81ea\u52a8\u5347\u7ea7\u4e86\u4e2a\u5c0f\u7248\u672c\uff0cAPI \u53d8\u4e86\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6839\u56e0\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<p>LangChain \u662f\u76ee\u524d\u8fed\u4ee3\u901f\u5ea6\u6700\u5feb\u7684\u5f00\u6e90\u9879\u76ee\u4e4b\u4e00\uff08\u6709\u65f6\u4e00\u5929\u53d1\u4e09\u4e2a\u7248\u672c\uff09\u3002\u4e3a\u4e86\u8ffd\u6c42\u65b0\u529f\u80fd\uff0c\u5b83\u7ecf\u5e38\u505a\u7834\u574f\u6027\u66f4\u65b0\uff08Breaking Changes\uff09\u3002\u800c\u4e14\uff0cLangChain \u751f\u6001\u6781\u5176\u5e9e\u5927\uff0c<strong>\u4f9d\u8d56\u5305\uff08OpenAI SDK\u3001Pydantic\u3001NumPy\uff09\u4e4b\u95f4\u7684\u7248\u672c\u51b2\u7a81\u5982\u5bb6\u5e38\u4fbf\u996d<\/strong>\u3002<\/p>\n\n\n\n<p><strong>LangChain \u89e3\u6cd5\uff1aLCEL \u4e0e\u7248\u672c\u9501\u5b9a<\/strong><\/p>\n\n\n\n<p><strong>\ud83d\udd391. \u62e5\u62b1 LCEL (LangChain Expression Language)<\/strong><\/p>\n\n\n\n<p>\u5728\u4e4b\u524d\u7684\u7b14\u8bb0\u91cc\u6211\u53cd\u590d\u63d0\u5230 LCEL\u3002\u5b83\u4e0d\u4ec5\u4ec5\u662f\u8bed\u6cd5\u7cd6\uff0c\u5b83\u662f\u7a33\u5b9a\u6027\u7684\u4fdd\u969c\u3002<\/p>\n\n\n\n<p>LangChain \u65e7\u7248\u7684\u8bb8\u591a \u201c\u9ed1\u76d2\u94fe\u201d\uff08\u5982 <code>RetrievalQA<\/code>\u3001<code>ConversationalRetrievalChain<\/code>\uff09\u5c01\u88c5\u592a\u91cd\uff0c\u4e00\u65e6\u5e95\u5c42\u903b\u8f91\u53d8\u52a8\uff0c\u4e0a\u5c42\u5c31\u6302\u3002<\/p>\n\n\n\n<p>\u800c LCEL \u4f7f\u7528\u7684\u662f\u539f\u5b50\u64cd\u4f5c\uff08<code>prompt | model | parser<\/code>\uff09\uff0c\u8fd9\u4e9b\u57fa\u7840\u539f\u8bed\u975e\u5e38\u7a33\u5b9a\uff0c\u6781\u5c11\u53d8\u52a8\u3002<\/p>\n\n\n\n<p>\u5efa\u8bae\uff1a\u65b0\u9879\u76ee\u5168\u90e8\u4f7f\u7528 LCEL \u5199\u6cd5\uff0c\u5c3d\u91cf\u5c11\u7528 <code>langchain.chains<\/code> \u4e0b\u90a3\u4e9b\u5c01\u88c5\u597d\u7684\u7c7b\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd392. \u9501\u5b9a requirements.txt<\/strong><\/p>\n\n\n\n<p>\u8fd9\u5bf9\u4ea7\u54c1\u7ecf\u7406\u6765\u8bf4\u662f\u7ba1\u7406\u8981\u6c42\u3002\u4e0d\u8981\u8ba9\u5f00\u53d1\u5199 <code>langchain<\/code> \u8fd9\u79cd\u4e0d\u5e26\u7248\u672c\u53f7\u7684\u4f9d\u8d56\u3002<\/p>\n\n\n\n<p>\u5fc5\u987b\u9501\u6b7b\u4e3b\u7248\u672c\u53f7\u548c\u6b21\u7248\u672c\u53f7\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=\"\">langchain==1.1.0\nlangchain-community==0.4.1\nlangchain-openai==1.1.0<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd393. \u4f7f\u7528 LangSmith \u8fdb\u884c\u56de\u5f52\u6d4b\u8bd5<\/strong><\/p>\n\n\n\n<p>LangChain \u5b98\u65b9\u7684 LangSmith \u5e73\u53f0\u4e0d\u4ec5\u80fd\u770b\u65e5\u5fd7\uff0c\u8fd8\u80fd\u505a\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5f55\u5236\u4e00\u7ec4 50 \u4e2a \u201c\u9ec4\u91d1\u95ee\u7b54\u5bf9\u201d\u3002\u6bcf\u6b21\u5347\u7ea7\u5e93\u7248\u672c\u4e4b\u524d\uff0c\u5148\u8dd1\u4e00\u904d\u8fd9 50 \u4e2a Case\u3002\u5982\u679c\u51c6\u786e\u7387\u4ece 95% \u6389\u5230\u4e86 80%\uff0c\u575a\u51b3\u4e0d\u5347\u7ea7\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u4e94\u3001\u7ed3\u6784\u5316\u8f93\u51fa\u7684\u4e0d\u7a33\u5b9a\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u73b0\u8c61\u63cf\u8ff0\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>\u4f60\u8981\u6c42 AI \u8fd4\u56de JSON \u683c\u5f0f\uff1a<code>{ 'name': 'Old K', 'age': 30 }<\/code>\u3002<\/li>\n\n\n\n<li>AI \u6709\u65f6\u5019\u5fc3\u60c5\u597d\u4f1a\u8fd4\u56de JSON\u3002<\/li>\n\n\n\n<li>\u6709\u65f6\u5019\u4f1a\u8fd4\u56de\uff1a\u201c\u597d\u7684\uff0c\u8fd9\u662f\u4f60\u8981\u7684 JSON\uff1ajson { &#8230;} \u201d\u3002<\/li>\n\n\n\n<li>\u6709\u65f6\u5019\u4f1a\u8fd4\u56de\uff1a<code>{ 'name': 'Old K', 'age': 30, }<\/code>\uff08\u6ce8\u610f\u90a3\u4e2a\u591a\u4f59\u7684\u9017\u53f7\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4f1a\u5bfc\u81f4\u4f60\u7684\u540e\u7aef JSON \u89e3\u6790\u5668\u62a5\u9519\uff0c\u6574\u4e2a\u670d\u52a1\u5d29\u6e83\u3002<\/p>\n\n\n\n<p><strong>\u6839\u56e0\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<p>\u5927\u6a21\u578b\u672c\u8d28\u662f\u751f\u6210\u6587\u672c\u7684\uff0c\u5b83\u4e0d\u61c2 JSON \u8bed\u6cd5\u3002\u65e9\u671f\u7684\u505a\u6cd5\u662f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u4fee\u8865\uff0c\u8fd9\u975e\u5e38\u8106\u5f31\u3002<\/p>\n\n\n\n<p><strong>LangChain \u89e3\u6cd5\uff1aPydantic Output Parser &amp; Tool Binding<\/strong><\/p>\n\n\n\n<p>\u73b0\u5728\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u6807\u51c6\u7b54\u6848\u53ea\u6709\u4e00\u4e2a\u5b57\uff1aPydantic\u3002<\/p>\n\n\n\n<p>LangChain \u6df1\u5ea6\u96c6\u6210\u4e86 Pydantic \u5e93\u3002\u4f60\u53ea\u9700\u8981\u5b9a\u4e49\u597d Python \u7684\u7c7b\uff0cLangChain \u4f1a\u81ea\u52a8\u751f\u6210 Prompt \u6307\u4ee4\uff0c\u5e76\u5728\u6a21\u578b\u8f93\u51fa\u9519\u8bef\u65f6\u81ea\u52a8\u91cd\u8bd5\u3002<\/p>\n\n\n\n<p>\u66f4\u9ad8\u7ea7\u7684\u505a\u6cd5\u662f\u4f7f\u7528<strong>\u6a21\u578b\u539f\u751f\u7684 Tool Calling<\/strong>\uff08\u5728 LangChain \u4e2d\u901a\u8fc7 <code>.with_structured_output()<\/code> \u5b9e\u73b0\uff09\u3002<\/p>\n\n\n\n<p><strong>\u4ee3\u7801\u5757<\/strong><\/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=\"\"># \u518d\u6b21\u5f3a\u8c03\uff0c\u8fd9\u662f\u76ee\u524d\u6700\u7a33\u7684\u5199\u6cd5\nclass UserInfo(BaseModel):\n    name: str\n    age: int\n\n# \u5927\u90e8\u5206\u73b0\u4ee3\u6a21\u578b\uff08GPT-3.5+\uff0cClaude 3\uff09\u90fd\u652f\u6301\u8fd9\u79cd\u539f\u751f\u6a21\u5f0f\n# \u6a21\u578b\u4e0d\u4f1a\u8f93\u51fa\u5e9f\u8bdd\uff0c\u76f4\u63a5\u8f93\u51fa\u7b26\u5408 Schema \u7684\u5bf9\u8c61\nchain = prompt | llm.with_structured_output(UserInfo)<\/pre>\n\n\n\n<p><strong>\u4ea7\u54c1\u7ecf\u7406 Insight:<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u7684\u4ea7\u54c1\u7ecf\u5e38\u56e0\u4e3a \u201c\u683c\u5f0f\u89e3\u6790\u9519\u8bef\u201d \u800c\u62a5\u9519\uff0c\u53bb\u68c0\u67e5\u4ee3\u7801\u3002\u5982\u679c\u5f00\u53d1\u8fd8\u5728\u7528 <code>RegexParser<\/code> \u6216\u8005\u5728 Prompt \u91cc\u5199 \u201c\u8bf7\u4e0d\u8981\u8f93\u51fa Markdown \u7b26\u53f7\u201d\uff0c\u8bf7\u8ba9\u4ed6\u4eec\u7acb\u523b\u91cd\u6784\uff0c\u6539\u7528 <code>with_structured_output<\/code>\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u516d\u3001\u6765\uff0c\u603b\u7ed3\u4e00\u4e0b\u5427\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e00\u7bc7\u7b14\u8bb0\u91cc\uff0c\u6211\u4eec\u4e0d\u518d\u8c08\u8bba\u90a3\u4e9b\u4ee4\u4eba\u5174\u594b\u7684\u65b0\u6982\u5ff5\uff0c\u800c\u662f\u9762\u5bf9\u51b7\u51b0\u51b0\u7684\u73b0\u5b9e\u95ee\u9898\u3002<\/p>\n\n\n\n<ul>\n<li>RAG \u5e7b\u89c9 \u2192\u2192 \u7528<strong>\u5f15\u7528\uff08Citations\uff09\u548c\u5de5\u5177\u7ed1\u5b9a<\/strong>\u6765\u7ea6\u675f\u3002<\/li>\n\n\n\n<li>\u68c0\u7d22\u5931\u6548 \u2192\u2192 \u7528 ** \u6df7\u5408\u68c0\u7d22\uff08Ensemble\uff09<strong>\u548c<\/strong>\u91cd\u6392\u5e8f\uff08Rerank\uff09** \u6765\u8865\u6551\u3002<\/li>\n\n\n\n<li>\u957f\u6587\u9057\u5fd8 \u2192\u2192 \u7528 ** \u91cd\u6392\u5e8f\uff08Reorder\uff09** \u628a\u5173\u952e\u4fe1\u606f\u9876\u5230\u4e24\u5934\u3002<\/li>\n\n\n\n<li>\u4ee3\u7801\u5d29\u574f \u2192\u2192 \u62e5\u62b1 <strong>LCEL<\/strong>\uff0c<strong>\u9501\u6b7b\u7248\u672c<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5176\u5b9e\uff0cAI \u5e94\u7528\u5f00\u53d1\u7684\u95e8\u69db\u4e0d\u4ec5\u5728\u4e8e\u5199\u51fa\u7b2c\u4e00\u884c\u4ee3\u7801\uff0c\u66f4\u5728\u4e8e\u5982\u4f55\u5904\u7406\u8fd9 1% \u7684 Edge Case\uff08\u8fb9\u7f18\u60c5\u51b5\uff09\u3002<\/p>\n\n\n\n<p>\u5f53\u4f60\u80fd\u719f\u7ec3\u5730\u6307\u6325\u56e2\u961f\u4f7f\u7528\u8fd9\u4e9b\u624b\u6bb5\u53bb\u6392\u5751\u65f6\uff0c\u4f60\u624b\u91cc\u7684\u4ea7\u54c1\u624d\u80fd\u771f\u6b63\u4ece Demo \u8d70\u5411 Production\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u5c0f\u7ea2\u4e66 AI\u4ea7\u54c1\u8d75\u54e5 LangChain \u907f\u5751\u624b\u518c_\u7cfb\u521712\uff1aRAG \u5e7b\u89c9\u3001\u68c0\u7d22\u5931\u6548\u3001\u7ec4\u4ef6\u517c\u5bb9\uff0c\u8fd9\u4e9b\u95ee\u9898 [&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\/20296"}],"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=20296"}],"version-history":[{"count":13,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/20296\/revisions"}],"predecessor-version":[{"id":20350,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/20296\/revisions\/20350"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}