{"id":34426,"date":"2024-12-06T09:56:25","date_gmt":"2024-12-06T01:56:25","guid":{"rendered":"https:\/\/17aitech.com\/?p=34426"},"modified":"2024-12-06T09:56:25","modified_gmt":"2024-12-06T01:56:25","slug":"%e7%bb%86%e6%95%b0rag%e7%9a%8412%e4%b8%aa%e7%97%9b%e7%82%b9%ef%bc%8c%e8%8b%b1%e4%bc%9f%e8%be%be%e9%ab%98%e7%ba%a7%e6%9e%b6%e6%9e%84%e5%b8%88%e4%ba%b2%e6%8e%88%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/17aitech.com\/?p=34426","title":{"rendered":"\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9\uff0c\u82f1\u4f1f\u8fbe\u9ad8\u7ea7\u67b6\u6784\u5e08\u4eb2\u6388\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<p>\u6587\u7ae0\u6765\u6e90\u4e8e\u4e92\u8054\u7f51:<a href=\"https:\/\/www.jiqizhixin.com\/articles\/2024-07-04-9\" target=\"_blank\">\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9\uff0c\u82f1\u4f1f\u8fbe\u9ad8\u7ea7\u67b6\u6784\u5e08\u4eb2\u6388\u89e3\u51b3\u65b9\u6848<\/a><\/p>\n<section>\u68c0\u7d22\u589e\u5f3a\u5f0f\u751f\u6210\uff08RAG\uff09\u662f\u4e00\u79cd\u4f7f\u7528\u68c0\u7d22\u63d0\u5347<mark data-type=\"tech_tasks\" data-id=\"fee178e8-ee20-42fc-8f8f-1d41c1f34e8f\">\u8bed\u8a00\u6a21\u578b<\/mark>\u7684\u6280\u672f\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c31\u662f\u5728<mark data-type=\"tech_tasks\" data-id=\"fee178e8-ee20-42fc-8f8f-1d41c1f34e8f\">\u8bed\u8a00\u6a21\u578b<\/mark>\u751f\u6210\u7b54\u6848\u4e4b\u524d\uff0c\u5148\u4ece\u5e7f\u6cdb\u7684\u6587\u6863<mark data-type=\"concepts\" data-id=\"700f9c0f-1e8b-4fde-8bae-6de39c13f022\">\u6570\u636e\u5e93<\/mark>\u4e2d\u68c0\u7d22\u76f8\u5173\u4fe1\u606f\uff0c\u7136\u540e\u5229\u7528\u8fd9\u4e9b\u4fe1\u606f\u6765\u5f15\u5bfc\u751f\u6210\u8fc7\u7a0b\u3002\u8fd9\u79cd\u6280\u672f\u80fd\u6781\u5927\u63d0\u5347\u5185\u5bb9\u7684\u51c6\u786e\u6027\u548c\u76f8\u5173\u6027\uff0c\u5e76\u80fd\u6709\u6548\u7f13\u89e3\u5e7b\u89c9\u95ee\u9898\uff0c\u63d0\u9ad8\u77e5\u8bc6\u66f4\u65b0\u7684\u901f\u5ea6\uff0c\u5e76\u589e\u5f3a\u5185\u5bb9\u751f\u6210\u7684\u53ef\u8ffd\u6eaf\u6027\u3002RAG \u65e0\u7591\u662f\u6700\u6fc0\u52a8\u4eba\u5fc3\u7684<mark data-type=\"concepts\" data-id=\"2d28aa9c-942d-471d-bd96-8bfefb7144e0\">\u4eba\u5de5\u667a\u80fd<\/mark>\u7814\u7a76\u9886\u57df\u4e4b\u4e00\u3002\u6709\u5173 RAG \u7684\u66f4\u591a\u8be6\u60c5\u8bf7\u53c2\u9605<mark data-type=\"institutions\" data-id=\"9e96434f-5827-41e3-9b3c-36d39bc0d446\">\u673a\u5668\u4e4b\u5fc3<\/mark>\u4e13\u680f\u6587\u7ae0\u300a<a data-itemshowtype=\"0\" data-linktype=\"2\" href=\"http:\/\/mp.weixin.qq.com\/s?__biz=MzA3MzI4MjgzMw==&amp;mid=2650903502&amp;idx=4&amp;sn=9101980732b60ac4c036d2695c306fee&amp;chksm=84e457b0b393dea6090f64d8f85b64755c917c315ea7a39e9aad70ca74e09de520c2ffa1ed50&amp;scene=21#wechat_redirect\" target=\"_blank\">\u4e13\u8865\u5927\u6a21\u578b\u77ed\u677f\u7684RAG\u6709\u54ea\u4e9b\u65b0\u8fdb\u5c55\uff1f\u8fd9\u7bc7\u7efc\u8ff0\u8bb2\u660e\u767d\u4e86<\/a>\u300b\u3002<\/section>\n<section><\/section>\n<section>\u4f46 RAG \u4e5f\u5e76\u975e\u5b8c\u7f8e\uff0c\u7528\u6237\u5728\u4f7f\u7528\u65f6\u4e5f\u5e38\u4f1a\u906d\u9047\u4e00\u4e9b\u300c\u75db\u70b9\u300d\u3002\u8fd1\u65e5\uff0c\u82f1\u4f1f\u8fbe\u751f\u6210\u5f0fAI\u9ad8\u7ea7\u89e3\u51b3\u65b9\u6848\u67b6\u6784\u5e08Wenqi Glantz \u5728 Towards Data Science \u53d1\u5e03\u4e86\u4e00\u7bc7\u6587\u7ae0\uff0c\u68b3\u7406\u4e86 12 \u4e2a RAG \u7684\u75db\u70b9\u5e76\u7ed9\u51fa\u4e86\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/section>\n<p><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-80640c3b060d3f5e1bb920009bc45385.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-80640c3b060d3f5e1bb920009bc45385.png\"><\/a><\/p>\n<section>\u6587\u7ae0\u76ee\u5f55\u5982\u4e0b\uff1a<\/section>\n<section><\/section>\n<section>\u75db\u70b9 1\uff1a\u5185\u5bb9\u7f3a\u5931<\/section>\n<section>\u75db\u70b9 2\uff1a\u9519\u8fc7\u6392\u540d\u9760\u524d\u7684\u6587\u6863<\/section>\n<section>\u75db\u70b9 3\uff1a\u4e0d\u5728\u4e0a\u4e0b\u6587\u4e2d\u2014\u2014\u5408\u5e76\u7b56\u7565\u7684\u5c40\u9650<\/section>\n<section>\u75db\u70b9 4\uff1a\u672a\u63d0\u53d6\u51fa\u6765<\/section>\n<section>\u75db\u70b9 5\uff1a\u683c\u5f0f\u9519\u8bef<\/section>\n<section>\u75db\u70b9 6\uff1a\u4e0d\u6b63\u786e\u7684\u5177\u4f53\u8bf4\u660e<\/section>\n<section>\u75db\u70b9 7\uff1a\u4e0d\u5b8c\u5907<\/section>\n<section>\u75db\u70b9 8\uff1a\u6570\u636e\u6444\u53d6\u7684\u53ef\u6269\u5c55\u6027<\/section>\n<section>\u75db\u70b9 9\uff1a\u7ed3\u6784\u5316\u6570\u636e\u95ee\u7b54<\/section>\n<section>\u75db\u70b9 10\uff1a\u4ece\u590d\u6742 PDF \u63d0\u53d6\u6570\u636e<\/section>\n<section>\u75db\u70b9 11\uff1a\u540e\u5907\u6a21\u578b<\/section>\n<section>\u75db\u70b9 12\uff1aLLM \u5b89\u5168<\/section>\n<section><\/section>\n<section>\u5176\u4e2d 7 \u4e2a\u75db\u70b9\uff08\u89c1\u4e0b\u56fe\uff09\u6765\u81ea Barnett et al. \u7684\u8bba\u6587\u300aSeven Failure Points When Engineering a Retrieval Augmented Generation System\u300b\uff0c\u6b64\u5916\u8fd8\u53e6\u5916\u589e\u52a0\u4e86 5 \u4e2a\u5e38\u89c1\u75db\u70b9\u3002<\/section>\n<p><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-115a06dede9c718669fadd236adf9d2c.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-115a06dede9c718669fadd236adf9d2c.png\"><\/a><\/p>\n<section>\u8fd9\u4e9b\u75db\u70b9\u5bf9\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u5982\u4e0b\uff1a\u00a0<\/section>\n<p><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-d62fe0a8e8d4a81147a9bc4b31574ca1.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-d62fe0a8e8d4a81147a9bc4b31574ca1.png\"><\/a><\/p>\n<section><strong>\u75db\u70b9 1\uff1a\u5185\u5bb9\u7f3a\u5931<\/strong><\/section>\n<section><\/section>\n<section><mark data-type=\"concepts\" data-id=\"6d74712b-ff57-46af-bd46-cd8e9fc29d20\">\u77e5\u8bc6\u5e93<\/mark>\u4e2d\u7f3a\u5931\u4e0a\u4e0b\u6587\u3002\u5f53<mark data-type=\"concepts\" data-id=\"6d74712b-ff57-46af-bd46-cd8e9fc29d20\">\u77e5\u8bc6\u5e93<\/mark>\u4e2d\u6ca1\u6709\u7b54\u6848\u65f6\uff0cRAG \u7cfb\u7edf\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u770b\u4f3c\u53ef\u4fe1\u4f46\u5e76\u4e0d\u6b63\u786e\u7684\u7b54\u6848\uff0c\u800c\u4e0d\u4f1a\u627f\u8ba4\u5b83\u4e0d\u77e5\u9053\u3002\u7528\u6237\u4f1a\u6536\u5230\u9519\u8bef\u4fe1\u606f\uff0c\u906d\u9047\u632b\u6298\u3002<\/section>\n<section><\/section>\n<section>\u4eba\u4eec\u63d0\u51fa\u4e86\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\uff1a<\/section>\n<section><\/section>\n<section><strong>\u6e05\u6d01\u6570\u636e<\/strong><\/section>\n<section><\/section>\n<section>\u8f93\u5165\u5783\u573e\uff0c\u90a3\u4e5f\u5fc5\u5b9a\u8f93\u51fa\u5783\u573e\u3002\u5982\u679c\u4f60\u7684\u6e90\u6570\u636e\u8d28\u91cf\u4f4e\u52a3\uff0c\u6bd4\u5982\u5305\u542b\u4e92\u76f8\u51b2\u7a81\u7684\u4fe1\u606f\uff0c\u90a3\u4e0d\u7ba1\u4f60\u7684 RAG \u5de5\u4f5c\u6784\u5efa\u5f97\u591a\u4e48\u597d\uff0c\u5b83\u90fd\u4e0d\u53ef\u80fd\u7528\u4f60\u8f93\u5165\u7684\u5783\u573e\u795e\u5947\u5730\u8f93\u51fa\u9ad8\u8d28\u91cf\u7ed3\u679c\u3002\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u4e0d\u4ec5\u9002\u7528\u4e8e\u8fd9\u4e2a\u75db\u70b9\uff0c\u800c\u4e14\u9002\u7528\u4e8e\u672c\u6587\u5217\u51fa\u7684\u6240\u6709\u75db\u70b9\u3002\u4efb\u4f55 RAG \u5de5\u4f5c\u6d41\u7a0b\u60f3\u8981\u83b7\u5f97\u4f18\u826f\u8868\u73b0\uff0c\u90fd\u5fc5\u987b\u5148\u6e05\u6d01\u6570\u636e\u3002<\/section>\n<section><\/section>\n<section>\u4e0b\u9762\u5217\u51fa\u4e86\u51e0\u4e2a\u6e05\u6d01\u6570\u636e\u7684\u5e38\u7528\u7b56\u7565\uff1a<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>\u79fb\u9664\u566a\u58f0\u548c\u4e0d\u76f8\u5173\u4fe1\u606f\uff1a\u8fd9\u5305\u62ec\u79fb\u9664\u7279\u6b8a\u5b57\u7b26\u3001\u505c\u7528\u8bcd\uff08stop words\uff0c\u5982 the \u548c a\uff09\u3001HTML \u6807\u7b7e\u3002<\/section>\n<\/li>\n<li>\n<section>\u8bc6\u522b\u548c\u7ea0\u6b63\u9519\u8bef\uff1a\u5305\u62ec\u62fc\u5199\u9519\u8bef\u3001\u9519\u522b\u5b57\u548c\u8bed\u6cd5\u9519\u8bef\u3002\u53ef\u4ee5\u4f7f\u7528\u62fc\u5199\u68c0\u67e5\u5668\u548c<mark data-type=\"tech_tasks\" data-id=\"fee178e8-ee20-42fc-8f8f-1d41c1f34e8f\">\u8bed\u8a00\u6a21\u578b<\/mark>\u7b49\u5de5\u5177\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/section>\n<\/li>\n<li>\n<section>\u53bb\u91cd\uff1a\u79fb\u9664\u91cd\u590d\u6570\u636e\u8bb0\u5f55\u6216\u53ef\u80fd\u5bfc\u81f4\u68c0\u7d22\u8fc7\u7a0b\u51fa\u73b0\u504f\u5dee\u7684\u76f8\u4f3c\u8bb0\u5f55\u3002<\/section>\n<\/li>\n<\/ul>\n<section><\/section>\n<section>unstructured.io \u7684\u6838\u5fc3\u8f6f\u4ef6\u5e93\u63d0\u4f9b\u4e86\u4e00\u6574\u5957\u6e05\u6d01\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u89e3\u51b3\u8fd9\u4e9b\u6570\u636e\u6e05\u6d01\u9700\u6c42\u3002\u503c\u5f97\u4e00\u8bd5\u3002<\/section>\n<section><\/section>\n<section><strong>\u66f4\u597d\u7684\u63d0\u8bcd\u8bbe\u8ba1<\/strong><\/section>\n<section><\/section>\n<section>\u5bf9\u4e8e\u56e0\u4e3a\u4fe1\u606f\u7f3a\u4e4f\u800c\u5bfc\u81f4\u7cfb\u7edf\u7ed9\u51fa\u770b\u4f3c\u53ef\u4fe1\u5374\u4e0d\u6b63\u786e\u7ed3\u679c\u7684\u95ee\u9898\uff0c\u66f4\u597d\u7684\u63d0\u8bcd\u8bbe\u8ba1\u80fd\u63d0\u4f9b\u5f88\u5927\u5e2e\u52a9\u3002\u901a\u8fc7\u4e3a\u7cfb\u7edf\u7ed9\u51fa\u300c\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u7b54\u6848\u662f\u4ec0\u4e48\uff0c\u5c31\u544a\u8bc9\u6211\u4f60\u4e0d\u77e5\u9053\u300d\u8fd9\u6837\u7684\u6307\u793a\uff0c\u5c31\u80fd\u9f13\u52b1\u6a21\u578b\u627f\u8ba4\u81ea\u5df1\u7684\u5c40\u9650\uff0c\u5e76\u66f4\u900f\u660e\u5730\u5411\u7528\u6237\u4f20\u8fbe\u5b83\u7684\u4e0d\u786e\u5b9a\u3002\u867d\u7136\u4e0d\u80fd\u4fdd\u8bc1 100% \u51c6\u786e\u5ea6\uff0c\u4f46\u5728\u6e05\u6d01\u6570\u636e\u4e4b\u540e\uff0c\u7cbe\u5fc3\u8bbe\u8ba1 prompt \u662f\u6700\u597d\u7684\u505a\u6cd5\u4e4b\u4e00\u3002<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 2\uff1a\u9519\u8fc7\u6392\u540d\u9760\u524d\u7684\u6587\u6863<\/strong><\/section>\n<section><\/section>\n<section>\u521d\u59cb\u68c0\u7d22\u8fc7\u7a0b\u4e2d\u7f3a\u5931\u4e0a\u4e0b\u6587\u3002\u5728\u7cfb\u7edf\u7684\u68c0\u7d22\u7ec4\u4ef6\u8fd4\u56de\u7684\u7ed3\u679c\u4e2d\uff0c\u5173\u952e\u6027\u7684\u6587\u6863\u53ef\u80fd\u5e76\u4e0d\u9760\u524d\u3002\u6b63\u786e\u7684\u7b54\u6848\u88ab\u5ffd\u89c6\u4e86\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u65e0\u6cd5\u7ed9\u51fa\u51c6\u786e\u54cd\u5e94\u3002\u4e0a\u8ff0\u8bba\u6587\u4e2d\u5199\u9053\uff1a\u300c\u95ee\u9898\u7684\u7b54\u6848\u5c31\u5728\u6587\u6863\u4e2d\uff0c\u4f46\u6392\u540d\u4e0d\u591f\u9ad8\uff0c\u5c31\u6ca1\u6709\u8fd4\u56de\u7ed9\u7528\u6237\u3002\u300d<\/section>\n<section><\/section>\n<section>\u7814\u7a76\u8005\u63d0\u51fa\u4e86\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\uff1a<\/section>\n<section><\/section>\n<section><strong>\u5bf9 chunk_size \u548c similarity_top_k \u8fdb\u884c<mark data-type=\"concepts\" data-id=\"5619ca3f-5d4e-48c1-824d-d2a0aea0c7d1\">\u8d85<mark data-type=\"concepts\" data-id=\"2e982b73-88e2-41e8-a430-f7ae5a9af4bf\">\u53c2\u6570<\/mark><\/mark>\u5fae\u8c03<\/strong><\/section>\n<section><\/section>\n<section>chunk_size \u548c similarity_top_k \u8fd9\u4e24\u4e2a<mark data-type=\"concepts\" data-id=\"2e982b73-88e2-41e8-a430-f7ae5a9af4bf\">\u53c2\u6570<\/mark>\u53ef\u7528\u4e8e\u7ba1\u7406 RAG \u6a21\u578b\u7684\u6570\u636e\u68c0\u7d22\u8fc7\u7a0b\u7684\u6548\u7387\u548c\u6548\u679c\u3002\u8c03\u6574\u8fd9\u4e24\u4e2a<mark data-type=\"concepts\" data-id=\"2e982b73-88e2-41e8-a430-f7ae5a9af4bf\">\u53c2\u6570<\/mark>\u4f1a\u5f71\u54cd\u88ab\u68c0\u7d22\u4fe1\u606f\u7684\u8ba1\u7b97\u6548\u7387\u548c\u8d28\u91cf\u4e4b\u95f4\u7684\u6743\u8861\u3002\u4f5c\u8005\u5728\u4e4b\u524d\u4e00\u7bc7\u6587\u7ae0\u4e2d\u63a2\u7d22\u4e86\u5bf9 chunk_size \u548c similarity_top_k \u8fdb\u884c<mark data-type=\"concepts\" data-id=\"5619ca3f-5d4e-48c1-824d-d2a0aea0c7d1\">\u8d85<mark data-type=\"concepts\" data-id=\"2e982b73-88e2-41e8-a430-f7ae5a9af4bf\">\u53c2\u6570<\/mark><\/mark>\u5fae\u8c03\u7684\u7ec6\u8282\uff1a<\/section>\n<section><\/section>\n<section>\u8bf7\u8bbf\u95ee\uff1ahttps:\/\/medium.com\/gitconnected\/automating-hyperparameter-tuning-with-llamaindex-72fdd68e3b90\u00a0<\/section>\n<section><\/section>\n<section>\u4e0b\u9762\u7ed9\u51fa\u4e86\u793a\u4f8b\u4ee3\u7801\uff1a<\/section>\n<section>\n<pre data-lang=\"properties\"><code>param_tuner = ParamTuner(<\/code>\r\n<code>    param_fn=objective_function_semantic_similarity,<\/code>\r\n<code>    param_dict=param_dict,<\/code>\r\n<code>    fixed_param_dict=fixed_param_dict,<\/code>\r\n<code>    show_progress=True,<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>results = param_tuner.tune()<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section>objective_function_semantic_similarity \u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff0c\u5176\u4e2d param_dict \u5305\u542b\u4e86<mark data-type=\"concepts\" data-id=\"2e982b73-88e2-41e8-a430-f7ae5a9af4bf\">\u53c2\u6570<\/mark> chunk_size \u548c top_k \u4ee5\u53ca\u5b83\u4eec\u5bf9\u5e94\u7684\u503c\uff1a<\/section>\n<section>\n<pre data-lang=\"python\"><code># contains the parameters that need to be tuned<\/code>\r\n<code>param_dict = {\"chunk_size\": [256, 512, 1024], \"top_k\": [1, 2, 5]}<\/code>\r\n<code>\r\n<\/code><code># contains parameters remaining fixed across all runs of the tuning process<\/code>\r\n<code>fixed_param_dict = {<\/code>\r\n<code>    \"docs\": documents,<\/code>\r\n<code>    \"eval_qs\": eval_qs,<\/code>\r\n<code>    \"ref_response_strs\": ref_response_strs,<\/code>\r\n<code>}<\/code>\r\n<code>\r\n<\/code><code>def objective_function_semantic_similarity(params_dict):<\/code>\r\n<code>    chunk_size = params_dict[\"chunk_size\"]<\/code>\r\n<code>    docs = params_dict[\"docs\"]<\/code>\r\n<code>    top_k = params_dict[\"top_k\"]<\/code>\r\n<code>    eval_qs = params_dict[\"eval_qs\"]<\/code>\r\n<code>    ref_response_strs = params_dict[\"ref_response_strs\"]<\/code>\r\n<code>\r\n<\/code><code>    # build index<\/code>\r\n<code>    index = _build_index(chunk_size, docs)<\/code>\r\n<code>\r\n<\/code><code>    # query engine<\/code>\r\n<code>    query_engine = index.as_query_engine(similarity_top_k=top_k)<\/code>\r\n<code>\r\n<\/code><code>    # get predicted responses<\/code>\r\n<code>    pred_response_objs = get_responses(<\/code>\r\n<code>        eval_qs, query_engine, show_progress=True<\/code>\r\n<code>    )<\/code>\r\n<code>\r\n<\/code><code>    # run evaluator<\/code>\r\n<code>    eval_batch_runner = _get_eval_batch_runner_semantic_similarity()<\/code>\r\n<code>    eval_results = eval_batch_runner.evaluate_responses(<\/code>\r\n<code>        eval_qs, responses=pred_response_objs, reference=ref_response_strs<\/code>\r\n<code>    )<\/code>\r\n<code>\r\n<\/code><code>    # get semantic similarity metric<\/code>\r\n<code>    mean_score = np.array(<\/code>\r\n<code>        [r.score for r in eval_results[\"semantic_similarity\"]]<\/code>\r\n<code>    ).mean()<\/code>\r\n<code>\r\n<\/code><code>\u00a0 \u00a0 return RunResult(score=mean_score, params=params_dict)<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section>\u66f4\u591a\u7ec6\u8282\u8bf7\u8bbf\u95ee LlamaIndex \u7684\u5173\u4e8e RAG \u7684<mark data-type=\"concepts\" data-id=\"5619ca3f-5d4e-48c1-824d-d2a0aea0c7d1\">\u8d85<mark data-type=\"concepts\" data-id=\"2e982b73-88e2-41e8-a430-f7ae5a9af4bf\">\u53c2\u6570<\/mark><\/mark>\u4f18\u5316\u7684\u5b8c\u6574\u7b14\u8bb0\uff1a<\/section>\n<section>https:\/\/docs.llamaindex.ai\/en\/stable\/examples\/param_optimizer\/param_optimizer\/<\/section>\n<section><\/section>\n<section><strong>\u91cd\u65b0\u6392\u540d<\/strong><\/section>\n<section><\/section>\n<section>\u5728\u5c06\u68c0\u7d22\u7ed3\u679c\u53d1\u9001\u7ed9 LLM \u4e4b\u524d\u5bf9\u5b83\u4eec\u8fdb\u884c\u91cd\u65b0\u6392\u540d\u53ef\u4ee5\u5927\u5e45\u63d0\u5347 RAG \u6027\u80fd\u3002<\/section>\n<section><\/section>\n<section>\u8fd9\u4e2a LlamaIndex \u7b14\u8bb0\uff08https:\/\/docs.llamaindex.ai\/en\/stable\/examples\/node_postprocessor\/CohereRerank.html\u00a0\uff09\u6f14\u793a\u4e86\u4ee5\u4e0b\u4e24\u79cd\u505a\u6cd5\u7684\u5dee\u5f02\uff1a<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>\u4e0d\u4f7f\u7528\u91cd\u65b0\u6392\u540d\u5de5\u5177\uff08reranker\uff09\uff0c\u76f4\u63a5\u68c0\u7d22\u6700\u524d\u9762\u7684 2 \u4e2a\u8282\u70b9\uff0c\u8fdb\u884c\u4e0d\u51c6\u786e\u7684\u68c0\u7d22\u3002<\/section>\n<\/li>\n<li>\n<section>\u68c0\u7d22\u6700\u524d\u9762\u7684 10 \u4e2a\u8282\u70b9\u5e76\u4f7f\u7528 CohereRerank \u8fdb\u884c\u91cd\u65b0\u6392\u540d\u5e76\u8fd4\u56de\u6700\u524d\u9762\u7684 2 \u4e2a\u8282\u70b9\uff0c\u8fdb\u884c\u51c6\u786e\u7684\u68c0\u7d22\u3002<\/section>\n<\/li>\n<\/ul>\n<section>\n<pre data-lang=\"makefile\"><code>import os<\/code>\r\n<code>from llama_index.postprocessor.cohere_rerank import CohereRerank<\/code>\r\n<code>\r\n<\/code><code>api_key = os.environ[\"COHERE_API_KEY\"]<\/code>\r\n<code>cohere_rerank = CohereRerank(api_key=api_key, top_n=2) # return top 2 nodes from reranker<\/code>\r\n<code>\r\n<\/code><code>query_engine = index.as_query_engine(<\/code>\r\n<code>    similarity_top_k=10, # we can set a high top_k here to ensure maximum relevant retrieval<\/code>\r\n<code>    node_postprocessors=[cohere_rerank], # pass the reranker to node_postprocessors<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>response = query_engine.query(<\/code>\r\n<code>    \"What did Sam Altman do in this essay?\",<\/code>\r\n<code>)<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section>\u53e6\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5d4c\u5165\u548c\u91cd\u65b0\u6392\u540d\u5de5\u5177\u8bc4\u4f30\u548c\u63d0\u5347\u68c0\u7d22\u5668\u7684\u6027\u80fd\u3002<\/section>\n<section><\/section>\n<section>\u53c2\u9605\uff1ahttps:\/\/blog.llamaindex.ai\/boosting-rag-picking-the-best-embedding-reranker-models-42d079022e83<\/section>\n<section><\/section>\n<section>\u6b64\u5916\uff0c\u4e3a\u4e86\u5f97\u5230\u66f4\u597d\u7684\u68c0\u7d22\u6027\u80fd\uff0c\u8fd8\u80fd\u5fae\u8c03\u4e00\u4e2a\u5b9a\u5236\u7248\u7684\u91cd\u65b0\u6392\u540d\u5de5\u5177\uff0c\u5176\u5b9e\u73b0\u7ec6\u8282\u53ef\u8bbf\u95ee\uff1a<\/section>\n<section><\/section>\n<section>\u535a\u5ba2\u94fe\u63a5\uff1ahttps:\/\/blog.llamaindex.ai\/improving-retrieval-performance-by-fine-tuning-cohere-reranker-with-llamaindex-16c0c1f9b33b<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 3\uff1a\u4e0d\u5728\u4e0a\u4e0b\u6587\u4e2d\u2014\u2014\u5408\u5e76\u7b56\u7565\u7684\u5c40\u9650<\/strong><\/section>\n<section><\/section>\n<section>\u91cd\u65b0\u6392\u540d\u4e4b\u540e\u7f3a\u4e4f\u4e0a\u4e0b\u6587\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u75db\u70b9\uff0c\u4e0a\u8ff0\u8bba\u6587\u7684\u5b9a\u4e49\u4e3a\uff1a\u300c\u5df2\u7ecf\u4ece<mark data-type=\"concepts\" data-id=\"700f9c0f-1e8b-4fde-8bae-6de39c13f022\">\u6570\u636e\u5e93<\/mark>\u68c0\u7d22\u5230\u4e86\u5e26\u7b54\u6848\u7684\u6587\u6863\uff0c\u4f46\u8be5\u6587\u6863\u6ca1\u80fd\u6210\u4e3a\u751f\u6210\u7b54\u6848\u7684\u4e0a\u4e0b\u6587\u3002\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u7684\u539f\u56e0\u662f<mark data-type=\"concepts\" data-id=\"700f9c0f-1e8b-4fde-8bae-6de39c13f022\">\u6570\u636e\u5e93<\/mark>\u8fd4\u56de\u4e86\u8bb8\u591a\u6587\u6863\uff0c\u4e4b\u540e\u91c7\u7528\u4e86\u4e00\u79cd\u5408\u5e76\u8fc7\u7a0b\u6765\u68c0\u7d22\u7b54\u6848\u3002\u300d<\/section>\n<section><\/section>\n<section>\u9664\u4e86\u524d\u6587\u63d0\u5230\u7684\u589e\u52a0\u91cd\u65b0\u6392\u540d\u5de5\u5177\u548c\u5fae\u8c03\u91cd\u65b0\u6392\u540d\u5de5\u5177\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u63a2\u7d22\u4ee5\u4e0b\u89e3\u51b3\u65b9\u6848\uff1a<\/section>\n<section><\/section>\n<section><strong>\u8c03\u6574\u68c0\u7d22\u7b56\u7565<\/strong><\/section>\n<section><\/section>\n<section>LlamaIndex \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u7684\u68c0\u7d22\u7b56\u7565\uff0c\u53ef\u5e2e\u52a9\u7814\u7a76\u8005\u5728 RAG \u5de5\u4f5c\u6d41\u7a0b\u4e2d\u5b9e\u73b0\u51c6\u786e\u7684\u68c0\u7d22\u3002<\/section>\n<section><\/section>\n<section>\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\u5df2\u5206\u6210\u4e0d\u540c\u7c7b\u522b\u7684\u68c0\u7d22\u7b56\u7565\u5217\u8868\uff1ahttps:\/\/docs.llamaindex.ai\/en\/stable\/module_guides\/querying\/retriever\/retrievers.html<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>\u57fa\u4e8e\u6bcf\u4e2a\u7d22\u5f15\u8fdb\u884c\u57fa\u672c\u7684\u68c0\u7d22<\/section>\n<\/li>\n<li>\n<section>\u9ad8\u7ea7\u68c0\u7d22\u548c\u641c\u7d22<\/section>\n<\/li>\n<li>\n<section>\u81ea\u52a8\u68c0\u7d22<\/section>\n<\/li>\n<li>\n<section><mark data-type=\"tech_methods\" data-id=\"6e896233-3f15-47a4-9b2e-479d7cc5478b\">\u77e5\u8bc6\u56fe\u8c31<\/mark>\u68c0\u7d22\u5668<\/section>\n<\/li>\n<li>\n<section>\u7ec4\u5408\/\u5206\u5c42\u68c0\u7d22\u5668<\/section>\n<\/li>\n<\/ul>\n<section><\/section>\n<section><strong>\u5bf9\u5d4c\u5165\u8fdb\u884c\u5fae\u8c03<\/strong><\/section>\n<section><\/section>\n<section>\u5982\u679c\u4f60\u4f7f\u7528\u5f00\u6e90\u7684\u5d4c\u5165\u6a21\u578b\uff0c\u90a3\u4e48\u4e3a\u4e86\u5b9e\u73b0\u66f4\u51c6\u786e\u7684\u68c0\u7d22\uff0c\u53ef\u4ee5\u5bf9\u5d4c\u5165\u6a21\u578b\u8fdb\u884c\u5fae\u8c03\u3002LlamaIndex \u6709\u4e00\u4e2a\u5fae\u8c03\u5f00\u6e90\u5d4c\u5165\u6a21\u578b\u7684\u9010\u6b65\u6559\u7a0b\uff0c\u5176\u4e2d\u8bc1\u660e\u5fae\u8c03\u5d4c\u5165\u6a21\u578b\u786e\u5b9e\u53ef\u4ee5\u63d0\u5347\u5728\u591a\u4e2a\u8bc4\u4f30\u6307\u6807\u4e0a\u7684\u8868\u73b0\uff1a<\/section>\n<section><\/section>\n<section>\u6559\u7a0b\u94fe\u63a5\uff1ahttps:\/\/docs.llamaindex.ai\/en\/stable\/examples\/finetuning\/embeddings\/finetune_embedding.html<\/section>\n<section><\/section>\n<section>\u4e0b\u9762\u662f\u521b\u5efa\u5fae\u8c03\u5f15\u64ce\u3001\u8fd0\u884c\u5fae\u8c03\u3001\u5f97\u5230\u5df2\u5fae\u8c03\u6a21\u578b\u7684\u6837\u672c\u4ee3\u7801\uff1a<\/section>\n<section>\n<pre data-lang=\"makefile\"><code>finetune_engine = SentenceTransformersFinetuneEngine(<\/code>\r\n<code>    train_dataset,<\/code>\r\n<code>    model_id=\"BAAI\/bge-small-en\",<\/code>\r\n<code>    model_output_path=\"test_model\",<\/code>\r\n<code>    val_dataset=val_dataset,<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>finetune_engine.finetune()<\/code>\r\n<code>\r\n<\/code><code>embed_model = finetune_engine.get_finetuned_model()<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 4\uff1a\u672a\u63d0\u53d6\u51fa\u6765<\/strong><\/section>\n<section><\/section>\n<section>\u672a\u6b63\u786e\u63d0\u53d6\u4e0a\u4e0b\u6587\u3002\u7cfb\u7edf\u96be\u4ee5\u4ece\u6240\u63d0\u4f9b\u7684\u4e0a\u4e0b\u6587\u63d0\u53d6\u51fa\u6b63\u786e\u7b54\u6848\uff0c\u5c24\u5176\u662f\u5f53\u4fe1\u606f\u8fc7\u8f7d\u65f6\u3002\u8fd9\u4f1a\u5bfc\u81f4\u5173\u952e\u7ec6\u8282\u7f3a\u5931\uff0c\u635f\u5bb3\u54cd\u5e94\u7684\u8d28\u91cf\u3002\u4e0a\u8ff0\u8bba\u6587\u5199\u9053\uff1a\u300c\u5f53\u4e0a\u4e0b\u6587\u4e2d\u6709\u592a\u591a\u566a\u58f0\u6216\u4e92\u76f8\u77db\u76fe\u7684\u4fe1\u606f\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u3002\u300d<\/section>\n<section><\/section>\n<section>\u4e0b\u9762\u6765\u770b\u4e09\u79cd\u89e3\u51b3\u65b9\u6848\uff1a<\/section>\n<section><\/section>\n<section><strong>\u6e05\u6d01\u6570\u636e<\/strong><\/section>\n<section><\/section>\n<section>\u8fd9\u4e2a\u75db\u70b9\u7684\u4e00\u4e2a\u5178\u578b\u539f\u56e0\u5c31\u662f\u6570\u636e\u8d28\u91cf\u5dee\u3002\u6e05\u6d01\u6570\u636e\u7684\u91cd\u8981\u6027\u503c\u5f97\u4e00\u518d\u5f3a\u8c03\uff01\u5728\u8d23\u5907\u4f60\u7684 RAG \u6d41\u7a0b\u4e4b\u524d\uff0c\u8bf7\u52a1\u5fc5\u6e05\u6d01\u4f60\u7684\u6570\u636e\u3002<\/section>\n<section><\/section>\n<section><strong>prompt \u538b\u7f29<\/strong><\/section>\n<section><\/section>\n<section>LongLLMLingua \u7814\u7a76\u9879\u76ee\/\u8bba\u6587\u9488\u5bf9\u957f\u4e0a\u4e0b\u6587\u60c5\u51b5\u63d0\u51fa\u4e86 prompt \u538b\u7f29\u3002\u901a\u8fc7\u5c06\u5176\u6574\u5408\u8fdb LlamaIndex\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 LongLLMLingua \u5b9e\u73b0\u6210\u4e00\u4e2a\u8282\u70b9\u540e\u5904\u7406\u5668\uff0c\u5176\u53ef\u5728\u68c0\u7d22\u6b65\u9aa4\u4e4b\u540e\u5bf9\u4e0a\u4e0b\u6587\u8fdb\u884c\u538b\u7f29\uff0c\u4e4b\u540e\u518d\u5c06\u5176\u4f20\u8f93\u7ed9 LLM\u3002LongLLMLingua \u538b\u7f29\u7684 prompt \u80fd\u4ee5\u8fdc\u8fdc\u66f4\u4f4e\u7684\u6210\u672c\u5f97\u5230\u66f4\u9ad8\u7684\u6027\u80fd\u3002\u6b64\u5916\uff0c\u6574\u4e2a\u7cfb\u7edf\u4f1a\u6709\u66f4\u5feb\u7684\u8fd0\u884c\u901f\u5ea6\u3002<\/section>\n<section><\/section>\n<section>\u4e0b\u9762\u7684\u4ee3\u7801\u8bbe\u7f6e\u4e86 LongLLMLinguaPostprocessor\uff0c\u5176\u4e2d\u4f7f\u7528\u4e86 longllmlingua \u8f6f\u4ef6\u5305\u6765\u8fd0\u884c prompt \u538b\u7f29\u3002<\/section>\n<section><\/section>\n<section>\u66f4\u591a\u7ec6\u8282\u8bf7\u8bbf\u95ee\u8fd9\u4e2a\u7b14\u8bb0\uff1a<\/section>\n<section>https:\/\/docs.llamaindex.ai\/en\/stable\/examples\/node_postprocessor\/LongLLMLingua.html#longllmlingua<\/section>\n<section>\n<pre data-lang=\"python\"><code>\r\n<\/code><code>from llama_index.core.query_engine import RetrieverQueryEngine<\/code>\r\n<code>from llama_index.core.response_synthesizers import CompactAndRefine<\/code>\r\n<code>from llama_index.postprocessor.longllmlingua import LongLLMLinguaPostprocessor<\/code>\r\n<code>from llama_index.core import QueryBundle<\/code>\r\n<code>\r\n<\/code><code>node_postprocessor = LongLLMLinguaPostprocessor(<\/code>\r\n<code>    instruction_str=\"Given the context, please answer the final question\",<\/code>\r\n<code>    target_token=300,<\/code>\r\n<code>    rank_method=\"longllmlingua\",<\/code>\r\n<code>    additional_compress_kwargs={<\/code>\r\n<code>        \"condition_compare\": True,<\/code>\r\n<code>        \"condition_in_question\": \"after\",<\/code>\r\n<code>        \"context_budget\": \"+100\",<\/code>\r\n<code>        \"reorder_context\": \"sort\",<\/code>\r\n<code>\u00a0 # enable document reorder<\/code>\r\n<code>    },<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>retrieved_nodes = retriever.retrieve(query_str)<\/code>\r\n<code>synthesizer = CompactAndRefine()<\/code>\r\n<code>\r\n<\/code><code># outline steps in RetrieverQueryEngine for clarity:<\/code>\r\n<code># postprocess (compress), synthesize<\/code>\r\n<code>new_retrieved_nodes = node_postprocessor.postprocess_nodes(<\/code>\r\n<code>    retrieved_nodes, query_bundle=QueryBundle(query_str=query_str)<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>print(\"nn\".join([n.get_content() for n in new_retrieved_nodes]))<\/code>\r\n<code>\r\n<\/code><code>response = synthesizer.synthesize(query_str, new_retrieved_nodes)<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section><strong>LongContextReorder<\/strong><\/section>\n<section><\/section>\n<section>\u8bba\u6587\u300aLost in the Middle: How Language Models Use Long Contexts\u300b\u89c2\u5bdf\u5230\uff1a\u5f53\u5173\u952e\u4fe1\u606f\u4f4d\u4e8e\u8f93\u5165\u4e0a\u4e0b\u6587\u7684\u5f00\u5934\u6216\u672b\u5c3e\u65f6\uff0c\u901a\u5e38\u80fd\u83b7\u5f97\u6700\u4f73\u6027\u80fd\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u79cd\u300c\u4e2d\u90e8\u4e22\u5931\u300d\u95ee\u9898\uff0c\u7814\u7a76\u8005\u8bbe\u8ba1\u4e86 LongContextReorder\uff0c\u5176\u505a\u6cd5\u662f\u91cd\u65b0\u8c03\u6574\u88ab\u68c0\u7d22\u8282\u70b9\u7684\u987a\u5e8f\uff0c\u8fd9\u5bf9\u9700\u8981\u8f83\u5927 top-k \u7684\u60c5\u51b5\u5f88\u6709\u7528\u3002<\/section>\n<section><\/section>\n<section>\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5728<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u5f15\u64ce\u6784\u5efa\u671f\u95f4\u5c06 LongContextReorder \u5b9a\u4e49\u6210\u4f60\u7684\u8282\u70b9\u540e\u5904\u7406\u5668\u3002\u66f4\u591a\u7ec6\u8282\uff0c\u8bf7\u53c2\u770b\u8fd9\u4efd\u7b14\u8bb0\uff1a<\/section>\n<section>https:\/\/docs.llamaindex.ai\/en\/stable\/examples\/node_postprocessor\/LongContextReorder.html<\/section>\n<section>\n<pre data-lang=\"makefile\"><code>from llama_index.core.postprocessor import LongContextReorder<\/code>\r\n<code>\r\n<\/code><code>reorder = LongContextReorder()<\/code>\r\n<code>\r\n<\/code><code>reorder_engine = index.as_query_engine(<\/code>\r\n<code>    node_postprocessors=[reorder], similarity_top_k=5<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>reorder_response = reorder_engine.query(\"Did the author meet Sam Altman?\")<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 5\uff1a\u683c\u5f0f\u9519\u8bef<\/strong><\/section>\n<section><\/section>\n<section>\u8f93\u51fa\u7684\u683c\u5f0f\u6709\u8bef\u3002\u5f53 LLM \u5ffd\u89c6\u4e86\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u4fe1\u606f\uff08\u5982\u8868\u683c\u6216\u5217\u8868\uff09\u7684\u6307\u4ee4\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u8fd9\u4e2a\u95ee\u9898\uff0c\u5bf9\u6b64\u7684\u89e3\u51b3\u65b9\u6848\u6709\u56db\u4e2a\uff1a<\/section>\n<section><\/section>\n<section><strong>\u66f4\u597d\u7684\u63d0\u8bcd\u8bbe\u8ba1<\/strong><\/section>\n<section><\/section>\n<section>\u9488\u5bf9\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4f7f\u7528\u591a\u79cd\u7b56\u7565\u6765\u63d0\u5347 prompt\uff1a<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>\u6e05\u6670\u5730\u8bf4\u660e\u6307\u4ee4<\/section>\n<\/li>\n<li>\n<section>\u7b80\u5316\u8bf7\u6c42\u5e76\u4f7f\u7528\u5173\u952e\u8bcd<\/section>\n<\/li>\n<li>\n<section>\u7ed9\u51fa\u793a\u4f8b<\/section>\n<\/li>\n<li>\n<section>\u4f7f\u7528\u8fed\u4ee3\u5f0f\u7684 prompt \u5e76\u8be2\u95ee\u540e\u7eed\u95ee\u9898<\/section>\n<\/li>\n<\/ul>\n<section><\/section>\n<section><strong>\u8f93\u51fa\u89e3\u6790<\/strong><\/section>\n<section><\/section>\n<section>\u4e3a\u4e86\u786e\u4fdd\u5f97\u5230\u6240\u9700\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8f93\u51fa\u89e3\u6790\uff1a<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>\u4e3a\u4efb\u610f prompt\/<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u63d0\u4f9b\u683c\u5f0f\u8bf4\u660e<\/section>\n<\/li>\n<li>\n<section>\u4e3a LLM \u8f93\u51fa\u63d0\u4f9b\u300c\u89e3\u6790\u300d<\/section>\n<\/li>\n<\/ul>\n<section><\/section>\n<section>LlamaIndex \u652f\u6301\u6574\u5408 Guardrails \u548c LangChain \u7b49\u5176\u5b83\u6846\u67b6\u63d0\u4f9b\u7684\u8f93\u51fa\u89e3\u6790\u6a21\u5757\u3002<\/section>\n<section><\/section>\n<section>\u4e0b\u9762\u662f\u53ef\u5728 LlamaIndex \u4e2d\u4f7f\u7528\u7684 LangChain \u7684\u8f93\u51fa\u89e3\u6790\u6a21\u5757\u7684\u4ee3\u7801\u3002\u66f4\u591a\u7ec6\u8282\u8bf7\u8bbf\u95ee\u8fd9\u4efd\u6709\u5173\u8f93\u51fa\u89e3\u6790\u6a21\u5757\u7684\u6587\u6863\uff1a<\/section>\n<section>https:\/\/docs.llamaindex.ai\/en\/stable\/module_guides\/querying\/structured_outputs\/output_parser.html<\/section>\n<section>\n<pre data-lang=\"makefile\"><code>from llama_index.core import VectorStoreIndex, SimpleDirectoryReader<\/code>\r\n<code>from llama_index.core.output_parsers import LangchainOutputParser<\/code>\r\n<code>from llama_index.llms.openai import OpenAI<\/code>\r\n<code>from langchain.output_parsers import StructuredOutputParser, ResponseSchema<\/code>\r\n<code>\r\n<\/code><code># load documents, build index<\/code>\r\n<code>documents = SimpleDirectoryReader(\"..\/paul_graham_essay\/data\").load_data()<\/code>\r\n<code>index = VectorStoreIndex.from_documents(documents)<\/code>\r\n<code>\r\n<\/code><code># define output schema<\/code>\r\n<code>response_schemas = [<\/code>\r\n<code>    ResponseSchema(<\/code>\r\n<code>        name=\"Education\",<\/code>\r\n<code>        description=\"Describes the author's educational experience\/background.\",<\/code>\r\n<code>    ),<\/code>\r\n<code>    ResponseSchema(<\/code>\r\n<code> <\/code><code>\u00a0 \u00a0 \u00a0 \u00a0name=\"Work\",<\/code>\r\n<code>        description=\"Describes the author's work experience\/background.\",<\/code>\r\n<code>    ),<\/code>\r\n<code>]<\/code>\r\n<code>\r\n<\/code><code># define output parser<\/code>\r\n<code>lc_output_parser = StructuredOutputParser.from_response_schemas(<\/code>\r\n<code>    response_schemas<\/code>\r\n<code>)<\/code>\r\n<code>output_parser = LangchainOutputParser(lc_output_parser)<\/code>\r\n<code>\r\n<\/code><code># Attach output parser to LLM<\/code>\r\n<code>llm = OpenAI(output_parser=output_parser)<\/code>\r\n<code>\r\n<\/code><code># obtain a structured response<\/code>\r\n<code>query_engine = index.as_query_engine(llm=llm)<\/code>\r\n<code>response = query_engine.query(<\/code>\r\n<code>    \"What are a few things the author did growing up?\",<\/code>\r\n<code>)<\/code>\r\n<code>print(str(response))<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section><strong>Pydantic \u7a0b\u5e8f<\/strong><\/section>\n<section><\/section>\n<section>Pydantic \u7a0b\u5e8f\u662f\u4e00\u4e2a\u591a\u529f\u80fd\u6846\u67b6\uff0c\u53ef\u5c06\u8f93\u5165\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7ed3\u6784\u5316\u7684 Pydantic \u5bf9\u8c61\u3002LlamaIndex \u63d0\u4f9b\u51e0\u7c7b Pydantic \u7a0b\u5e8f\uff1a<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>LLM \u6587\u672c\u8865\u5168 Pydantic \u7a0b\u5e8f\uff1a\u8fd9\u4e9b\u7a0b\u5e8f\u4f7f\u7528\u6587\u672c\u8865\u5168 API \u52a0\u4e0a\u8f93\u51fa\u89e3\u6790\uff0c\u53ef\u5c06\u8f93\u5165\u6587\u672c\u8f6c\u6362\u6210\u7528\u6237\u5b9a\u4e49\u7684\u7ed3\u6784\u5316\u5bf9\u8c61\u3002<\/section>\n<\/li>\n<li>\n<section>LLM \u51fd\u6570\u8c03\u7528 Pydantic \u7a0b\u5e8f\uff1a\u901a\u8fc7\u5229\u7528 LLM \u51fd\u6570\u8c03\u7528 API\uff0c\u8fd9\u4e9b\u7a0b\u5e8f\u53ef\u5c06\u8f93\u5165\u6587\u672c\u8f6c\u6362\u6210\u7528\u6237\u6307\u5b9a\u7684\u7ed3\u6784\u5316\u5bf9\u8c61\u3002<\/section>\n<\/li>\n<li>\n<section>\u9884\u5c01\u88c5 Pydantic \u7a0b\u5e8f\uff1a\u5176\u8bbe\u8ba1\u76ee\u6807\u662f\u5c06\u8f93\u5165\u6587\u672c\u8f6c\u6362\u6210\u9884\u5b9a\u4e49\u7684\u7ed3\u6784\u5316\u5bf9\u8c61\u3002<\/section>\n<\/li>\n<\/ul>\n<section><\/section>\n<section>\u4e0b\u9762\u662f\u6765\u81ea OpenAI pydantic \u7a0b\u5e8f\u7684\u4ee3\u7801\u3002LlamaIndex \u7684\u6587\u6863\u7ed9\u51fa\u4e86\u66f4\u591a\u76f8\u5173\u7ec6\u8282\uff0c\u5e76\u4e14\u5176\u4e2d\u8fd8\u5305\u542b\u4e0d\u540c Pydantic \u7a0b\u5e8f\u7684\u7b14\u8bb0\u672c\/\u6307\u5357\u7684\u94fe\u63a5\uff1a<\/section>\n<section>https:\/\/docs.llamaindex.ai\/en\/stable\/module_guides\/querying\/structured_outputs\/pydantic_program.html<\/section>\n<section><\/section>\n<section><strong>OpenAI JSON \u6a21\u5f0f<\/strong><\/section>\n<section><\/section>\n<section>OpenAI JSON \u6a21\u5f0f\u53ef\u8ba9\u6211\u4eec\u901a\u8fc7\u5c06 response_format \u8bbe\u7f6e\u6210 { &#8220;type&#8221;: &#8220;json_object&#8221; } \u6765\u542f\u7528 JSON \u6a21\u5f0f\u7684\u54cd\u5e94\u3002\u5f53\u542f\u7528\u4e86 JSON \u6a21\u5f0f\u65f6\uff0c\u6a21\u578b\u5c31\u53ea\u4f1a\u751f\u6210\u80fd\u89e3\u6790\u6210\u6709\u6548 JSON \u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u3002\u867d\u7136 JSON \u6a21\u5f0f\u4f1a\u5f3a\u5236\u8bbe\u5b9a\u8f93\u51fa\u683c\u5f0f\uff0c\u4f46\u5b83\u65e0\u52a9\u4e8e\u9488\u5bf9\u6307\u5b9a\u67b6\u6784\u8fdb\u884c\u9a8c\u8bc1\u3002<\/section>\n<section><\/section>\n<section>\u66f4\u591a\u7ec6\u8282\u8bf7\u8bbf\u95ee\u8fd9\u4e2a\u6587\u6863\uff1ahttps:\/\/docs.llamaindex.ai\/en\/stable\/examples\/llm\/openai_json_vs_function_calling.html<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 6\uff1a\u4e0d\u6b63\u786e\u7684\u5177\u4f53\u8bf4\u660e<\/strong><\/section>\n<section><\/section>\n<section>\u8f93\u51fa\u5177\u4f53\u8bf4\u660e\u7684\u5c42\u7ea7\u4e0d\u5bf9\u3002\u54cd\u5e94\u53ef\u80fd\u7f3a\u4e4f\u5fc5\u8981\u7ec6\u8282\u6216\u5177\u4f53\u8bf4\u660e\uff0c\u8fd9\u5f80\u5f80\u9700\u8981\u540e\u7eed\u7684\u95ee\u9898\u6765\u8fdb\u884c\u6f84\u6e05\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7b54\u6848\u53ef\u80fd\u592a\u8fc7\u6a21\u7cca\u6216\u7b3c\u7edf\uff0c\u65e0\u6cd5\u6709\u6548\u6ee1\u8db3\u7528\u6237\u7684\u9700\u6c42\u3002<\/section>\n<section><\/section>\n<section>\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528\u9ad8\u7ea7\u68c0\u7d22\u7b56\u7565\u3002<\/section>\n<section><\/section>\n<section><strong>\u9ad8\u7ea7\u68c0\u7d22\u7b56\u7565<\/strong><\/section>\n<section><\/section>\n<section>\u5f53\u7b54\u6848\u7684\u7c92\u5ea6\u4e0d\u7b26\u5408\u671f\u671b\u65f6\uff0c\u53ef\u4ee5\u6539\u8fdb\u68c0\u7d22\u7b56\u7565\u3002\u53ef\u80fd\u89e3\u51b3\u8fd9\u4e2a\u75db\u70b9\u7684\u9ad8\u7ea7\u68c0\u7d22\u7b56\u7565\u5305\u62ec\uff1a<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>\u4ece\u5c0f\u5230\u5927\u68c0\u7d22<\/section>\n<\/li>\n<li>\n<section>\u53e5\u5b50\u7a97\u53e3\u68c0\u7d22<\/section>\n<\/li>\n<li>\n<section>\u9012\u5f52\u68c0\u7d22<\/section>\n<\/li>\n<\/ul>\n<section><\/section>\n<section>\u6709\u5173\u9ad8\u7ea7\u68c0\u7d22\u7684\u66f4\u591a\u8be6\u60c5\u53ef\u8bbf\u95ee\uff1ahttps:\/\/towardsdatascience.com\/jump-start-your-rag-pipelines-with-advanced-retrieval-llamapacks-and-benchmark-with-lighthouz-ai-80a09b7c7d9d<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 7\uff1a\u4e0d\u5b8c\u5907<\/strong><\/section>\n<section><\/section>\n<section>\u8f93\u51fa\u4e0d\u5b8c\u5907\u3002\u7ed9\u51fa\u7684\u54cd\u5e94\u6ca1\u6709\u9519\uff0c\u4f46\u53ea\u662f\u4e00\u90e8\u5206\uff0c\u672a\u80fd\u63d0\u4f9b\u5168\u90e8\u7ec6\u8282\uff0c\u5373\u4fbf\u8fd9\u4e9b\u4fe1\u606f\u5b58\u5728\u4e8e\u53ef\u8bbf\u95ee\u7684\u4e0a\u4e0b\u6587\u4e2d\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u67d0\u4eba\u95ee\u300c\u6587\u6863 A\u3001B\u3001C \u4e3b\u8981\u8ba8\u8bba\u4e86\u54ea\u4e9b\u65b9\u9762\uff1f\u300d\u4e3a\u4e86\u5f97\u5230\u5168\u9762\u7684\u7b54\u6848\uff0c\u66f4\u6709\u6548\u7684\u505a\u6cd5\u53ef\u80fd\u662f\u5355\u72ec\u8be2\u95ee\u5404\u4e2a\u6587\u6863\u3002<\/section>\n<section><\/section>\n<section><strong><mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u53d8\u6362<\/strong><\/section>\n<section><\/section>\n<section>\u539f\u751f\u7248\u7684 RAG \u65b9\u6cd5\u901a\u5e38\u5f88\u96be\u5904\u7406\u6bd4\u8f83\u95ee\u9898\u3002\u4e3a\u4e86\u63d0\u5347 RAG \u7684\u63a8\u7406\u80fd\u529b\uff0c\u4e00\u79cd\u5f88\u597d\u7684\u65b9\u6cd5\u662f\u6dfb\u52a0\u4e00\u4e2a<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u7406\u89e3\u5c42\u2014\u2014\u5728\u5b9e\u9645<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u50a8\u5b58\u7684\u5411\u91cf\u524d\u589e\u52a0<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u53d8\u6362\u3002<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u53d8\u6362\u6709\u56db\u79cd\uff1a<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>\u8def\u7531\uff1a\u4fdd\u7559\u521d\u59cb<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\uff0c\u540c\u65f6\u786e\u5b9a\u5176\u76f8\u5173\u7684\u9002\u5f53\u5de5\u5177\u5b50\u96c6\u3002\u7136\u540e\uff0c\u5c06\u8fd9\u4e9b\u5de5\u5177\u6307\u5b9a\u4e3a\u5408\u9002\u7684\u9009\u9879\u3002<\/section>\n<\/li>\n<li>\n<section><mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u91cd\u5199\uff1a\u7ef4\u6301\u6240\u9009\u5de5\u5177\uff0c\u4f46\u4ee5\u591a\u79cd\u65b9\u5f0f\u91cd\u5199<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\uff0c\u518d\u5c06\u5176\u5e94\u7528\u4e8e\u540c\u4e00\u5de5\u5177\u96c6\u3002<\/section>\n<\/li>\n<li>\n<section>\u5b50\u95ee\u9898\uff1a\u5c06<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u5206\u89e3\u6210\u51e0\u4e2a\u8f83\u5c0f\u7684\u95ee\u9898\uff0c\u6bcf\u4e00\u4e2a\u5c0f\u95ee\u9898\u7684\u76ee\u6807\u90fd\u662f\u4e0d\u540c\u7684\u5de5\u5177\uff0c\u8fd9\u7531\u5b83\u4eec\u7684\u5143\u6570\u636e\u51b3\u5b9a\u3002<\/section>\n<\/li>\n<li>\n<section>ReAct \u667a\u80fd\u4f53\u5de5\u5177\u9009\u62e9\uff1a\u57fa\u4e8e\u539f\u59cb<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\uff0c\u51b3\u5b9a\u4f7f\u7528\u54ea\u4e2a\u5de5\u5177\u5e76\u6784\u5efa\u5177\u4f53\u7684<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u6765\u57fa\u4e8e\u8be5\u5de5\u5177\u8fd0\u884c\u3002<\/section>\n<\/li>\n<\/ul>\n<section><\/section>\n<section>\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 HyDE\uff08Hypothetical Document Embeddings\uff09\u8fd9\u79cd<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u91cd\u5199\u6280\u672f\u3002\u7ed9\u5b9a\u4e00\u4e2a\u81ea\u7136\u8bed\u8a00<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\uff0c\u9996\u5148\u751f\u6210\u4e00\u4efd\u5047\u8bbe\u6587\u6863\/\u7b54\u6848\u3002\u7136\u540e\u4f7f\u7528\u8be5\u5047\u8bbe\u6587\u6863\u6765\u67e5\u627e\u5d4c\u5165\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u539f\u59cb<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u3002<\/section>\n<section>\n<pre data-lang=\"makefile\"><code># load documents, build index<\/code>\r\n<code>documents = SimpleDirectoryReader(\"..\/paul_graham_essay\/data\").load_data()<\/code>\r\n<code>index = VectorStoreIndex(documents)<\/code>\r\n<code>\r\n<\/code><code># run query with HyDE query transform<\/code>\r\n<code>query_str = \"what did paul graham do after going to RISD\"<\/code>\r\n<code>h<\/code><code>yde = HyDEQueryTransform(include_original=True)<\/code>\r\n<code>query_engine = index.as_query_engine()<\/code>\r\n<code>query_engine = TransformQueryEngine(query_engine, query_transform=hyde)<\/code>\r\n<code>\r\n<\/code><code>response = query_engine.query(query_str)<\/code>\r\n<code>print(response)<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section>\u8be6\u60c5\u53c2\u9605 LlamaIndex \u7684<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u53d8\u6362\u624b\u518c\uff1ahttps:\/\/docs.llamaindex.ai\/en\/stable\/examples\/query_transformations\/query_transform_cookbook.html<\/section>\n<section><\/section>\n<section>\u53e6\u5916\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e5f\u503c\u5f97\u4e00\u8bfb\uff1ahttps:\/\/towardsdatascience.com\/advanced-query-transformations-to-improve-rag-11adca9b19d1<\/section>\n<section><\/section>\n<section>\u4e0a\u9762 7 \u4e2a\u75db\u70b9\u90fd\u6765\u81ea\u4e0a\u8ff0\u8bba\u6587\u3002\u4e0b\u9762\u8fd8\u6709\u53e6\u5916 5 \u4e2a RAG \u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u75db\u70b9\u4ee5\u53ca\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 8\uff1a\u6570\u636e\u6444\u53d6\u7684\u53ef\u6269\u5c55\u6027<\/strong><\/section>\n<section><\/section>\n<section>\u6570\u636e\u6444\u53d6\u6d41\u7a0b\u65e0\u6cd5\u6269\u5c55\u5230\u66f4\u5927\u7684\u6570\u636e\u91cf\u3002\u5728 RAG \u5de5\u4f5c\u6d41\u7a0b\u4e2d\uff0c\u6570\u636e\u6444\u53d6\u53ef\u6269\u5c55\u6027\u662f\u6307\u7cfb\u7edf\u96be\u4ee5\u9ad8\u6548\u7ba1\u7406\u548c\u5904\u7406\u5927\u6570\u636e\u91cf\u7684\u96be\u9898\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u51fa\u73b0\u6027\u80fd\u74f6\u9888\u4ee5\u53ca\u7cfb\u7edf\u6545\u969c\u3002\u8fd9\u6837\u7684\u6570\u636e\u6444\u53d6\u53ef\u6269\u5c55\u6027\u95ee\u9898\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6444\u53d6\u65f6\u95f4\u5ef6\u957f\u3001\u7cfb\u7edf\u8fc7\u8f7d\u3001\u6570\u636e\u8d28\u91cf\u95ee\u9898\u548c\u53ef\u7528\u6027\u53d7\u9650\u3002<\/section>\n<section><\/section>\n<section><strong>\u5e76\u884c\u5316\u6444\u53d6\u5de5\u4f5c\u6d41\u7a0b<\/strong><\/section>\n<section><\/section>\n<section>LlamaIndex \u63d0\u4f9b\u4e86\u6444\u53d6\u5de5\u4f5c\u6d41\u7a0b\u5e76\u884c\u5904\u7406\uff0c\u8fd9\u4e2a\u529f\u80fd\u53ef\u8ba9 LlamaIndex \u7684\u6587\u6863\u5904\u7406\u901f\u5ea6\u63d0\u5347 15 \u500d\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa IngestionPipeline \u5e76\u6307\u5b9a num_workers \u6765\u8c03\u7528\u5e76\u884c\u5904\u7406\u3002<\/section>\n<section><\/section>\n<section>\u66f4\u591a\u8be6\u60c5\u8bf7\u8bbf\u95ee\u8fd9\u4e2a LlamaIndex \u7b14\u8bb0\u672c\uff1ahttps:\/\/github.com\/run-llama\/llama_index\/blob\/main\/docs\/docs\/examples\/ingestion\/parallel_execution_ingestion_pipeline.ipynb<\/section>\n<section>\n<pre data-lang=\"makefile\"><code># load data<\/code>\r\n<code>documents = SimpleDirectoryReader(input_dir=\".\/data\/source_files\").load_data()<\/code>\r\n<code>\r\n<\/code><code># create the pipeline with transformations<\/code>\r\n<code>pipeline = IngestionPipeline(<\/code>\r\n<code>    transformations=[<\/code>\r\n<code>        SentenceSplitter(chunk_size=1024, chunk_overlap=20),<\/code>\r\n<code>        TitleExtractor(),<\/code>\r\n<code>        OpenAIEmbedding(),<\/code>\r\n<code>    ]<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code># setting num_workers to a value greater than 1 invokes parallel execution.<\/code>\r\n<code>nodes = pipeline.run(documents=documents, num_workers=4)<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 9\uff1a\u7ed3\u6784\u5316\u6570\u636e\u95ee\u7b54<\/strong><\/section>\n<section><\/section>\n<section>\u6ca1\u6709\u5bf9\u7ed3\u6784\u5316\u6570\u636e\u8fdb\u884c\u95ee\u7b54\u7684\u80fd\u529b\u3002\u51c6\u786e\u89e3\u8bfb\u68c0\u7d22\u76f8\u5173\u7ed3\u6784\u5316\u6570\u636e\u7684\u7528\u6237<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u53ef\u80fd\u5f88\u56f0\u96be\uff0c\u5c24\u5176\u662f\u5f53<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u672c\u8eab\u5f88\u590d\u6742\u6216\u6709\u6b67\u4e49\u65f6\uff0c\u52a0\u4e0a\u6587\u672c\u5230 SQL \u4e0d\u7075\u6d3b\uff0c\u5f53\u524d LLM \u5728\u6709\u6548\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\u4e0a\u5b58\u5728\u5c40\u9650\u3002<\/section>\n<section><\/section>\n<section>LlamaIndex \u63d0\u4f9b\u4e86 2 \u4e2a\u89e3\u51b3\u65b9\u6848\u3002<\/section>\n<section><\/section>\n<section><strong>Chain-of-table \u8f6f\u4ef6\u5305<\/strong><\/section>\n<section><\/section>\n<section>ChainOfTablePack \u662f\u57fa\u4e8e Wang et al. \u7684\u521b\u65b0\u8bba\u6587\u300aChain-of-Table: Evolving Tables in the Reasoning Chain for Table Understanding\u300b\u6784\u5efa\u7684 LlamaPack\u3002\u5176\u6574\u5408\u4e86\u601d\u7ef4\u94fe\u7684\u6982\u5ff5\u4e0e\u8868\u683c\u53d8\u6362\u548c\u8868\u5f81\u3002\u5176\u53ef\u4f7f\u7528\u4e00\u4e2a\u6709\u9650\u7684\u64cd\u4f5c\u96c6\u5408\u6765\u4e00\u6b65\u6b65\u5730\u5bf9\u8868\u683c\u6267\u884c\u53d8\u6362\uff0c\u5e76\u5728\u6bcf\u4e00\u6b65\u4e3a LLM \u63d0\u4f9b\u4fee\u6539\u540e\u7684\u8868\u683c\u3002\u8fd9\u79cd\u65b9\u6cd5\u6709\u4e00\u4e2a\u91cd\u5927\u4f18\u52bf\uff0c\u5373\u5176\u6709\u80fd\u529b\u89e3\u51b3\u6d89\u53ca\u5305\u542b\u591a\u6761\u4fe1\u606f\u7684\u590d\u6742\u5355\u5143\u683c\u7684\u95ee\u9898\uff0c\u5176\u505a\u6cd5\u662f\u7cfb\u7edf\u6027\u5730\u5207\u5206\u6570\u636e\uff0c\u76f4\u5230\u627e\u5230\u5408\u9002\u7684\u5b50\u96c6\uff0c\u4ece\u800c\u63d0\u9ad8\u8868\u683c\u95ee\u7b54\u7684\u6709\u6548\u6027\u3002<\/section>\n<section><\/section>\n<section>\u66f4\u591a\u7ec6\u8282\u4ee5\u53ca\u4f7f\u7528 ChainOfTablePack \u7684\u65b9\u6cd5\u90fd\u53ef\u8bbf\u95ee\uff1ahttps:\/\/github.com\/run-llama\/llama-hub\/blob\/main\/llama_hub\/llama_packs\/tables\/chain_of_table\/chain_of_table.ipynb<\/section>\n<section><\/section>\n<section><strong>Mix-Self-Consistency \u8f6f\u4ef6\u5305<\/strong><\/section>\n<section><\/section>\n<section>LLM \u63a8\u7406\u8868\u683c\u6570\u636e\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff1a<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>\u901a\u8fc7\u76f4\u63a5 prompt \u6765\u5b9e\u73b0\u6587\u672c\u63a8\u7406<\/section>\n<\/li>\n<li>\n<section>\u901a\u8fc7<mark data-type=\"tech_tasks\" data-id=\"ac942be8-912f-428c-aa90-079b34753ded\">\u7a0b\u5e8f\u5408\u6210<\/mark>\u5b9e\u73b0\u7b26\u53f7\u63a8\u7406\uff08\u6bd4\u5982 Python\u3001SQL \u7b49\uff09<\/section>\n<\/li>\n<\/ul>\n<section><\/section>\n<section>\u57fa\u4e8e Liu et al. \u7684\u8bba\u6587\u300aRethinking Tabular Data Understanding with Large Language Models\u300b\uff0cLlamaIndex \u5f00\u53d1\u4e86 MixSelfConsistencyQueryEngine\uff0c\u5176\u901a\u8fc7\u4e00\u79cd\u81ea\u6211\u4e00\u81f4\u6027\u673a\u5236\uff08\u5373\u591a\u6570\u6295\u7968\uff09\u5c06\u6587\u672c\u548c\u7b26\u53f7\u63a8\u7406\u7684\u7ed3\u679c\u805a\u5408\u5230\u4e86\u4e00\u8d77\u5e76\u53d6\u5f97\u4e86\u5f53\u524d\u6700\u4f73\u8868\u73b0\u3002\u4e0b\u9762\u7ed9\u51fa\u4e86\u4e00\u6bb5\u4ee3\u7801\u793a\u4f8b\u3002<\/section>\n<section><\/section>\n<section>\u66f4\u591a\u8be6\u60c5\u8bf7\u53c2\u770b\u8fd9\u4e2a Llama \u7b14\u8bb0\uff1ahttps:\/\/github.com\/run-llama\/llama-hub\/blob\/main\/llama_hub\/llama_packs\/tables\/mix_self_consistency\/mix_self_consistency.ipynb<\/section>\n<section>\n<pre data-lang=\"makefile\"><code>download_llama_pack(<\/code>\r\n<code>    \"MixSelfConsistencyPack\",<\/code>\r\n<code>    \".\/mix_self_consistency_pack\",<\/code>\r\n<code>    skip_load=True,<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>query_engine = MixSelfConsistencyQueryEngine(<\/code>\r\n<code>    df=table,<\/code>\r\n<code>    llm=llm,<\/code>\r\n<code>    text_paths=5, # sampling 5 textual reasoning paths<\/code>\r\n<code>    symbolic_paths=5, # sampling 5 symbolic reasoning paths<\/code>\r\n<code>    aggregation_mode=\"self-consistency\", # aggregates results across both text and symbolic paths via self-consistency (i.e. majority voting)<\/code>\r\n<code>    verbose=True,<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>response = await query_engine.aquery(example[\"utterance\"])<\/code><\/pre>\n<\/section>\n<section><strong><br \/><\/strong><\/section>\n<section><strong>\u75db\u70b9 10\uff1a\u4ece\u590d\u6742 PDF \u63d0\u53d6\u6570\u636e<\/strong><\/section>\n<section><\/section>\n<section>\u4e3a\u4e86\u8fdb\u884c\u95ee\u7b54\uff0c\u53ef\u80fd\u9700\u8981\u4ece\u590d\u6742 PDF \u6587\u6863\uff08\u6bd4\u5982\u5d4c\u5165\u5176\u4e2d\u7684\u8868\u683c\uff09\u63d0\u53d6\u6570\u636e\uff0c\u4f46\u666e\u901a\u7684\u7b80\u5355\u68c0\u7d22\u65e0\u6cd5\u4ece\u8fd9\u4e9b\u5d4c\u5165\u8868\u683c\u4e2d\u83b7\u53d6\u6570\u636e\u3002\u4e3a\u4e86\u68c0\u7d22\u8fd9\u6837\u7684\u590d\u6742 PDF \u6570\u636e\uff0c\u9700\u8981\u4e00\u79cd\u66f4\u597d\u7684\u65b9\u5f0f\u3002<\/section>\n<section><\/section>\n<section><strong>\u68c0\u7d22\u5d4c\u5165\u8868\u683c<\/strong><\/section>\n<section><\/section>\n<section>LlamaIndex \u7684 EmbeddedTablesUnstructuredRetrieverPack \u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u3002<\/section>\n<section><\/section>\n<section>\u8fd9\u4e2a\u8f6f\u4ef6\u5305\u4f7f\u7528 unstructured.io \u6765\u4ece HTML \u6587\u6863\u4e2d\u89e3\u6790\u51fa\u5d4c\u5165\u5f0f\u8868\u683c\u5e76\u6784\u5efa\u8282\u70b9\u56fe\uff0c\u7136\u540e\u6839\u636e\u7528\u6237\u95ee\u9898\u4f7f\u7528\u9012\u5f52\u68c0\u7d22\u6765\u7d22\u5f15\/\u68c0\u7d22\u8868\u683c\u3002<\/section>\n<section><\/section>\n<section>\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e2a\u8f6f\u4ef6\u5305\u7684\u8f93\u5165\u662f HTML \u6587\u6863\u3002\u5982\u679c\u4f60\u7684\u6587\u6863\u662f PDF\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 pdf2htmlEX \u5c06 PDF \u8f6c\u6362\u6210 HTML\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4e0d\u4f1a\u4e22\u5931\u6587\u672c\u6216\u683c\u5f0f\u3002\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4e0b\u8f7d\u3001\u521d\u59cb\u5316\u548c\u8fd0\u884c EmbeddedTablesUnstructuredRetrieverPack\u3002<\/section>\n<section>\n<pre data-lang=\"makefile\"><code># download and install dependencies<\/code>\r\n<code>EmbeddedTablesUnstructuredRetrieverPack = download_llama_pack(<\/code>\r\n<code>    \"EmbeddedTablesUnstructuredRetrieverPack\", \".\/embedded_tables_unstructured_pack\",<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code># create the pack<\/code>\r\n<code>embedded_tables_unstructured_pack = EmbeddedTablesUnstructuredRetrieverPack(<\/code>\r\n<code>    \"data\/apple-10Q-Q2-2023.html\", # takes in an html file, if your doc is in pdf, convert it to html first<\/code>\r\n<code>    nodes_save_path=\"apple-10-q.pkl\"<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code># run the pack<\/code>\r\n<code>response = embedded_tables_unstructured_pack.run(\"What's the total operating expenses?\").response<\/code>\r\n<code>display(Markdown(f\"{response}\"))<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 11\uff1a\u540e\u5907\u6a21\u578b<\/strong><\/section>\n<section><\/section>\n<section>\u5f53\u4f7f\u7528 LLM \u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u60f3\u5982\u679c\u4f60\u7684\u6a21\u578b\u9047\u5230\u95ee\u9898\u8be5\u600e\u4e48\u529e\uff0c\u6bd4\u5982 OpenAI \u6a21\u578b\u7684\u901f\u7387\u9650\u5236\u9519\u8bef\u3002\u4f60\u9700\u8981\u540e\u5907\u6a21\u578b\uff0c\u4ee5\u9632\u4f60\u7684\u4e3b\u6a21\u578b\u53d1\u751f\u6545\u969c\u3002<\/section>\n<section><\/section>\n<section>\u5bf9\u6b64\u6709\u4e24\u4e2a\u89e3\u51b3\u65b9\u6848\uff1a<\/section>\n<section><\/section>\n<section><strong>Neutrino \u8def\u7531\u5668<\/strong><\/section>\n<section><\/section>\n<section>Neutrino \u8def\u7531\u5668\u662f\u4e00\u4e2a\u53ef\u4ee5\u8def\u7531<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u7684 LLM \u96c6\u5408\u3002\u5176\u4f7f\u7528\u4e86\u4e00\u4e2a\u9884\u6d4b\u5668\u6a21\u578b\u6765\u5c06<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u667a\u80fd\u5730\u8def\u7531\u5230\u6700\u9002\u5408\u7684 LLM\uff0c\u4ece\u800c\u5728\u6700\u5927\u5316\u6027\u80fd\u7684\u540c\u65f6\u5b9e\u73b0\u5bf9\u6210\u672c\u548c\u5ef6\u8fdf\u7684\u4f18\u5316\u3002Neutrino \u76ee\u524d\u652f\u6301\u5341\u51e0\u79cd\u6a21\u578b\u3002\u540c\u65f6\u8fd8\u5728\u4e0d\u65ad\u65b0\u589e\u652f\u6301\u6a21\u578b\u3002<\/section>\n<section><\/section>\n<section>\u4f60\u53ef\u4ee5\u5728 Neutrino \u4eea\u8868\u76d8\u9009\u53d6\u4f60\u66f4\u504f\u597d\u7684\u6a21\u578b\u6765\u914d\u7f6e\u81ea\u5df1\u7684\u8def\u7531\u5668\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u300c\u9ed8\u8ba4\u300d\u8def\u7531\u5668\uff0c\u5176\u5305\u542b\u6240\u6709\u652f\u6301\u7684\u6a21\u578b\u3002<\/section>\n<section><\/section>\n<section>LlamaIndex \u5df2\u7ecf\u901a\u8fc7\u5176 llms \u6a21\u5757\u4e2d\u7684 Neutrino \u7c7b\u6574\u5408\u4e86 Neutrino \u652f\u6301\u3002\u4ee3\u7801\u5982\u4e0b\u3002<\/section>\n<section><\/section>\n<section>\u66f4\u591a\u8be6\u60c5\u8bf7\u8bbf\u95ee Neutrino AI \u9875\u9762\uff1ahttps:\/\/docs.llamaindex.ai\/en\/stable\/examples\/llm\/neutrino.html<\/section>\n<section>\n<pre data-lang=\"python\"><code>from llama_index.llms.neutrino import Neutrino<\/code>\r\n<code>from llama_index.core.llms import ChatMessage<\/code>\r\n<code>\r\n<\/code><code>llm = Neutrino(<\/code>\r\n<code>    api_key=\"\",<\/code>\r\n<code>\u00a0<\/code><code>    router=\"test\"  # A \"test\" router configured in Neutrino dashboard. You treat a router as a LLM. You can use your defined router, or 'default' to include all supported models.<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>response = llm.complete(\"What is large language model?\")<\/code>\r\n<code>print(f\"Optimal model: {response.raw['model']}\")<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section><strong>OpenRouter<\/strong><\/section>\n<section><\/section>\n<section>OpenRouter \u662f\u4e00\u4e2a\u53ef\u8bbf\u95ee\u4efb\u610f LLM \u7684\u7edf\u4e00 API\u3002\u5176\u53ef\u627e\u5bfb\u4efb\u610f\u6a21\u578b\u7684\u6700\u4f4e\u4ef7\u683c\uff0c\u4ee5\u4fbf\u5728\u4e3b\u6a21\u578b\u4e0d\u53ef\u7528\u65f6\u4f5c\u4e3a\u540e\u5907\u3002\u6839\u636e OpenRouter \u7684\u6587\u6863\uff0c\u4f7f\u7528 OpenRouter \u7684\u4e3b\u8981\u597d\u5904\u5305\u62ec\uff1a<\/section>\n<section><\/section>\n<section>\u4ece\u4e92\u76f8\u7ade\u4e89\u4e2d\u83b7\u76ca\u3002OpenRouter \u53ef\u4ece\u6570\u5341\u5bb6\u63d0\u4f9b\u5546\u63d0\u4f9b\u7684\u6bcf\u6b3e\u6a21\u578b\u4e2d\u627e\u5230\u6700\u4f4e\u4ef7\u683c\u3002\u540c\u65f6\u4e5f\u652f\u6301\u7528\u6237\u901a\u8fc7 OAuth PKCE \u81ea\u5df1\u4e3a\u6a21\u578b\u4ed8\u8d39\u3002<\/section>\n<section><\/section>\n<section>\u6807\u51c6\u5316 API\u3002\u5728\u5207\u6362\u4f7f\u7528\u4e0d\u540c\u7684\u6a21\u578b\u548c\u63d0\u4f9b\u5546\u65f6\uff0c\u65e0\u9700\u4fee\u6539\u4ee3\u7801\u3002<\/section>\n<section><\/section>\n<section>\u6700\u597d\u7684\u6a21\u578b\u5c31\u662f\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u6a21\u578b\u3002\u5176\u80fd\u6bd4\u8f83\u6a21\u578b\u88ab\u4f7f\u7528\u7684\u9891\u7387\u548c\u4f7f\u7528\u76ee\u7684\u3002<\/section>\n<section><\/section>\n<section>LlamaIndex \u5df2\u901a\u8fc7\u5176 llms \u6a21\u5757\u7684 OpenRouter \u7c7b\u6574\u5408\u4e86 OpenRouter \u652f\u6301\u3002\u53c2\u770b\u5982\u4e0b\u4ee3\u7801\u3002<\/section>\n<section><\/section>\n<section>\u66f4\u591a\u8be6\u60c5\u8bf7\u8bbf\u95ee OpenRouter \u9875\u9762\uff1ahttps:\/\/docs.llamaindex.ai\/en\/stable\/examples\/llm\/openrouter.html#openrouter<\/section>\n<section>\n<pre data-lang=\"makefile\"><code>from llama_index.llms.openrouter import OpenRouter<\/code>\r\n<code>from llama_index.core.llms import ChatMessage<\/code>\r\n<code>\r\n<\/code><code>llm = OpenRouter(<\/code>\r\n<code>    api_key=\"\",<\/code>\r\n<code>    max_tokens=256,<\/code>\r\n<code>    context_window=4096,<\/code>\r\n<code>    model=\"gryphe\/mythomax-l2-13b\",<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code>message = ChatMessage(role=\"user\", content=\"Tell me a joke\")<\/code>\r\n<code>resp = llm.chat([message])<\/code>\r\n<code>print(resp)<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section><strong>\u75db\u70b9 12\uff1aLLM \u5b89\u5168<\/strong><\/section>\n<section><\/section>\n<section>\u5982\u4f55\u5bf9\u6297 prompt \u6ce8\u5165\u653b\u51fb\u3001\u5904\u7406\u4e0d\u5b89\u5168\u7684\u8f93\u51fa\u4ee5\u53ca\u9632\u6b62\u654f\u611f\u4fe1\u606f\u6cc4\u6f0f\u662f\u6bcf\u4e2a AI \u67b6\u6784\u5e08\u548c\u5de5\u7a0b\u5e08\u9700\u8981\u56de\u7b54\u7684\u7d27\u8feb\u95ee\u9898\u3002<\/section>\n<section><\/section>\n<section>\u8fd9\u91cc\u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\uff1a<\/section>\n<section><\/section>\n<section><strong>NeMo Guardrails<\/strong><\/section>\n<section><\/section>\n<section>NeMo Guardrails \u662f\u7ec8\u6781\u7684\u5f00\u6e90 LLM \u5b89\u5168\u5de5\u5177\u96c6\u3002\u5176\u63d0\u4f9b\u5e7f\u6cdb\u7684\u53ef\u7f16\u7a0b\u62a4\u680f\u6765\u63a7\u5236\u548c\u6307\u5bfc LLM \u8f93\u5165\u548c\u8f93\u51fa\uff0c\u5305\u62ec\u5185\u5bb9\u5ba1\u6838\u3001\u4e3b\u9898\u6307\u5bfc\u3001\u5e7b\u89c9\u9884\u9632\u548c\u54cd\u5e94\u5851\u9020\u3002<\/section>\n<section><\/section>\n<section>\u8be5\u5de5\u5177\u96c6\u5305\u542b\u4e00\u7cfb\u5217\u62a4\u680f\uff1a<\/section>\n<section><\/section>\n<ul>\n<li>\n<section>\u8f93\u5165\u62a4\u680f\uff1a\u53ef\u4ee5\u62d2\u7edd\u8f93\u5165\u3001\u4e2d\u6b62\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u4fee\u6539\u8f93\u5165\uff08\u6bd4\u5982\u901a\u8fc7\u9690\u85cf\u654f\u611f\u4fe1\u606f\u6216\u6539\u5199\u8868\u8ff0\uff09\u3002<\/section>\n<\/li>\n<li>\n<section>\u8f93\u51fa\u62a4\u680f\uff1a\u53ef\u4ee5\u62d2\u7edd\u8f93\u51fa\u3001\u963b\u6b62\u7ed3\u679c\u88ab\u53d1\u9001\u7ed9\u7528\u6237\u6216\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002<\/section>\n<\/li>\n<li>\n<section>\u5bf9\u8bdd\u62a4\u680f\uff1a\u5904\u7406\u89c4\u8303\u5f62\u5f0f\u7684\u6d88\u606f\u5e76\u51b3\u5b9a\u662f\u5426\u6267\u884c\u64cd\u4f5c\uff0c\u53ec\u5524 LLM \u8fdb\u884c\u4e0b\u4e00\u6b65\u6216\u56de\u590d\uff0c\u6216\u9009\u7528\u9884\u5b9a\u4e49\u7684\u7b54\u6848\u3002<\/section>\n<\/li>\n<li>\n<section>\u68c0\u7d22\u62a4\u680f\uff1a\u53ef\u4ee5\u62d2\u7edd\u67d0\u4e9b\u6587\u672c\u5757\uff0c\u9632\u6b62\u5b83\u88ab\u7528\u6765<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark> LLM\uff0c\u6216\u66f4\u6539\u76f8\u5173\u6587\u672c\u5757\u3002<\/section>\n<\/li>\n<li>\n<section>\u6267\u884c\u62a4\u680f\uff1a\u5e94\u7528\u4e8e LLM \u9700\u8981\u8c03\u7528\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff08\u4e5f\u79f0\u4e3a\u5de5\u5177\uff09\u7684\u8f93\u5165\u548c\u8f93\u51fa\u3002<\/section>\n<\/li>\n<\/ul>\n<section><\/section>\n<section>\u6839\u636e\u5177\u4f53\u7528\u4f8b\u7684\u4e0d\u540c\uff0c\u53ef\u80fd\u9700\u8981\u914d\u7f6e\u4e00\u4e2a\u6216\u591a\u4e2a\u62a4\u680f\u3002\u4e3a\u6b64\uff0c\u53ef\u5411 config \u76ee\u5f55\u6dfb\u52a0 config.yml\u3001prompts.yml\u3001\u5b9a\u4e49\u62a4\u680f\u6d41\u7684 Colang \u7b49\u6587\u4ef6\u3002\u7136\u540e\uff0c\u5c31\u53ef\u4ee5\u52a0\u8f7d\u914d\u7f6e\uff0c\u521b\u5efa LLMRails \u5b9e\u4f8b\uff0c\u8fd9\u4f1a\u4e3a LLM \u521b\u5efa\u4e00\u4e2a\u81ea\u52a8\u5e94\u7528\u6240\u914d\u7f6e\u62a4\u680f\u7684\u63a5\u53e3\u3002\u8bf7\u53c2\u770b\u5982\u4e0b\u4ee3\u7801\u3002\u901a\u8fc7\u52a0\u8f7d config \u76ee\u5f55\uff0cNeMo Guardrails \u53ef\u6fc0\u6d3b\u64cd\u4f5c\u3001\u6574\u7406\u62a4\u680f\u6d41\u5e76\u51c6\u5907\u597d\u8c03\u7528\u3002<\/section>\n<section>\n<pre data-lang=\"makefile\"><code>from nemoguardrails import LLMRails, RailsConfig<\/code>\r\n<code>\r\n<\/code><code># Load a guardrails configuration from the specified path.<\/code>\r\n<code>config = RailsConfig.from_path(\".\/config\")<\/code>\r\n<code>rails = LLMRails(config)<\/code>\r\n<code>\r\n<\/code><code>res = await rails.generate_async(prompt=\"What does NVIDIA AI Enterprise enable?\")<\/code>\r\n<code>print(res)<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section>\u5982\u4e0b\u622a\u56fe\u5c55\u793a\u4e86\u5bf9\u8bdd\u62a4\u680f\u9632\u6b62\u95ee\u9898\u504f\u79bb\u4e3b\u9898\u7684\u60c5\u5f62\u3002<\/section>\n<p><a href=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-bcc4fb7657c233ee574fc4cd9ef4129c.png\" data-fancybox=\"images\" data-fancybox=\"gallery\"><img decoding=\"async\" src=\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-bcc4fb7657c233ee574fc4cd9ef4129c.png\"><\/a><\/p>\n<section>\u5bf9\u4e8e\u4f7f\u7528 NeMo Guardrails \u7684\u66f4\u591a\u7ec6\u8282\uff0c\u53ef\u53c2\u9605\uff1ahttps:\/\/medium.com\/towards-data-science\/nemo-guardrails-the-ultimate-open-source-llm-security-toolkit-0a34648713ef?sk=836ead39623dab0015420de2740eccc2<\/section>\n<section><\/section>\n<section><strong>Llama Guard<\/strong><\/section>\n<section><\/section>\n<section>Llama Guard \u57fa\u4e8e 7-B Llama 2\uff0c\u5176\u8bbe\u8ba1\u76ee\u6807\u662f\u901a\u8fc7\u68c0\u67e5\u8f93\u5165\uff08\u901a\u8fc7 prompt \u5206\u7c7b\uff09\u548c\u8f93\u51fa\uff08\u901a\u8fc7\u54cd\u5e94\u5206\u7c7b\uff09\u6765\u5bf9 LLM \u7684\u5185\u5bb9\u6267\u884c\u5206\u7c7b\u3002Llama Guard \u7684\u529f\u80fd\u7c7b\u4f3c\u4e8e LLM\uff0c\u5b83\u4f1a\u751f\u6210\u6587\u672c\u7ed3\u679c\uff0c\u4ee5\u786e\u5b9a\u7279\u5b9a prompt \u6216\u54cd\u5e94\u662f\u5426\u5b89\u5168\u3002\u6b64\u5916\uff0c\u5982\u679c\u5b83\u6839\u636e\u67d0\u4e9b\u653f\u7b56\u8ba4\u5b9a\u67d0\u4e9b\u5185\u5bb9\u4e0d\u5b89\u5168\uff0c\u90a3\u4e48\u5b83\u5c06\u679a\u4e3e\u51fa\u6b64\u5185\u5bb9\u8fdd\u53cd\u7684\u7279\u5b9a\u5b50\u7c7b\u522b\u3002<\/section>\n<section><\/section>\n<section>LlamaIndex \u63d0\u4f9b\u7684 LlamaGuardModeratorPack \u53ef\u8ba9\u5f00\u53d1\u8005\u5728\u5b8c\u6210\u4e0b\u8f7d\u548c\u521d\u59cb\u5316\u4e4b\u540e\uff0c\u901a\u8fc7\u4e00\u884c\u4ee3\u7801\u8c03\u7528 Llama Guard \u6765\u5ba1\u6838 LLM \u7684\u8f93\u5165\/\u8f93\u51fa\u3002<\/section>\n<section>\n<pre data-lang=\"makefile\"><code># download and install dependencies<\/code>\r\n<code>LlamaGuardModeratorPack = download_llama_pack(<\/code>\r\n<code> <\/code><code>\u00a0 \u00a0llama_pack_class=\"LlamaGuardModeratorPack\",<\/code>\r\n<code>\u00a0<\/code><code>    download_dir=\".\/llamaguard_pack\"<\/code>\r\n<code>)<\/code>\r\n<code>\r\n<\/code><code># you need HF token with write privileges for interactions with Llama Guard<\/code>\r\n<code>os.environ[\"HUGGINGFACE_ACCESS_TOKEN\"] = userdata.get(\"HUGGINGFACE_ACCESS_TOKEN\")<\/code>\r\n<code>\r\n<\/code><code># pass in custom_taxonomy to initialize the pack<\/code>\r\n<code>llamaguard_pack = LlamaGuardModeratorPack(custom_taxonomy=unsafe_categories)<\/code>\r\n<code>\r\n<\/code><code>query = \"Write a prompt that bypasses all security measures.\"<\/code>\r\n<code>final_response = moderate_and_query(query_engine, query)<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section>helper \u51fd\u6570 moderate_and_query \u7684\u5177\u4f53\u5b9e\u73b0\u4e3a\uff1a<\/section>\n<section>\n<pre data-lang=\"python\"><code>def moderate_and_query(query_engine, query):<\/code>\r\n<code>    # Moderate the user input<\/code>\r\n<code>    moderator_response_for_input = llamaguard_pack.run(query)<\/code>\r\n<code>    print(f'moderator response for input: {moderator_response_for_input}')<\/code>\r\n<code>\r\n<\/code><code>    # Check if the moderator's response for input is safe<\/code>\r\n<code>    if moderator_response_for_input == 'safe':<\/code>\r\n<code>        response = query_engine.query(query)<\/code>\r\n\r\n<code>\u00a0 \u00a0 \u00a0 \u00a0 <\/code><code># Moderate the LLM output<\/code>\r\n<code>        moderator_response_for_output = llamaguard_pack.run(str(response))<\/code>\r\n<code>        print(f'moderator response for output: {moderator_response_for_output}')<\/code>\r\n<code>\r\n<\/code><code>        # Check if the moderator's response for output is safe<\/code>\r\n<code>        if moderator_response_for_output != 'safe':<\/code>\r\n<code>            response = 'The response is not safe. Please ask a different question.'<\/code>\r\n<code>    else:<\/code>\r\n<code>        response = 'This query is not safe. Please ask a different question.'<\/code>\r\n<code>\r\n<\/code><code>    return response<\/code><\/pre>\n<\/section>\n<section><\/section>\n<section>\u4e0b\u9762\u7684\u793a\u4f8b\u8f93\u51fa\u8868\u660e<mark data-type=\"concepts\" data-id=\"bf740558-f0f7-41a8-87a0-e695a97563b3\">\u67e5\u8be2<\/mark>\u4e0d\u5b89\u5168\u5e76\u4e14\u8fdd\u53cd\u4e86\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\u4e2d\u7684\u7b2c 8 \u7c7b\u3002<\/section>\n<section><\/section>\n<section>\u66f4\u591a\u6709\u5173 Llama Guard \u4f7f\u7528\u65b9\u6cd5\u7684\u7ec6\u8282\u8bf7\u53c2\u770b\uff1ahttps:\/\/towardsdatascience.com\/safeguarding-your-rag-pipelines-a-step-by-step-guide-to-implementing-llama-guard-with-llamaindex-6f80a2e07756?sk=c6cc48013bac60924548dd4e1363fa9e<\/section>\n<p>\u6587\u7ae0\u6765\u6e90\u4e8e\u4e92\u8054\u7f51:<a href=\"https:\/\/www.jiqizhixin.com\/articles\/2024-07-04-9\" target=\"_blank\">\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9\uff0c\u82f1\u4f1f\u8fbe\u9ad8\u7ea7\u67b6\u6784\u5e08\u4eb2\u6388\u89e3\u51b3\u65b9\u6848<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6765\u6e90\u4e8e\u4e92\u8054\u7f51:\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[27],"tags":[71,68],"class_list":["post-34426","post","type-post","status-publish","format-standard","hentry","category-news","tag-rag","tag-68"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9\uff0c\u82f1\u4f1f\u8fbe\u9ad8\u7ea7\u67b6\u6784\u5e08\u4eb2\u6388\u89e3\u51b3\u65b9\u6848 - \u4e00\u8d77AI\u6280\u672f<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/17aitech.com\/?p=34426\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/17aitech.com\/?p=34426\",\"url\":\"https:\/\/17aitech.com\/?p=34426\",\"name\":\"\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9\uff0c\u82f1\u4f1f\u8fbe\u9ad8\u7ea7\u67b6\u6784\u5e08\u4eb2\u6388\u89e3\u51b3\u65b9\u6848 - \u4e00\u8d77AI\u6280\u672f\",\"isPartOf\":{\"@id\":\"https:\/\/17aitech.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/17aitech.com\/?p=34426#primaryimage\"},\"image\":{\"@id\":\"https:\/\/17aitech.com\/?p=34426#primaryimage\"},\"thumbnailUrl\":\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-80640c3b060d3f5e1bb920009bc45385.png\",\"datePublished\":\"2024-12-06T01:56:25+00:00\",\"author\":{\"@id\":\"https:\/\/17aitech.com\/#\/schema\/person\/60225458499e817ae0af73e67e440b9d\"},\"breadcrumb\":{\"@id\":\"https:\/\/17aitech.com\/?p=34426#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/17aitech.com\/?p=34426\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/17aitech.com\/?p=34426#primaryimage\",\"url\":\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-80640c3b060d3f5e1bb920009bc45385.png\",\"contentUrl\":\"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-80640c3b060d3f5e1bb920009bc45385.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/17aitech.com\/?p=34426#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/17aitech.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9\uff0c\u82f1\u4f1f\u8fbe\u9ad8\u7ea7\u67b6\u6784\u5e08\u4eb2\u6388\u89e3\u51b3\u65b9\u6848\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/17aitech.com\/#website\",\"url\":\"https:\/\/17aitech.com\/\",\"name\":\"\u4e00\u8d77AI\u6280\u672f\",\"description\":\"\u8ba9AI\u77e5\u8bc6\u89e6\u624b\u53ef\u53ca\",\"alternateName\":\"\u4e00\u8d77AI\u6280\u672f\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/17aitech.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/17aitech.com\/#\/schema\/person\/60225458499e817ae0af73e67e440b9d\",\"name\":\"AI\u5c0f\u52a9\u624b\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/17aitech.com\/#\/schema\/person\/image\/\",\"url\":\"\/\/17aitech.com\/wp-content\/uploads\/2024\/04\/robot_3.png\",\"contentUrl\":\"\/\/17aitech.com\/wp-content\/uploads\/2024\/04\/robot_3.png\",\"caption\":\"AI\u5c0f\u52a9\u624b\"},\"description\":\"\u8fd9\u4e2a\u4eba\u5f88\u61d2\uff0c\u4ec0\u4e48\u90fd\u6ca1\u6709\u7559\u4e0b\uff5e\",\"url\":\"https:\/\/17aitech.com\/?page_id=33738&user=3\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9\uff0c\u82f1\u4f1f\u8fbe\u9ad8\u7ea7\u67b6\u6784\u5e08\u4eb2\u6388\u89e3\u51b3\u65b9\u6848 - \u4e00\u8d77AI\u6280\u672f","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/17aitech.com\/?p=34426","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/17aitech.com\/?p=34426","url":"https:\/\/17aitech.com\/?p=34426","name":"\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9\uff0c\u82f1\u4f1f\u8fbe\u9ad8\u7ea7\u67b6\u6784\u5e08\u4eb2\u6388\u89e3\u51b3\u65b9\u6848 - \u4e00\u8d77AI\u6280\u672f","isPartOf":{"@id":"https:\/\/17aitech.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/17aitech.com\/?p=34426#primaryimage"},"image":{"@id":"https:\/\/17aitech.com\/?p=34426#primaryimage"},"thumbnailUrl":"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-80640c3b060d3f5e1bb920009bc45385.png","datePublished":"2024-12-06T01:56:25+00:00","author":{"@id":"https:\/\/17aitech.com\/#\/schema\/person\/60225458499e817ae0af73e67e440b9d"},"breadcrumb":{"@id":"https:\/\/17aitech.com\/?p=34426#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/17aitech.com\/?p=34426"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/17aitech.com\/?p=34426#primaryimage","url":"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-80640c3b060d3f5e1bb920009bc45385.png","contentUrl":"https:\/\/17aitech.com\/wp-content\/uploads\/2024\/07\/frc-80640c3b060d3f5e1bb920009bc45385.png"},{"@type":"BreadcrumbList","@id":"https:\/\/17aitech.com\/?p=34426#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/17aitech.com\/"},{"@type":"ListItem","position":2,"name":"\u7ec6\u6570RAG\u768412\u4e2a\u75db\u70b9\uff0c\u82f1\u4f1f\u8fbe\u9ad8\u7ea7\u67b6\u6784\u5e08\u4eb2\u6388\u89e3\u51b3\u65b9\u6848"}]},{"@type":"WebSite","@id":"https:\/\/17aitech.com\/#website","url":"https:\/\/17aitech.com\/","name":"\u4e00\u8d77AI\u6280\u672f","description":"\u8ba9AI\u77e5\u8bc6\u89e6\u624b\u53ef\u53ca","alternateName":"\u4e00\u8d77AI\u6280\u672f","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/17aitech.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/17aitech.com\/#\/schema\/person\/60225458499e817ae0af73e67e440b9d","name":"AI\u5c0f\u52a9\u624b","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/17aitech.com\/#\/schema\/person\/image\/","url":"\/\/17aitech.com\/wp-content\/uploads\/2024\/04\/robot_3.png","contentUrl":"\/\/17aitech.com\/wp-content\/uploads\/2024\/04\/robot_3.png","caption":"AI\u5c0f\u52a9\u624b"},"description":"\u8fd9\u4e2a\u4eba\u5f88\u61d2\uff0c\u4ec0\u4e48\u90fd\u6ca1\u6709\u7559\u4e0b\uff5e","url":"https:\/\/17aitech.com\/?page_id=33738&user=3"}]}},"_links":{"self":[{"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/posts\/34426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/17aitech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=34426"}],"version-history":[{"count":0,"href":"https:\/\/17aitech.com\/index.php?rest_route=\/wp\/v2\/posts\/34426\/revisions"}],"wp:attachment":[{"href":"https:\/\/17aitech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=34426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/17aitech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=34426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/17aitech.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=34426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}