{"id":3619,"date":"2024-05-07T16:59:22","date_gmt":"2024-05-07T08:59:22","guid":{"rendered":"https:\/\/www.aqwu.net\/wp\/?p=3619"},"modified":"2024-05-07T17:35:38","modified_gmt":"2024-05-07T09:35:38","slug":"%e4%b8%80%e7%a7%8d%e5%b8%b8%e8%a7%81%e7%9a%84rag%ef%bc%88retrieval-augmented-generation%ef%bc%89%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=3619","title":{"rendered":"\u4e00\u79cd\u5e38\u89c1\u7684RAG\uff08Retrieval-Augmented Generation\uff09\u7684\u5b9e\u73b0\u65b9\u5f0f"},"content":{"rendered":"\n<p>\u4e00\u79cd\u5e38\u89c1\u7684RAG\uff08Retrieval-Augmented Generation\uff09\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5176\u4e2d\u5229\u7528\u4e00\u4e2a\u8f83\u5c0f\u7684\u6a21\u578b\u6765\u5feb\u901f\u751f\u6210\u6587\u672c\u7684\u5d4c\u5165\uff0c\u7528\u4e8e\u68c0\u7d22\uff0c\u7136\u540e\u4f7f\u7528\u4e00\u4e2a\u8f83\u5927\u7684\u6a21\u578b\u6765\u8fdb\u884c\u6df1\u5165\u7684\u751f\u6210\u6216\u63a8\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4fdd\u6301\u54cd\u5e94\u901f\u5ea6\u7684\u540c\u65f6\u63d0\u9ad8\u751f\u6210\u5185\u5bb9\u7684\u8d28\u91cf\u548c\u76f8\u5173\u6027\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u5982\u4f55\u5b9e\u65bd\u8fd9\u4e00\u7b56\u7565\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u521b\u5efa\u5d4c\u5165<\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u4f7f\u7528\u4e00\u4e2a\u8f83\u5c0f\u7684\u6a21\u578b\uff0c\u5982<code>distilBERT<\/code>\uff0c<code>MiniLM<\/code>\u7b49\uff0c\u8fd9\u4e9b\u6a21\u578b\u5728\u4fdd\u6301\u8f83\u597d\u6027\u80fd\u7684\u540c\u65f6\u8ba1\u7b97\u5f00\u9500\u8f83\u4f4e\uff0c\u9002\u5408\u7528\u4e8e\u5d4c\u5165\u751f\u6210\u3002\u8fd9\u4e9b\u5d4c\u5165\u5c06\u7528\u4e8e\u5feb\u901f\u68c0\u7d22\u6587\u672c\u5185\u5bb9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5efa\u7acb\u68c0\u7d22\u7cfb\u7edf<\/h3>\n\n\n\n<p>\u4f7f\u7528\u751f\u6210\u7684\u5d4c\u5165\u5efa\u7acb\u4e00\u4e2a\u68c0\u7d22\u7cfb\u7edf\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982FAISS\u8fd9\u6837\u7684\u5e93\u6765\u5b58\u50a8\u548c\u7d22\u5f15\u5d4c\u5165\uff0c\u4ee5\u4fbf\u4e8e\u5feb\u901f\u68c0\u7d22\u6700\u76f8\u5173\u7684\u6587\u6863\u7247\u6bb5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528\u5927\u6a21\u578b\u8fdb\u884c\u751f\u6210<\/h3>\n\n\n\n<p>\u5728\u68c0\u7d22\u5230\u4e0e\u67e5\u8be2\u6700\u76f8\u5173\u7684\u6587\u672c\u540e\uff0c\u5c06\u8fd9\u4e9b\u6587\u672c\u4e0e\u7528\u6237\u7684\u67e5\u8be2\u4e00\u8d77\u4f5c\u4e3a\u8f93\u5165\uff0c\u4f20\u9012\u7ed9\u4e00\u4e2a\u8f83\u5927\u7684\u3001\u529f\u80fd\u66f4\u5f3a\u5927\u7684\u6a21\u578b\uff0c\u5982<code>BERT-large<\/code>\u3001<code>GPT-3<\/code>\u7b49\uff0c\u4ee5\u751f\u6210\u8be6\u7ec6\u7684\u56de\u7b54\u6216\u8fdb\u884c\u6df1\u5165\u7684\u5206\u6790\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5316\u7684Python\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4e24\u4e2a\u4e0d\u540c\u5927\u5c0f\u7684\u6a21\u578b\u6765\u5b9e\u65bd\u8fd9\u79cd\u7b56\u7565\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >import torch\nfrom transformers import AutoTokenizer, AutoModel, BertModel\nfrom sentence_transformers import SentenceTransformer\nimport faiss\nimport numpy as np\n\n# \u5c0f\u6a21\u578b\u7528\u4e8e\u521b\u5efa\u5d4c\u5165\nembedder = SentenceTransformer('all-MiniLM-L6-v2')\n\n# \u5927\u6a21\u578b\u7528\u4e8e\u751f\u6210\ntokenizer = AutoTokenizer.from_pretrained('bert-large-uncased')\nmodel = BertModel.from_pretrained('bert-large-uncased')\n\n# \u793a\u4f8b\u6570\u636e\uff08\u5e94\u66ff\u6362\u4e3a\u5b9e\u9645\u7684PDF\u6587\u672c\u63d0\u53d6\u7ed3\u679c\uff09\ndocuments = [\"This is a sample document.\", \"This is another example document.\"]\n\n# \u521b\u5efa\u6587\u6863\u5d4c\u5165\nembeddings = embedder.encode(documents)\n\n# \u5efa\u7acbFAISS\u7d22\u5f15\nindex = faiss.IndexFlatL2(embeddings.shape[1])\nindex.add(embeddings.astype('float32'))\n\n# \u7528\u6237\u67e5\u8be2\nquery = \"I need information about sample.\"\n\n# \u67e5\u8be2\u5d4c\u5165\nquery_embedding = embedder.encode([query])[0].astype('float32')\n\n# \u68c0\u7d22\u6700\u76f8\u5173\u7684\u6587\u6863\nD, I = index.search(np.array([query_embedding]), k=1)\nretrieved_doc = documents[I[0][0]]\n\n# \u4f7f\u7528\u5927\u6a21\u578b\u8fdb\u884c\u63a8\u7406\ninputs = tokenizer(retrieved_doc + \" \" + query, return_tensors='pt', truncation=True, padding='max_length', max_length=512)\nwith torch.no_grad():\n    outputs = model(**inputs)\n    response_embedding = outputs.last_hidden_state.mean(dim=1)\n\n# \u6839\u636eresponse_embedding\u751f\u6210\u6700\u7ec8\u8f93\u51fa\uff08\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\uff09\n\n<\/pre><\/div>\n\n\n\n<p>\u8fd9\u4e2a\u4ee3\u7801\u9996\u5148\u4f7f\u7528\u4e00\u4e2a\u5c0f\u6a21\u578b\u6765\u521b\u5efa\u6587\u6863\u5d4c\u5165\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u5d4c\u5165\u5b58\u50a8\u5728FAISS\u7d22\u5f15\u4e2d\u3002\u5f53\u7528\u6237\u53d1\u51fa\u67e5\u8be2\u65f6\uff0c\u8be5\u67e5\u8be2\u88ab\u540c\u6837\u8f6c\u6362\u6210\u5d4c\u5165\uff0c\u5e76\u5728FAISS\u4e2d\u68c0\u7d22\u51fa\u6700\u76f8\u5173\u7684\u6587\u6863\u3002\u6700\u540e\uff0c\u5c06\u68c0\u7d22\u5230\u7684\u6587\u6863\u4e0e\u67e5\u8be2\u4e00\u8d77\u9001\u5230\u4e00\u4e2a\u5927\u6a21\u578b\u4e2d\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u5b83\u7ed3\u5408\u4e86\u4e24\u79cd\u6a21\u578b\u7684\u4f18\u52bf\uff1a\u5c0f\u6a21\u578b\u7684\u901f\u5ea6\u548c\u5927\u6a21\u578b\u7684\u6df1\u5ea6\u7406\u89e3\u80fd\u529b\u3002\u6b64\u5916\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u4e00\u6b65\u8c03\u6574\u6a21\u578b\u9009\u62e9\u548c\u5904\u7406\u6d41\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u79cd\u5e38\u89c1\u7684RAG\uff08Retrieval-Augmented Generation\uff09\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5176\u4e2d\u5229\u7528\u4e00\u4e2a\u8f83\u5c0f\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","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":"set","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":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[444,443,442],"tags":[422],"class_list":["post-3619","post","type-post","status-publish","format-standard","hentry","category-ai","category-llm","category-llms","tag-rag"],"views":1865,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3619"}],"version-history":[{"count":2,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3619\/revisions"}],"predecessor-version":[{"id":3621,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3619\/revisions\/3621"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}