{"id":55371,"date":"2025-02-19T13:05:01","date_gmt":"2025-02-19T05:05:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/55371\/"},"modified":"2025-02-19T13:05:01","modified_gmt":"2025-02-19T05:05:01","slug":"chatollama-%e7%ac%94%e8%ae%b0-%e5%ae%9e%e7%8e%b0%e9%ab%98%e7%ba%a7rag%e7%9a%84%e7%94%9f%e4%ba%a7%e5%8c%96%e5%92%8c%e5%9f%ba%e4%ba%8eredis%e7%9a%84%e6%96%87%e6%a1%a3%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/55371\/","title":{"rendered":"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><strong>ChatOllama<\/strong>&nbsp;\u662f\u4e00\u6b3e\u57fa\u4e8eLLMs\u7684\u5f00\u6e90\u804a\u5929\u673a\u5668\u4eba\u3002\u5bf9ChatOllama\u7684\u8be6\u7ec6\u4ecb\u7ecd\u8bf7\u70b9\u51fb\u4e0b\u65b9\u94fe\u63a5\u3002<\/p>\n<p> <\/p>\n<p>&nbsp;<\/p>\n<p><strong>ChatOllama | \u57fa\u4e8eOllama\u7684100%\u672c\u5730RAG\u5e94\u7528<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p> \u6700\u521d\u7684\u76ee\u6807\u662f\u4e3a\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a100%\u672c\u5730RAG\u5e94\u7528\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u968f\u7740\u5176\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7528\u6237\u63d0\u51fa\u6709\u4ef7\u503c\u7684\u9700\u6c42\u3002\u73b0\u5728\uff0c`ChatOllama` \u652f\u6301\u591a\u79cd\u8bed\u8a00\u6a21\u578b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> \u652f\u6301\u7684\u6a21\u578b<\/li>\n<li>OpenAI<\/li>\n<li>Azure OpenAI<\/li>\n<li><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u7528 ChatOllama, \u7528\u6237\u53ef\u4ee5\uff1a<\/p>\n<ul>\n<li>\u7ba1\u7406Ollama\u6a21\u578b\uff08\u62c9\u53d6\/\u5220\u9664\uff09<\/li>\n<li>\u7ba1\u7406\u77e5\u8bc6\u5e93\uff08\u521b\u5efa\/\u5220\u9664\uff09<\/li>\n<li>\u4e0eLLMs\u81ea\u7531\u5bf9\u8bdd<\/li>\n<li>\u7ba1\u7406\u4e2a\u4eba\u77e5\u8bc6\u5e93<\/li>\n<li>\u901a\u8fc7\u4e2a\u4eba\u77e5\u8bc6\u5e93\u4e0eLLMs\u4ea4\u6d41<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u63a2\u8ba8\u5982\u4f55\u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u3002\u6211\u5df2\u7ecf\u5b66\u4e60\u4e86RAG\u7684\u57fa\u672c\u548c\u8fdb\u9636\u6280\u5de7\uff0c\u4f46\u5c06RAG\u6295\u5165\u751f\u4ea7\u4ecd\u7136\u6709\u8bb8\u591a\u9700\u8981\u5904\u7406\u7684\u4e8b\u9879\u3002\u6211\u5c06\u5206\u4eab\u5728ChatOllama\u4e2d\u5df2\u7ecf\u505a\u7684\u5de5\u4f5c\u4ee5\u53ca\u4e3a\u5b9e\u73b0RAG\u7684\u751f\u4ea7\u5316\u6240\u505a\u7684\u51c6\u5907\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"\/\/www.w3.org\/2000\/svg'%20viewBox='0%200%20800%20447'%3E%3C\/svg%3E\" title=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe\" alt=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe\" \/><br \/>\n<img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.aisharenet.com\/wp-content\/uploads\/2024\/04\/46b718bd35d18cd.png\" title=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe1\" alt=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe1\" \/> <\/p>\n<p>ChatOllama | RAG\u6784\u5efa\u5316\u8fd0\u884c<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728ChatOllama\u5e73\u53f0\u4e2d\uff0c\u6211\u4eec\u8fd0\u7528\u5230\u4e86<strong>LangChain.js<\/strong>\u6765\u64cd\u63a7RAG\u6d41\u7a0b\u3002\u5728ChatOllama\u7684\u77e5\u8bc6\u5e93\u4e2d\uff0c\u8d85\u8d8a\u4e8e\u539f\u59cbRAG\u7684\u7236\u6587\u6863\u68c0\u7d22\u5668\u5f97\u4ee5\u8fd0\u7528\u3002\u8ba9\u6211\u4eec\u6df1\u5ea6\u63a2\u7a76\u5176\u6784\u67b6\u53ca\u6267\u884c\u7ec6\u8282\u3002\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u542f\u53d1\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h1>\u7236\u6587\u6863\u68c0\u7d22\u5668\u7684\u5b9e\u7528\u5316<\/h1>\n<p>&nbsp;<\/p>\n<p>\u4e3a\u4e86\u8ba9\u7236\u6587\u6863\u68c0\u7d22\u5668\u5728\u4e00\u4e2a\u771f\u5b9e\u4ea7\u54c1\u4e2d\u8fd0\u884c\uff0c\u6211\u4eec\u9700\u7406\u89e3\u5176\u6838\u5fc3\u539f\u7406\uff0c\u5e76\u4e3a\u751f\u4ea7\u76ee\u7684\u9009\u53d6\u5408\u9002\u7684\u5b58\u50a8\u5143\u4ef6\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u7236\u6587\u6863\u68c0\u7d22\u5668\u7684\u6838\u5fc3\u539f\u7406<\/h2>\n<p>\u5728\u4e3a\u68c0\u7d22\u9700\u6c42\u5c06\u6587\u6863\u62c6\u5206\u65f6\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u51b2\u7a81\u7684\u9700\u6c42\uff1a<\/p>\n<ul>\n<li>\u4f60\u53ef\u80fd\u5e0c\u671b\u6587\u6863\u8d8a\u5c0f\u8d8a\u597d\uff0c\u8fd9\u6837\u5b83\u4eec\u7684\u5d4c\u5165\u5f0f\u5185\u5bb9\u53ef\u4ee5\u6700\u7cbe\u51c6\u5730\u4f53\u73b0\u5b83\u4eec\u7684\u542b\u4e49\u3002\u5982\u679c\u5185\u5bb9\u8fc7\u957f\uff0c\u5d4c\u5165\u5f0f\u4fe1\u606f\u53ef\u80fd\u4f1a\u5931\u53bb\u5176\u542b\u4e49\u3002<\/li>\n<li>\u4f60\u4e5f\u60f3\u8981\u8db3\u591f\u957f\u7684\u6587\u6863\uff0c\u4ee5\u4fdd\u8bc1\u6bcf\u6bb5\u5185\u5bb9\u5177\u5907\u5b8c\u6574\u7684\u5168\u6587\u73af\u5883\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u7531LangChain.js\u63d0\u4f9b\u7684<code>ParentDocumentRetriever<\/code>\u901a\u8fc7\u5c06\u6587\u6863\u62c6\u5206\u4e3a\u5c0f\u5757\u6765\u53d6\u5f97\u8fd9\u4e2a\u5e73\u8861\u3002\u6bcf\u6b21\u68c0\u7d22\u65f6\uff0c\u5b83\u63d0\u53d6\u5c0f\u5757\uff0c\u7136\u540e\u67e5\u627e\u5404\u4e2a\u5c0f\u5757\u5bf9\u5e94\u7684\u7236\u6587\u6863\uff0c\u8fd4\u56de\u66f4\u5927\u8303\u56f4\u7684\u6587\u6863\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u7236\u6587\u6863\u4f1a\u901a\u8fc7doc id\u4e0e\u5c0f\u5757\u76f8\u8054\u7cfb\u3002\u6211\u4eec\u7a0d\u540e\u4f1a\u8fdb\u4e00\u6b65\u89e3\u91ca\u5176\u8fd0\u884c\u65b9\u5f0f\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684<code>parent document<\/code>\u6307\u7684\u662f\u5c0f\u5757\u7684\u6765\u6e90\u6587\u6863\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u67b6\u6784<\/h2>\n<p>\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u7236\u6587\u6863\u68c0\u7d22\u5668\u7684\u6574\u4f53\u67b6\u6784\u56fe\u3002<\/p>\n<p>\u6bcf\u4e2a\u5c0f\u5757\u4f1a\u7ecf\u8fc7\u5904\u7406\u8f6c\u53d8\u4e3a\u5411\u91cf\u6570\u636e\uff0c\u7136\u540e\u5b58\u50a8\u5728\u5411\u91cf\u6570\u636e\u5e93\u4e2d\u3002 \u68c0\u7d22\u8fd9\u4e9b\u5c0f\u5757\u7684\u8fc7\u7a0b\u5c06\u5982\u540c\u5728\u539f\u59cbRAG\u8bbe\u7f6e\u4e2d\u6240\u505a\u7684\u90a3\u6837\u3002<\/p>\n<p>\u7236\u6587\u6863\uff08\u5757-1\u3001\u5757-2\u3001&#8230;&#8230;\u5757-m\uff09\u88ab\u5206\u5272\u6210\u66f4\u5c0f\u7684\u90e8\u5206\u3002 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u7528\u5230\u4e86\u4e24\u4e2a\u4e0d\u540c\u7684\u6587\u672c\u5206\u5272\u65b9\u6cd5\uff1a\u4e00\u4e2a\u8f83\u5927\u7684\u7528\u4e8e\u7236\u6587\u6863\uff0c\u53e6\u4e00\u4e2a\u8f83\u5c0f\u7684\u7528\u4e8e\u66f4\u5c0f\u7684\u5757\u3002\u6bcf\u4e2a\u7236\u6587\u6863\u90fd\u88ab\u8d4b\u4e88\u4e00\u4e2a\u6587\u6863ID\uff0c\u7136\u540e\u5c06\u6b64ID\u4f5c\u4e3a\u5143\u6570\u636e\u8bb0\u5f55\u5728\u5176\u5bf9\u5e94\u7684\u5c0f\u5757\u4e2d\u3002 \u8fd9\u786e\u4fdd\u4e86\u6bcf\u4e2a\u5c0f\u5757\u90fd\u80fd\u901a\u8fc7\u5b58\u50a8\u5728\u5143\u6570\u636e\u4e2d\u7684\u6587\u6863ID\u627e\u5230\u5176\u76f8\u5e94\u7684\u7236\u6587\u6863\u3002<\/p>\n<p>\u68c0\u7d22\u7236\u6587\u6863\u7684\u8fc7\u7a0b\u4e0e\u8fd9\u5e76\u4e0d\u76f8\u540c\u3002\u4e0d\u9700\u8981\u76f8\u4f3c\u6027\u641c\u7d22\uff0c\u53ea\u9700\u63d0\u4f9b\u6587\u6863ID\u5c31\u53ef\u4ee5\u627e\u5230\u5176\u5bf9\u5e94\u7684\u7236\u6587\u6863\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u952e-\u503c\u5b58\u50a8\u7cfb\u7edf\u5c31\u8db3\u591f\u80dc\u4efb\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"\/\/www.w3.org\/2000\/svg'%20viewBox='0%200%20800%20448'%3E%3C\/svg%3E\" title=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe2\" alt=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe2\" \/><br \/>\n<img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.aisharenet.com\/wp-content\/uploads\/2024\/04\/4388b7e17490a8b.png\" title=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe3\" alt=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe3\" \/> <\/p>\n<p>\u7236\u6587\u6863\u68c0\u7d22\u5668<\/p>\n<p>&nbsp;<\/p>\n<h2>\u5b58\u50a8\u90e8\u5206<\/h2>\n<p>\u6709\u4e24\u79cd\u6570\u636e\u9700\u8981\u5b58\u50a8\uff1a<\/p>\n<ul>\n<li>\u5c0f\u89c4\u6a21\u7684\u5411\u91cf\u6570\u636e<\/li>\n<li>\u542b\u6709\u5176\u6587\u6863ID\u7684\u7236\u6587\u6863\u7684\u5b57\u7b26\u4e32\u6570\u636e<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u6240\u6709\u4e3b\u6d41\u7684\u5411\u91cf\u6570\u636e\u5e93\u90fd\u662f\u53ef\u4ee5\u4f7f\u7528\u7684\u3002\u5728`ChatOllama`\u4e2d\uff0c\u6211\u9009\u62e9\u4e86Chroma\u3002<\/p>\n<p>\u5bf9\u4e8e\u7236\u6587\u6863\u6570\u636e\uff0c\u6211\u9009\u7528\u4e86\u5f53\u524d\u6700\u6d41\u884c\u5e76\u5177\u6709\u9ad8\u5ea6\u6269\u5c55\u6027\u7684\u952e\u503c\u5b58\u50a8\u7cfb\u7edfRedis\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>LangChain.js\u7f3a\u5c11\u7684\u5185\u5bb9<\/h2>\n<p>LangChain.js\u4e3a\u5b57\u8282\u548c\u6587\u6863\u5b58\u50a8\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u96c6\u6210\u65b9\u5f0f\uff1a<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>\u652f\u6301`IORedis`\uff1a<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>RedisByteStore\u4e2d\u7f3a\u5c11\u7684\u4e00\u90e8\u5206\u662f<code>collection<\/code>\u673a\u5236\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5728\u5904\u7406\u4e0d\u540c\u77e5\u8bc6\u5e93\u4e2d\u7684\u6587\u6863\u65f6\uff0c\u5404\u4e2a\u77e5\u8bc6\u5e93\u5c06\u4ee5\u4e00\u4e2a<code>collection<\/code>\u96c6\u5408\u7684\u5f62\u5f0f\u8fdb\u884c\u7ec4\u7ec7\uff0c\u540c\u4e00\u5e93\u4e2d\u7684\u6587\u6863\u5c06\u88ab\u8f6c\u5316\u4e3a\u5411\u91cf\u6570\u636e\u5e76\u5b58\u50a8\u5728\u5411\u91cf\u6570\u636e\u5e93\u5982Chroma\u7684\u67d0\u4e00\u4e2a<code>collection<\/code>\u96c6\u5408\u4e2d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u5220\u9664\u4e00\u4e2a\u77e5\u8bc6\u5e93\uff1f\u6211\u4eec\u80af\u5b9a\u53ef\u4ee5\u5220\u9664Chroma\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u4e2a`collection`\u96c6\u5408\u3002\u4f46\u662f\u6211\u4eec\u5982\u4f55\u6309\u7167\u77e5\u8bc6\u5e93\u7684\u7ef4\u5ea6\u6765\u6e05\u7406\u6587\u6863\u5b58\u50a8\u5462\uff1f\u56e0\u4e3a\u5982RedisByteStore\u4e4b\u7c7b\u7684\u7ec4\u4ef6\u5e76\u4e0d\u652f\u6301`collection`\u529f\u80fd\uff0c\u56e0\u6b64\u6211\u5fc5\u987b\u81ea\u884c\u5b9e\u73b0\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>ChatOllama RedisDocStore<\/h2>\n<p>\u5728Redis\u4e2d\uff0c\u5e76\u672a\u6709\u540d\u4e3a`collection`\u7684\u5185\u7f6e\u529f\u80fd\u3002\u5f00\u53d1\u8005\u901a\u5e38\u4f1a\u901a\u8fc7\u4f7f\u7528\u524d\u7f00\u952e\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0`collection`\u529f\u80fd\u3002\u4e0b\u56fe\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u524d\u7f00\u6765\u6807\u8bc6\u4e0d\u540c\u7684\u96c6\u5408\uff1a<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"\/\/www.w3.org\/2000\/svg'%20viewBox='0%200%20800%20297'%3E%3C\/svg%3E\" title=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe4\" alt=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe4\" \/><br \/>\n<img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/www.aisharenet.com\/wp-content\/uploads\/2024\/04\/833a09b60a27cf6.png\" title=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe5\" alt=\"ChatOllama \u7b14\u8bb0 | \u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u548c\u57fa\u4e8eRedis\u7684\u6587\u6863\u6570\u636e\u5e93\u63d2\u56fe5\" \/> <\/p>\n<p>\u5e26\u6709\u524d\u7f00\u7684Redis\u952e\u8868\u793a\u5f62\u5f0f<\/p>\n<p>&nbsp;<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u6765\u770b\u4e00\u4e0b\uff0c\u662f\u5982\u4f55\u5b9e\u65bd\u4ee5<strong>Redis<\/strong>\u4e3a\u80cc\u666f\u7684\u6587\u6863\u5b58\u50a8\u529f\u80fd\u7684\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5173\u952e\u4fe1\u606f\uff1a<\/strong><\/p>\n<ul>\n<li>\u6bcf\u4e00\u4e2a`RedisDocstore`\u5728\u521d\u59cb\u5316\u65f6\u90fd\u9700\u8f93\u5165\u4e00\u4e2a`namespace`\u53c2\u6570\u3002\uff08\u5f53\u524d\u5728namespace\u548ccollection\u7684\u547d\u540d\u53ef\u80fd\u5e76\u672a\u7edf\u4e00\uff09<\/li>\n<li>\u5728\u8fdb\u884cget\u548cset\u64cd\u4f5c\u65f6\uff0c\u952e\u90fd\u5148\u8fdb\u884c`namespace`\u5904\u7406\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<blockquote>\n<p>import { Document } from &#8220;@langchain\/core\/documents&#8221;;<br \/> import { BaseStoreInterface } from &#8220;@langchain\/core\/stores&#8221;;<br \/> import { Redis } from &#8220;ioredis&#8221;;<br \/> import { createRedisClient } from &#8220;@\/server\/store\/redis&#8221;;<\/p>\n<p>export class RedisDocstore implements BaseStoreInterface&lt;string, Document&gt;<br \/> {<br \/> _namespace: string;<br \/> _client: Redis;<\/p>\n<p>constructor(namespace: string) {<br \/> this._namespace = namespace;<br \/> this._client = createRedisClient();<br \/> }<\/p>\n<p>serializeDocument(doc: Document): string {<br \/> return JSON.stringify(doc);<br \/> }<\/p>\n<p>deserializeDocument(jsonString: string): Document {<br \/> const obj = JSON.parse(jsonString);<br \/> return new Document(obj);<br \/> }<\/p>\n<p>getNamespacedKey(key: string): string {<br \/> return `${this._namespace}:${key}`;<br \/> }<\/p>\n<p>getKeys(): Promise&lt;string[]&gt; {<br \/> return new Promise((resolve, reject) =&gt; {<br \/> const stream = this._client.scanStream({ match: this._namespace + &#8216;*&#8217; });<\/p>\n<p>const keys: string[] = [];<br \/> stream.on(&#8216;data&#8217;, (resultKeys) =&gt; {<br \/> keys.push(&#8230;resultKeys);<br \/> });<\/p>\n<p>stream.on(&#8216;end&#8217;, () =&gt; {<br \/> resolve(keys);<br \/> });<\/p>\n<p>stream.on(&#8216;error&#8217;, (err) =&gt; {<br \/> reject(err);<br \/> });<br \/> });<br \/> }<\/p>\n<p>addText(key: string, value: string) {<br \/> this._client.set(this.getNamespacedKey(key), value);<br \/> }<\/p>\n<p>async search(search: string): Promise&lt;Document&gt; {<br \/> const result = await this._client.get(this.getNamespacedKey(search));<br \/> if (!result) {<br \/> throw new Error(`ID ${search} not found.`);<br \/> } else {<br \/> const document = this.deserializeDocument(result);<br \/> return document;<br \/> }<br \/> }<\/p>\n<p>\/**<br \/> * Adds new documents to the store.<br \/> * @param texts An object where the keys are document IDs and the values are the documents themselves.<br \/> * @returns Void<br \/> *\/<br \/> async add(texts: Record&lt;string, Document&gt;): Promise&lt;void&gt; {<br \/> for (const [key, value] of Object.entries(texts)) {<br \/> console.log(`Adding ${key} to the store: ${this.serializeDocument(value)}`);<br \/> }<\/p>\n<p>const keys = [&#8230;await this.getKeys()];<br \/> const overlapping = Object.keys(texts).filter((x) =&gt; keys.includes(x));<\/p>\n<p>if (overlapping.length &gt; 0) {<br \/> throw new Error(`Tried to add ids that already exist: ${overlapping}`);<br \/> }<\/p>\n<p>for (const [key, value] of Object.entries(texts)) {<br \/> this.addText(key, this.serializeDocument(value));<br \/> }<br \/> }<\/p>\n<p>async mget(keys: string[]): Promise&lt;Document[]&gt; {<br \/> return Promise.all(keys.map((key) =&gt; {<br \/> const document = this.search(key);<br \/> return document;<br \/> }));<br \/> }<\/p>\n<p>async mset(keyValuePairs: [string, Document][]): Promise&lt;void&gt; {<br \/> await Promise.all(<br \/> keyValuePairs.map(([key, value]) =&gt; this.add({ [key]: value }))<br \/> );<br \/> }<\/p>\n<p>async mdelete(_keys: string[]): Promise&lt;void&gt; {<br \/> throw new Error(&#8220;Not implemented.&#8221;);<br \/> }<\/p>\n<p>\/\/ eslint-disable-next-line require-yield<br \/> async *yieldKeys(_prefix?: string): AsyncGenerator&lt;string&gt; {<br \/> throw new Error(&#8220;Not implemented&#8221;);<br \/> }<\/p>\n<p>async deleteAll(): Promise&lt;void&gt; {<br \/> return new Promise((resolve, reject) =&gt; {<br \/> let  = &#8216;0&#8217;;<\/p>\n<p>const scanCallback = (err, result) =&gt; {<br \/> if (err) {<br \/> reject(err);<br \/> return;<br \/> }<\/p>\n<p>const [nextCursor, keys] = result;<\/p>\n<p>\/\/ Delete keys matching the prefix<br \/> keys.forEach((key) =&gt; {<br \/> this._client.del(key);<br \/> });<\/p>\n<p>\/\/ If the cursor is &#8216;0&#8217;, we&#8217;ve iterated through all keys<br \/> if (nextCursor === &#8216;0&#8217;) {<br \/> resolve();<br \/> } else {<br \/> \/\/  scanning with the next cursor<br \/> this._client.scan(nextCursor, &#8216;MATCH&#8217;, `${this._namespace}:*`, scanCallback);<br \/> }<br \/> };<\/p>\n<p>\/\/ Start the initial SCAN operation<br \/> this._client.scan(cursor, &#8216;MATCH&#8217;, `${this._namespace}:*`, scanCallback);<br \/> });<br \/> }<br \/> }<\/p>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>\u60a8\u53ef\u4ee5\u5c06\u6b64\u7ec4\u4ef6\u4e0e ParentDocumentRetriever \u65e0\u7f1d\u7ed3\u5408\u4f7f\u7528\uff1a<\/p>\n<blockquote>\n<p>retriever = new ParentDocumentRetriever({<br \/> vectorstore: chromaClient,<br \/> docstore: new RedisDocstore(collectionName),<br \/> parentSplitter: new RecursiveCharacterTextSplitter({<br \/> chunkOverlap: 200,<br \/> chunkSize: 1000,<br \/> }),<br \/> childSplitter: new RecursiveCharacterTextSplitter({<br \/> chunkOverlap: 50,<br \/> chunkSize: 200,<br \/> }),<br \/> childK: 20,<br \/> parentK: 5,<br \/> });<\/p>\n<\/blockquote>\n<p>\u76ee\u524d\uff0c\u6211\u4eec\u5df2\u7ecf\u62e5\u6709\u9ad8\u7ea7RAG\u7684\u53ef\u6269\u5c55\u5b58\u50a8\u89e3\u51b3\u65b9\u6848\u2014\u2014Parent Document Retriever\uff0c\u540c\u65f6\u8fd8\u6709`Chroma`\u548c`Redis`\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; ChatOllama&nbsp;\u662f\u4e00\u6b3e\u57fa\u4e8eLLMs\u7684\u5f00\u6e90\u804a\u5929\u673a\u5668\u4eba\u3002\u5bf9ChatOllama\u7684\u8be6\u7ec6\u4ecb\u7ecd\u8bf7\u70b9\u51fb\u4e0b\u65b9\u94fe\u63a5\u3002 &nbsp; ChatOllama | \u57fa\u4e8eOllama\u7684100%\u672c\u5730RAG\u5e94\u7528 &nbsp; \u6700\u521d\u7684\u76ee\u6807\u662f\u4e3a\u7528\u6237\u63d0\u4f9b\u4e00\u4e2a100%\u672c\u5730RAG\u5e94\u7528\u3002 &nbsp; \u968f\u7740\u5176\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7528\u6237\u63d0\u51fa\u6709\u4ef7\u503c\u7684\u9700\u6c42\u3002\u73b0\u5728\uff0c`ChatOllama` \u652f\u6301\u591a\u79cd\u8bed\u8a00\u6a21\u578b\u5305\u62ec\uff1a \u652f\u6301\u7684\u6a21\u578b OpenAI Azure OpenAI &nbsp; \u4f7f\u7528 ChatOllama, \u7528\u6237\u53ef\u4ee5\uff1a \u7ba1\u7406Ollama\u6a21\u578b\uff08\u62c9\u53d6\/\u5220\u9664\uff09 \u7ba1\u7406\u77e5\u8bc6\u5e93\uff08\u521b\u5efa\/\u5220\u9664\uff09 \u4e0eLLMs\u81ea\u7531\u5bf9\u8bdd \u7ba1\u7406\u4e2a\u4eba\u77e5\u8bc6\u5e93 \u901a\u8fc7\u4e2a\u4eba\u77e5\u8bc6\u5e93\u4e0eLLMs\u4ea4\u6d41 &nbsp; \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u63a2\u8ba8\u5982\u4f55\u5b9e\u73b0\u9ad8\u7ea7RAG\u7684\u751f\u4ea7\u5316\u3002\u6211\u5df2\u7ecf\u5b66\u4e60\u4e86RAG\u7684\u57fa\u672c\u548c\u8fdb\u9636\u6280\u5de7\uff0c\u4f46\u5c06RAG\u6295\u5165\u751f\u4ea7\u4ecd\u7136\u6709\u8bb8\u591a\u9700\u8981\u5904\u7406\u7684\u4e8b\u9879\u3002\u6211\u5c06\u5206\u4eab\u5728ChatOllama\u4e2d\u5df2\u7ecf\u505a\u7684\u5de5\u4f5c\u4ee5\u53ca\u4e3a\u5b9e\u73b0RAG\u7684\u751f\u4ea7\u5316\u6240\u505a\u7684\u51c6\u5907\u3002 &nbsp; ChatOllama | RAG\u6784\u5efa\u5316\u8fd0\u884c &nbsp; \u5728ChatOllama\u5e73\u53f0\u4e2d\uff0c\u6211\u4eec\u8fd0\u7528\u5230\u4e86LangChain.js\u6765\u64cd\u63a7RAG\u6d41\u7a0b\u3002\u5728ChatOllama\u7684\u77e5\u8bc6\u5e93\u4e2d\uff0c\u8d85\u8d8a\u4e8e\u539f\u59cbRAG\u7684\u7236\u6587\u6863\u68c0\u7d22\u5668\u5f97\u4ee5\u8fd0\u7528\u3002\u8ba9\u6211\u4eec\u6df1\u5ea6\u63a2\u7a76\u5176\u6784\u67b6\u53ca\u6267\u884c\u7ec6\u8282\u3002\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u542f\u53d1\u3002 &nbsp; &nbsp; \u7236\u6587\u6863\u68c0\u7d22\u5668\u7684\u5b9e\u7528\u5316 &nbsp; \u4e3a\u4e86\u8ba9\u7236\u6587\u6863\u68c0\u7d22\u5668\u5728\u4e00\u4e2a\u771f\u5b9e\u4ea7\u54c1\u4e2d\u8fd0\u884c\uff0c\u6211\u4eec\u9700\u7406\u89e3\u5176\u6838\u5fc3\u539f\u7406\uff0c\u5e76\u4e3a\u751f\u4ea7\u76ee\u7684\u9009\u53d6\u5408\u9002\u7684\u5b58\u50a8\u5143\u4ef6\u3002 &nbsp; \u7236\u6587\u6863\u68c0\u7d22\u5668\u7684\u6838\u5fc3\u539f\u7406 \u5728\u4e3a\u68c0\u7d22\u9700\u6c42\u5c06\u6587\u6863\u62c6\u5206\u65f6\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u51b2\u7a81\u7684\u9700\u6c42\uff1a \u4f60\u53ef\u80fd\u5e0c\u671b\u6587\u6863\u8d8a\u5c0f\u8d8a\u597d\uff0c\u8fd9\u6837\u5b83\u4eec\u7684\u5d4c\u5165\u5f0f\u5185\u5bb9\u53ef\u4ee5\u6700\u7cbe\u51c6\u5730\u4f53\u73b0\u5b83\u4eec\u7684\u542b\u4e49\u3002\u5982\u679c\u5185\u5bb9\u8fc7\u957f\uff0c\u5d4c\u5165\u5f0f\u4fe1\u606f\u53ef\u80fd\u4f1a\u5931\u53bb\u5176\u542b\u4e49\u3002 \u4f60\u4e5f\u60f3\u8981\u8db3\u591f\u957f\u7684\u6587\u6863\uff0c\u4ee5\u4fdd\u8bc1\u6bcf\u6bb5\u5185\u5bb9\u5177\u5907\u5b8c\u6574\u7684\u5168\u6587\u73af\u5883\u3002 &nbsp; \u7531LangChain.js\u63d0\u4f9b\u7684ParentDocumentRetriever\u901a\u8fc7\u5c06\u6587\u6863\u62c6\u5206\u4e3a\u5c0f\u5757\u6765\u53d6\u5f97\u8fd9\u4e2a\u5e73\u8861\u3002\u6bcf\u6b21\u68c0\u7d22\u65f6\uff0c\u5b83\u63d0\u53d6\u5c0f\u5757\uff0c\u7136\u540e\u67e5\u627e\u5404\u4e2a\u5c0f\u5757\u5bf9\u5e94\u7684\u7236\u6587\u6863\uff0c\u8fd4\u56de\u66f4\u5927\u8303\u56f4\u7684\u6587\u6863\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u7236\u6587\u6863\u4f1a\u901a\u8fc7doc id\u4e0e\u5c0f\u5757\u76f8\u8054\u7cfb\u3002\u6211\u4eec\u7a0d\u540e\u4f1a\u8fdb\u4e00\u6b65\u89e3\u91ca\u5176\u8fd0\u884c\u65b9\u5f0f\u3002 \u6ce8\u610f\uff0c\u8fd9\u91cc\u7684parent document\u6307\u7684\u662f\u5c0f\u5757\u7684\u6765\u6e90\u6587\u6863\u3002 &nbsp; \u67b6\u6784 \u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u7236\u6587\u6863\u68c0\u7d22\u5668\u7684\u6574\u4f53\u67b6\u6784\u56fe\u3002 \u6bcf\u4e2a\u5c0f\u5757\u4f1a\u7ecf\u8fc7\u5904\u7406\u8f6c\u53d8\u4e3a\u5411\u91cf\u6570\u636e\uff0c\u7136\u540e\u5b58\u50a8\u5728\u5411\u91cf\u6570\u636e\u5e93\u4e2d\u3002 \u68c0\u7d22\u8fd9\u4e9b\u5c0f\u5757\u7684\u8fc7\u7a0b\u5c06\u5982\u540c\u5728\u539f\u59cbRAG\u8bbe\u7f6e\u4e2d\u6240\u505a\u7684\u90a3\u6837\u3002 \u7236\u6587\u6863\uff08\u5757-1\u3001\u5757-2\u3001&#8230;&#8230;\u5757-m\uff09\u88ab\u5206\u5272\u6210\u66f4\u5c0f\u7684\u90e8\u5206\u3002 \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u7528\u5230\u4e86\u4e24\u4e2a\u4e0d\u540c\u7684\u6587\u672c\u5206\u5272\u65b9\u6cd5\uff1a\u4e00\u4e2a\u8f83\u5927\u7684\u7528\u4e8e\u7236\u6587\u6863\uff0c\u53e6\u4e00\u4e2a\u8f83\u5c0f\u7684\u7528\u4e8e\u66f4\u5c0f\u7684\u5757\u3002\u6bcf\u4e2a\u7236\u6587\u6863\u90fd\u88ab\u8d4b\u4e88\u4e00\u4e2a\u6587\u6863ID\uff0c\u7136\u540e\u5c06\u6b64ID\u4f5c\u4e3a\u5143\u6570\u636e\u8bb0\u5f55\u5728\u5176\u5bf9\u5e94\u7684\u5c0f\u5757\u4e2d\u3002 \u8fd9\u786e\u4fdd\u4e86\u6bcf\u4e2a\u5c0f\u5757\u90fd\u80fd\u901a\u8fc7\u5b58\u50a8\u5728\u5143\u6570\u636e\u4e2d\u7684\u6587\u6863ID\u627e\u5230\u5176\u76f8\u5e94\u7684\u7236\u6587\u6863\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-55371","post","type-post","status-publish","format-standard","hentry","category-ai"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/55371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=55371"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/55371\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=55371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=55371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=55371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}