[{"data":1,"prerenderedAt":1189},["ShallowReactive",2],{"navigation_docs":3,"-ai-rag-rag-intro-principles-and-implementation":352,"-ai-rag-rag-intro-principles-and-implementation-surround":1184},[4,54,83,116,125,161,179,200],{"title":5,"path":6,"stem":7,"children":8},"Ai","/ai","ai",[9,12,42,46,50],{"title":10,"path":6,"stem":11},"AI 技術探索","ai/index",{"title":13,"path":14,"stem":15,"children":16,"page":41},"Rag","/ai/rag","ai/RAG",[17,21,25,29,33,37],{"title":18,"path":19,"stem":20},"BM25 演算法深入解析：從 TF-IDF 到現代搜尋引擎的核心技術","/ai/rag/bm25-deep-dive-from-tf-idf-to-rag","ai/RAG/bm25-deep-dive-from-tf-idf-to-rag",{"title":22,"path":23,"stem":24},"Dense Retrieval（向量檢索）深入解析","/ai/rag/dense-retrieval-deep-dive","ai/RAG/dense-retrieval-deep-dive",{"title":26,"path":27,"stem":28},"RAG（Retrieval-Augmented Generation）原理與實作入門","/ai/rag/rag-intro-principles-and-implementation","ai/RAG/rag-intro-principles-and-implementation",{"title":30,"path":31,"stem":32},"RAG Retrieval Strategy 深入解析","/ai/rag/rag-retrieval-strategy-deep-dive","ai/RAG/rag-retrieval-strategy-deep-dive",{"title":34,"path":35,"stem":36},"Reciprocal Rank Fusion（RRF）教學：打造更穩定的檢索融合策略","/ai/rag/rrf-reciprocal-rank-fusion-guide","ai/RAG/rrf-reciprocal-rank-fusion-guide",{"title":38,"path":39,"stem":40},"TF-IDF（Term Frequency–Inverse Document Frequency）深入解析","/ai/rag/tf-idf-deep-dive","ai/RAG/tf-idf-deep-dive",false,{"title":43,"path":44,"stem":45},"Docus AI 功能完整實作指南","/ai/docus-ai-implementation","ai/docus-ai-implementation",{"title":47,"path":48,"stem":49},"llms.txt 是什麼？在 Nuxt 中控制 AI 與搜尋引擎爬蟲的新方式","/ai/llms-txt","ai/llms-txt",{"title":51,"path":52,"stem":53},"MCP（Model Context Protocol）是什麼？用「AI 的 USB-C」理解模型如何安全連接工具與資料","/ai/mcp_concept","ai/mcp_concept",{"title":55,"path":56,"stem":57,"children":58,"page":41},"Data Analysis","/data-analysis","data-analysis",[59,63,67,71,75,79],{"title":60,"path":61,"stem":62},"Exploratory Data Analysis（EDA）資料探索分析入門","/data-analysis/exploratory-data-analysis-eda","data-analysis/exploratory-data-analysis-eda",{"title":64,"path":65,"stem":66},"PR-AUC 深入解析：不平衡分類問題的重要評估指標","/data-analysis/pr-auc-for-imbalanced-classification","data-analysis/pr-auc-for-imbalanced-classification",{"title":68,"path":69,"stem":70},"ROC-AUC 深入解析：理解分類模型的判斷能力","/data-analysis/roc-auc-complete-guide","data-analysis/roc-auc-complete-guide",{"title":72,"path":73,"stem":74},"SHAP（SHapley Additive exPlanations）模型解釋方法深入解析","/data-analysis/shap-model-interpretation-complete-guide","data-analysis/shap-model-interpretation-complete-guide",{"title":76,"path":77,"stem":78},"Social Network Analysis（SNA）深入解析","/data-analysis/social-network-analysis-sna","data-analysis/social-network-analysis-sna",{"title":80,"path":81,"stem":82},"Time-based Split：為什麼時間序列資料不能使用 Random Split？","/data-analysis/time-based-split-vs-random-split","data-analysis/time-based-split-vs-random-split",{"title":84,"path":85,"stem":86,"children":87,"page":41},"MachineLearning","/machine_learning","machine_learning",[88,92,96,100,104,108,112],{"title":89,"path":90,"stem":91},"Confusion Matrix（混淆矩陣）完整解析","/machine_learning/confusion-matrix","machine_learning/confusion-matrix",{"title":93,"path":94,"stem":95},"Decision Tree 模型完整教學：從原理到 Python 實作","/machine_learning/decision-tree-complete-guide","machine_learning/decision-tree-complete-guide",{"title":97,"path":98,"stem":99},"為什麼 Decision Tree 在 Tabular Data 上常常勝過 Deep Learning？","/machine_learning/decision-tree-vs-deep-learning-on-tabular-data","machine_learning/decision-tree-vs-deep-learning-on-tabular-data",{"title":101,"path":102,"stem":103},"LightGBM 模型入門：理解高效能的 Gradient Boosting 演算法","/machine_learning/lightgbm-intro-for-tabular-data","machine_learning/lightgbm-intro-for-tabular-data",{"title":105,"path":106,"stem":107},"Logistic Regression（Logit 回歸）完整教學","/machine_learning/logistic-regression-complete-guide","machine_learning/logistic-regression-complete-guide",{"title":109,"path":110,"stem":111},"為什麼模型訓練需要 Train / Validation / Test Dataset？","/machine_learning/train-validation-test-dataset","machine_learning/train-validation-test-dataset",{"title":113,"path":114,"stem":115},"XGBoost 原理與實務教學","/machine_learning/xgboost-complete-guide","machine_learning/xgboost-complete-guide",{"title":117,"path":118,"stem":119,"children":120,"page":41},"Network","/network","network",[121],{"title":122,"path":123,"stem":124},"DNS 設定入門：CNAME vs A Record 完整解析","/network/dns-cname-vs-a-record-guide","network/dns-cname-vs-a-record-guide",{"title":126,"path":127,"stem":128,"children":129},"Nuxt","/nuxt","nuxt",[130,133,137,141,145,149,153,157],{"title":131,"path":127,"stem":132},"Nuxt 實戰指南","nuxt/index",{"title":134,"path":135,"stem":136},"Abstraction Layer 與 Adapter 的差別，一次搞懂設計角色","/nuxt/adapter_abstraction_layer","nuxt/adapter_abstraction_layer",{"title":138,"path":139,"stem":140},"Nuxt 4 全面理解 — 專案檔案結構解析","/nuxt/directory_structure","nuxt/directory_structure",{"title":142,"path":143,"stem":144},"Nuxt 前端 build 流程完整解析","/nuxt/frontend_build","nuxt/frontend_build",{"title":146,"path":147,"stem":148},"Nitro 是什麼？從 Nuxt 專案到 Server 與 Edge 的關鍵引擎","/nuxt/nitro","nuxt/nitro",{"title":150,"path":151,"stem":152},"深入理解 Nuxt 的 .nuxt 與 .output：為什麼部署時一定要分清楚？","/nuxt/nuxt_and_output","nuxt/nuxt_and_output",{"title":154,"path":155,"stem":156},"Nuxt 中 dev、build、preview 的差異一次搞懂","/nuxt/nuxt_dev_build_preview","nuxt/nuxt_dev_build_preview",{"title":158,"path":159,"stem":160},"Nuxt Image vs \u003Cimg>：為什麼 Nuxt 3 專案幾乎都該用 \u003CNuxtImg>？","/nuxt/nuxt_image","nuxt/nuxt_image",{"title":162,"path":163,"stem":164,"children":165},"Python 程式開發","/python","python/index",[166,167,171,175],{"title":162,"path":163,"stem":164},{"title":168,"path":169,"stem":170},"如何在 FastAPI 中調用記憶體 Buffer 回傳圖片 (附實例)","/python/memory_buffer","python/memory_buffer",{"title":172,"path":173,"stem":174},"Python 單元測試教學：如何為你的程式撰寫 Test","/python/python-unit-testing-unittest-pytest","python/python-unit-testing-unittest-pytest",{"title":176,"path":177,"stem":178},"如何使用 uv 取代 pip：改善 Python 專案的開發流程","/python/uv","python/uv",{"title":180,"path":181,"stem":182,"children":183,"page":41},"Statistic","/statistic","statistic",[184,188,192,196],{"title":185,"path":186,"stem":187},"卡方檢定（Chi-Square Test）入門教學","/statistic/chi-square-test-intro","statistic/chi-square-test-intro",{"title":189,"path":190,"stem":191},"常見抽樣方法（Sampling Methods）教學：Stratified、Quota、Convenience、Systematic、Simple Random","/statistic/common-sampling-methods-guide","statistic/common-sampling-methods-guide",{"title":193,"path":194,"stem":195},"Cramér’s V 指標介紹：如何衡量兩個類別變數之間的關聯","/statistic/cramers-v-for-categorical-association","statistic/cramers-v-for-categorical-association",{"title":197,"path":198,"stem":199},"Mann–Whitney U 與 Kolmogorov–Smirnov（KS）檢定：非參數統計檢定的入門指南","/statistic/mann-whitney-u-and-ks-test-intro","statistic/mann-whitney-u-and-ks-test-intro",{"title":201,"path":202,"stem":203,"children":204},"WebDev","/web_dev","web_dev",[205,208,234,248,282,312,334],{"title":206,"path":202,"stem":207},"Web 前端開發","web_dev/index",{"title":209,"path":210,"stem":211,"children":212},"瀏覽器與渲染","/web_dev/browser","web_dev/browser/index",[213,214,218,222,226,230],{"title":209,"path":210,"stem":211},{"title":215,"path":216,"stem":217},"瀏覽器儲存空間完整解析：Cookie、localStorage、IndexedDB 到 Cache Storage","/web_dev/browser/browser-storage-comprehensive-guide","web_dev/browser/browser-storage-comprehensive-guide",{"title":219,"path":220,"stem":221},"DOM (Document Object Model) 深入解析","/web_dev/browser/dom","web_dev/browser/dom",{"title":223,"path":224,"stem":225},"Service Worker Request Flow 深入解析","/web_dev/browser/service-worker-request-flow","web_dev/browser/service-worker-request-flow",{"title":227,"path":228,"stem":229},"CSR、SSR 與 SSG 是什麼？前端渲染策略完整比較","/web_dev/browser/ssr_csr_ssg","web_dev/browser/ssr_csr_ssg",{"title":231,"path":232,"stem":233},"Virtual DOM 深入解析：為什麼它能優化前端效能？","/web_dev/browser/virtual_dom","web_dev/browser/virtual_dom",{"title":235,"path":236,"stem":237,"children":238},"圖形技術","/web_dev/graphics","web_dev/graphics/index",[239,240,244],{"title":235,"path":236,"stem":237},{"title":241,"path":242,"stem":243},"為什麼 Three.js 專案幾乎都選擇 CSR？從 SSR 問題談起","/web_dev/graphics/threejs_csr","web_dev/graphics/threejs_csr",{"title":245,"path":246,"stem":247},"WebGL 是什麼？為什麼前端 3D 幾乎都靠它？","/web_dev/graphics/webgl","web_dev/graphics/webGL",{"title":249,"path":250,"stem":251,"children":252},"架構與配置","/web_dev/infrastructure","web_dev/infrastructure/index",[253,254,258,262,266,270,274,278],{"title":249,"path":250,"stem":251},{"title":255,"path":256,"stem":257},"Zeabur + K3s + Nuxt 部署架構完整解析","/web_dev/infrastructure/k3s-zeabur","web_dev/infrastructure/K3s-zeabur",{"title":259,"path":260,"stem":261},"ECS vs Docker vs Kubernetes：從部署堆疊理解三層架構","/web_dev/infrastructure/ecs-docker-kubernetes-stack","web_dev/infrastructure/ecs-docker-kubernetes-stack",{"title":263,"path":264,"stem":265},"ECS vs VPS 是什麼？從部署網站的角度一次搞懂差別","/web_dev/infrastructure/ecs-vs-vps","web_dev/infrastructure/ecs-vs-vps",{"title":267,"path":268,"stem":269},"Zeabur 內網服務連線完整入門指南","/web_dev/infrastructure/k3s-internal-networking","web_dev/infrastructure/k3s-internal-networking",{"title":271,"path":272,"stem":273},"使用 Microservices 架構設計系統的優勢解析","/web_dev/infrastructure/microservices-architecture-advantages","web_dev/infrastructure/microservices-architecture-advantages",{"title":275,"path":276,"stem":277},"Nginx 入門教學：從反向代理到與 Kubernetes 的架構比較","/web_dev/infrastructure/nginx-intro","web_dev/infrastructure/nginx-intro",{"title":279,"path":280,"stem":281},"YAML 配置是什麼？為什麼現代開發都在用它","/web_dev/infrastructure/yaml-configuration","web_dev/infrastructure/yaml-configuration",{"title":283,"path":284,"stem":285,"children":286},"網絡與通訊","/web_dev/network","web_dev/network/index",[287,288,292,296,300,304,308],{"title":283,"path":284,"stem":285},{"title":289,"path":290,"stem":291},"HTTP Request 結構完整解析：從 Request Line 到 Header 一次看懂","/web_dev/network/http-request-structure","web_dev/network/http-request-structure",{"title":293,"path":294,"stem":295},"OSI 模型（Open Systems Interconnection Model）完整解析","/web_dev/network/osi-model","web_dev/network/osi-model",{"title":297,"path":298,"stem":299},"RESTful API 設計原則與實務解析","/web_dev/network/restful-api","web_dev/network/restful-api",{"title":301,"path":302,"stem":303},"RESTful API 五大設計原則深入解析","/web_dev/network/restful-api-principles","web_dev/network/restful-api-principles",{"title":305,"path":306,"stem":307},"Server-Sent Events (SSE) 深入解析與實作教學","/web_dev/network/sse-introduction","web_dev/network/sse-introduction",{"title":309,"path":310,"stem":311},"WebSocket 入門教學：從概念到 Node.js 實作","/web_dev/network/websocket-introduction","web_dev/network/websocket-introduction",{"title":313,"path":314,"stem":315,"children":316},"認證與安全","/web_dev/security","web_dev/security/index",[317,318,322,326,330],{"title":313,"path":314,"stem":315},{"title":319,"path":320,"stem":321},"使用 Cookie 與 Session 建立使用者驗證（完整新手教學）","/web_dev/security/cookie-session-authentication","web_dev/security/cookie-session-authentication",{"title":323,"path":324,"stem":325},"JWT 驗證機制完整解析：從登入流程到實務應用","/web_dev/security/jwt-authentication","web_dev/security/jwt-authentication",{"title":327,"path":328,"stem":329},"Secret Key 簽名是什麼？從零理解資料簽名的本質","/web_dev/security/secret-key-signing","web_dev/security/secret-key-signing",{"title":331,"path":332,"stem":333},"SSH（Secure Shell）是什麼？從遠端登入到安全通道的核心概念","/web_dev/security/ssh","web_dev/security/ssh",{"title":335,"path":336,"stem":337,"children":338},"SEO 與規範","/web_dev/seo","web_dev/seo/index",[339,340,344,348],{"title":335,"path":336,"stem":337},{"title":341,"path":342,"stem":343},"Canonical URL 是什麼？用生活化方式搞懂前端 SEO 的基本保命符","/web_dev/seo/canonical_link","web_dev/seo/canonical_link",{"title":345,"path":346,"stem":347},"robots.txt 是什麼？SEO 的第一道守門員","/web_dev/seo/robot_txt","web_dev/seo/robot_txt",{"title":349,"path":350,"stem":351},"Nuxt SEO 中的 Meta 與 SEO 工具是如何運作的？","/web_dev/seo/seo","web_dev/seo/seo",{"id":353,"title":26,"body":354,"description":1172,"extension":1173,"links":1174,"meta":1175,"navigation":810,"path":27,"seo":1182,"stem":28,"__hash__":1183},"docs/ai/RAG/rag-intro-principles-and-implementation.md",{"type":355,"value":356,"toc":1146},"minimark",[357,362,366,374,377,394,400,403,409,415,418,422,425,437,440,452,455,466,469,505,508,527,529,533,536,541,544,561,564,570,576,580,583,589,592,603,606,610,617,623,626,629,640,644,650,653,667,674,678,681,689,695,706,710,713,721,724,730,733,735,739,742,748,751,753,757,1008,1011,1022,1025,1027,1031,1034,1037,1043,1046,1049,1052,1062,1065,1069,1072,1075,1081,1084,1086,1090,1093,1097,1100,1104,1112,1116,1119,1121,1124,1135,1142],[358,359,361],"h2",{"id":360},"為什麼大型語言模型需要-rag","為什麼大型語言模型需要 RAG？",[363,364,365],"p",{},"近年來大型語言模型（Large Language Models, LLM）在自然語言處理領域取得了非常驚人的成果，例如像是 ChatGPT 或其他生成式 AI，都能夠回答問題、撰寫文章、甚至協助寫程式。",[363,367,368,369,373],{},"然而，這些模型仍然存在一個重要限制：",[370,371,372],"strong",{},"它們的知識主要來自於訓練資料，而不是即時資料來源","。",[363,375,376],{},"這會帶來幾個常見問題：",[378,379,380,384,391],"ul",{},[381,382,383],"li",{},"模型可能不知道最新資訊（例如公司最新政策或產品文件）",[381,385,386,387,390],{},"模型可能產生 ",[370,388,389],{},"hallucination（幻覺）","，也就是看似合理但實際錯誤的回答",[381,392,393],{},"企業場景中的內部資料（知識庫、技術文件、客戶資料、研究報告）通常不會拿去重訓模型",[363,395,396,397,373],{},"因此，一種非常重要的技術就出現了：",[370,398,399],{},"Retrieval-Augmented Generation（RAG）",[363,401,402],{},"RAG 的核心概念是：",[404,405,406],"blockquote",{},[363,407,408],{},"在生成答案之前，先從外部知識庫「檢索（retrieve）」相關資料，再把資料交給 LLM 生成答案。",[363,410,411,412,373],{},"換句話說，模型不再只依賴訓練時的知識，而是可以 ",[370,413,414],{},"即時查詢資料庫並生成回答",[416,417],"hr",{},[358,419,421],{"id":420},"rag-的基本概念","RAG 的基本概念",[363,423,424],{},"Retrieval-Augmented Generation 可以從字面理解為兩個部分：",[378,426,427,432],{},[381,428,429],{},[370,430,431],{},"Retrieval（檢索）",[381,433,434],{},[370,435,436],{},"Generation（生成）",[363,438,439],{},"整個流程可以簡化為三個步驟：",[441,442,443,446,449],"ol",{},[381,444,445],{},"使用者提出問題",[381,447,448],{},"系統先到知識庫搜尋相關文件",[381,450,451],{},"將文件與問題一起交給 LLM 生成答案",[363,453,454],{},"流程概念如下：",[456,457,463],"pre",{"className":458,"code":460,"language":461,"meta":462},[459],"language-text","User Question\n   │\n   ▼\nVector Search（檢索相關文件）\n   │\n   ▼\nRelevant Documents\n   │\n   ▼\nLLM（生成答案）\n   │\n   ▼\nFinal Response\n","text","",[464,465,460],"code",{"__ignoreMap":462},[363,467,468],{},"與傳統 LLM 的差異：",[470,471,472,485],"table",{},[473,474,475],"thead",{},[476,477,478,482],"tr",{},[479,480,481],"th",{},"方法",[479,483,484],{},"知識來源",[486,487,488,497],"tbody",{},[476,489,490,494],{},[491,492,493],"td",{},"Traditional LLM",[491,495,496],{},"模型訓練資料",[476,498,499,502],{},[491,500,501],{},"RAG",[491,503,504],{},"外部知識庫 + 模型",[363,506,507],{},"因此 RAG 可以大幅提升：",[378,509,510,516,521],{},[381,511,512,513],{},"回答的",[370,514,515],{},"準確度",[381,517,512,518],{},[370,519,520],{},"可解釋性",[381,522,523,524],{},"系統的",[370,525,526],{},"可更新性",[416,528],{},[358,530,532],{"id":531},"rag-系統的核心架構","RAG 系統的核心架構",[363,534,535],{},"一個完整的 RAG 系統通常包含以下幾個核心組件。",[537,538,540],"h3",{"id":539},"_1-文件資料來源documents","1. 文件資料來源（Documents）",[363,542,543],{},"首先需要一個知識來源，例如：",[378,545,546,549,552,555,558],{},[381,547,548],{},"PDF 文件",[381,550,551],{},"技術文件",[381,553,554],{},"Wiki",[381,556,557],{},"企業知識庫",[381,559,560],{},"資料庫資料",[363,562,563],{},"例如：",[456,565,568],{"className":566,"code":567,"language":461,"meta":462},[459],"company_docs/\n├── policy.pdf\n├── product_manual.pdf\n└── engineering_notes.md\n",[464,569,567],{"__ignoreMap":462},[363,571,572,573,373],{},"這些文件就是 RAG 系統的 ",[370,574,575],{},"知識基礎",[537,577,579],{"id":578},"_2-文件切分chunking","2. 文件切分（Chunking）",[363,581,582],{},"LLM 無法一次讀取非常長的文件，因此通常需要先把文件切成較小的片段（chunk）。",[456,584,587],{"className":585,"code":586,"language":461,"meta":462},[459],"Original document\n   ↓\nSplit into chunks\nchunk 1\nchunk 2\nchunk 3\nchunk 4\n",[464,588,586],{"__ignoreMap":462},[363,590,591],{},"常見切分策略：",[378,593,594,597,600],{},[381,595,596],{},"固定長度切分（如 500 tokens）",[381,598,599],{},"句子或段落切分",[381,601,602],{},"Sliding window",[363,604,605],{},"好的 chunking 策略會顯著影響 RAG 效果。",[537,607,609],{"id":608},"_3-向量化embedding","3. 向量化（Embedding）",[363,611,612,613,616],{},"接著把每個文字 chunk 轉換成 ",[370,614,615],{},"向量（vector）","，這個過程稱為 embedding。",[456,618,621],{"className":619,"code":620,"language":461,"meta":462},[459],"\"RAG improves LLM accuracy\"\n    ↓ embedding model\n[0.23, -0.91, 0.44, ...]\n",[464,622,620],{"__ignoreMap":462},[363,624,625],{},"embedding 的目的，是讓語意相似的文字在向量空間中彼此接近。",[363,627,628],{},"常見 embedding 模型：",[378,630,631,634,637],{},[381,632,633],{},"OpenAI embedding models",[381,635,636],{},"sentence-transformers",[381,638,639],{},"BGE embeddings",[537,641,643],{"id":642},"_4-向量資料庫vector-database","4. 向量資料庫（Vector Database）",[363,645,646,647,373],{},"當文件都轉換成向量後，就可以存進 ",[370,648,649],{},"向量資料庫（vector database）",[363,651,652],{},"常見選擇：",[378,654,655,658,661,664],{},[381,656,657],{},"FAISS",[381,659,660],{},"Milvus",[381,662,663],{},"Pinecone",[381,665,666],{},"Weaviate",[363,668,669,670,673],{},"這些資料庫支援 ",[370,671,672],{},"semantic search（語意搜尋）","：比對語意，而不只是關鍵字。",[537,675,677],{"id":676},"_5-retrieval語意搜尋","5. Retrieval（語意搜尋）",[363,679,680],{},"使用者提出問題時，系統會：",[441,682,683,686],{},[381,684,685],{},"把問題轉成 embedding",[381,687,688],{},"在向量資料庫搜尋最相似文件",[456,690,693],{"className":691,"code":692,"language":461,"meta":462},[459],"User question: \"How does RAG work?\"\n   ↓ embedding\nSearch similar vectors\n   ↓\nTop 3 relevant chunks\n",[464,694,692],{"__ignoreMap":462},[363,696,697,698,701,702,705],{},"這一步常使用 ",[370,699,700],{},"cosine similarity"," 或 ",[370,703,704],{},"inner product"," 計算相似度。",[537,707,709],{"id":708},"_6-generationllm-生成回答","6. Generation（LLM 生成回答）",[363,711,712],{},"最後把：",[378,714,715,718],{},[381,716,717],{},"使用者問題",[381,719,720],{},"檢索到的文件",[363,722,723],{},"一起放進 prompt，交給 LLM 生成答案。",[456,725,728],{"className":726,"code":727,"language":461,"meta":462},[459],"Context: [retrieved documents]\nQuestion: How does RAG work?\nAnswer:\n",[464,729,727],{"__ignoreMap":462},[363,731,732],{},"這也是 RAG 能降低 hallucination 的關鍵：回答會更受檢索內容約束。",[416,734],{},[358,736,738],{"id":737},"rag-的完整流程","RAG 的完整流程",[363,740,741],{},"整合起來，一個典型 RAG pipeline 如下：",[456,743,746],{"className":744,"code":745,"language":461,"meta":462},[459],"Documents\n   │\n   ▼\nChunking\n   │\n   ▼\nEmbedding\n   │\n   ▼\nVector Database\n   │\n   ▼\nUser Question\n   │\n   ▼\nEmbedding\n   │\n   ▼\nVector Search\n   │\n   ▼\nRelevant Context\n   │\n   ▼\nLLM Generation\n   │\n   ▼\nFinal Answer\n",[464,747,745],{"__ignoreMap":462},[363,749,750],{},"在實務中，這個 pipeline 通常會被包裝成 API 或聊天系統。",[416,752],{},[358,754,756],{"id":755},"一個簡單的-rag-python-範例","一個簡單的 RAG Python 範例",[456,758,762],{"className":759,"code":760,"language":761,"meta":462,"style":462},"language-python shiki shiki-themes github-dark","from sentence_transformers import SentenceTransformer\nimport faiss\nimport numpy as np\n\ndocs = [\n    \"RAG combines retrieval and generation\",\n    \"Vector databases enable semantic search\",\n    \"LLMs generate natural language answers\"\n]\n\nmodel = SentenceTransformer(\"all-MiniLM-L6-v2\")\ndoc_embeddings = model.encode(docs)\n\nindex = faiss.IndexFlatL2(doc_embeddings.shape[1])\nindex.add(np.array(doc_embeddings))\n\nquery = \"What is RAG?\"\nquery_embedding = model.encode([query])\nD, I = index.search(np.array(query_embedding), k=2)\n\nretrieved_docs = [docs[i] for i in I[0]]\nprint(retrieved_docs)\n","python",[464,763,764,783,791,805,812,824,834,842,848,854,859,876,887,892,910,916,921,932,943,965,970,999],{"__ignoreMap":462},[765,766,769,773,777,780],"span",{"class":767,"line":768},"line",1,[765,770,772],{"class":771},"snl16","from",[765,774,776],{"class":775},"s95oV"," sentence_transformers ",[765,778,779],{"class":771},"import",[765,781,782],{"class":775}," SentenceTransformer\n",[765,784,786,788],{"class":767,"line":785},2,[765,787,779],{"class":771},[765,789,790],{"class":775}," faiss\n",[765,792,794,796,799,802],{"class":767,"line":793},3,[765,795,779],{"class":771},[765,797,798],{"class":775}," numpy ",[765,800,801],{"class":771},"as",[765,803,804],{"class":775}," np\n",[765,806,808],{"class":767,"line":807},4,[765,809,811],{"emptyLinePlaceholder":810},true,"\n",[765,813,815,818,821],{"class":767,"line":814},5,[765,816,817],{"class":775},"docs ",[765,819,820],{"class":771},"=",[765,822,823],{"class":775}," [\n",[765,825,827,831],{"class":767,"line":826},6,[765,828,830],{"class":829},"sU2Wk","    \"RAG combines retrieval and generation\"",[765,832,833],{"class":775},",\n",[765,835,837,840],{"class":767,"line":836},7,[765,838,839],{"class":829},"    \"Vector databases enable semantic search\"",[765,841,833],{"class":775},[765,843,845],{"class":767,"line":844},8,[765,846,847],{"class":829},"    \"LLMs generate natural language answers\"\n",[765,849,851],{"class":767,"line":850},9,[765,852,853],{"class":775},"]\n",[765,855,857],{"class":767,"line":856},10,[765,858,811],{"emptyLinePlaceholder":810},[765,860,862,865,867,870,873],{"class":767,"line":861},11,[765,863,864],{"class":775},"model ",[765,866,820],{"class":771},[765,868,869],{"class":775}," SentenceTransformer(",[765,871,872],{"class":829},"\"all-MiniLM-L6-v2\"",[765,874,875],{"class":775},")\n",[765,877,879,882,884],{"class":767,"line":878},12,[765,880,881],{"class":775},"doc_embeddings ",[765,883,820],{"class":771},[765,885,886],{"class":775}," model.encode(docs)\n",[765,888,890],{"class":767,"line":889},13,[765,891,811],{"emptyLinePlaceholder":810},[765,893,895,898,900,903,907],{"class":767,"line":894},14,[765,896,897],{"class":775},"index ",[765,899,820],{"class":771},[765,901,902],{"class":775}," faiss.IndexFlatL2(doc_embeddings.shape[",[765,904,906],{"class":905},"sDLfK","1",[765,908,909],{"class":775},"])\n",[765,911,913],{"class":767,"line":912},15,[765,914,915],{"class":775},"index.add(np.array(doc_embeddings))\n",[765,917,919],{"class":767,"line":918},16,[765,920,811],{"emptyLinePlaceholder":810},[765,922,924,927,929],{"class":767,"line":923},17,[765,925,926],{"class":775},"query ",[765,928,820],{"class":771},[765,930,931],{"class":829}," \"What is RAG?\"\n",[765,933,935,938,940],{"class":767,"line":934},18,[765,936,937],{"class":775},"query_embedding ",[765,939,820],{"class":771},[765,941,942],{"class":775}," model.encode([query])\n",[765,944,946,949,951,954,958,960,963],{"class":767,"line":945},19,[765,947,948],{"class":775},"D, I ",[765,950,820],{"class":771},[765,952,953],{"class":775}," index.search(np.array(query_embedding), ",[765,955,957],{"class":956},"s9osk","k",[765,959,820],{"class":771},[765,961,962],{"class":905},"2",[765,964,875],{"class":775},[765,966,968],{"class":767,"line":967},20,[765,969,811],{"emptyLinePlaceholder":810},[765,971,973,976,978,981,984,987,990,993,996],{"class":767,"line":972},21,[765,974,975],{"class":775},"retrieved_docs ",[765,977,820],{"class":771},[765,979,980],{"class":775}," [docs[i] ",[765,982,983],{"class":771},"for",[765,985,986],{"class":775}," i ",[765,988,989],{"class":771},"in",[765,991,992],{"class":775}," I[",[765,994,995],{"class":905},"0",[765,997,998],{"class":775},"]]\n",[765,1000,1002,1005],{"class":767,"line":1001},22,[765,1003,1004],{"class":905},"print",[765,1006,1007],{"class":775},"(retrieved_docs)\n",[363,1009,1010],{},"這段程式碼示範了 RAG 核心概念：",[441,1012,1013,1016,1019],{},[381,1014,1015],{},"文件轉 embedding",[381,1017,1018],{},"建立向量索引",[381,1020,1021],{},"搜尋最相關文件",[363,1023,1024],{},"在實際系統中，這些文件會再送入 LLM 生成最終回答。",[416,1026],{},[358,1028,1030],{"id":1029},"rag-在實務中的應用場景","RAG 在實務中的應用場景",[363,1032,1033],{},"RAG 已成為現代 AI 系統常見架構，應用包括：",[537,1035,1036],{"id":1036},"企業知識庫問答",[456,1038,1041],{"className":1039,"code":1040,"language":461,"meta":462},[459],"Q: 公司報銷流程是什麼？\n",[464,1042,1040],{"__ignoreMap":462},[363,1044,1045],{},"系統先搜尋公司政策文件，再生成答案。",[537,1047,1048],{"id":1048},"文件搜尋與摘要",[363,1050,1051],{},"適用於：",[378,1053,1054,1057,1060],{},[381,1055,1056],{},"法律文件",[381,1058,1059],{},"研究論文",[381,1061,551],{},[363,1063,1064],{},"可快速定位相關段落並生成摘要。",[537,1066,1068],{"id":1067},"ai-客服","AI 客服",[363,1070,1071],{},"可連接 FAQ、產品文件、支援文件，生成更精準回覆。",[537,1073,1074],{"id":1074},"程式碼助理",[456,1076,1079],{"className":1077,"code":1078,"language":461,"meta":462},[459],"How to use this API?\n",[464,1080,1078],{"__ignoreMap":462},[363,1082,1083],{},"可先檢索程式文件再生成說明。",[416,1085],{},[358,1087,1089],{"id":1088},"rag-的限制與挑戰","RAG 的限制與挑戰",[363,1091,1092],{},"雖然 RAG 很強大，但仍有挑戰：",[537,1094,1096],{"id":1095},"retrieval-quality","Retrieval quality",[363,1098,1099],{},"若檢索結果不相關，LLM 回答仍可能錯誤。",[537,1101,1103],{"id":1102},"chunking-strategy","Chunking strategy",[378,1105,1106,1109],{},[381,1107,1108],{},"chunk 太小：上下文不足",[381,1110,1111],{},"chunk 太大：搜尋精準度下降",[537,1113,1115],{"id":1114},"latency","Latency",[363,1117,1118],{},"RAG 涉及 embedding、vector search、LLM generation，延遲通常高於純 LLM。",[416,1120],{},[358,1122,1123],{"id":1123},"結論",[363,1125,1126,1127,1130,1131,1134],{},"Retrieval-Augmented Generation（RAG）是一種把 ",[370,1128,1129],{},"資訊檢索（IR）"," 與 ",[370,1132,1133],{},"大型語言模型（LLM）"," 結合的架構。透過先檢索再生成，RAG 能讓模型使用外部知識，提升回答準確性與可信度。",[363,1136,1137,1138,1141],{},"RAG 已廣泛用於企業知識庫、文件搜尋、AI 客服與程式碼助理等場景。隨著向量資料庫與 embedding 技術進步，RAG 也逐漸成為打造 ",[370,1139,1140],{},"企業級 AI 系統"," 的核心技術之一。",[1143,1144,1145],"style",{},"html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .s9osk, html code.shiki .s9osk{--shiki-default:#FFAB70}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":462,"searchDepth":785,"depth":785,"links":1147},[1148,1149,1150,1158,1159,1160,1166,1171],{"id":360,"depth":785,"text":361},{"id":420,"depth":785,"text":421},{"id":531,"depth":785,"text":532,"children":1151},[1152,1153,1154,1155,1156,1157],{"id":539,"depth":793,"text":540},{"id":578,"depth":793,"text":579},{"id":608,"depth":793,"text":609},{"id":642,"depth":793,"text":643},{"id":676,"depth":793,"text":677},{"id":708,"depth":793,"text":709},{"id":737,"depth":785,"text":738},{"id":755,"depth":785,"text":756},{"id":1029,"depth":785,"text":1030,"children":1161},[1162,1163,1164,1165],{"id":1036,"depth":793,"text":1036},{"id":1048,"depth":793,"text":1048},{"id":1067,"depth":793,"text":1068},{"id":1074,"depth":793,"text":1074},{"id":1088,"depth":785,"text":1089,"children":1167},[1168,1169,1170],{"id":1095,"depth":793,"text":1096},{"id":1102,"depth":793,"text":1103},{"id":1114,"depth":793,"text":1115},{"id":1123,"depth":785,"text":1123},"介紹 Retrieval-Augmented Generation（RAG）的核心概念、系統架構與實作流程，理解大型語言模型如何透過外部知識提升回答品質。","md",null,{"tags":1176,"category":501,"date":1181},[1177,1178,7,1179,1180],"rag","llm","vector-database","machine-learning","2026-03-13",{"title":26,"description":1172},"nWlm0KWG8aKZxBjpReJ5vpG8QE031MWYfak2_WWl3RE",[1185,1187],{"title":22,"path":23,"stem":24,"description":1186,"children":-1},"從原理到實務應用，完整理解 Dense Retrieval 在現代搜尋與 RAG 系統中的角色與運作方式。",{"title":30,"path":31,"stem":32,"description":1188,"children":-1},"從 BM25、Dense Retrieval 到 Hybrid 與 Reranker，完整理解 RAG 系統中的檢索策略與其對模型品質的影響。",1776690844615]