{"id":1376,"date":"2024-05-07T17:31:31","date_gmt":"2024-05-07T17:31:31","guid":{"rendered":"https:\/\/www.nicekj.com\/?p=1376"},"modified":"2024-05-07T17:31:49","modified_gmt":"2024-05-07T17:31:49","slug":"ruhebimiandayuyanmoxingraoguozhishikuluandadeqingkuangllamaindex-yuanai-2","status":"publish","type":"post","link":"https:\/\/www.nicekj.com\/ruhebimiandayuyanmoxingraoguozhishikuluandadeqingkuangllamaindex-yuanai-2.html","title":{"rendered":"\u5982\u4f55\u907f\u514d\u5927\u8bed\u8a00\u6a21\u578b\u7ed5\u8fc7\u77e5\u8bc6\u5e93\u4e71\u7b54\u7684\u60c5\u51b5\uff1fLlamaIndex \u539f\u7406\u4e0e\u5e94\u7528\u7b80\u4ecb"},"content":{"rendered":"<p><strong>\u672c\u6587\u9996\u53d1\u4e8e\u535a\u5ba2 <a href=\"https:\/\/link.juejin.cn?target=https%3A%2F%2Fliduos.com%2Fcategories%2FLLM-%25E5%25BA%2594%25E7%2594%25A8%25E5%25BC%2580%25E5%258F%2591%25E5%25AE%259E%25E8%25B7%25B5%2F\" target=\"_blank\" title=\"https:\/\/liduos.com\/categories\/LLM-%E5%BA%94%E7%94%A8%E5%BC%80%E5%8F%91%E5%AE%9E%E8%B7%B5\/\" ref=\"nofollow noopener noreferrer\" rel=\"noopener\">LLM \u5e94\u7528\u5f00\u53d1\u5b9e\u8df5<\/a><\/strong><\/p>\n<blockquote>\n<p>\u968f\u7740 LangChain + LLM \u65b9\u6848\u5feb\u901f\u666e\u53ca\uff0c\u77e5\u8bc6\u95ee\u7b54\u7c7b\u5e94\u7528\u7684\u5f00\u53d1\u53d8\u5f97\u5bb9\u6613\uff0c\u4f46\u662f\u9762\u5bf9\u56de\u7b54\u51c6\u786e\u5ea6\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\uff0c\u5219\u66b4\u9732\u51fa\u4e00\u4e9b\u5c40\u9650\u6027\uff0c\u6bd4\u5982\u5411\u91cf\u67e5\u8be2\u65b9\u5f0f\u5f97\u5230\u7684\u5185\u5bb9\u4e0d\u5339\u914d\uff0cLLM \u5bf9\u610f\u56fe\u8bc6\u522b\u4e0d\u51c6\u3002\u6240\u4ee5 LlamaIndex(\u4e5f\u79f0\u4e3a GPT Index)\u7531\u793e\u533a\u987a\u52bf\u63a8\u51fa\uff0c\u662f\u4e00\u4e2a\u5f00\u53d1\u8005\u53cb\u597d\u7684\u63a5\u53e3\uff0c\u5b83\u5c06\u5916\u90e8\u6570\u636e\u8fde\u63a5\u5230 LLM\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5de5\u5177\u6765\u7b80\u5316\u6d41\u7a0b\uff0c\u5305\u62ec\u53ef\u4ee5\u4e0e\u5404\u79cd\u73b0\u6709\u6570\u636e\u6e90\u548c\u683c\u5f0f(\u5982 api\u3001pdf\u3001\u6587\u6863\u548c SQL \u7b49)\u96c6\u6210\u7684\u6570\u636e\u8fde\u63a5\u5668\uff0c\u6781\u5927\u7684\u6539\u5584\u4e86\u4e0a\u8ff0\u95ee\u9898\u3002\u672c\u6587\u8bd1\u81ea <a href=\"https:\/\/link.juejin.cn?target=https%3A%2F%2Fbetterprogramming.pub%2Fllamaindex-how-to-use-index-correctly\" target=\"_blank\" title=\"https:\/\/betterprogramming.pub\/llamaindex-how-to-use-index-correctly\" ref=\"nofollow noopener noreferrer\" rel=\"noopener\">LlamaIndex: How to Use Index Correctly<\/a>\uff0c\u7ffb\u8bd1\u5185\u5bb9\u5e76\u975e\u539f\u7248\u65e0\u5bf9\u7167\u7ffb\u8bd1\uff0c\u6709\u6240\u589e\u51cf\u3002<\/p>\n<\/blockquote>\n<h2 data-id=\"heading-0\">LlamaIndex\u7d22\u5f15<\/h2>\n<h3 data-id=\"heading-1\">\u5217\u8868\u7d22\u5f15<\/h3>\n<blockquote>\n<p>\u5b83\u5bf9\u4e8e\u7efc\u5408\u8de8\u591a\u4e2a\u6570\u636e\u6e90\u7684\u4fe1\u606f\u7684\u7b54\u6848\u975e\u5e38\u6709\u7528<\/p>\n<\/blockquote>\n<p>\u5217\u8868\u7d22\u5f15\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u8282\u70b9\u6309\u987a\u5e8f\u5b58\u50a8\u3002\u5728\u7d22\u5f15\u6784\u5efa\u671f\u95f4\uff0c\u6587\u6863\u6587\u672c\u88ab\u5206\u5757\u3001\u8f6c\u6362\u4e3a\u8282\u70b9\u5e76\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002<\/p>\n<p>\u5728\u67e5\u8be2\u671f\u95f4\uff0c\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u5176\u4ed6\u67e5\u8be2\u53c2\u6570\uff0cLlamaIndex\u53ea\u662f\u5c06\u5217\u8868\u4e2d\u7684\u6240\u6709node\u52a0\u8f7d\u5230Response Synthesis\u6a21\u5757\u4e2d\u3002<\/p>\n<p>\u5217\u8868\u7d22\u5f15\u63d0\u4f9b\u4e86\u8bb8\u591a\u67e5\u8be2\u5217\u8868\u7d22\u5f15\u7684\u65b9\u6cd5\uff0c\u4ece\u57fa\u4e8e\u5d4c\u5165\u7684\u67e5\u8be2\u4e2d\u83b7\u53d6\u524dk\u4e2a\u90bb\u5c45\uff0c\u6216\u8005\u6dfb\u52a0\u4e00\u4e2a\u5173\u952e\u5b57\u8fc7\u6ee4\u5668\uff0c\u5982\u4e0b\u6240\u793a:<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/417ff63715888e917385c7fc3636ca99.png\" alt=\"img\" \/><\/figure>\n<\/p>\n<p>img<\/p>\n<p>LlamaIndex\u4e3a\u5217\u8868\u7d22\u5f15\u63d0\u4f9bEmbedding\u652f\u6301\u3002\u9664\u4e86\u6bcf\u4e2a\u8282\u70b9\u5b58\u50a8\u6587\u672c\u4e4b\u5916\uff0c\u6bcf\u4e2a\u8282\u70b9\u8fd8\u53ef\u4ee5\u9009\u62e9\u5b58\u50a8Embedding\u3002\u5728\u67e5\u8be2\u671f\u95f4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8c03\u7528LLM\u5408\u6210\u7b54\u6848\u4e4b\u524d\uff0c\u4f7f\u7528Embeddings\u5bf9\u8282\u70b9\u8fdb\u884c\u6700\u5927\u76f8\u4f3c\u5ea6\u68c0\u7d22\u3002<\/p>\n<p>\u5728<strong>\u7d22\u5f15\u6784\u5efa\u8fc7\u7a0b\u4e2d\uff0cLlamaIndex\u4e0d\u4f1a\u751f\u6210Embedding\uff0c\u800c\u662f\u5728\u67e5\u8be2\u65f6\u751f\u6210<\/strong>\uff0c\u8fd9\u79cd\u8bbe\u8ba1\u907f\u514d\u4e86\u5728\u7d22\u5f15\u6784\u5efa\u671f\u95f4\u4e3a\u6240\u6709\u6587\u672c\u5757\u751f\u6210Embeddings\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5927\u91cf\u6570\u636e\u7684\u5f00\u9500\u3002<\/p>\n<h3 data-id=\"heading-2\">\u5411\u91cf\u5b58\u50a8\u7d22\u5f15<\/h3>\n<blockquote>\n<p>\u5b83\u662f\u6700\u5e38\u89c1\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\uff0c\u5141\u8bb8\u5bf9\u5927\u578b\u6570\u636e\u8bed\u6599\u5e93\u56de\u7b54\u67e5\u8be2<\/p>\n<\/blockquote>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>GPTVectorStoreIndex<\/code>\u4f7f\u7528\u5185\u5b58\u4e2d\u7684<code>SimpleVectorStore<\/code>\u4f5c\u4e3a\u9ed8\u8ba4\u5b58\u50a8\u4e0a\u4e0b\u6587\u7684\u4e00\u90e8\u5206\u521d\u59cb\u5316\uff0c<strong>\u57fa\u4e8e\u5411\u91cf\u5b58\u50a8\u7684\u7d22\u5f15\u5728\u7d22\u5f15\u6784\u5efa\u671f\u95f4\u751f\u6210Embeddings<\/strong><\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/1da1bff993e173b70d8f74eba7067825.png\" alt=\"img\" \/><\/figure>\n<\/p>\n<p>img<\/p>\n<h3 data-id=\"heading-3\">\u6811\u72b6\u7d22\u5f15<\/h3>\n<blockquote>\n<p>\u5b83\u5bf9\u603b\u7ed3\u4e00\u7ec4\u6587\u4ef6\u5f88\u6709\u7528<\/p>\n<\/blockquote>\n<p>\u6811\u72b6\u7d22\u5f15\u662f\u6811\u7ed3\u6784\u7d22\u5f15\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u662f\u5b50\u8282\u70b9\u7684\u6458\u8981\u3002\u5728\u7d22\u5f15\u6784\u5efa\u671f\u95f4\uff0c\u6811\u4ee5\u81ea\u4e0b\u800c\u4e0a\u7684\u65b9\u5f0f\u6784\u5efa\uff0c\u76f4\u5230\u6211\u4eec\u6700\u7ec8\u5f97\u5230\u4e00\u7ec4\u6839\u8282\u70b9\u3002\u6811\u72b6\u7d22\u5f15\u4ece\u4e00\u7ec4\u8282\u70b9(\u6210\u4e3a\u8be5\u6811\u4e2d\u7684\u53f6\u8282\u70b9)\u6784\u5efa\u5c42\u6b21\u6811\u3002<\/p>\n<p>\u67e5\u8be2\u6811\u72b6\u7d22\u5f15\u6d89\u53ca\u4ece\u6839\u8282\u70b9\u5411\u4e0b\u904d\u5386\u5230\u53f6\u8282\u70b9\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b(<code>child_branch_factor=1<\/code>)\uff0c\u67e5\u8be2\u5728\u7ed9\u5b9a\u7236\u8282\u70b9\u7684\u60c5\u51b5\u4e0b\u9009\u62e9\u4e00\u4e2a\u5b50\u8282\u70b9\u3002\u5982\u679c<code>child_branch_factor=2<\/code>\uff0c\u5219\u67e5\u8be2\u5728\u6bcf\u4e2a\u7ea7\u522b\u9009\u62e9\u4e24\u4e2a\u5b50\u8282\u70b9\u3002<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/56f2c22f8ec8c0e2d35d604b25297880.png\" alt=\"img\" \/><\/figure>\n<\/p>\n<p>img<\/p>\n<p>\u4e3a\u4e86\u5728\u67e5\u8be2\u671f\u95f4\u6784\u5efa\u6811\u72b6\u7d22\u5f15\uff0c\u6211\u4eec\u9700\u8981\u5c06<strong>retriver_mode<\/strong>\u548c<strong>response_mode<\/strong>\u6dfb\u52a0\u5230\u67e5\u8be2\u5f15\u64ce\uff0c\u5e76\u5c06GPTTreeIndex\u4e2d\u7684build_tree\u53c2\u6570\u8bbe\u7f6e\u4e3aFalse<\/p>\n<pre><\/div><div class=\"code-block-extension-headerRight\"><span class=\"code-block-extension-lang\">ini<\/span><div class=\"code-block-extension-copyCodeBtn\">\u590d\u5236\u4ee3\u7801<\/div><\/div><\/div><code class=\"hljs language-ini code-block-extension-codeShowNum\" lang=\"ini\"><span class=\"code-block-extension-codeLine\" data-line-num=\"1\"><span class=\"hljs-attr\">index_light<\/span> = GPTTreeIndex.from_documents(documents, build_tree=<span class=\"hljs-literal\">False<\/span>)<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"2\"><span class=\"hljs-attr\">query_engine<\/span> = index_light.as_query_engine(<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"3\">    <span class=\"hljs-attr\">retriever_mode<\/span>=<span class=\"hljs-string\">\"all_leaf\"<\/span>,<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"4\">    <span class=\"hljs-attr\">response_mode<\/span>=<span class=\"hljs-string\">'tree_summarize'<\/span>,<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"5\">)<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"6\">query_engine.query(\"What is net operating income?\")<\/span>\n<\/code><\/pre>\n<h3 data-id=\"heading-4\">\u5173\u952e\u8bcd\u8868\u7d22\u5f15<\/h3>\n<blockquote>\n<p>\u4ed6\u5bf9\u4e8e\u5c06\u67e5\u8be2\u8def\u7531\u5230\u4e0d\u540c\u7684\u6570\u636e\u6e90\u975e\u5e38\u6709\u7528<\/p>\n<\/blockquote>\n<p>\u5173\u952e\u5b57\u8868\u7d22\u5f15\u4ece\u6bcf\u4e2aNode\u63d0\u53d6\u5173\u952e\u5b57\uff0c\u5e76\u6784\u5efa\u4ece\u6bcf\u4e2a\u5173\u952e\u5b57\u5230\u8be5\u5173\u952e\u5b57\u5bf9\u5e94\u7684Node\u7684\u6620\u5c04\u3002<\/p>\n<p>\u5728\u67e5\u8be2\u65f6\uff0c\u4ece\u67e5\u8be2\u4e2d\u63d0\u53d6\u76f8\u5173\u5173\u952e\u5b57\uff0c\u5e76\u5c06\u5176\u4e0e\u9884\u63d0\u53d6\u7684Node\u5173\u952e\u5b57\u8fdb\u884c\u5339\u914d\uff0c\u83b7\u53d6\u76f8\u5e94\u7684Node\u3002\u63d0\u53d6\u7684\u8282\u70b9\u88ab\u4f20\u9012\u5230\u54cd\u5e94\u5408\u6210\u6a21\u5757\u3002<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/580bacdfc978de6550aab1433220fd3b.png\" alt=\"img\" \/><\/figure>\n<\/p>\n<p>img<\/p>\n<ul>\n<li>\n<p><strong><code>GPTKeywordTableIndex<\/code><\/strong>&#8211; \u4f7f\u7528LLM\u4ece\u6bcf\u4e2a\u6587\u6863\u4e2d\u63d0\u53d6\u5173\u952e\u5b57\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u786e\u5b9e\u9700\u8981\u5728\u6784\u5efa\u671f\u95f4\u8c03\u7528LLM<\/p>\n<\/li>\n<li>\n<p><strong><code>GPTSimpleKeywordTableIndex<\/code><\/strong>&#8211; \u4f7f\u7528regex\u5173\u952e\u5b57\u63d0\u53d6\u5668\u4ece\u6bcf\u4e2a\u6587\u6863\u4e2d\u63d0\u53d6\u5173\u952e\u5b57\uff0c\u5728\u6784\u5efa\u671f\u95f4\u4e0d\u4f1a\u8c03\u7528LLM<\/p>\n<\/li>\n<\/ul>\n<h3 data-id=\"heading-5\">\u53ef\u7ec4\u5408\u6027\u56fe\u7d22\u5f15<\/h3>\n<blockquote>\n<p>\u5b83\u5bf9\u4e8e\u6784\u5efa\u77e5\u8bc6\u56fe\u8c31\u5f88\u6709\u7528<\/p>\n<\/blockquote>\n<p>\u4f7f\u7528LlamaIndex\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u73b0\u6709\u7d22\u5f15\u4e4b\u4e0a\u6784\u5efa\u7d22\u5f15\u6765\u521b\u5efa\u590d\u5408\u7d22\u5f15\u3002\u8be5\u7279\u6027\u4f7f\u60a8\u80fd\u591f\u6709\u6548\u5730\u7d22\u5f15\u5b8c\u6574\u7684\u6587\u6863\u5c42\u6b21\u7ed3\u6784\uff0c\u5e76\u4e3aGPT\u63d0\u4f9b\u91cf\u8eab\u5b9a\u5236\u7684\u77e5\u8bc6\u3002<\/p>\n<p>\u901a\u8fc7\u5229\u7528\u53ef\u7ec4\u5408\u6027\uff0c\u53ef\u4ee5\u5728\u591a\u4e2a\u7ea7\u522b\u5b9a\u4e49\u7d22\u5f15\uff0c\u4f8b\u5982\u4e3a\u5355\u4e2a\u6587\u6863\u5b9a\u4e49\u4f4e\u7ea7\u7d22\u5f15\uff0c\u4e3a\u6587\u6863\u7ec4\u5b9a\u4e49\u9ad8\u7ea7\u7d22\u5f15\u3002\u8003\u8651\u4e0b\u9762\u7684\u4f8b\u5b50:<\/p>\n<ul>\n<li>\n<p>\u53ef\u4ee5\u4e3a\u6bcf\u4e2a\u6587\u6863\u4e2d\u7684\u6587\u672c\u521b\u5efa\u6811\u7d22\u5f15\u3002<\/p>\n<\/li>\n<li>\n<p>\u751f\u6210\u4e00\u4e2a\u5217\u8868\u7d22\u5f15\uff0c\u6db5\u76d6\u6240\u6709\u7684\u6811\u7d22\u5f15\u4e3a\u6574\u4e2a\u6587\u6863\u96c6\u5408\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4e00\u4e2a\u573a\u666f\u6765\u6f14\u793a\u53ef\u7ec4\u5408\u6027\u56fe\u7d22\u5f15\u7684\u80fd\u529b:<\/p>\n<ul>\n<li>\n<p>\u4ece\u591a\u4e2a\u6587\u6863\u521b\u5efa\u6811\u7d22\u5f15<\/p>\n<\/li>\n<li>\n<p>\u4ece\u6811\u7d22\u5f15\u751f\u6210\u6458\u8981<\/p>\n<\/li>\n<li>\n<p>\u63a5\u4e0b\u6765\u57283\u4e2a\u6811\u7d22\u5f15\u7684\u9876\u90e8\u521b\u5efa\u4e00\u4e2a\u5217\u8868\u7d22\u5f15\u7684\u56fe\uff0c\u56e0\u4e3a\u5217\u8868\u7d22\u5f15\u9002\u5408\u4e8e\u5408\u6210\u8de8\u591a\u4e2a\u6570\u636e\u6e90\u7ec4\u5408\u4fe1\u606f\u7684\u7b54\u6848<\/p>\n<\/li>\n<li>\n<p>\u6700\u540e\u67e5\u8be2\u56fe<\/p>\n<p>from llama_index.indices.composability import ComposableGraph<\/p>\n<p>from langchain.chat_models import ChatOpenAI\nfrom llama_index import LLMPredictor<\/p>\n<h1 data-id=\"heading-6\">setting up vector indicies for each year<\/h1>\n<p>service_context = ServiceContext.from_defaults(chunk_size_limit=512)\nindex_set = {}\nfor year in years:\nstorage_context = StorageContext.from_defaults()\ncur_index = GPTVectorStoreIndex.from_documents(\ndocuments=doc_set[year],\nservice_context=service_context,\nstorage_context=storage_context\n)\nindex_set[year] = cur_index\n# store index in the local env, so you don&#8217;t need to do it over again\nstorage_context.persist(f&#8217;.\/storage_index\/apple-10k\/{year}&#8217;)<\/p>\n<h1 data-id=\"heading-7\">define an LLMPredictor set number of output tokens<\/h1>\n<p>llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0, max_tokens=512, model_name=&#8217;gpt-3.5-turbo&#8217;))\nservice_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)\nstorage_context = StorageContext.from_defaults()<\/p>\n<h2 data-id=\"heading-8\">define a list index over the vector indicies<\/h2>\n<h2 data-id=\"heading-9\">allow us to synthesize information across  each index<\/h2>\n<p>index_summary = [index_set[year].as_query_engine().query(&#8220;Summary this document in 100 words&#8221;).response for year in years]\ngraph = ComposableGraph.from_indices(\nGPTListIndex,\n[index_set[y] for y in years],\nindex_summaries=index_summary,\nservice_context=service_context,\nstorage_context=storage_context\n)<\/p>\n<p>root_id = graph.root_id<\/p>\n<p>#save to disk\nstorage_context.persist(f&#8217;.\/storage_index\/apple-10k\/root&#8217;)<\/p>\n<h2 data-id=\"heading-10\">querying graph<\/h2>\n<p>custom_query_engines = {\nindex_set[year].index_id: index_set[year].as_query_engine() for year in years\n}<\/p>\n<p>query_engine = graph.as_query_engine(\ncustom_query_engines=custom_query_engines\n)<\/p>\n<p>response = query_engine.query(&#8220;Outline the financial statement of Q2 2023&#8221;)\nresponse.response<\/p>\n<\/li>\n<\/ul>\n<h3 data-id=\"heading-11\">Pandas\u7d22\u5f15\u548cSQL\u7d22\u5f15<\/h3>\n<blockquote>\n<p>\u5b83\u5bf9\u7ed3\u6784\u5316\u6570\u636e\u5f88\u6709\u7528<\/p>\n<\/blockquote>\n<pre><\/div><div class=\"code-block-extension-headerRight\"><span class=\"code-block-extension-lang\">ini<\/span><div class=\"code-block-extension-copyCodeBtn\">\u590d\u5236\u4ee3\u7801<\/div><\/div><\/div><code class=\"hljs language-ini code-block-extension-codeShowNum\" lang=\"ini\"><span class=\"code-block-extension-codeLine\" data-line-num=\"1\">from llama_index.indices.struct_store import GPTPandasIndex<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"2\">import pandas as pd<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"3\"><\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"4\"><span class=\"hljs-attr\">df<\/span> = pd.read_csv(<span class=\"hljs-string\">\"titanic_train.csv\"<\/span>)<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"5\"><\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"6\"><span class=\"hljs-attr\">index<\/span> = GPTPandasIndex(df=df)<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"7\"><\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"8\"><span class=\"hljs-attr\">query_engine<\/span> = index.as_query_engine(<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"9\">    <span class=\"hljs-attr\">verbose<\/span>=<span class=\"hljs-literal\">True<\/span><\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"10\">)<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"11\"><span class=\"hljs-attr\">response<\/span> = query_engine.query(<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"12\">    \"What is the correlation between survival and age?\",<\/span>\n<span class=\"code-block-extension-codeLine\" data-line-num=\"13\">)<\/span>\n<\/code><\/pre>\n<h3 data-id=\"heading-12\">\u6587\u6863\u6458\u8981\u7d22\u5f15<\/h3>\n<blockquote>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5168\u65b0\u7684LlamaIndex\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u662f\u4e3a\u4e86\u95ee\u7b54\u800c\u5236\u4f5c\u7684\u3002<\/p>\n<\/blockquote>\n<p>\u901a\u5e38\uff0c\u5927\u591a\u6570\u7528\u6237\u4ee5\u4ee5\u4e0b\u65b9\u5f0f\u5f00\u53d1\u57fa\u4e8eLLM\u7684QA\u7cfb\u7edf:<\/p>\n<ol>\n<li>\n<p>\u83b7\u53d6\u6e90\u6587\u6863\u5e76\u5c06\u5176\u5206\u6210\u6587\u672c\u5757\u3002<\/p>\n<\/li>\n<li>\n<p>\u7136\u540e\u5c06\u6587\u672c\u5757\u5b58\u50a8\u5728\u77e2\u91cf\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u67e5\u8be2\u671f\u95f4\uff0c\u901a\u8fc7\u4f7f\u7528\u76f8\u4f3c\u5ea6\u548c\/\u6216\u5173\u952e\u5b57\u8fc7\u6ee4\u5668\u8fdb\u884cEmbedding\u6765\u68c0\u7d22\u6587\u672c\u5757\u3002<\/p>\n<\/li>\n<li>\n<p>\u6267\u884c\u6574\u5408\u540e\u7684\u54cd\u5e94\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5b58\u5728\u4e00\u4e9b\u5f71\u54cd\u68c0\u7d22\u6027\u80fd\u7684\u5c40\u9650\u6027\uff1a<\/p>\n<ol>\n<li>\n<p>\u6587\u672c\u5757\u6ca1\u6709\u5b8c\u6574\u7684\u5168\u5c40\u4e0a\u4e0b\u6587\uff0c\u8fd9\u901a\u5e38\u9650\u5236\u4e86\u95ee\u7b54\u8fc7\u7a0b\u7684\u6709\u6548\u6027\u3002<\/p>\n<\/li>\n<li>\n<p>\u9700\u8981\u4ed4\u7ec6\u8c03\u4f18top-k \/\u76f8\u4f3c\u6027\u5206\u6570\u9608\u503c\uff0c\u56e0\u4e3a\u8fc7\u5c0f\u7684\u503c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u9519\u8fc7\u76f8\u5173\u4e0a\u4e0b\u6587\uff0c\u800c\u8fc7\u5927\u7684\u503c\u53ef\u80fd\u4f1a\u589e\u52a0\u4e0d\u76f8\u5173\u4e0a\u4e0b\u6587\u7684\u6210\u672c\u548c\u5ef6\u8fdf\u3002<\/p>\n<\/li>\n<li>\n<p>Embeddings\u53ef\u80fd\u5e76\u4e0d\u603b\u662f\u4e3a\u4e00\u4e2a\u95ee\u9898\u9009\u62e9\u6700\u5408\u9002\u7684\u4e0a\u4e0b\u6587\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u8fc7\u7a0b\u672c\u8d28\u4e0a\u662f\u5206\u522b\u51b3\u5b9a\u6587\u672c\u548c\u4e0a\u4e0b\u6587\u7684\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4e3a\u4e86\u589e\u5f3a\u68c0\u7d22\u7ed3\u679c\uff0c\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u6dfb\u52a0\u4e86\u5173\u952e\u5b57\u8fc7\u6ee4\u5668\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u6709\u5176\u81ea\u8eab\u7684\u6311\u6218\uff0c\u4f8b\u5982\u901a\u8fc7\u624b\u5de5\u6216\u4f7f\u7528NLP\u5173\u952e\u5b57\u63d0\u53d6\/\u4e3b\u9898\u6807\u8bb0\u6a21\u578b\u4e3a\u6bcf\u4e2a\u6587\u6863\u786e\u5b9a\u9002\u5f53\u7684\u5173\u952e\u5b57\uff0c\u4ee5\u53ca\u4ece\u67e5\u8be2\u4e2d\u63a8\u65ad\u6b63\u786e\u7684\u5173\u952e\u5b57\u3002<\/p>\n<p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.nicekj.com\/wp-content\/uploads\/replace\/931562cc9ec156c9eabf2a052ea3fc1b.png\" alt=\"img\" \/><\/figure>\n<\/p>\n<p>img<\/p>\n<p>\u8fd9\u5c31\u662f LlamaIndex \u5f15\u5165\u6587\u6863\u6458\u8981\u7d22\u5f15\u7684\u539f\u56e0\uff0c\u5b83\u53ef\u4ee5\u4e3a\u6bcf\u4efd\u6587\u6863\u63d0\u53d6\u975e\u7ed3\u6784\u5316\u6587\u672c\u6458\u8981\u5e76\u7f16\u5236\u7d22\u5f15\uff0c\u4ece\u800c\u63d0\u9ad8\u68c0\u7d22\u6027\u80fd\uff0c\u8d85\u8d8a\u73b0\u6709\u65b9\u6cd5\u3002\u8be5\u7d22\u5f15\u6bd4\u5355\u4e00\u6587\u672c\u5757\u5305\u542b\u66f4\u591a\u4fe1\u606f\uff0c\u6bd4\u5173\u952e\u5b57\u6807\u7b7e\u5177\u6709\u66f4\u591a\u8bed\u4e49\u3002\u5b83\u8fd8\u5141\u8bb8\u7075\u6d3b\u7684\u68c0\u7d22\uff0c\u5305\u62ec\u57fa\u4e8e LLM \u548c\u5d4c\u5165\u7684\u65b9\u6cd5\u3002\u5728\u6784\u5efa\u671f\u95f4\uff0c\u8be5\u7d22\u5f15\u63a5\u6536\u6587\u6863\u5e76\u4f7f\u7528 LLM \u4ece\u6bcf\u4e2a\u6587\u6863\u4e2d\u63d0\u53d6\u6458\u8981\u3002\u5728\u67e5\u8be2\u65f6\uff0c\u5b83\u4f1a\u6839\u636e\u6458\u8981\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u68c0\u7d22\u76f8\u5173\u6587\u6863\uff1a<\/p>\n<ul>\n<li>\n<p>\u57fa\u4e8e LLM \u7684\u68c0\u7d22\uff1a\u83b7\u53d6\u6587\u6863\u6458\u8981\u96c6\u5408\u5e76\u8bf7\u6c42 LLM \u8bc6\u522b\u76f8\u5173\u6587\u6863+\u76f8\u5173\u6027\u5f97\u5206<\/p>\n<\/li>\n<li>\n<p>\u57fa\u4e8e\u5d4c\u5165\u7684\u68c0\u7d22\uff1a\u5229\u7528\u6458\u8981\u5d4c\u5165\u76f8\u4f3c\u6027\u6765\u68c0\u7d22\u76f8\u5173\u6587\u6863\uff0c\u5e76\u5bf9\u68c0\u7d22\u7ed3\u679c\u7684\u6570\u91cf\u65bd\u52a0\u9876k\u9650\u5236\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6587\u6863\u6458\u8981\u7d22\u5f15\u7684\u68c0\u7d22\u7c7b\u4e3a\u4efb\u4f55\u9009\u5b9a\u7684\u6587\u6863\u68c0\u7d22\u6240\u6709\u8282\u70b9\uff0c\u800c\u4e0d\u662f\u5728\u8282\u70b9\u7ea7\u8fd4\u56de\u76f8\u5173\u5757\u3002<\/p>\n<h3 data-id=\"heading-13\">\u77e5\u8bc6\u56fe\u8c31\u7d22\u5f15<\/h3>\n<p>\u5b83\u901a\u8fc7\u5728\u4e00\u7ec4\u6587\u6863\u4e2d\u63d0\u53d6\u77e5\u8bc6\u4e09\u5143\u7ec4\uff08\u4e3b\u8bed\u3001\u8c13\u8bed\u3001\u5bbe\u8bed\uff09\u6765\u5efa\u7acb\u7d22\u5f15\u3002 \u5728\u67e5\u8be2\u65f6\uff0c\u5b83\u65e2\u53ef\u4ee5\u53ea\u4f7f\u7528\u77e5\u8bc6\u56fe\u8c31\u4f5c\u4e3a\u4e0a\u4e0b\u6587\u8fdb\u884c\u67e5\u8be2\uff0c\u4e5f\u53ef\u4ee5\u5229\u7528\u6bcf\u4e2a\u5b9e\u4f53\u7684\u5e95\u5c42\u6587\u672c\u4f5c\u4e3a\u4e0a\u4e0b\u6587\u8fdb\u884c\u67e5\u8be2\u3002\u901a\u8fc7\u5229\u7528\u5e95\u5c42\u6587\u672c\uff0c\u6211\u4eec\u53ef\u4ee5\u9488\u5bf9\u6587\u6863\u5185\u5bb9\u63d0\u51fa\u66f4\u590d\u6742\u7684\u67e5\u8be2\u3002<\/p>\n<h3 data-id=\"heading-14\">\u7d22\u5f15\u6210\u672c\u548c\u7d22\u5f15\u65f6\u95f4<\/h3>\n<ul>\n<li>\n<p>\u7d22\u5f15\u7684\u6210\u672c\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u91cd\u8981\u56e0\u7d20\uff0c\u5728\u5904\u7406\u6d77\u91cf\u6570\u636e\u96c6\u65f6\uff0c\u8fd9\u4e00\u70b9\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/li>\n<li>\n<p>\u7d22\u5f15\u901f\u5ea6\uff0c\u6574\u4e2a\u89e3\u51b3\u65b9\u6848\u7684\u8fd0\u884c\u51c6\u5907\u65f6\u95f4\u3002\u7d22\u5f15\u65f6\u95f4\u5404\u4e0d\u76f8\u540c\uff0c\u4f46\u90fd\u662f\u4e00\u6b21\u6027\u7684\uff0c\u901a\u5e38 40 \u9875\u7684 PDF \u6587\u4ef6\u5927\u7ea6\u9700\u8981 5 \u79d2\u949f\u3002\u8bd5\u60f3\u4e00\u4e0b\uff0c\u5982\u679c\u4e00\u4e2a\u5e9e\u5927\u7684\u6570\u636e\u96c6\u8d85\u8fc7 10 \u4e07\u9875\uff0c\u53ef\u80fd\u9700\u8981\u51e0\u5929\u65f6\u95f4\uff0c\u53ef\u4ee5\u5229\u7528\u5f02\u6b65\u65b9\u6cd5\u6765\u7f29\u77ed\u7d22\u5f15\u65f6\u95f4\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u66f4\u591a\u5185\u5bb9\u5728\u516c\u53f7\uff1a<a href=\"https:\/\/link.juejin.cn?target=https%3A%2F%2Fliduos.com%2Fwxqcode.png\" target=\"_blank\" title=\"https:\/\/liduos.com\/wxqcode.png\" ref=\"nofollow noopener noreferrer\" rel=\"noopener\">LLM \u5e94\u7528\u5168\u6808\u5f00\u53d1<\/a><\/strong><\/p>","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u9996\u53d1\u4e8e\u535a\u5ba2 LLM \u5e94\u7528\u5f00\u53d1\u5b9e\u8df5 LlamaIndex\u7d22\u5f15 \u5217\u8868\u7d22\u5f15 \u5217\u8868\u7d22\u5f15\u662f\u4e00\u79cd\u7b80\u5355\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u8282\u70b9\u6309\u987a\u5e8f\u5b58\u50a8\u3002\u5728\u7d22\u5f15\u6784\u5efa\u671f\u95f4\uff0c\u6587\u6863\u6587\u672c\u88ab\u5206\u5757\u3001\u8f6c\u6362\u4e3a\u8282\u70b9\u5e76\u5b58\u50a8\u5728\u5217\u8868\u4e2d\u3002 \u5728\u67e5\u8be2\u671f\u95f4\uff0c\u5982\u679c<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_title":"","rank_math_description":"","rank_math_focus_keyword":"","views":"1","footnotes":""},"categories":[3],"tags":[126,127,128,129,136],"collection":[],"class_list":["post-1376","post","type-post","status-publish","format-standard","hentry","category-fenlei2","tag-gpt","tag-ai","tag-128","tag-129","tag-136"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/posts\/1376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/comments?post=1376"}],"version-history":[{"count":0,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/posts\/1376\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/media?parent=1376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/categories?post=1376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/tags?post=1376"},{"taxonomy":"collection","embeddable":true,"href":"https:\/\/www.nicekj.com\/nicekj2024\/wp\/v2\/collection?post=1376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}