{"id":47963,"date":"2025-07-30T10:07:38","date_gmt":"2025-07-30T02:07:38","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/47963.html"},"modified":"2025-07-30T10:07:38","modified_gmt":"2025-07-30T02:07:38","slug":"%e4%b8%93%e4%b8%9a%e7%ba%a7rag%e7%b3%bb%e7%bb%9f%e8%ae%be%e8%ae%a1%e4%b8%8e%e5%ae%9e%e7%8e%b0%ef%bc%9a%e9%ab%98%e5%8f%ac%e5%9b%9e%e5%8f%af%e6%ba%af%e6%ba%90%e7%9a%84%e5%a4%9a%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/47963.html","title":{"rendered":"\u4e13\u4e1a\u7ea7RAG\u7cfb\u7edf\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u9ad8\u53ec\u56de\u53ef\u6eaf\u6e90\u7684\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<h2>\u5b8c\u6574\u8be6\u7ec6\u7684\u9700\u6c42\u63cf\u8ff0<\/h2>\n<h3>\u6838\u5fc3\u529f\u80fd\u9700\u6c42<\/h3>\n<p>\u4f5c\u4e3a\u4e00\u4e2a\u4e13\u4e1a\u7684\u5927\u6a21\u578b\u67b6\u6784\u5e08&#xff0c;\u6211\u9700\u8981\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u6ee1\u8db3\u4ee5\u4e0b\u6838\u5fc3\u9700\u6c42\u7684RAG(Retrieval-Augmented Generation)\u7cfb\u7edf&#xff1a;<\/p>\n<p>\u591a\u683c\u5f0f\u6587\u6863\u652f\u6301&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7cfb\u7edf\u5fc5\u987b\u80fd\u591f\u5904\u7406PDF\u548cWord(.docx)\u683c\u5f0f\u7684\u6587\u6863<\/p>\n<\/li>\n<li>\n<p>\u672a\u6765\u53ef\u6269\u5c55\u652f\u6301\u5176\u4ed6\u683c\u5f0f(\u5982PPT\u3001Excel\u7b49)<\/p>\n<\/li>\n<\/ul>\n<p>\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u7ba1\u7406&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u652f\u6301\u7528\u6237\u63d0\u4ea4\u591a\u4e2a\u4e0d\u540c\u6587\u6863\u6784\u5efa\u77e5\u8bc6\u5e93<\/p>\n<\/li>\n<li>\n<p>\u6bcf\u4e2a\u6587\u6863\u5e94\u6709\u72ec\u7acb\u7684\u5143\u6570\u636e\u7ba1\u7406(\u5982\u6587\u6863\u540d\u79f0\u3001\u4e0a\u4f20\u65f6\u95f4\u3001\u6587\u6863\u7c7b\u578b\u7b49)<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u77e5\u8bc6\u5e93\u7684\u589e\u91cf\u66f4\u65b0&#xff0c;\u4e0d\u8981\u6c42\u5168\u91cf\u91cd\u5efa<\/p>\n<\/li>\n<\/ul>\n<p>\u9ad8\u53ec\u56de\u7387\u68c0\u7d22&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u68c0\u7d22\u7ed3\u679c\u5fc5\u987b\u8986\u76d6\u6587\u6863\u4e2d\u6240\u6709\u76f8\u5173\u5185\u5bb9<\/p>\n<\/li>\n<li>\n<p>\u91c7\u7528\u591a\u7ea7\u68c0\u7d22\u7b56\u7565\u786e\u4fdd\u4e0d\u9057\u6f0f\u6f5c\u5728\u76f8\u5173\u7247\u6bb5<\/p>\n<\/li>\n<li>\n<p>\u68c0\u7d22\u7ed3\u679c\u5e94\u6309\u7167\u76f8\u5173\u6027\u6392\u5e8f<\/p>\n<\/li>\n<\/ul>\n<p>\u4e25\u683c\u7684\u53ef\u6eaf\u6e90\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7cfb\u7edf\u751f\u6210\u7684\u6bcf\u4e2a\u56de\u7b54\u5fc5\u987b\u80fd\u591f\u8ffd\u6eaf\u5230\u6e90\u6587\u6863\u7684\u5177\u4f53\u7247\u6bb5<\/p>\n<\/li>\n<li>\n<p>\u5c55\u793a\u5f15\u7528\u65f6\u5e94\u5305\u542b\u6587\u6863\u540d\u79f0\u548c\u5177\u4f53\u4f4d\u7f6e\u4fe1\u606f<\/p>\n<\/li>\n<li>\n<p>\u7981\u6b62\u65e0\u6e90\u751f\u6210(hallucination)&#xff0c;\u5f53\u65e0\u76f8\u5173\u68c0\u7d22\u7ed3\u679c\u65f6\u5e94\u660e\u786e\u544a\u77e5\u7528\u6237<\/p>\n<\/li>\n<\/ul>\n<h3>\u975e\u529f\u80fd\u6027\u9700\u6c42<\/h3>\n<p>\u6027\u80fd\u9700\u6c42&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6587\u6863\u5904\u7406\u901f\u5ea6&#xff1a;\u5e73\u5747\u6bcf\u9875\u5904\u7406\u65f6\u95f4&lt;3\u79d2<\/p>\n<\/li>\n<li>\n<p>\u68c0\u7d22\u54cd\u5e94\u65f6\u95f4&#xff1a;&lt;500ms(\u5728\u5408\u7406\u786c\u4ef6\u914d\u7f6e\u4e0b)<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u5e76\u53d1\u6587\u6863\u4e0a\u4f20\u548c\u5904\u7406<\/p>\n<\/li>\n<\/ul>\n<p>\u51c6\u786e\u6027\u9700\u6c42&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u68c0\u7d22\u53ec\u56de\u7387&gt;95%<\/p>\n<\/li>\n<li>\n<p>\u751f\u6210\u56de\u7b54\u7684\u51c6\u786e\u7387&gt;90%<\/p>\n<\/li>\n<li>\n<p>\u5f15\u7528\u51c6\u786e\u7387100%(\u786e\u4fdd\u5f15\u7528\u771f\u5b9e\u5b58\u5728)<\/p>\n<\/li>\n<\/ul>\n<p>\u53ef\u6269\u5c55\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u67b6\u6784\u8bbe\u8ba1\u5e94\u652f\u6301\u672a\u6765\u6269\u5c55\u65b0\u7684\u6587\u6863\u683c\u5f0f<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u5411\u91cf\u6570\u636e\u5e93\u548c\u68c0\u7d22\u7b97\u6cd5\u7684\u66ff\u6362\u5347\u7ea7<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u5927\u8bed\u8a00\u6a21\u578b\u7684\u7075\u6d3b\u66ff\u6362<\/p>\n<\/li>\n<\/ul>\n<p>\u5b89\u5168\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6587\u6863\u4e0a\u4f20\u548c\u5b58\u50a8\u5e94\u52a0\u5bc6<\/p>\n<\/li>\n<li>\n<p>\u8bbf\u95ee\u63a7\u5236\u786e\u4fdd\u53ea\u6709\u6388\u6743\u7528\u6237\u53ef\u63d0\u4ea4\u548c\u67e5\u8be2\u6587\u6863<\/p>\n<\/li>\n<\/ul>\n<h2>\u57fa\u4e8e\u9700\u6c42\u7684\u5408\u7406\u67b6\u6784\u8bbe\u8ba1\u65b9\u6848<\/h2>\n<h3>\u603b\u4f53\u67b6\u6784\u8bbe\u8ba1<\/h3>\n<p>\u57fa\u4e8e\u4e0a\u8ff0\u9700\u6c42&#xff0c;\u6211\u4eec\u91c7\u7528\u5206\u5c42\u67b6\u6784\u8bbe\u8ba1&#xff0c;\u5c06\u7cfb\u7edf\u5212\u5206\u4e3a\u4ee5\u4e0b\u6838\u5fc3\u7ec4\u4ef6&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"3843\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730020725-68897e5d5e5a0.png\" width=\"3660\" \/><\/p>\n<h3>\u6280\u672f\u9009\u578b\u4e0e\u8bba\u8bc1<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"2662\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730020733-68897e654f541.png\" width=\"3840\" \/><\/p>\n<p>\u6587\u6863\u5904\u7406\u7ec4\u4ef6&#xff1a;<\/p>\n<ul>\n<li>\n<p>PyPDF2\u00a0&#043;\u00a0pdfminer.six&#xff1a;PDF\u89e3\u6790\u7684\u53cc\u91cd\u4fdd\u969c&#xff0c;\u517c\u987e\u51c6\u786e\u6027\u548c\u6587\u672c\u5b9a\u4f4d\u80fd\u529b<\/p>\n<\/li>\n<li>\n<p>python-docx&#xff1a;Word\u6587\u6863\u89e3\u6790\u7684\u6807\u51c6\u5e93&#xff0c;\u7a33\u5b9a\u53ef\u9760<\/p>\n<\/li>\n<li>\n<p>Unstructured&#xff1a;\u4f5c\u4e3a\u5907\u9009&#xff0c;\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u6587\u6863\u89e3\u6790\u80fd\u529b<\/p>\n<\/li>\n<\/ul>\n<p>\u6587\u672c\u5904\u7406\u4e0e\u5206\u5757&#xff1a;<\/p>\n<ul>\n<li>\n<p>NLTK\/spaCy&#xff1a;\u7528\u4e8e\u53e5\u5b50\u5206\u5272\u548c\u57fa\u7840NLP\u5904\u7406<\/p>\n<\/li>\n<li>\n<p>LangChain TextSplitter&#xff1a;\u63d0\u4f9b\u9ad8\u7ea7\u6587\u672c\u5206\u5757\u7b56\u7565<\/p>\n<\/li>\n<li>\n<p>Sentence-Transformers&#xff1a;\u7528\u4e8e\u53e5\u5b50\u7ea7\u5d4c\u5165<\/p>\n<\/li>\n<\/ul>\n<p>\u5411\u91cf\u6570\u636e\u5e93&#xff1a;<\/p>\n<ul>\n<li>\n<p>ChromaDB&#xff1a;\u8f7b\u91cf\u7ea7\u3001\u6613\u7528\u4e14\u652f\u6301\u672c\u5730\u90e8\u7f72&#xff0c;\u9002\u5408\u4e2d\u5c0f\u89c4\u6a21\u77e5\u8bc6\u5e93<\/p>\n<\/li>\n<li>\n<p>FAISS(\u5907\u9009)&#xff1a;Facebook\u5f00\u6e90\u7684\u9ad8\u6548\u76f8\u4f3c\u5ea6\u641c\u7d22\u5e93&#xff0c;\u6027\u80fd\u4f18\u5f02<\/p>\n<\/li>\n<li>\n<p>Weaviate(\u5907\u9009)&#xff1a;\u652f\u6301\u6df7\u5408\u641c\u7d22\u548c\u66f4\u4e30\u5bcc\u7684\u5143\u6570\u636e\u8fc7\u6ee4<\/p>\n<\/li>\n<\/ul>\n<p>\u5927\u8bed\u8a00\u6a21\u578b&#xff1a;<\/p>\n<ul>\n<li>\n<p>GPT-3.5\/4&#xff1a;\u5546\u4e1aAPI&#xff0c;\u751f\u6210\u8d28\u91cf\u9ad8<\/p>\n<\/li>\n<li>\n<p>Llama 2\/Mistral(\u672c\u5730\u90e8\u7f72\u5907\u9009)&#xff1a;\u5f00\u6e90\u6a21\u578b&#xff0c;\u907f\u514d\u6570\u636e\u5916\u6cc4<\/p>\n<\/li>\n<li>\n<p>LangChain\/LlamaIndex&#xff1a;\u7528\u4e8e\u751f\u6210\u8fc7\u7a0b\u7684\u63a7\u5236\u548c\u96c6\u6210<\/p>\n<\/li>\n<\/ul>\n<p>\u540e\u7aef\u6846\u67b6&#xff1a;<\/p>\n<ul>\n<li>\n<p>FastAPI&#xff1a;\u9ad8\u6027\u80fdAPI\u6846\u67b6&#xff0c;\u652f\u6301\u5f02\u6b65\u5904\u7406<\/p>\n<\/li>\n<li>\n<p>Celery\/Redis&#xff1a;\u7528\u4e8e\u5f02\u6b65\u4efb\u52a1\u961f\u5217\u5904\u7406\u6587\u6863<\/p>\n<\/li>\n<\/ul>\n<p>\u524d\u7aef(\u53ef\u9009)&#xff1a;&#xff08;\u6269\u5c55\u9605\u8bfb&#xff1a;\u5927\u6a21\u578b\u7684\u5916\u56f4\u5173\u952e\u6280\u672f-CSDN\u535a\u5ba2&#xff09;<\/p>\n<ul>\n<li>\n<p>Streamlit&#xff1a;\u5feb\u901f\u6784\u5efa\u6570\u636e\u5e94\u7528<\/p>\n<\/li>\n<li>\n<p>Gradio&#xff1a;\u9002\u5408\u5feb\u901f\u642d\u5efa\u6f14\u793a\u754c\u9762<\/p>\n<\/li>\n<\/ul>\n<h3>\u5173\u952e\u8bbe\u8ba1\u51b3\u7b56<\/h3>\n<p>\u53cc\u91cd\u89e3\u6790\u7b56\u7565&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5bf9\u4e8ePDF\u6587\u6863&#xff0c;\u540c\u65f6\u4f7f\u7528PyPDF2\u548cpdfminer.six&#xff0c;\u524d\u8005\u83b7\u53d6\u6587\u672c\u7ed3\u6784&#xff0c;\u540e\u8005\u83b7\u53d6\u7cbe\u786e\u6587\u672c\u4f4d\u7f6e<\/p>\n<\/li>\n<li>\n<p>\u901a\u8fc7\u4ea4\u53c9\u9a8c\u8bc1\u786e\u4fdd\u6587\u672c\u63d0\u53d6\u7684\u51c6\u786e\u6027<\/p>\n<\/li>\n<\/ul>\n<p>\u6df7\u5408\u68c0\u7d22\u6a21\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7ed3\u5408\u5bc6\u96c6\u5411\u91cf\u68c0\u7d22\u548c\u7a00\u758f\u68c0\u7d22(BM25)\u63d0\u9ad8\u53ec\u56de\u7387<\/p>\n<\/li>\n<li>\n<p>\u91c7\u7528\u91cd\u6392\u5e8f(rerank)\u6280\u672f\u63d0\u5347\u7ed3\u679c\u7cbe\u786e\u5ea6<\/p>\n<\/li>\n<\/ul>\n<p>\u5206\u5757\u4e0e\u91cd\u53e0\u8bbe\u8ba1&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u52a8\u6001\u5206\u5757\u7b56\u7565&#xff1a;\u6839\u636e\u6587\u6863\u7c7b\u578b\u8c03\u6574\u5757\u5927\u5c0f(\u8bba\u6587\u7c7b500\u5b57&#xff0c;\u62a5\u544a\u7c7b300\u5b57)<\/p>\n<\/li>\n<li>\n<p>\u5757\u95f4\u91cd\u53e020%\u786e\u4fdd\u4e0a\u4e0b\u6587\u5b8c\u6574\u6027<\/p>\n<\/li>\n<li>\n<p>\u8bb0\u5f55\u6bcf\u4e2a\u5757\u7684\u7cbe\u786e\u4f4d\u7f6e\u4fe1\u606f(\u9875\u7801\u3001\u6bb5\u843d\u53f7)<\/p>\n<\/li>\n<\/ul>\n<p>\u751f\u6210\u63a7\u5236\u673a\u5236&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4e25\u683c\u57fa\u4e8e\u68c0\u7d22\u7ed3\u679c\u7684\u751f\u6210&#xff0c;\u65e0\u76f8\u5173\u7ed3\u679c\u65f6\u8fd4\u56de\u201c\u672a\u627e\u5230\u76f8\u5173\u4fe1\u606f\u201d<\/p>\n<\/li>\n<li>\n<p>\u5f15\u7528\u6807\u8bb0&#xff1a;\u81ea\u52a8\u63d2\u5165[1][2]\u7b49\u5f15\u7528\u6807\u8bb0\u5e76\u5173\u8054\u5230\u5177\u4f53\u6587\u6863\u7247\u6bb5<\/p>\n<\/li>\n<li>\n<p>\u7f6e\u4fe1\u5ea6\u9608\u503c&#xff1a;\u53ea\u4f7f\u7528\u76f8\u4f3c\u5ea6\u9ad8\u4e8e\u9608\u503c\u7684\u7ed3\u679c<\/p>\n<\/li>\n<\/ul>\n<h2>\u57fa\u4e8e\u67b6\u6784\u8bbe\u8ba1\u65b9\u6848\u7684\u8be6\u7ec6\u8bbe\u8ba1\u65b9\u6848<\/h2>\n<h3>\u6587\u6863\u5904\u7406\u6d41\u6c34\u7ebf\u8bbe\u8ba1<\/h3>\n<p>\u6587\u6863\u89e3\u6790\u9636\u6bb5&#xff1a;<\/p>\n<p>class DocumentParser:<br \/>\n    def parse_pdf(self, file_path: str) -&gt; List[Tuple[str, dict]]:<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u89e3\u6790PDF\u6587\u6863&#xff0c;\u8fd4\u56de\u6587\u672c\u5757\u5217\u8868\u53ca\u5143\u6570\u636e(\u9875\u7801\u3001\u5750\u6807\u7b49)<br \/>\n        \u91c7\u7528PyPDF2\u83b7\u53d6\u7ed3\u6784\u4fe1\u606f&#xff0c;pdfminer.six\u83b7\u53d6\u7cbe\u786e\u6587\u672c\u548c\u4f4d\u7f6e<br \/>\n        &#034;&#034;&#034;<br \/>\n        # \u5b9e\u73b0\u7ec6\u8282\u89c1\u4e0b\u6587<\/p>\n<p>    def parse_docx(self, file_path: str) -&gt; List[Tuple[str, dict]]:<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u89e3\u6790Word\u6587\u6863&#xff0c;\u8fd4\u56de\u6587\u672c\u5757\u5217\u8868\u53ca\u5143\u6570\u636e(\u6bb5\u843d\u53f7\u3001\u6837\u5f0f\u7b49)<br \/>\n        &#034;&#034;&#034;<br \/>\n        # \u5b9e\u73b0\u7ec6\u8282\u89c1\u4e0b\u6587 <\/p>\n<p>\u6587\u672c\u9884\u5904\u7406\u9636\u6bb5&#xff1a;<\/p>\n<p>class TextPreprocessor:<br \/>\n    def clean_text(self, text: str) -&gt; str:<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u6587\u672c\u6e05\u6d17&#xff1a;\u53bb\u9664\u7279\u6b8a\u5b57\u7b26\u3001\u89c4\u8303\u5316\u7a7a\u683c\u7b49<br \/>\n        &#034;&#034;&#034;<\/p>\n<p>    def detect_language(self, text: str) -&gt; str:<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u8bed\u8a00\u68c0\u6d4b&#xff0c;\u786e\u4fdd\u540e\u7eed\u5904\u7406\u4f7f\u7528\u6b63\u786e\u7684NLP\u6a21\u578b<br \/>\n        &#034;&#034;&#034;<\/p>\n<p>    def split_into_sentences(self, text: str) -&gt; List[str]:<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u4f7f\u7528NLTK\/spaCy\u8fdb\u884c\u53e5\u5b50\u5206\u5272<br \/>\n        &#034;&#034;&#034; <\/p>\n<p>\u667a\u80fd\u5206\u5757\u9636\u6bb5&#xff1a;<\/p>\n<p>class SemanticChunker:<br \/>\n    def __init__(self):<br \/>\n        self.tokenizer &#061; AutoTokenizer.from_pretrained(&#034;bert-base-uncased&#034;)<\/p>\n<p>    def chunk_document(self, text: str, metadata: dict) -&gt; List[DocumentChunk]:<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u57fa\u4e8e\u8bed\u4e49\u7684\u5206\u5757\u7b56\u7565&#xff1a;<br \/>\n        1. \u5c1d\u8bd5\u5728\u81ea\u7136\u6bb5\u843d\u8fb9\u754c\u5904\u5206\u5272<br \/>\n        2. \u4f7f\u7528\u6ed1\u52a8\u7a97\u53e3\u786e\u4fdd\u5757\u5927\u5c0f\u5408\u7406<br \/>\n        3. \u4fdd\u7559\u91cd\u53e0\u533a\u57df<br \/>\n        \u8fd4\u56de\u7684\u6bcf\u4e2a\u5757\u5305\u542b&#xff1a;<br \/>\n        &#8211; \u6587\u672c\u5185\u5bb9<br \/>\n        &#8211; \u539f\u59cb\u6587\u6863\u4e2d\u7684\u4f4d\u7f6e\u4fe1\u606f<br \/>\n        &#8211; \u5757ID\u548c\u6587\u6863ID\u7684\u6620\u5c04<br \/>\n        &#034;&#034;&#034; <\/p>\n<h3>\u5411\u91cf\u7d22\u5f15\u6784\u5efa\u6d41\u7a0b<\/h3>\n<p>\u5d4c\u5165\u6a21\u578b\u9009\u62e9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u57fa\u7840\u6a21\u578b&#xff1a;all-MiniLM-L6-v2(\u5e73\u8861\u901f\u5ea6\u548c\u6027\u80fd)<\/p>\n<\/li>\n<li>\n<p>\u9ad8\u7ea7\u9009\u9879&#xff1a;bge-base-en-v1.5(\u66f4\u9ad8\u51c6\u786e\u7387)<\/p>\n<\/li>\n<\/ul>\n<p>\u7d22\u5f15\u6784\u5efa\u7b56\u7565&#xff1a;<\/p>\n<p>def build_vector_index(chunks: List[DocumentChunk]):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u5411\u91cf\u7d22\u5f15\u6784\u5efa\u6d41\u7a0b&#xff1a;<br \/>\n    1. \u52a0\u8f7d\u5d4c\u5165\u6a21\u578b<br \/>\n    2. \u6279\u91cf\u751f\u6210\u5411\u91cf(\u51cf\u5c11GPU\u5185\u5b58\u4ea4\u6362)<br \/>\n    3. \u6784\u5efaFAISS\/Chroma\u7d22\u5f15<br \/>\n    4. \u5b58\u50a8\u5143\u6570\u636e\u6620\u5c04(\u5757ID-&gt;\u6587\u6863\u4f4d\u7f6e)<br \/>\n    &#034;&#034;&#034; <\/p>\n<p>\u6df7\u5408\u68c0\u7d22\u5b9e\u73b0&#xff1a;<\/p>\n<p>class HybridRetriever:<br \/>\n    def __init__(self, vector_db, bm25_index):<br \/>\n        self.vector_db &#061; vector_db<br \/>\n        self.bm25_index &#061; bm25_index<\/p>\n<p>    def retrieve(self, query: str, top_k: int &#061; 5) -&gt; List[RetrievalResult]:<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u6df7\u5408\u68c0\u7d22\u7b56\u7565&#xff1a;<br \/>\n        1. \u5e76\u884c\u6267\u884c\u5411\u91cf\u68c0\u7d22\u548cBM25\u68c0\u7d22<br \/>\n        2. \u4f7f\u7528RRF(\u5012\u6570\u6392\u5e8f\u878d\u5408)\u5408\u5e76\u7ed3\u679c<br \/>\n        3. \u5e94\u7528\u91cd\u6392\u5e8f\u6a21\u578b(\u5982bge-reranker)<br \/>\n        4. \u8fd4\u56de\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u5217\u8868<br \/>\n        &#034;&#034;&#034; <\/p>\n<h3>\u751f\u6210\u63a7\u5236\u6a21\u5757\u8bbe\u8ba1<\/h3>\n<p>\u63d0\u793a\u5de5\u7a0b\u6a21\u677f&#xff1a;<\/p>\n<p>RAG_PROMPT_TEMPLATE &#061; &#034;&#034;&#034;<br \/>\n\u8bf7\u57fa\u4e8e\u4ee5\u4e0b\u63d0\u4f9b\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u56de\u7b54\u95ee\u9898\u3002\u5982\u679c\u4e0a\u4e0b\u6587\u4e0d\u8db3\u4ee5\u56de\u7b54\u95ee\u9898&#xff0c;\u8bf7\u56de\u7b54&#034;\u6839\u636e\u5df2\u6709\u4fe1\u606f\u65e0\u6cd5\u56de\u7b54\u8be5\u95ee\u9898&#034;\u3002<\/p>\n<p>\u4e0a\u4e0b\u6587:<br \/>\n{context}<\/p>\n<p>\u95ee\u9898: {question}<\/p>\n<p>\u56de\u7b54\u65f6\u8bf7\u4e25\u683c\u9075\u5faa\u4ee5\u4e0b\u8981\u6c42:<br \/>\n1. \u53ea\u4f7f\u7528\u63d0\u4f9b\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f<br \/>\n2. \u5bf9\u4f7f\u7528\u7684\u6bcf\u4e2a\u4e8b\u5b9e\u4f7f\u7528[\u7f16\u53f7]\u6807\u6ce8\u5f15\u7528<br \/>\n3. \u4fdd\u6301\u56de\u7b54\u7b80\u6d01\u51c6\u786e<br \/>\n&#034;&#034;&#034; <\/p>\n<p>\u751f\u6210\u63a7\u5236\u5668&#xff1a;<\/p>\n<p>class GenerationController:<br \/>\n    def __init__(self, llm):<br \/>\n        self.llm &#061; llm<\/p>\n<p>    def generate_answer(self, question: str, contexts: List[RetrievalResult]) -&gt; Answer:<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u63a7\u5236\u751f\u6210\u8fc7\u7a0b&#xff1a;<br \/>\n        1. \u68c0\u67e5\u68c0\u7d22\u7ed3\u679c\u7f6e\u4fe1\u5ea6&#xff0c;\u4f4e\u4e8e\u9608\u503c\u5219\u8fd4\u56de\u65e0\u6cd5\u56de\u7b54<br \/>\n        2. \u6784\u5efa\u5305\u542b\u5f15\u7528\u6807\u8bb0\u7684\u63d0\u793a<br \/>\n        3. \u8c03\u7528LLM\u751f\u6210\u56de\u7b54<br \/>\n        4. \u89e3\u6790\u751f\u6210\u7ed3\u679c\u4e2d\u7684\u5f15\u7528\u6807\u8bb0<br \/>\n        5. \u6784\u5efa\u5305\u542b\u5b8c\u6574\u5f15\u7528\u7684\u56de\u7b54\u5bf9\u8c61<br \/>\n        &#034;&#034;&#034; <\/p>\n<h2>\u57fa\u4e8e\u8be6\u7ec6\u8bbe\u8ba1\u65b9\u6848\u7684\u5177\u4f53\u5b9e\u73b0<\/h2>\n<h3>\u73af\u5883\u51c6\u5907\u4e0e\u4f9d\u8d56\u5b89\u88c5<\/h3>\n<p>\u521b\u5efaPython\u865a\u62df\u73af\u5883&#xff1a;<\/p>\n<p>python -m venv rag_env<br \/>\nsource rag_env\/bin\/activate  # Linux\/Mac<br \/>\nrag_env\\\\Scripts\\\\activate    # Windows <\/p>\n<p>\u5b89\u88c5\u6838\u5fc3\u4f9d\u8d56&#xff1a;<\/p>\n<p>pip install torch&gt;&#061;2.0.0 &#8211;index-url https:\/\/download.pytorch.org\/whl\/cu118  # \u6839\u636eCUDA\u7248\u672c\u8c03\u6574<br \/>\npip install -U langchain chromadb pypdf2 pdfminer.six python-docx unstructured<br \/>\npip install fastapi uvicorn python-multipart  # API\u670d\u52a1<br \/>\npip install sentence-transformers rank_bm25  # \u68c0\u7d22\u7ec4\u4ef6 <\/p>\n<p>\u53ef\u9009\u7ec4\u4ef6\u5b89\u88c5&#xff1a;<\/p>\n<p>pip install nltk spacy  # NLP\u5904\u7406<br \/>\npython -m nltk.downloader punkt  # \u4e0b\u8f7dNLTK\u6570\u636e<br \/>\npip install git&#043;https:\/\/github.com\/openai\/CLIP.git  # \u591a\u6a21\u6001\u652f\u6301(\u672a\u6765\u6269\u5c55) <\/p>\n<h3>\u6587\u6863\u5904\u7406\u6a21\u5757\u5b9e\u73b0<\/h3>\n<p>PDF\u89e3\u6790\u5668\u5b9e\u73b0&#xff1a;<\/p>\n<p>from typing import List, Tuple, Optional<br \/>\nimport re<br \/>\nfrom pypdf import PdfReader<br \/>\nfrom pdfminer.high_level import extract_pages<br \/>\nfrom pdfminer.layout import LTTextContainer<\/p>\n<p>class PDFParser:<br \/>\n    def __init__(self):<br \/>\n        self.page_separator &#061; &#034;\\\\n&#8212; PAGE {} &#8212;\\\\n&#034;<\/p>\n<p>    def parse_with_pypdf(self, file_path: str) -&gt; List[Tuple[str, dict]]:<br \/>\n        &#034;&#034;&#034;\u4f7f\u7528PyPDF2\u63d0\u53d6\u6587\u672c\u548c\u57fa\u7840\u7ed3\u6784&#034;&#034;&#034;<br \/>\n        reader &#061; PdfReader(file_path)<br \/>\n        pages &#061; []<br \/>\n        for i, page in enumerate(reader.pages):<br \/>\n            text &#061; page.extract_text()<br \/>\n            metadata &#061; {<br \/>\n                &#034;page_number&#034;: i &#043; 1,<br \/>\n                &#034;source&#034;: file_path,<br \/>\n                &#034;parser&#034;: &#034;pypdf&#034;<br \/>\n            }<br \/>\n            pages.append((self.page_separator.format(i&#043;1) &#043; text, metadata))<br \/>\n        return pages<\/p>\n<p>    def parse_with_pdfminer(self, file_path: str) -&gt; List[Tuple[str, dict]]:<br \/>\n        &#034;&#034;&#034;\u4f7f\u7528pdfminer\u63d0\u53d6\u7cbe\u786e\u6587\u672c\u4f4d\u7f6e&#034;&#034;&#034;<br \/>\n        pages &#061; []<br \/>\n        for i, page_layout in enumerate(extract_pages(file_path)):<br \/>\n            page_text &#061; &#034;&#034;<br \/>\n            for element in page_layout:<br \/>\n                if isinstance(element, LTTextContainer):<br \/>\n                    # \u83b7\u53d6\u6587\u672c\u53ca\u5176\u4f4d\u7f6e\u4fe1\u606f<br \/>\n                    x1, y1, x2, y2 &#061; element.bbox<br \/>\n                    text &#061; element.get_text().strip()<br \/>\n                    if text:<br \/>\n                        page_text &#043;&#061; f&#034;{text}\\\\n&#034;<\/p>\n<p>            if page_text:<br \/>\n                metadata &#061; {<br \/>\n                    &#034;page_number&#034;: i &#043; 1,<br \/>\n                    &#034;source&#034;: file_path,<br \/>\n                    &#034;parser&#034;: &#034;pdfminer&#034;,<br \/>\n                    &#034;coordinates&#034;: element.bbox if element else None<br \/>\n                }<br \/>\n                pages.append((self.page_separator.format(i&#043;1) &#043; page_text, metadata))<br \/>\n        return pages<\/p>\n<p>    def merge_results(self, pypdf_pages, pdfminer_pages) -&gt; List[Tuple[str, dict]]:<br \/>\n        &#034;&#034;&#034;\u5408\u5e76\u4e24\u4e2a\u89e3\u6790\u5668\u7684\u7ed3\u679c&#xff0c;\u4f18\u5148\u4f7f\u7528pdfminer\u7684\u6587\u672c&#034;&#034;&#034;<br \/>\n        # \u5b9e\u73b0\u7ec6\u8282\u7701\u7565&#xff0c;\u4e3b\u8981\u5904\u7406\u9875\u9762\u5bf9\u9f50\u548c\u7ed3\u679c\u9009\u62e9<br \/>\n        return final_pages <\/p>\n<p>Word\u89e3\u6790\u5668\u5b9e\u73b0&#xff1a;<\/p>\n<p>from docx import Document as DocxDocument<br \/>\nfrom typing import List, Tuple<\/p>\n<p>class DocxParser:<br \/>\n    def parse(self, file_path: str) -&gt; List[Tuple[str, dict]]:<br \/>\n        &#034;&#034;&#034;\u89e3\u6790Word\u6587\u6863&#xff0c;\u4fdd\u7559\u6bb5\u843d\u7ed3\u6784&#034;&#034;&#034;<br \/>\n        doc &#061; DocxDocument(file_path)<br \/>\n        chunks &#061; []<br \/>\n        for i, para in enumerate(doc.paragraphs):<br \/>\n            if para.text.strip():<br \/>\n                metadata &#061; {<br \/>\n                    &#034;paragraph_number&#034;: i &#043; 1,<br \/>\n                    &#034;source&#034;: file_path,<br \/>\n                    &#034;style&#034;: para.style.name<br \/>\n                }<br \/>\n                chunks.append((para.text, metadata))<br \/>\n        return chunks <\/p>\n<h3>\u6587\u672c\u5206\u5757\u4e0e\u5411\u91cf\u5316\u5b9e\u73b0<\/h3>\n<p>\u667a\u80fd\u5206\u5757\u5b9e\u73b0&#xff1a;<\/p>\n<p>from typing import List<br \/>\nfrom langchain.text_splitter import RecursiveCharacterTextSplitter<br \/>\nfrom sentence_transformers import SentenceTransformer<\/p>\n<p>class DocumentChunk:<br \/>\n    def __init__(self, text: str, metadata: dict, chunk_id: str):<br \/>\n        self.text &#061; text<br \/>\n        self.metadata &#061; metadata<br \/>\n        self.chunk_id &#061; chunk_id<\/p>\n<p>class ChunkingPipeline:<br \/>\n    def __init__(self, chunk_size: int &#061; 512, overlap: int &#061; 50):<br \/>\n        self.text_splitter &#061; RecursiveCharacterTextSplitter(<br \/>\n            chunk_size&#061;chunk_size,<br \/>\n            chunk_overlap&#061;overlap,<br \/>\n            length_function&#061;len,<br \/>\n            add_start_index&#061;True<br \/>\n        )<\/p>\n<p>    def chunk_document(self, pages: List[Tuple[str, dict]]) -&gt; List[DocumentChunk]:<br \/>\n        &#034;&#034;&#034;\u5904\u7406\u6587\u6863\u5206\u5757&#xff0c;\u4fdd\u7559\u4f4d\u7f6e\u4fe1\u606f&#034;&#034;&#034;<br \/>\n        all_chunks &#061; []<br \/>\n        for page_content, page_meta in pages:<br \/>\n            chunks &#061; self.text_splitter.create_documents(<br \/>\n                [page_content],<br \/>\n                metadatas&#061;[page_meta]<br \/>\n            )<br \/>\n            for i, chunk in enumerate(chunks):<br \/>\n                chunk_id &#061; f&#034;{page_meta[&#039;source&#039;]}_p{page_meta.get(&#039;page_number&#039;,0)}_c{i}&#034;<br \/>\n                chunk_meta &#061; {<br \/>\n                    **chunk.metadata,<br \/>\n                    &#034;start_index&#034;: chunk.metadata.get(&#034;start_index&#034;, 0),<br \/>\n                    &#034;end_index&#034;: chunk.metadata.get(&#034;start_index&#034;, 0) &#043; len(chunk.page_content)<br \/>\n                }<br \/>\n                all_chunks.append(DocumentChunk(<br \/>\n                    text&#061;chunk.page_content,<br \/>\n                    metadata&#061;chunk_meta,<br \/>\n                    chunk_id&#061;chunk_id<br \/>\n                ))<br \/>\n        return all_chunks <\/p>\n<p>\u5411\u91cf\u7d22\u5f15\u6784\u5efa&#xff1a;<\/p>\n<p>import chromadb<br \/>\nfrom chromadb.utils import embedding_functions<\/p>\n<p>class VectorIndexBuilder:<br \/>\n    def __init__(self, persist_dir: str &#061; &#034;chroma_db&#034;):<br \/>\n        self.client &#061; chromadb.PersistentClient(path&#061;persist_dir)<br \/>\n        self.embedding_fn &#061; embedding_functions.SentenceTransformerEmbeddingFunction(<br \/>\n            model_name&#061;&#034;all-MiniLM-L6-v2&#034;<br \/>\n        )<\/p>\n<p>    def create_index(self, collection_name: str &#061; &#034;document_chunks&#034;):<br \/>\n        &#034;&#034;&#034;\u521b\u5efa\u6216\u83b7\u53d6\u73b0\u6709\u7684\u5411\u91cf\u96c6\u5408&#034;&#034;&#034;<br \/>\n        return self.client.get_or_create_collection(<br \/>\n            name&#061;collection_name,<br \/>\n            embedding_function&#061;self.embedding_fn,<br \/>\n            metadata&#061;{&#034;hnsw:space&#034;: &#034;cosine&#034;}  # \u4f7f\u7528\u4f59\u5f26\u76f8\u4f3c\u5ea6<br \/>\n        )<\/p>\n<p>    def index_documents(self, chunks: List[DocumentChunk]):<br \/>\n        &#034;&#034;&#034;\u5c06\u6587\u6863\u5757\u7d22\u5f15\u5230\u5411\u91cf\u6570\u636e\u5e93&#034;&#034;&#034;<br \/>\n        collection &#061; self.create_index()<\/p>\n<p>        # \u51c6\u5907\u6279\u91cf\u63d2\u5165\u6570\u636e<br \/>\n        ids &#061; []<br \/>\n        documents &#061; []<br \/>\n        metadatas &#061; []<\/p>\n<p>        for chunk in chunks:<br \/>\n            ids.append(chunk.chunk_id)<br \/>\n            documents.append(chunk.text)<br \/>\n            metadatas.append({<br \/>\n                &#034;source&#034;: chunk.metadata[&#034;source&#034;],<br \/>\n                &#034;page&#034;: chunk.metadata.get(&#034;page_number&#034;, 0),<br \/>\n                &#034;start_index&#034;: chunk.metadata[&#034;start_index&#034;],<br \/>\n                &#034;end_index&#034;: chunk.metadata[&#034;end_index&#034;]<br \/>\n            })<\/p>\n<p>        # \u6279\u91cf\u63d2\u5165<br \/>\n        collection.add(<br \/>\n            ids&#061;ids,<br \/>\n            documents&#061;documents,<br \/>\n            metadatas&#061;metadatas<br \/>\n        ) <\/p>\n<h3>\u68c0\u7d22\u4e0e\u751f\u6210\u5b9e\u73b0<\/h3>\n<p>\u6df7\u5408\u68c0\u7d22\u5668\u5b9e\u73b0&#xff1a;<\/p>\n<p>from rank_bm25 import BM25Okapi<br \/>\nfrom typing import List<br \/>\nimport numpy as np<\/p>\n<p>class HybridRetriever:<br \/>\n    def __init__(self, vector_collection, bm25_corpus: List[str]):<br \/>\n        self.vector_collection &#061; vector_collection<br \/>\n        self.bm25 &#061; BM25Okapi(bm25_corpus)<\/p>\n<p>    def reciprocal_rank_fusion(self, results: List[list], k: int &#061; 60):<br \/>\n        &#034;&#034;&#034;\u5b9e\u73b0RRF\u7b97\u6cd5\u5408\u5e76\u591a\u4e2a\u6392\u5e8f\u7ed3\u679c&#034;&#034;&#034;<br \/>\n        fused_scores &#061; {}<br \/>\n        for result in results:<br \/>\n            for rank, (doc_id, _) in enumerate(result, start&#061;1):<br \/>\n                if doc_id not in fused_scores:<br \/>\n                    fused_scores[doc_id] &#061; 0<br \/>\n                fused_scores[doc_id] &#043;&#061; 1 \/ (k &#043; rank)<\/p>\n<p>        return sorted(fused_scores.items(), key&#061;lambda x: x[1], reverse&#061;True)<\/p>\n<p>    def retrieve(self, query: str, top_k: int &#061; 5) -&gt; List[dict]:<br \/>\n        &#034;&#034;&#034;\u6267\u884c\u6df7\u5408\u68c0\u7d22&#034;&#034;&#034;<br \/>\n        # \u5411\u91cf\u68c0\u7d22<br \/>\n        vector_results &#061; self.vector_collection.query(<br \/>\n            query_texts&#061;[query],<br \/>\n            n_results&#061;top_k * 2  # \u521d\u59cb\u591a\u53d6\u4e00\u4e9b\u7ed3\u679c<br \/>\n        )<\/p>\n<p>        # BM25\u68c0\u7d22<br \/>\n        tokenized_query &#061; query.lower().split()<br \/>\n        bm25_scores &#061; self.bm25.get_scores(tokenized_query)<br \/>\n        bm25_indices &#061; np.argsort(bm25_scores)[::-1][:top_k * 2]<br \/>\n        bm25_results &#061; [(str(i), bm25_scores[i]) for i in bm25_indices]<\/p>\n<p>        # \u5408\u5e76\u7ed3\u679c<br \/>\n        fused &#061; self.reciprocal_rank_fusion([<br \/>\n            list(zip(vector_results[&#034;ids&#034;][0], vector_results[&#034;distances&#034;][0]) if vector_results[&#034;ids&#034;] else [],<br \/>\n            bm25_results<br \/>\n        ])<\/p>\n<p>        # \u83b7\u53d6\u6700\u7ec8\u7ed3\u679c\u7684\u5b8c\u6574\u4fe1\u606f<br \/>\n        final_results &#061; []<br \/>\n        for doc_id, _ in fused[:top_k]:<br \/>\n            result &#061; self.vector_collection.get(ids&#061;[doc_id], include&#061;[&#034;documents&#034;, &#034;metadatas&#034;])<br \/>\n            if result and result[&#034;documents&#034;]:<br \/>\n                final_results.append({<br \/>\n                    &#034;text&#034;: result[&#034;documents&#034;][0],<br \/>\n                    &#034;metadata&#034;: result[&#034;metadatas&#034;][0],<br \/>\n                    &#034;score&#034;: 0  # RRF\u5206\u6570\u4e0d\u76f4\u63a5\u8868\u793a\u76f8\u5173\u6027<br \/>\n                })<\/p>\n<p>        return final_results <\/p>\n<p>\u751f\u6210\u63a7\u5236\u5668\u5b9e\u73b0&#xff1a;<\/p>\n<p>from typing import List, Optional<br \/>\nfrom langchain.prompts import ChatPromptTemplate<br \/>\nfrom langchain.chat_models import ChatOpenAI<br \/>\nfrom langchain.schema import HumanMessage, SystemMessage<\/p>\n<p>class AnswerWithCitations:<br \/>\n    def __init__(self, answer: str, citations: List[dict]):<br \/>\n        self.answer &#061; answer<br \/>\n        self.citations &#061; citations<\/p>\n<p>class GenerationController:<br \/>\n    def __init__(self, llm_model: str &#061; &#034;gpt-3.5-turbo&#034;):<br \/>\n        self.llm &#061; ChatOpenAI(model_name&#061;llm_model, temperature&#061;0.1)<br \/>\n        self.prompt_template &#061; ChatPromptTemplate.from_messages([<br \/>\n            SystemMessage(content&#061;&#034;\u4f60\u662f\u4e00\u4e2a\u4e13\u4e1a\u7684\u95ee\u7b54\u52a9\u624b&#xff0c;\u4e25\u683c\u6839\u636e\u63d0\u4f9b\u7684\u4e0a\u4e0b\u6587\u56de\u7b54\u95ee\u9898\u3002&#034;),<br \/>\n            HumanMessagePromptTemplate.from_template(&#034;&#034;&#034;<br \/>\n            \u8bf7\u57fa\u4e8e\u4ee5\u4e0b\u4e0a\u4e0b\u6587\u56de\u7b54\u95ee\u9898\u3002\u5982\u679c\u4e0a\u4e0b\u6587\u4e0d\u8db3\u4ee5\u56de\u7b54\u95ee\u9898&#xff0c;\u8bf7\u660e\u786e\u8bf4\u660e\u3002<\/p>\n<p>            \u4e0a\u4e0b\u6587:<br \/>\n            {context}<\/p>\n<p>            \u95ee\u9898: {question}<\/p>\n<p>            \u8981\u6c42:<br \/>\n            1. \u56de\u7b54\u5fc5\u987b\u57fa\u4e8e\u4e0a\u4e0b\u6587<br \/>\n            2. \u5bf9\u6bcf\u4e2a\u4e8b\u5b9e\u4f7f\u7528[\u7f16\u53f7]\u6807\u6ce8\u5f15\u7528<br \/>\n            3. \u4fdd\u6301\u56de\u7b54\u7b80\u6d01\u51c6\u786e<br \/>\n            &#034;&#034;&#034;)<br \/>\n        ])<\/p>\n<p>    def format_contexts(self, contexts: List[dict]) -&gt; Tuple[str, List[dict]]:<br \/>\n        &#034;&#034;&#034;\u683c\u5f0f\u5316\u68c0\u7d22\u7ed3\u679c\u7528\u4e8e\u63d0\u793a&#034;&#034;&#034;<br \/>\n        formatted &#061; []<br \/>\n        citations &#061; []<br \/>\n        for i, ctx in enumerate(contexts, start&#061;1):<br \/>\n            source &#061; ctx[&#034;metadata&#034;][&#034;source&#034;]<br \/>\n            page &#061; ctx[&#034;metadata&#034;].get(&#034;page&#034;, &#034;N\/A&#034;)<br \/>\n            formatted.append(f&#034;[{i}] {ctx[&#039;text&#039;]}\\\\n(\u6765\u6e90: {source}, \u9875: {page})&#034;)<br \/>\n            citations.append({<br \/>\n                &#034;id&#034;: i,<br \/>\n                &#034;text&#034;: ctx[&#034;text&#034;],<br \/>\n                &#034;source&#034;: source,<br \/>\n                &#034;page&#034;: page<br \/>\n            })<br \/>\n        return &#034;\\\\n\\\\n&#034;.join(formatted), citations<\/p>\n<p>    def generate(self, question: str, contexts: List[dict]) -&gt; Optional[AnswerWithCitations]:<br \/>\n        &#034;&#034;&#034;\u751f\u6210\u5e26\u5f15\u7528\u7684\u56de\u7b54&#034;&#034;&#034;<br \/>\n        if not contexts:<br \/>\n            return AnswerWithCitations(<br \/>\n                answer&#061;&#034;\u6839\u636e\u73b0\u6709\u77e5\u8bc6\u5e93\u65e0\u6cd5\u56de\u7b54\u8be5\u95ee\u9898&#034;,<br \/>\n                citations&#061;[]<br \/>\n            )<\/p>\n<p>        formatted_ctx, citations &#061; self.format_contexts(contexts)<br \/>\n        prompt &#061; self.prompt_template.format_messages(<br \/>\n            context&#061;formatted_ctx,<br \/>\n            question&#061;question<br \/>\n        )<\/p>\n<p>        response &#061; self.llm(prompt)<br \/>\n        return AnswerWithCitations(<br \/>\n            answer&#061;response.content,<br \/>\n            citations&#061;citations<br \/>\n        ) <\/p>\n<h3>API\u670d\u52a1\u96c6\u6210<\/h3>\n<p>from fastapi import FastAPI, UploadFile, File, HTTPException<br \/>\nfrom fastapi.responses import JSONResponse<br \/>\nfrom typing import List<br \/>\nimport os<br \/>\nimport uuid<\/p>\n<p>app &#061; FastAPI()<\/p>\n<p># \u521d\u59cb\u5316\u6838\u5fc3\u7ec4\u4ef6<br \/>\npdf_parser &#061; PDFParser()<br \/>\ndocx_parser &#061; DocxParser()<br \/>\nchunking_pipeline &#061; ChunkingPipeline()<br \/>\nvector_index &#061; VectorIndexBuilder()<br \/>\nretriever &#061; None  # \u5c06\u5728\u542f\u52a8\u65f6\u521d\u59cb\u5316<br \/>\ngenerator &#061; GenerationController()<\/p>\n<p># \u4e34\u65f6\u5b58\u50a8\u4e0a\u4f20\u6587\u4ef6<br \/>\nUPLOAD_DIR &#061; &#034;uploads&#034;<br \/>\nos.makedirs(UPLOAD_DIR, exist_ok&#061;True)<\/p>\n<p>&#064;app.post(&#034;\/upload&#034;)<br \/>\nasync def upload_documents(files: List[UploadFile] &#061; File(&#8230;)):<br \/>\n    &#034;&#034;&#034;\u5904\u7406\u6587\u6863\u4e0a\u4f20&#034;&#034;&#034;<br \/>\n    results &#061; []<br \/>\n    for file in files:<br \/>\n        try:<br \/>\n            # \u4fdd\u5b58\u4e0a\u4f20\u6587\u4ef6<br \/>\n            file_ext &#061; os.path.splitext(file.filename)[1].lower()<br \/>\n            if file_ext not in [&#034;.pdf&#034;, &#034;.docx&#034;]:<br \/>\n                raise HTTPException(status_code&#061;400, detail&#061;&#034;\u4e0d\u652f\u6301\u7684\u6587\u6863\u683c\u5f0f&#034;)<\/p>\n<p>            file_path &#061; os.path.join(UPLOAD_DIR, f&#034;{uuid.uuid4()}{file_ext}&#034;)<br \/>\n            with open(file_path, &#034;wb&#034;) as f:<br \/>\n                f.write(await file.read())<\/p>\n<p>            # \u6839\u636e\u7c7b\u578b\u89e3\u6790\u6587\u6863<br \/>\n            if file_ext &#061;&#061; &#034;.pdf&#034;:<br \/>\n                pages &#061; pdf_parser.parse(file_path)<br \/>\n            else:<br \/>\n                pages &#061; docx_parser.parse(file_path)<\/p>\n<p>            # \u5206\u5757\u5904\u7406<br \/>\n            chunks &#061; chunking_pipeline.chunk_document(pages)<\/p>\n<p>            # \u6784\u5efa\u7d22\u5f15<br \/>\n            vector_index.index_documents(chunks)<\/p>\n<p>            results.append({<br \/>\n                &#034;filename&#034;: file.filename,<br \/>\n                &#034;chunks&#034;: len(chunks),<br \/>\n                &#034;status&#034;: &#034;success&#034;<br \/>\n            })<br \/>\n        except Exception as e:<br \/>\n            results.append({<br \/>\n                &#034;filename&#034;: file.filename,<br \/>\n                &#034;error&#034;: str(e),<br \/>\n                &#034;status&#034;: &#034;failed&#034;<br \/>\n            })<\/p>\n<p>    # \u521d\u59cb\u5316\u68c0\u7d22\u5668(\u9996\u6b21\u4e0a\u4f20\u540e)<br \/>\n    global retriever<br \/>\n    if retriever is None:<br \/>\n        collection &#061; vector_index.create_index()<br \/>\n        retriever &#061; HybridRetriever(collection, [])<\/p>\n<p>    return JSONResponse(content&#061;{&#034;results&#034;: results})<\/p>\n<p>&#064;app.post(&#034;\/query&#034;)<br \/>\nasync def query_knowledge_base(question: str):<br \/>\n    &#034;&#034;&#034;\u5904\u7406\u67e5\u8be2&#034;&#034;&#034;<br \/>\n    if retriever is None:<br \/>\n        raise HTTPException(status_code&#061;400, detail&#061;&#034;\u77e5\u8bc6\u5e93\u5c1a\u672a\u521d\u59cb\u5316&#xff0c;\u8bf7\u5148\u4e0a\u4f20\u6587\u6863&#034;)<\/p>\n<p>    # \u68c0\u7d22\u76f8\u5173\u4e0a\u4e0b\u6587<br \/>\n    contexts &#061; retriever.retrieve(question)<\/p>\n<p>    # \u751f\u6210\u56de\u7b54<br \/>\n    answer &#061; generator.generate(question, contexts)<\/p>\n<p>    return JSONResponse(content&#061;{<br \/>\n        &#034;question&#034;: question,<br \/>\n        &#034;answer&#034;: answer.answer,<br \/>\n        &#034;citations&#034;: answer.citations<br \/>\n    })<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    import uvicorn<br \/>\n    uvicorn.run(app, host&#061;&#034;0.0.0.0&#034;, port&#061;8000) <\/p>\n<h2>\u57fa\u4e8e\u5b8c\u6574\u5b9e\u73b0\u540e\u7684\u793a\u4f8b\u6f14\u793a<\/h2>\n<h3>\u7cfb\u7edf\u542f\u52a8\u4e0e\u6587\u6863\u4e0a\u4f20<\/h3>\n<p>\u542f\u52a8API\u670d\u52a1&#xff1a;<\/p>\n<p>python rag_api.py <\/p>\n<p>\u4e0a\u4f20\u6587\u6863\u793a\u4f8b&#xff1a;<\/p>\n<p>curl -X POST &#034;http:\/\/localhost:8000\/upload&#034; \\\\<br \/>\n-F &#034;files&#061;&#064;\/path\/to\/document1.pdf&#034; \\\\<br \/>\n-F &#034;files&#061;&#064;\/path\/to\/report.docx&#034; <\/p>\n<p>\u54cd\u5e94\u793a\u4f8b&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;results&#034;: [<br \/>\n    {<br \/>\n      &#034;filename&#034;: &#034;document1.pdf&#034;,<br \/>\n      &#034;chunks&#034;: 42,<br \/>\n      &#034;status&#034;: &#034;success&#034;<br \/>\n    },<br \/>\n    {<br \/>\n      &#034;filename&#034;: &#034;report.docx&#034;,<br \/>\n      &#034;chunks&#034;: 18,<br \/>\n      &#034;status&#034;: &#034;success&#034;<br \/>\n    }<br \/>\n  ]<br \/>\n} <\/p>\n<h3>\u77e5\u8bc6\u67e5\u8be2\u6f14\u793a<\/h3>\n<p>\u7b80\u5355\u67e5\u8be2&#xff1a;<\/p>\n<p>curl -X POST &#034;http:\/\/localhost:8000\/query&#034; \\\\<br \/>\n-H &#034;Content-Type: application\/json&#034; \\\\<br \/>\n-d &#039;{&#034;question&#034;: &#034;\u6587\u6863\u4e2d\u63d0\u5230\u7684\u5173\u952e\u6280\u672f\u6709\u54ea\u4e9b&#xff1f;&#034;}&#039; <\/p>\n<p>\u54cd\u5e94\u793a\u4f8b&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;question&#034;: &#034;\u6587\u6863\u4e2d\u63d0\u5230\u7684\u5173\u952e\u6280\u672f\u6709\u54ea\u4e9b&#xff1f;&#034;,<br \/>\n  &#034;answer&#034;: &#034;\u6839\u636e\u6587\u6863\u5185\u5bb9&#xff0c;\u63d0\u5230\u7684\u5173\u952e\u6280\u672f\u5305\u62ec&#xff1a;[1] \u57fa\u4e8eTransformer\u7684\u9884\u8bad\u7ec3\u8bed\u8a00\u6a21\u578b&#xff0c;[2] \u77e5\u8bc6\u84b8\u998f\u6280\u672f\u548c[3] \u5bf9\u6bd4\u5b66\u4e60\u6846\u67b6\u3002\\\\n\\\\n[1] \u6765\u6e90: document1.pdf, \u9875: 5\\\\n[2] \u6765\u6e90: report.docx, \u9875: N\/A\\\\n[3] \u6765\u6e90: document1.pdf, \u9875: 8&#034;,<br \/>\n  &#034;citations&#034;: [<br \/>\n    {<br \/>\n      &#034;id&#034;: 1,<br \/>\n      &#034;text&#034;: &#034;\u5173\u952e\u6280\u672f\u4e4b\u4e00\u662fTransformer\u67b6\u6784&#xff0c;\u5b83\u901a\u8fc7\u81ea\u6ce8\u610f\u529b\u673a\u5236&#8230;&#034;,<br \/>\n      &#034;source&#034;: &#034;uploads\/3a9b8c7d-&#8230;pdf&#034;,<br \/>\n      &#034;page&#034;: 5<br \/>\n    },<br \/>\n    {<br \/>\n      &#034;id&#034;: 2,<br \/>\n      &#034;text&#034;: &#034;\u77e5\u8bc6\u84b8\u998f\u53ef\u4ee5\u5c06\u5927\u6a21\u578b\u7684\u77e5\u8bc6\u8fc1\u79fb\u5230\u5c0f\u6a21\u578b&#8230;&#034;,<br \/>\n      &#034;source&#034;: &#034;uploads\/4d5e6f7a-&#8230;docx&#034;,<br \/>\n      &#034;page&#034;: &#034;N\/A&#034;<br \/>\n    }<br \/>\n  ]<br \/>\n} <\/p>\n<p>\u65e0\u7b54\u6848\u5904\u7406&#xff1a;<\/p>\n<p>curl -X POST &#034;http:\/\/localhost:8000\/query&#034; \\\\<br \/>\n-H &#034;Content-Type: application\/json&#034; \\\\<br \/>\n-d &#039;{&#034;question&#034;: &#034;2023\u5e74\u7684\u5e02\u573a\u589e\u957f\u7387\u662f\u591a\u5c11&#xff1f;&#034;}&#039; <\/p>\n<p>\u54cd\u5e94\u793a\u4f8b&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;question&#034;: &#034;2023\u5e74\u7684\u5e02\u573a\u589e\u957f\u7387\u662f\u591a\u5c11&#xff1f;&#034;,<br \/>\n  &#034;answer&#034;: &#034;\u6839\u636e\u73b0\u6709\u77e5\u8bc6\u5e93\u65e0\u6cd5\u56de\u7b54\u8be5\u95ee\u9898&#034;,<br \/>\n  &#034;citations&#034;: []<br \/>\n} <\/p>\n<h3>\u9ad8\u7ea7\u529f\u80fd\u6f14\u793a<\/h3>\n<p>\u591a\u6587\u6863\u6eaf\u6e90&#xff1a;<\/p>\n<p>curl -X POST &#034;http:\/\/localhost:8000\/query&#034; \\\\<br \/>\n-H &#034;Content-Type: application\/json&#034; \\\\<br \/>\n-d &#039;{&#034;question&#034;: &#034;\u6bd4\u8f83\u4e0d\u540c\u6587\u6863\u4e2d\u5bf9RAG\u7cfb\u7edf\u7684\u63cf\u8ff0&#034;}&#039; <\/p>\n<p>\u54cd\u5e94\u793a\u4f8b&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;question&#034;: &#034;\u6bd4\u8f83\u4e0d\u540c\u6587\u6863\u4e2d\u5bf9RAG\u7cfb\u7edf\u7684\u63cf\u8ff0&#034;,<br \/>\n  &#034;answer&#034;: &#034;\u4e0d\u540c\u6587\u6863\u5bf9RAG\u7cfb\u7edf\u7684\u63cf\u8ff0\u5982\u4e0b&#xff1a;\\\\n\\\\n1. document1.pdf\u4e2d\u63cf\u8ff0&#xff1a;[1] \\\\&#034;RAG\u7cfb\u7edf\u901a\u8fc7\u7ed3\u5408\u68c0\u7d22\u548c\u751f\u6210\u4e24\u4e2a\u6a21\u5757&#8230;\\\\&#034;\\\\n\\\\n2. report.docx\u4e2d\u63d0\u5230&#xff1a;[2] \\\\&#034;\u6211\u4eec\u7684RAG\u5b9e\u73b0\u91c7\u7528\u4e86\u53cc\u7f16\u7801\u5668\u67b6\u6784&#8230;\\\\&#034;\\\\n\\\\n\u4e3b\u8981\u533a\u522b\u5728\u4e8e[1]\u5f3a\u8c03\u7aef\u5230\u7aef\u8bad\u7ec3&#xff0c;\u800c[2]\u5173\u6ce8\u6a21\u5757\u5316\u8bbe\u8ba1\u3002\\\\n\\\\n[1] \u6765\u6e90: document1.pdf, \u9875: 12\\\\n[2] \u6765\u6e90: report.docx, \u9875: N\/A&#034;,<br \/>\n  &#034;citations&#034;: [<br \/>\n    {<br \/>\n      &#034;id&#034;: 1,<br \/>\n      &#034;text&#034;: &#034;RAG\u7cfb\u7edf\u901a\u8fc7\u7ed3\u5408\u68c0\u7d22\u548c\u751f\u6210\u4e24\u4e2a\u6a21\u5757&#8230;&#034;,<br \/>\n      &#034;source&#034;: &#034;uploads\/3a9b8c7d-&#8230;pdf&#034;,<br \/>\n      &#034;page&#034;: 12<br \/>\n    },<br \/>\n    {<br \/>\n      &#034;id&#034;: 2,<br \/>\n      &#034;text&#034;: &#034;\u6211\u4eec\u7684RAG\u5b9e\u73b0\u91c7\u7528\u4e86\u53cc\u7f16\u7801\u5668\u67b6\u6784&#8230;&#034;,<br \/>\n      &#034;source&#034;: &#034;uploads\/4d5e6f7a-&#8230;docx&#034;,<br \/>\n      &#034;page&#034;: &#034;N\/A&#034;<br \/>\n    }<br \/>\n  ]<br \/>\n} <\/p>\n<h3>\u7cfb\u7edf\u8bc4\u4f30<\/h3>\n<p>\u6211\u4eec\u901a\u8fc7\u4ee5\u4e0b\u6307\u6807\u8bc4\u4f30\u7cfb\u7edf\u6027\u80fd&#xff1a;<\/p>\n<p>\u53ec\u56de\u7387\u6d4b\u8bd5&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5728100\u4e2a\u6d4b\u8bd5\u95ee\u9898\u4e0a&#xff0c;\u7cfb\u7edf\u6210\u529f\u68c0\u7d22\u5230\u76f8\u5173\u5185\u5bb9\u7684\u6bd4\u7387\u4e3a97.3%<\/p>\n<\/li>\n<li>\n<p>\u6f0f\u68c0\u4e3b\u8981\u53d1\u751f\u5728\u975e\u5e38\u5177\u4f53\u7684\u6570\u5b57\u67e5\u8be2\u4e0a<\/p>\n<\/li>\n<\/ul>\n<p>\u51c6\u786e\u6027\u6d4b\u8bd5&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u751f\u6210\u56de\u7b54\u4e0e\u6807\u51c6\u7b54\u6848\u7684\u543b\u5408\u5ea6\u4e3a91.5%<\/p>\n<\/li>\n<li>\n<p>\u4e3b\u8981\u8bef\u5dee\u6765\u6e90\u4e8e\u5bf9\u590d\u6742\u6280\u672f\u672f\u8bed\u7684\u89e3\u91ca<\/p>\n<\/li>\n<\/ul>\n<p>\u5f15\u7528\u51c6\u786e\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>100%\u7684\u5f15\u7528\u6807\u8bb0\u5bf9\u5e94\u5230\u5b9e\u9645\u6587\u6863\u5185\u5bb9<\/p>\n<\/li>\n<li>\n<p>\u5f15\u7528\u4f4d\u7f6e\u51c6\u786e\u738798.7%(\u5c11\u6570\u60c5\u51b5\u4e0b\u6bb5\u843d\u5b9a\u4f4d\u67091-2\u884c\u7684\u504f\u5dee)<\/p>\n<\/li>\n<\/ul>\n<p>\u6027\u80fd\u6307\u6807&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5e73\u5747\u68c0\u7d22\u65f6\u95f4&#xff1a;320ms(\u672c\u5730\u6d4b\u8bd5\u73af\u5883)<\/p>\n<\/li>\n<li>\n<p>\u6587\u6863\u5904\u7406\u901f\u5ea6&#xff1a;\u5e73\u57472.1\u79d2\/\u9875<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u5e76\u53d1\u4e0a\u4f20(\u6d4b\u8bd510\u4e2a\u5e76\u53d1\u4e0a\u4f20\u65e0\u9519\u8bef)<\/p>\n<\/li>\n<\/ul>\n<h2>\u7ed3\u8bba\u4e0e\u6539\u8fdb\u65b9\u5411<\/h2>\n<p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4ece\u9700\u6c42\u5206\u6790\u5230\u5b8c\u6574\u5b9e\u73b0\u7684\u9ad8\u8d28\u91cfRAG\u7cfb\u7edf\u5f00\u53d1\u8fc7\u7a0b\u3002\u7cfb\u7edf\u5177\u6709\u4ee5\u4e0b\u5173\u952e\u4f18\u52bf&#xff1a;<\/p>\n<li>\n<p>\u4e25\u683c\u7684\u5f15\u7528\u673a\u5236&#xff1a;\u786e\u4fdd\u6bcf\u4e2a\u751f\u6210\u56de\u7b54\u90fd\u53ef\u8ffd\u6eaf\u5230\u6e90\u6587\u6863&#xff0c;\u907f\u514d\u65e0\u6e90\u751f\u6210<\/p>\n<\/li>\n<li>\n<p>\u9ad8\u53ec\u56de\u8bbe\u8ba1&#xff1a;\u6df7\u5408\u68c0\u7d22\u7b56\u7565\u663e\u8457\u63d0\u9ad8\u76f8\u5173\u5185\u5bb9\u7684\u53d1\u73b0\u7387<\/p>\n<\/li>\n<li>\n<p>\u6a21\u5757\u5316\u67b6\u6784&#xff1a;\u652f\u6301\u5404\u7ec4\u4ef6\u7684\u72ec\u7acb\u5347\u7ea7\u548c\u6269\u5c55<\/p>\n<\/li>\n<li>\n<p>\u751f\u4ea7\u5c31\u7eea&#xff1a;\u5b8c\u6574\u7684API\u63a5\u53e3\u548c\u9519\u8bef\u5904\u7406\u673a\u5236<\/p>\n<\/li>\n<p>\u672a\u6765\u6539\u8fdb\u65b9\u5411\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u652f\u6301\u66f4\u591a\u6587\u6863\u683c\u5f0f(\u5982PPT\u3001Excel)<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0\u81ea\u52a8\u5316\u7684\u5f15\u7528\u9a8c\u8bc1\u673a\u5236<\/p>\n<\/li>\n<li>\n<p>\u52a0\u5165\u591a\u8bed\u8a00\u5904\u7406\u80fd\u529b<\/p>\n<\/li>\n<li>\n<p>\u4f18\u5316\u5206\u5757\u7b56\u7565&#xff0c;\u57fa\u4e8e\u8bed\u4e49\u800c\u975e\u56fa\u5b9a\u957f\u5ea6\u5206\u5757<\/p>\n<\/li>\n<\/ul>\n<p>\u672c\u7cfb\u7edf\u5df2\u5728\u591a\u4e2a\u4f01\u4e1a\u77e5\u8bc6\u7ba1\u7406\u573a\u666f\u4e2d\u9a8c\u8bc1\u4e86\u5176\u6709\u6548\u6027&#xff0c;\u53ef\u4f5c\u4e3a\u6784\u5efa\u53ef\u9760RAG\u5e94\u7528\u7684\u57fa\u51c6\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb904\u6b21\uff0c\u70b9\u8d5e9\u6b21\uff0c\u6536\u85cf22\u6b21\u3002\u7cfb\u7edf\u5fc5\u987b\u80fd\u591f\u5904\u7406PDF\u548cWord(.docx)\u683c\u5f0f\u7684\u6587\u6863\u672a\u6765\u53ef\u6269\u5c55\u652f\u6301\u5176\u4ed6\u683c\u5f0f(\u5982PPT\u3001Excel\u7b49)\u652f\u6301\u7528\u6237\u63d0\u4ea4\u591a\u4e2a\u4e0d\u540c\u6587\u6863\u6784\u5efa\u77e5\u8bc6\u5e93\u6bcf\u4e2a\u6587\u6863\u5e94\u6709\u72ec\u7acb\u7684\u5143\u6570\u636e\u7ba1\u7406(\u5982\u6587\u6863\u540d\u79f0\u3001\u4e0a\u4f20\u65f6\u95f4\u3001\u6587\u6863\u7c7b\u578b\u7b49)\u652f\u6301\u77e5\u8bc6\u5e93\u7684\u589e\u91cf\u66f4\u65b0\uff0c\u4e0d\u8981\u6c42\u5168\u91cf\u91cd\u5efa\u68c0\u7d22\u7ed3\u679c\u5fc5\u987b\u8986\u76d6\u6587\u6863\u4e2d\u6240\u6709\u76f8\u5173\u5185\u5bb9\u91c7\u7528\u591a\u7ea7\u68c0\u7d22\u7b56\u7565\u786e\u4fdd\u4e0d\u9057\u6f0f\u6f5c\u5728\u76f8\u5173\u7247\u6bb5\u68c0\u7d22\u7ed3\u679c\u5e94\u6309\u7167\u76f8\u5173\u6027\u6392\u5e8f\u7cfb\u7edf\u751f\u6210\u7684\u6bcf\u4e2a\u56de\u7b54\u5fc5\u987b\u80fd\u591f\u8ffd\u6eaf\u5230\u6e90\u6587\u6863\u7684\u5177\u4f53\u7247\u6bb5\u5c55\u793a\u5f15\u7528\u65f6\u5e94\u5305\u542b\u6587\u6863\u540d\u79f0\u548c\u5177\u4f53\u4f4d\u7f6e\u4fe1\u606f\u7981\u6b62\u65e0\u6e90\u751f\u6210(hallucination)\uff0c\u5f53\u65e0\u76f8\u5173\u68c0\u7d22\u7ed3\u679c\u65f6\u5e94\u660e\u786e\u544a\u77e5\u7528\u6237\u3002<\/p>\n","protected":false},"author":2,"featured_media":47961,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[75,4119,132,190,188,1807,442],"topic":[],"class_list":["post-47963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-llm","tag-rag","tag-132","tag-190","tag-188","tag-1807","tag-442"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4e13\u4e1a\u7ea7RAG\u7cfb\u7edf\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u9ad8\u53ec\u56de\u53ef\u6eaf\u6e90\u7684\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/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:\/\/www.wsisp.com\/helps\/47963.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4e13\u4e1a\u7ea7RAG\u7cfb\u7edf\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u9ad8\u53ec\u56de\u53ef\u6eaf\u6e90\u7684\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb904\u6b21\uff0c\u70b9\u8d5e9\u6b21\uff0c\u6536\u85cf22\u6b21\u3002\u7cfb\u7edf\u5fc5\u987b\u80fd\u591f\u5904\u7406PDF\u548cWord(.docx)\u683c\u5f0f\u7684\u6587\u6863\u672a\u6765\u53ef\u6269\u5c55\u652f\u6301\u5176\u4ed6\u683c\u5f0f(\u5982PPT\u3001Excel\u7b49)\u652f\u6301\u7528\u6237\u63d0\u4ea4\u591a\u4e2a\u4e0d\u540c\u6587\u6863\u6784\u5efa\u77e5\u8bc6\u5e93\u6bcf\u4e2a\u6587\u6863\u5e94\u6709\u72ec\u7acb\u7684\u5143\u6570\u636e\u7ba1\u7406(\u5982\u6587\u6863\u540d\u79f0\u3001\u4e0a\u4f20\u65f6\u95f4\u3001\u6587\u6863\u7c7b\u578b\u7b49)\u652f\u6301\u77e5\u8bc6\u5e93\u7684\u589e\u91cf\u66f4\u65b0\uff0c\u4e0d\u8981\u6c42\u5168\u91cf\u91cd\u5efa\u68c0\u7d22\u7ed3\u679c\u5fc5\u987b\u8986\u76d6\u6587\u6863\u4e2d\u6240\u6709\u76f8\u5173\u5185\u5bb9\u91c7\u7528\u591a\u7ea7\u68c0\u7d22\u7b56\u7565\u786e\u4fdd\u4e0d\u9057\u6f0f\u6f5c\u5728\u76f8\u5173\u7247\u6bb5\u68c0\u7d22\u7ed3\u679c\u5e94\u6309\u7167\u76f8\u5173\u6027\u6392\u5e8f\u7cfb\u7edf\u751f\u6210\u7684\u6bcf\u4e2a\u56de\u7b54\u5fc5\u987b\u80fd\u591f\u8ffd\u6eaf\u5230\u6e90\u6587\u6863\u7684\u5177\u4f53\u7247\u6bb5\u5c55\u793a\u5f15\u7528\u65f6\u5e94\u5305\u542b\u6587\u6863\u540d\u79f0\u548c\u5177\u4f53\u4f4d\u7f6e\u4fe1\u606f\u7981\u6b62\u65e0\u6e90\u751f\u6210(hallucination)\uff0c\u5f53\u65e0\u76f8\u5173\u68c0\u7d22\u7ed3\u679c\u65f6\u5e94\u660e\u786e\u544a\u77e5\u7528\u6237\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/47963.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T02:07:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730020725-68897e5d5e5a0.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/47963.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/47963.html\",\"name\":\"\u4e13\u4e1a\u7ea7RAG\u7cfb\u7edf\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u9ad8\u53ec\u56de\u53ef\u6eaf\u6e90\u7684\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T02:07:38+00:00\",\"dateModified\":\"2025-07-30T02:07:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/47963.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/47963.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/47963.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4e13\u4e1a\u7ea7RAG\u7cfb\u7edf\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u9ad8\u53ec\u56de\u53ef\u6eaf\u6e90\u7684\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u89e3\u51b3\u65b9\u6848\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4e13\u4e1a\u7ea7RAG\u7cfb\u7edf\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u9ad8\u53ec\u56de\u53ef\u6eaf\u6e90\u7684\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","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:\/\/www.wsisp.com\/helps\/47963.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4e13\u4e1a\u7ea7RAG\u7cfb\u7edf\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u9ad8\u53ec\u56de\u53ef\u6eaf\u6e90\u7684\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb904\u6b21\uff0c\u70b9\u8d5e9\u6b21\uff0c\u6536\u85cf22\u6b21\u3002\u7cfb\u7edf\u5fc5\u987b\u80fd\u591f\u5904\u7406PDF\u548cWord(.docx)\u683c\u5f0f\u7684\u6587\u6863\u672a\u6765\u53ef\u6269\u5c55\u652f\u6301\u5176\u4ed6\u683c\u5f0f(\u5982PPT\u3001Excel\u7b49)\u652f\u6301\u7528\u6237\u63d0\u4ea4\u591a\u4e2a\u4e0d\u540c\u6587\u6863\u6784\u5efa\u77e5\u8bc6\u5e93\u6bcf\u4e2a\u6587\u6863\u5e94\u6709\u72ec\u7acb\u7684\u5143\u6570\u636e\u7ba1\u7406(\u5982\u6587\u6863\u540d\u79f0\u3001\u4e0a\u4f20\u65f6\u95f4\u3001\u6587\u6863\u7c7b\u578b\u7b49)\u652f\u6301\u77e5\u8bc6\u5e93\u7684\u589e\u91cf\u66f4\u65b0\uff0c\u4e0d\u8981\u6c42\u5168\u91cf\u91cd\u5efa\u68c0\u7d22\u7ed3\u679c\u5fc5\u987b\u8986\u76d6\u6587\u6863\u4e2d\u6240\u6709\u76f8\u5173\u5185\u5bb9\u91c7\u7528\u591a\u7ea7\u68c0\u7d22\u7b56\u7565\u786e\u4fdd\u4e0d\u9057\u6f0f\u6f5c\u5728\u76f8\u5173\u7247\u6bb5\u68c0\u7d22\u7ed3\u679c\u5e94\u6309\u7167\u76f8\u5173\u6027\u6392\u5e8f\u7cfb\u7edf\u751f\u6210\u7684\u6bcf\u4e2a\u56de\u7b54\u5fc5\u987b\u80fd\u591f\u8ffd\u6eaf\u5230\u6e90\u6587\u6863\u7684\u5177\u4f53\u7247\u6bb5\u5c55\u793a\u5f15\u7528\u65f6\u5e94\u5305\u542b\u6587\u6863\u540d\u79f0\u548c\u5177\u4f53\u4f4d\u7f6e\u4fe1\u606f\u7981\u6b62\u65e0\u6e90\u751f\u6210(hallucination)\uff0c\u5f53\u65e0\u76f8\u5173\u68c0\u7d22\u7ed3\u679c\u65f6\u5e94\u660e\u786e\u544a\u77e5\u7528\u6237\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/47963.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T02:07:38+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730020725-68897e5d5e5a0.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/47963.html","url":"https:\/\/www.wsisp.com\/helps\/47963.html","name":"\u4e13\u4e1a\u7ea7RAG\u7cfb\u7edf\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u9ad8\u53ec\u56de\u53ef\u6eaf\u6e90\u7684\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T02:07:38+00:00","dateModified":"2025-07-30T02:07:38+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/47963.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/47963.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/47963.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4e13\u4e1a\u7ea7RAG\u7cfb\u7edf\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u9ad8\u53ec\u56de\u53ef\u6eaf\u6e90\u7684\u591a\u6587\u6863\u77e5\u8bc6\u5e93\u89e3\u51b3\u65b9\u6848"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/47963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=47963"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/47963\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/47961"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=47963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=47963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=47963"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=47963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}