{"id":23,"date":"2026-11-08T13:15:57","date_gmt":"2026-11-08T13:15:57","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=23"},"modified":"2026-11-08T13:15:57","modified_gmt":"2026-11-08T13:15:57","slug":"rag-%e5%85%a5%e9%97%a8%ef%bc%9a%e5%90%91%e9%87%8f%e6%a3%80%e7%b4%a2-%e5%a4%a7%e6%a8%a1%e5%9e%8b%e9%97%ae%e7%ad%94%e6%b5%81%e6%b0%b4%e7%ba%bf","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=23","title":{"rendered":"RAG \u5165\u95e8\uff1a\u5411\u91cf\u68c0\u7d22 + \u5927\u6a21\u578b\u95ee\u7b54\u6d41\u6c34\u7ebf"},"content":{"rendered":"<p>\u5927\u6a21\u578b\u4e0d\u4f1a\u81ea\u52a8\u77e5\u9053\u4f60\u7684\u8bfe\u7a0b\u6587\u6863\u548c\u5185\u90e8 wiki\u3002<strong>RAG\uff08\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff09<\/strong>\u7684\u601d\u8def\u662f\uff1a\u5148\u68c0\u7d22\u76f8\u5173\u7247\u6bb5\uff0c\u518d\u8ba9\u6a21\u578b\u57fa\u4e8e\u7247\u6bb5\u56de\u7b54\uff0c\u4ece\u800c\u964d\u4f4e\u5e7b\u89c9\u3001\u53ef\u8ffd\u6eaf\u6765\u6e90\u3002<\/p>\n<h2>\u6d41\u6c34\u7ebf\u4e94\u6b65<\/h2>\n<ol>\n<li>\u6587\u6863\u5207\u5206\uff08chunk\uff09<\/li>\n<li>\u5411\u91cf\u5316\uff08embedding\uff09<\/li>\n<li>\u5199\u5165\u5411\u91cf\u5e93<\/li>\n<li>\u7528\u6237\u63d0\u95ee \u2192 \u68c0\u7d22 Top-K<\/li>\n<li>\u628a\u7247\u6bb5\u585e\u8fdb Prompt \u751f\u6210\u7b54\u6848<\/li>\n<\/ol>\n<h2>\u6700\u5c0f\u903b\u8f91\u4f2a\u4ee3\u7801<\/h2>\n<pre><code>chunks = split_docs(docs, size=500, overlap=80)\nvectors = embed(chunks)\nindex.upsert(vectors)\n\ndef answer(question: str) -&gt; str:\n    q_vec = embed([question])[0]\n    hits = index.search(q_vec, top_k=5)\n    context = \"\\n\\n\".join(h.text for h in hits)\n    prompt = f\"\u57fa\u4e8e\u4ee5\u4e0b\u8d44\u6599\u56de\u7b54\uff0c\u82e5\u8d44\u6599\u4e0d\u8db3\u8bf7\u8bf4\u4e0d\u77e5\u9053\uff1a\\n{context}\\n\\n\u95ee\u9898\uff1a{question}\"\n    return chat([{\"role\": \"user\", \"content\": prompt}])\n<\/code><\/pre>\n<h2>\u6548\u679c\u5173\u952e\u70b9<\/h2>\n<ul>\n<li>\u5207\u5206\u592a\u5927\uff1a\u68c0\u7d22\u4e0d\u51c6\uff1b\u592a\u5c0f\uff1a\u7f3a\u4e0a\u4e0b\u6587<\/li>\n<li>Top-K \u4e0d\u662f\u8d8a\u5927\u8d8a\u597d\uff0c\u566a\u58f0\u4f1a\u5e72\u6270\u56de\u7b54<\/li>\n<li>\u56de\u7b54\u65f6\u8981\u6c42\u300c\u5f15\u7528\u8d44\u6599\u7f16\u53f7\u300d\uff0c\u4fbf\u4e8e\u4eba\u5de5\u6838\u5bf9<\/li>\n<\/ul>\n<h2>\u4ec0\u4e48\u65f6\u5019\u4e0a RAG<\/h2>\n<ul>\n<li>\u77e5\u8bc6\u7ecf\u5e38\u66f4\u65b0\uff08\u8bfe\u7a0b\u3001\u516c\u544a\u3001\u6587\u6863\uff09<\/li>\n<li>\u9700\u8981\u53ef\u8ffd\u6eaf\uff0c\u4e0d\u80fd\u7eaf\u9760\u6a21\u578b\u8bb0\u5fc6<\/li>\n<li>\u5782\u76f4\u9886\u57df\u672f\u8bed\u591a<\/li>\n<\/ul>\n<p>\u5148\u7528\u672c\u5730\u51e0\u7bc7 Markdown \u8dd1\u901a\u95ed\u73af\uff0c\u518d\u4e0a\u751f\u4ea7\u5411\u91cf\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u6a21\u578b\u4e0d\u4f1a\u81ea\u52a8\u77e5\u9053\u4f60\u7684\u8bfe\u7a0b\u6587\u6863\u548c\u5185\u90e8 wiki\u3002RAG\uff08\u68c0\u7d22\u589e\u5f3a\u751f\u6210\uff09\u7684\u601d\u8def\u662f\uff1a\u5148\u68c0\u7d22\u76f8\u5173\u7247\u6bb5\uff0c\u518d\u8ba9\u6a21\u578b\u57fa\u4e8e\u7247 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-llm"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}