[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"i-ri:search-line":3,"i-ri:moon-line":8,"i-ri:sun-line":10,"i-ri:menu-3-line":12,"i-ri:close-line":14,"notes-module-items-ai":16,"notes-page-ai-03-rag\u002F01-vector-db-and-embeddings-node":74,"i-ri:arrow-left-s-line":1275,"i-ri:menu-2-line":1277,"i-ri:arrow-right-s-line":1279,"i-ep:copy-document":1281},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"currentColor\" d=\"m18.031 16.617l4.283 4.282l-1.415 1.415l-4.282-4.283A8.96 8.96 0 0 1 11 20c-4.968 0-9-4.032-9-9s4.032-9 9-9s9 4.032 9 9a8.96 8.96 0 0 1-1.969 5.617m-2.006-.742A6.98 6.98 0 0 0 18 11c0-3.867-3.133-7-7-7s-7 3.133-7 7s3.133 7 7 7a6.98 6.98 0 0 0 4.875-1.975z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"currentColor\" d=\"M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12S6.477 2 12 2h.1A6.98 6.98 0 0 0 10 7m-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938A8 8 0 0 0 4 12\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":11},"\u003Cpath fill=\"currentColor\" d=\"M12 18a6 6 0 1 1 0-12a6 6 0 0 1 0 12m0-2a4 4 0 1 0 0-8a4 4 0 0 0 0 8M11 1h2v3h-2zm0 19h2v3h-2zM3.515 4.929l1.414-1.414L7.05 5.636L5.636 7.05zM16.95 18.364l1.414-1.414l2.121 2.121l-1.414 1.414zm2.121-14.85l1.414 1.415l-2.121 2.121l-1.414-1.414zM5.636 16.95l1.414 1.414l-2.121 2.121l-1.414-1.414zM23 11v2h-3v-2zM4 11v2H1v-2z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":13},"\u003Cpath fill=\"currentColor\" d=\"M3 4h18v2H3zm6 7h12v2H9zm-6 7h18v2H3z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":15},"\u003Cpath fill=\"currentColor\" d=\"m12 10.587l4.95-4.95l1.414 1.414l-4.95 4.95l4.95 4.95l-1.415 1.414l-4.95-4.95l-4.949 4.95l-1.414-1.415l4.95-4.95l-4.95-4.95L7.05 5.638z\"\u002F>",[17,22,26,30,34,38,42,46,50,54,58,62,66,70],{"path":18,"title":19,"meta":20},"\u002Fnotes\u002Fai\u002F01-llm-basics\u002F01-hello-llm","LLM 交互基础：透过现象看本质",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},"2026-11-05T09:47:15.000Z",{"path":23,"title":24,"meta":25},"\u002Fnotes\u002Fai\u002F01-llm-basics\u002F02-streaming-and-prompts","进阶交互：流式传输与提示词工程",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":27,"title":28,"meta":29},"\u002Fnotes\u002Fai\u002F01-llm-basics\u002F03-function-calling","结构化输出与 Function Calling",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":31,"title":32,"meta":33},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F01-tool-use","Agent 核心能力 ①：Tool Use（工具调用）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":35,"title":36,"meta":37},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F02-mini-cursor-cli","实战项目：Mini Cursor（CLI 版）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":39,"title":40,"meta":41},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F03-memory","Agent 核心能力 ②：Memory（记忆管理）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":43,"title":44,"meta":45},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F04-planning","Agent 核心能力 ③：Planning（规划能力）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":47,"title":48,"meta":49},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F05-review-diff-chunking","代码改了很多，AI 为什么只审了前面？",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":51,"title":52,"meta":53},"\u002Fnotes\u002Fai\u002F02-agent-architecture","Agent 架构笔记",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":55,"title":56,"meta":57},"\u002Fnotes\u002Fai\u002F03-rag\u002F01-vector-db-and-embeddings-node","RAG ①：Embeddings 与向量数据库入门（Node.js 优先）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":59,"title":60,"meta":61},"\u002Fnotes\u002Fai\u002F04-document-extraction\u002F01-ocr-field-evidence","OCR 读对了字，为什么地址仍然错？",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":63,"title":64,"meta":65},"\u002Fnotes\u002Fai\u002F05-development-workflow\u002F01-task-recovery","AI 任务做一半，中断后怎么继续？",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":67,"title":68,"meta":69},"\u002Fnotes\u002Fai","AI 应用开发",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":71,"title":72,"meta":73},"\u002Fnotes\u002Fai\u002Froadmap","AI Agent 全栈开发学习路线图 (Roadmap)",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"id":75,"type":76,"sourcePath":77,"routePath":55,"path":55,"title":56,"updatedAt":21,"meta":78,"body":80,"toc":1266},"bm90ZTpub3Rlcy9haS8wMy1yYWcvMDEtdmVjdG9yLWRiLWFuZC1lbWJlZGRpbmdzLW5vZGUubWQ","note","notes\u002Fai\u002F03-rag\u002F01-vector-db-and-embeddings-node.md",{"id":75,"type":76,"sourcePath":77,"slug":79,"title":56,"updatedAt":21,"lastUpdated":21},"ai\u002F03-rag\u002F01-vector-db-and-embeddings-node",{"type":81,"children":82},"root",[83,91,114,118,125,130,135,150,155,173,176,182,187,220,225,238,241,247,252,285,290,293,299,304,329,713,718,731,734,740,745,781,786,1096,1101,1104,1110,1123,1147,1152,1223,1226,1232,1237,1255,1260],{"type":84,"tag":85,"props":86,"children":88},"element","h1",{"id":87},"rag-embeddings-与向量数据库入门nodejs-优先",[89],{"type":90,"value":56},"text",{"type":84,"tag":92,"props":93,"children":94},"blockquote",{},[95],{"type":84,"tag":96,"props":97,"children":98},"p",{},[99,105,107,112],{"type":84,"tag":100,"props":101,"children":102},"strong",{},[103],{"type":90,"value":104},"摘要",{"type":90,"value":106},"：RAG（Retrieval-Augmented Generation）的第一步不是“让模型更强”，而是把你的文档变成一种",{"type":84,"tag":100,"props":108,"children":109},{},[110],{"type":90,"value":111},"可检索的索引",{"type":90,"value":113},"。Embeddings 负责把文本映射到向量；向量数据库负责存储与相似度搜索。",{"type":84,"tag":115,"props":116,"children":117},"hr",{},[],{"type":84,"tag":119,"props":120,"children":122},"h2",{"id":121},"_1-embeddings把文本变成向量",[123],{"type":90,"value":124},"1. Embeddings：把文本变成向量",{"type":84,"tag":96,"props":126,"children":127},{},[128],{"type":90,"value":129},"Embedding 模型会把一段文本编码成一个高维向量（例如 1536\u002F3072 维）。语义越接近，向量越接近。",{"type":84,"tag":96,"props":131,"children":132},{},[133],{"type":90,"value":134},"你可以把它理解成：",{"type":84,"tag":136,"props":137,"children":138},"ul",{},[139,145],{"type":84,"tag":140,"props":141,"children":142},"li",{},[143],{"type":90,"value":144},"传统搜索：靠关键词匹配",{"type":84,"tag":140,"props":146,"children":147},{},[148],{"type":90,"value":149},"向量搜索：靠“语义距离”",{"type":84,"tag":96,"props":151,"children":152},{},[153],{"type":90,"value":154},"典型用途：",{"type":84,"tag":136,"props":156,"children":157},{},[158,163,168],{"type":84,"tag":140,"props":159,"children":160},{},[161],{"type":90,"value":162},"文档\u002FFAQ 检索",{"type":84,"tag":140,"props":164,"children":165},{},[166],{"type":90,"value":167},"相似问答",{"type":84,"tag":140,"props":169,"children":170},{},[171],{"type":90,"value":172},"聚类、去重",{"type":84,"tag":115,"props":174,"children":175},{},[],{"type":84,"tag":119,"props":177,"children":179},{"id":178},"_2-相似度指标你需要知道的最少知识",[180],{"type":90,"value":181},"2. 相似度指标（你需要知道的最少知识）",{"type":84,"tag":96,"props":183,"children":184},{},[185],{"type":90,"value":186},"常见三种：",{"type":84,"tag":136,"props":188,"children":189},{},[190,200,210],{"type":84,"tag":140,"props":191,"children":192},{},[193,198],{"type":84,"tag":100,"props":194,"children":195},{},[196],{"type":90,"value":197},"Cosine",{"type":90,"value":199},"（余弦相似度）：最常用",{"type":84,"tag":140,"props":201,"children":202},{},[203,208],{"type":84,"tag":100,"props":204,"children":205},{},[206],{"type":90,"value":207},"IP",{"type":90,"value":209},"（Inner Product）：有时等价于 cosine（取决于向量是否归一化）",{"type":84,"tag":140,"props":211,"children":212},{},[213,218],{"type":84,"tag":100,"props":214,"children":215},{},[216],{"type":90,"value":217},"L2",{"type":90,"value":219},"（欧氏距离）",{"type":84,"tag":96,"props":221,"children":222},{},[223],{"type":90,"value":224},"工程上你主要关注：",{"type":84,"tag":136,"props":226,"children":227},{},[228,233],{"type":84,"tag":140,"props":229,"children":230},{},[231],{"type":90,"value":232},"你选的向量库支持哪种 metric",{"type":84,"tag":140,"props":234,"children":235},{},[236],{"type":90,"value":237},"embedding 模型输出是否已归一化（normalized）",{"type":84,"tag":115,"props":239,"children":240},{},[],{"type":84,"tag":119,"props":242,"children":244},{"id":243},"_3-向量数据库选型milvus-chroma-pgvector",[245],{"type":90,"value":246},"3. 向量数据库选型：Milvus \u002F Chroma \u002F pgvector",{"type":84,"tag":96,"props":248,"children":249},{},[250],{"type":90,"value":251},"roadmap 里提到三类：",{"type":84,"tag":136,"props":253,"children":254},{},[255,265,275],{"type":84,"tag":140,"props":256,"children":257},{},[258,263],{"type":84,"tag":100,"props":259,"children":260},{},[261],{"type":90,"value":262},"Milvus",{"type":90,"value":264},"：生产级，能力全（适合大规模、团队协作）",{"type":84,"tag":140,"props":266,"children":267},{},[268,273],{"type":84,"tag":100,"props":269,"children":270},{},[271],{"type":90,"value":272},"Chroma",{"type":90,"value":274},"：轻量，本地开发友好（适合 demo\u002F小项目）",{"type":84,"tag":140,"props":276,"children":277},{},[278,283],{"type":84,"tag":100,"props":279,"children":280},{},[281],{"type":90,"value":282},"pgvector",{"type":90,"value":284},"：把向量能力放进 PostgreSQL（适合已有 PG 体系的团队）",{"type":84,"tag":96,"props":286,"children":287},{},[288],{"type":90,"value":289},"本文先不绑定具体向量库，先把“embedding + upsert + query”的抽象概念讲清楚。",{"type":84,"tag":115,"props":291,"children":292},{},[],{"type":84,"tag":119,"props":294,"children":296},{"id":295},"_4-nodejs计算-embeddingopenai-兼容",[297],{"type":90,"value":298},"4. Node.js：计算 Embedding（OpenAI 兼容）",{"type":84,"tag":96,"props":300,"children":301},{},[302],{"type":90,"value":303},"下面示例以 OpenAI 风格 SDK 为例（其它兼容平台结构类似）。",{"type":84,"tag":92,"props":305,"children":306},{},[307],{"type":84,"tag":96,"props":308,"children":309},{},[310,312,319,321,327],{"type":90,"value":311},"你只需要替换 ",{"type":84,"tag":313,"props":314,"children":316},"code",{"className":315},[],[317],{"type":90,"value":318},"baseURL",{"type":90,"value":320},"（如果不是 OpenAI）和 ",{"type":84,"tag":313,"props":322,"children":324},{"className":323},[],[325],{"type":90,"value":326},"apiKey",{"type":90,"value":328},"。",{"type":84,"tag":330,"props":331,"children":336},"pre",{"className":332,"code":333,"language":334,"meta":335,"style":335},"language-js shiki shiki-themes github-light github-dark","import OpenAI from \"openai\";\n\nconst client = new OpenAI({\n  apiKey: process.env.OPENAI_API_KEY,\n  \u002F\u002F baseURL: \"https:\u002F\u002Fxxx\u002Fv1\" \u002F\u002F 如果你用的是兼容 OpenAI 的第三方平台\n});\n\nexport async function embed(text) {\n  const resp = await client.embeddings.create({\n    model: \"text-embedding-3-small\",\n    input: text,\n  });\n\n  return resp.data[0].embedding; \u002F\u002F number[]\n}\n\n\u002F\u002F demo\nconst v = await embed(\"RAG 是什么？\");\nconsole.log(v.length);\n","js","",[337],{"type":84,"tag":313,"props":338,"children":339},{"__ignoreMap":335},[340,374,384,420,439,454,463,471,510,547,565,574,583,591,620,629,637,646,685],{"type":84,"tag":341,"props":342,"children":345},"span",{"class":343,"line":344},"line",1,[346,352,358,363,369],{"type":84,"tag":341,"props":347,"children":349},{"style":348},"--shiki-default:#D73A49;--shiki-dark:#F97583",[350],{"type":90,"value":351},"import",{"type":84,"tag":341,"props":353,"children":355},{"style":354},"--shiki-default:#24292E;--shiki-dark:#E1E4E8",[356],{"type":90,"value":357}," OpenAI ",{"type":84,"tag":341,"props":359,"children":360},{"style":348},[361],{"type":90,"value":362},"from",{"type":84,"tag":341,"props":364,"children":366},{"style":365},"--shiki-default:#032F62;--shiki-dark:#9ECBFF",[367],{"type":90,"value":368}," \"openai\"",{"type":84,"tag":341,"props":370,"children":371},{"style":354},[372],{"type":90,"value":373},";\n",{"type":84,"tag":341,"props":375,"children":377},{"class":343,"line":376},2,[378],{"type":84,"tag":341,"props":379,"children":381},{"emptyLinePlaceholder":380},true,[382],{"type":90,"value":383},"\n",{"type":84,"tag":341,"props":385,"children":387},{"class":343,"line":386},3,[388,393,399,404,409,415],{"type":84,"tag":341,"props":389,"children":390},{"style":348},[391],{"type":90,"value":392},"const",{"type":84,"tag":341,"props":394,"children":396},{"style":395},"--shiki-default:#005CC5;--shiki-dark:#79B8FF",[397],{"type":90,"value":398}," client",{"type":84,"tag":341,"props":400,"children":401},{"style":348},[402],{"type":90,"value":403}," =",{"type":84,"tag":341,"props":405,"children":406},{"style":348},[407],{"type":90,"value":408}," new",{"type":84,"tag":341,"props":410,"children":412},{"style":411},"--shiki-default:#6F42C1;--shiki-dark:#B392F0",[413],{"type":90,"value":414}," OpenAI",{"type":84,"tag":341,"props":416,"children":417},{"style":354},[418],{"type":90,"value":419},"({\n",{"type":84,"tag":341,"props":421,"children":423},{"class":343,"line":422},4,[424,429,434],{"type":84,"tag":341,"props":425,"children":426},{"style":354},[427],{"type":90,"value":428},"  apiKey: process.env.",{"type":84,"tag":341,"props":430,"children":431},{"style":395},[432],{"type":90,"value":433},"OPENAI_API_KEY",{"type":84,"tag":341,"props":435,"children":436},{"style":354},[437],{"type":90,"value":438},",\n",{"type":84,"tag":341,"props":440,"children":442},{"class":343,"line":441},5,[443,449],{"type":84,"tag":341,"props":444,"children":446},{"style":445},"--shiki-default:#6A737D;--shiki-dark:#6A737D",[447],{"type":90,"value":448},"  \u002F\u002F baseURL: \"https:\u002F\u002Fxxx\u002Fv1\"",{"type":84,"tag":341,"props":450,"children":451},{"style":445},[452],{"type":90,"value":453}," \u002F\u002F 如果你用的是兼容 OpenAI 的第三方平台\n",{"type":84,"tag":341,"props":455,"children":457},{"class":343,"line":456},6,[458],{"type":84,"tag":341,"props":459,"children":460},{"style":354},[461],{"type":90,"value":462},"});\n",{"type":84,"tag":341,"props":464,"children":466},{"class":343,"line":465},7,[467],{"type":84,"tag":341,"props":468,"children":469},{"emptyLinePlaceholder":380},[470],{"type":90,"value":383},{"type":84,"tag":341,"props":472,"children":474},{"class":343,"line":473},8,[475,480,485,490,495,500,505],{"type":84,"tag":341,"props":476,"children":477},{"style":348},[478],{"type":90,"value":479},"export",{"type":84,"tag":341,"props":481,"children":482},{"style":348},[483],{"type":90,"value":484}," async",{"type":84,"tag":341,"props":486,"children":487},{"style":348},[488],{"type":90,"value":489}," function",{"type":84,"tag":341,"props":491,"children":492},{"style":411},[493],{"type":90,"value":494}," embed",{"type":84,"tag":341,"props":496,"children":497},{"style":354},[498],{"type":90,"value":499},"(",{"type":84,"tag":341,"props":501,"children":503},{"style":502},"--shiki-default:#E36209;--shiki-dark:#FFAB70",[504],{"type":90,"value":90},{"type":84,"tag":341,"props":506,"children":507},{"style":354},[508],{"type":90,"value":509},") {\n",{"type":84,"tag":341,"props":511,"children":513},{"class":343,"line":512},9,[514,519,524,528,533,538,543],{"type":84,"tag":341,"props":515,"children":516},{"style":348},[517],{"type":90,"value":518},"  const",{"type":84,"tag":341,"props":520,"children":521},{"style":395},[522],{"type":90,"value":523}," resp",{"type":84,"tag":341,"props":525,"children":526},{"style":348},[527],{"type":90,"value":403},{"type":84,"tag":341,"props":529,"children":530},{"style":348},[531],{"type":90,"value":532}," await",{"type":84,"tag":341,"props":534,"children":535},{"style":354},[536],{"type":90,"value":537}," client.embeddings.",{"type":84,"tag":341,"props":539,"children":540},{"style":411},[541],{"type":90,"value":542},"create",{"type":84,"tag":341,"props":544,"children":545},{"style":354},[546],{"type":90,"value":419},{"type":84,"tag":341,"props":548,"children":550},{"class":343,"line":549},10,[551,556,561],{"type":84,"tag":341,"props":552,"children":553},{"style":354},[554],{"type":90,"value":555},"    model: ",{"type":84,"tag":341,"props":557,"children":558},{"style":365},[559],{"type":90,"value":560},"\"text-embedding-3-small\"",{"type":84,"tag":341,"props":562,"children":563},{"style":354},[564],{"type":90,"value":438},{"type":84,"tag":341,"props":566,"children":568},{"class":343,"line":567},11,[569],{"type":84,"tag":341,"props":570,"children":571},{"style":354},[572],{"type":90,"value":573},"    input: text,\n",{"type":84,"tag":341,"props":575,"children":577},{"class":343,"line":576},12,[578],{"type":84,"tag":341,"props":579,"children":580},{"style":354},[581],{"type":90,"value":582},"  });\n",{"type":84,"tag":341,"props":584,"children":586},{"class":343,"line":585},13,[587],{"type":84,"tag":341,"props":588,"children":589},{"emptyLinePlaceholder":380},[590],{"type":90,"value":383},{"type":84,"tag":341,"props":592,"children":594},{"class":343,"line":593},14,[595,600,605,610,615],{"type":84,"tag":341,"props":596,"children":597},{"style":348},[598],{"type":90,"value":599},"  return",{"type":84,"tag":341,"props":601,"children":602},{"style":354},[603],{"type":90,"value":604}," resp.data[",{"type":84,"tag":341,"props":606,"children":607},{"style":395},[608],{"type":90,"value":609},"0",{"type":84,"tag":341,"props":611,"children":612},{"style":354},[613],{"type":90,"value":614},"].embedding; ",{"type":84,"tag":341,"props":616,"children":617},{"style":445},[618],{"type":90,"value":619},"\u002F\u002F number[]\n",{"type":84,"tag":341,"props":621,"children":623},{"class":343,"line":622},15,[624],{"type":84,"tag":341,"props":625,"children":626},{"style":354},[627],{"type":90,"value":628},"}\n",{"type":84,"tag":341,"props":630,"children":632},{"class":343,"line":631},16,[633],{"type":84,"tag":341,"props":634,"children":635},{"emptyLinePlaceholder":380},[636],{"type":90,"value":383},{"type":84,"tag":341,"props":638,"children":640},{"class":343,"line":639},17,[641],{"type":84,"tag":341,"props":642,"children":643},{"style":445},[644],{"type":90,"value":645},"\u002F\u002F demo\n",{"type":84,"tag":341,"props":647,"children":649},{"class":343,"line":648},18,[650,654,659,663,667,671,675,680],{"type":84,"tag":341,"props":651,"children":652},{"style":348},[653],{"type":90,"value":392},{"type":84,"tag":341,"props":655,"children":656},{"style":395},[657],{"type":90,"value":658}," v",{"type":84,"tag":341,"props":660,"children":661},{"style":348},[662],{"type":90,"value":403},{"type":84,"tag":341,"props":664,"children":665},{"style":348},[666],{"type":90,"value":532},{"type":84,"tag":341,"props":668,"children":669},{"style":411},[670],{"type":90,"value":494},{"type":84,"tag":341,"props":672,"children":673},{"style":354},[674],{"type":90,"value":499},{"type":84,"tag":341,"props":676,"children":677},{"style":365},[678],{"type":90,"value":679},"\"RAG 是什么？\"",{"type":84,"tag":341,"props":681,"children":682},{"style":354},[683],{"type":90,"value":684},");\n",{"type":84,"tag":341,"props":686,"children":688},{"class":343,"line":687},19,[689,694,699,704,709],{"type":84,"tag":341,"props":690,"children":691},{"style":354},[692],{"type":90,"value":693},"console.",{"type":84,"tag":341,"props":695,"children":696},{"style":411},[697],{"type":90,"value":698},"log",{"type":84,"tag":341,"props":700,"children":701},{"style":354},[702],{"type":90,"value":703},"(v.",{"type":84,"tag":341,"props":705,"children":706},{"style":395},[707],{"type":90,"value":708},"length",{"type":84,"tag":341,"props":710,"children":711},{"style":354},[712],{"type":90,"value":684},{"type":84,"tag":96,"props":714,"children":715},{},[716],{"type":90,"value":717},"工程建议：",{"type":84,"tag":136,"props":719,"children":720},{},[721,726],{"type":84,"tag":140,"props":722,"children":723},{},[724],{"type":90,"value":725},"对输入做去噪（去掉多余空白、HTML 标签等）",{"type":84,"tag":140,"props":727,"children":728},{},[729],{"type":90,"value":730},"对同一段文本做缓存（embedding 成本可观）",{"type":84,"tag":115,"props":732,"children":733},{},[],{"type":84,"tag":119,"props":735,"children":737},{"id":736},"_5-一套最小的向量检索接口建议你先抽象出来",[738],{"type":90,"value":739},"5. 一套最小的向量检索接口（建议你先抽象出来）",{"type":84,"tag":96,"props":741,"children":742},{},[743],{"type":90,"value":744},"无论你用 Milvus\u002FChroma\u002Fpgvector，最终你都需要这三个能力：",{"type":84,"tag":136,"props":746,"children":747},{},[748,759,770],{"type":84,"tag":140,"props":749,"children":750},{},[751,757],{"type":84,"tag":313,"props":752,"children":754},{"className":753},[],[755],{"type":90,"value":756},"upsert(chunks[])",{"type":90,"value":758},"：写入\u002F更新向量",{"type":84,"tag":140,"props":760,"children":761},{},[762,768],{"type":84,"tag":313,"props":763,"children":765},{"className":764},[],[766],{"type":90,"value":767},"query(vector, topK)",{"type":90,"value":769},"：按向量相似度查 topK",{"type":84,"tag":140,"props":771,"children":772},{},[773,779],{"type":84,"tag":313,"props":774,"children":776},{"className":775},[],[777],{"type":90,"value":778},"delete(filter)",{"type":90,"value":780},"：删除（可选）",{"type":84,"tag":96,"props":782,"children":783},{},[784],{"type":90,"value":785},"你可以先定义一个最小接口：",{"type":84,"tag":330,"props":787,"children":791},{"className":788,"code":789,"language":790,"meta":335,"style":335},"language-ts shiki shiki-themes github-light github-dark","\u002F\u002F 伪代码\u002F接口定义\nexport interface VectorStore {\n  upsert(items: Array\u003C{ id: string; vector: number[]; text: string; metadata?: any }>): Promise\u003Cvoid>;\n  query(vector: number[], topK: number): Promise\u003CArray\u003C{ id: string; score: number; text: string; metadata?: any }>>;\n}\n","ts",[792],{"type":84,"tag":313,"props":793,"children":794},{"__ignoreMap":335},[795,803,825,955,1089],{"type":84,"tag":341,"props":796,"children":797},{"class":343,"line":344},[798],{"type":84,"tag":341,"props":799,"children":800},{"style":445},[801],{"type":90,"value":802},"\u002F\u002F 伪代码\u002F接口定义\n",{"type":84,"tag":341,"props":804,"children":805},{"class":343,"line":376},[806,810,815,820],{"type":84,"tag":341,"props":807,"children":808},{"style":348},[809],{"type":90,"value":479},{"type":84,"tag":341,"props":811,"children":812},{"style":348},[813],{"type":90,"value":814}," interface",{"type":84,"tag":341,"props":816,"children":817},{"style":411},[818],{"type":90,"value":819}," VectorStore",{"type":84,"tag":341,"props":821,"children":822},{"style":354},[823],{"type":90,"value":824}," {\n",{"type":84,"tag":341,"props":826,"children":827},{"class":343,"line":386},[828,833,837,842,847,852,857,862,866,871,876,881,885,890,895,899,903,907,911,916,921,926,931,935,940,945,950],{"type":84,"tag":341,"props":829,"children":830},{"style":411},[831],{"type":90,"value":832},"  upsert",{"type":84,"tag":341,"props":834,"children":835},{"style":354},[836],{"type":90,"value":499},{"type":84,"tag":341,"props":838,"children":839},{"style":502},[840],{"type":90,"value":841},"items",{"type":84,"tag":341,"props":843,"children":844},{"style":348},[845],{"type":90,"value":846},":",{"type":84,"tag":341,"props":848,"children":849},{"style":411},[850],{"type":90,"value":851}," Array",{"type":84,"tag":341,"props":853,"children":854},{"style":354},[855],{"type":90,"value":856},"\u003C{ ",{"type":84,"tag":341,"props":858,"children":859},{"style":502},[860],{"type":90,"value":861},"id",{"type":84,"tag":341,"props":863,"children":864},{"style":348},[865],{"type":90,"value":846},{"type":84,"tag":341,"props":867,"children":868},{"style":395},[869],{"type":90,"value":870}," string",{"type":84,"tag":341,"props":872,"children":873},{"style":354},[874],{"type":90,"value":875},"; ",{"type":84,"tag":341,"props":877,"children":878},{"style":502},[879],{"type":90,"value":880},"vector",{"type":84,"tag":341,"props":882,"children":883},{"style":348},[884],{"type":90,"value":846},{"type":84,"tag":341,"props":886,"children":887},{"style":395},[888],{"type":90,"value":889}," number",{"type":84,"tag":341,"props":891,"children":892},{"style":354},[893],{"type":90,"value":894},"[]; ",{"type":84,"tag":341,"props":896,"children":897},{"style":502},[898],{"type":90,"value":90},{"type":84,"tag":341,"props":900,"children":901},{"style":348},[902],{"type":90,"value":846},{"type":84,"tag":341,"props":904,"children":905},{"style":395},[906],{"type":90,"value":870},{"type":84,"tag":341,"props":908,"children":909},{"style":354},[910],{"type":90,"value":875},{"type":84,"tag":341,"props":912,"children":913},{"style":502},[914],{"type":90,"value":915},"metadata",{"type":84,"tag":341,"props":917,"children":918},{"style":348},[919],{"type":90,"value":920},"?:",{"type":84,"tag":341,"props":922,"children":923},{"style":395},[924],{"type":90,"value":925}," any",{"type":84,"tag":341,"props":927,"children":928},{"style":354},[929],{"type":90,"value":930}," }>)",{"type":84,"tag":341,"props":932,"children":933},{"style":348},[934],{"type":90,"value":846},{"type":84,"tag":341,"props":936,"children":937},{"style":411},[938],{"type":90,"value":939}," Promise",{"type":84,"tag":341,"props":941,"children":942},{"style":354},[943],{"type":90,"value":944},"\u003C",{"type":84,"tag":341,"props":946,"children":947},{"style":395},[948],{"type":90,"value":949},"void",{"type":84,"tag":341,"props":951,"children":952},{"style":354},[953],{"type":90,"value":954},">;\n",{"type":84,"tag":341,"props":956,"children":957},{"class":343,"line":422},[958,963,967,971,975,979,984,989,993,997,1002,1006,1010,1014,1019,1023,1027,1031,1035,1039,1044,1048,1052,1056,1060,1064,1068,1072,1076,1080,1084],{"type":84,"tag":341,"props":959,"children":960},{"style":411},[961],{"type":90,"value":962},"  query",{"type":84,"tag":341,"props":964,"children":965},{"style":354},[966],{"type":90,"value":499},{"type":84,"tag":341,"props":968,"children":969},{"style":502},[970],{"type":90,"value":880},{"type":84,"tag":341,"props":972,"children":973},{"style":348},[974],{"type":90,"value":846},{"type":84,"tag":341,"props":976,"children":977},{"style":395},[978],{"type":90,"value":889},{"type":84,"tag":341,"props":980,"children":981},{"style":354},[982],{"type":90,"value":983},"[], ",{"type":84,"tag":341,"props":985,"children":986},{"style":502},[987],{"type":90,"value":988},"topK",{"type":84,"tag":341,"props":990,"children":991},{"style":348},[992],{"type":90,"value":846},{"type":84,"tag":341,"props":994,"children":995},{"style":395},[996],{"type":90,"value":889},{"type":84,"tag":341,"props":998,"children":999},{"style":354},[1000],{"type":90,"value":1001},")",{"type":84,"tag":341,"props":1003,"children":1004},{"style":348},[1005],{"type":90,"value":846},{"type":84,"tag":341,"props":1007,"children":1008},{"style":411},[1009],{"type":90,"value":939},{"type":84,"tag":341,"props":1011,"children":1012},{"style":354},[1013],{"type":90,"value":944},{"type":84,"tag":341,"props":1015,"children":1016},{"style":411},[1017],{"type":90,"value":1018},"Array",{"type":84,"tag":341,"props":1020,"children":1021},{"style":354},[1022],{"type":90,"value":856},{"type":84,"tag":341,"props":1024,"children":1025},{"style":502},[1026],{"type":90,"value":861},{"type":84,"tag":341,"props":1028,"children":1029},{"style":348},[1030],{"type":90,"value":846},{"type":84,"tag":341,"props":1032,"children":1033},{"style":395},[1034],{"type":90,"value":870},{"type":84,"tag":341,"props":1036,"children":1037},{"style":354},[1038],{"type":90,"value":875},{"type":84,"tag":341,"props":1040,"children":1041},{"style":502},[1042],{"type":90,"value":1043},"score",{"type":84,"tag":341,"props":1045,"children":1046},{"style":348},[1047],{"type":90,"value":846},{"type":84,"tag":341,"props":1049,"children":1050},{"style":395},[1051],{"type":90,"value":889},{"type":84,"tag":341,"props":1053,"children":1054},{"style":354},[1055],{"type":90,"value":875},{"type":84,"tag":341,"props":1057,"children":1058},{"style":502},[1059],{"type":90,"value":90},{"type":84,"tag":341,"props":1061,"children":1062},{"style":348},[1063],{"type":90,"value":846},{"type":84,"tag":341,"props":1065,"children":1066},{"style":395},[1067],{"type":90,"value":870},{"type":84,"tag":341,"props":1069,"children":1070},{"style":354},[1071],{"type":90,"value":875},{"type":84,"tag":341,"props":1073,"children":1074},{"style":502},[1075],{"type":90,"value":915},{"type":84,"tag":341,"props":1077,"children":1078},{"style":348},[1079],{"type":90,"value":920},{"type":84,"tag":341,"props":1081,"children":1082},{"style":395},[1083],{"type":90,"value":925},{"type":84,"tag":341,"props":1085,"children":1086},{"style":354},[1087],{"type":90,"value":1088}," }>>;\n",{"type":84,"tag":341,"props":1090,"children":1091},{"class":343,"line":441},[1092],{"type":84,"tag":341,"props":1093,"children":1094},{"style":354},[1095],{"type":90,"value":628},{"type":84,"tag":96,"props":1097,"children":1098},{},[1099],{"type":90,"value":1100},"有了它，你的 RAG 逻辑就不会被某个向量库绑定死。",{"type":84,"tag":115,"props":1102,"children":1103},{},[],{"type":84,"tag":119,"props":1105,"children":1107},{"id":1106},"_6-最小-rag只差一步把检索结果拼回-prompt",[1108],{"type":90,"value":1109},"6. 最小 RAG（只差一步）：把检索结果拼回 Prompt",{"type":84,"tag":96,"props":1111,"children":1112},{},[1113,1115,1121],{"type":90,"value":1114},"在你有了 ",{"type":84,"tag":313,"props":1116,"children":1118},{"className":1117},[],[1119],{"type":90,"value":1120},"query()",{"type":90,"value":1122}," 之后，RAG 的第一版就能跑：",{"type":84,"tag":1124,"props":1125,"children":1126},"ol",{},[1127,1132,1137,1142],{"type":84,"tag":140,"props":1128,"children":1129},{},[1130],{"type":90,"value":1131},"用户问题 -> embedding",{"type":84,"tag":140,"props":1133,"children":1134},{},[1135],{"type":90,"value":1136},"去向量库 query topK",{"type":84,"tag":140,"props":1138,"children":1139},{},[1140],{"type":90,"value":1141},"把 topK 文本片段作为“证据”拼到 prompt",{"type":84,"tag":140,"props":1143,"children":1144},{},[1145],{"type":90,"value":1146},"让 LLM 基于证据回答，并要求引用证据",{"type":84,"tag":96,"props":1148,"children":1149},{},[1150],{"type":90,"value":1151},"拼 prompt 的示例：",{"type":84,"tag":330,"props":1153,"children":1157},{"className":1154,"code":1155,"language":1156,"meta":335,"style":335},"language-txt shiki shiki-themes github-light github-dark","你是一个文档问答助手。请只根据 EVIDENCE 回答，不要编造。\n\nEVIDENCE:\n- (doc1#chunk3) ...\n- (doc2#chunk7) ...\n\nQUESTION:\n{userQuestion}\n","txt",[1158],{"type":84,"tag":313,"props":1159,"children":1160},{"__ignoreMap":335},[1161,1169,1176,1184,1192,1200,1207,1215],{"type":84,"tag":341,"props":1162,"children":1163},{"class":343,"line":344},[1164],{"type":84,"tag":341,"props":1165,"children":1166},{},[1167],{"type":90,"value":1168},"你是一个文档问答助手。请只根据 EVIDENCE 回答，不要编造。\n",{"type":84,"tag":341,"props":1170,"children":1171},{"class":343,"line":376},[1172],{"type":84,"tag":341,"props":1173,"children":1174},{"emptyLinePlaceholder":380},[1175],{"type":90,"value":383},{"type":84,"tag":341,"props":1177,"children":1178},{"class":343,"line":386},[1179],{"type":84,"tag":341,"props":1180,"children":1181},{},[1182],{"type":90,"value":1183},"EVIDENCE:\n",{"type":84,"tag":341,"props":1185,"children":1186},{"class":343,"line":422},[1187],{"type":84,"tag":341,"props":1188,"children":1189},{},[1190],{"type":90,"value":1191},"- (doc1#chunk3) ...\n",{"type":84,"tag":341,"props":1193,"children":1194},{"class":343,"line":441},[1195],{"type":84,"tag":341,"props":1196,"children":1197},{},[1198],{"type":90,"value":1199},"- (doc2#chunk7) ...\n",{"type":84,"tag":341,"props":1201,"children":1202},{"class":343,"line":456},[1203],{"type":84,"tag":341,"props":1204,"children":1205},{"emptyLinePlaceholder":380},[1206],{"type":90,"value":383},{"type":84,"tag":341,"props":1208,"children":1209},{"class":343,"line":465},[1210],{"type":84,"tag":341,"props":1211,"children":1212},{},[1213],{"type":90,"value":1214},"QUESTION:\n",{"type":84,"tag":341,"props":1216,"children":1217},{"class":343,"line":473},[1218],{"type":84,"tag":341,"props":1219,"children":1220},{},[1221],{"type":90,"value":1222},"{userQuestion}\n",{"type":84,"tag":115,"props":1224,"children":1225},{},[],{"type":84,"tag":119,"props":1227,"children":1229},{"id":1228},"_7-下一篇写什么",[1230],{"type":90,"value":1231},"7. 下一篇写什么",{"type":84,"tag":96,"props":1233,"children":1234},{},[1235],{"type":90,"value":1236},"下一篇进入 RAG 的“ETL”部分：",{"type":84,"tag":136,"props":1238,"children":1239},{},[1240,1245,1250],{"type":84,"tag":140,"props":1241,"children":1242},{},[1243],{"type":90,"value":1244},"文档加载（Markdown\u002FHTML\u002FPDF）",{"type":84,"tag":140,"props":1246,"children":1247},{},[1248],{"type":90,"value":1249},"切分（Recursive \u002F MarkdownHeader）",{"type":84,"tag":140,"props":1251,"children":1252},{},[1253],{"type":90,"value":1254},"为什么不能随便按长度切",{"type":84,"tag":96,"props":1256,"children":1257},{},[1258],{"type":90,"value":1259},"并且会继续保持 Node.js 优先（示例会用 JS\u002FTS）。",{"type":84,"tag":1261,"props":1262,"children":1263},"style",{},[1264],{"type":90,"value":1265},"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);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":335,"searchDepth":376,"depth":376,"links":1267},[1268,1269,1270,1271,1272,1273,1274],{"id":121,"depth":376,"text":124},{"id":178,"depth":376,"text":181},{"id":243,"depth":376,"text":246},{"id":295,"depth":376,"text":298},{"id":736,"depth":376,"text":739},{"id":1106,"depth":376,"text":1109},{"id":1228,"depth":376,"text":1231},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1276},"\u003Cpath fill=\"currentColor\" d=\"m10.828 12l4.95 4.95l-1.414 1.415L8 12l6.364-6.364l1.414 1.414z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1278},"\u003Cpath fill=\"currentColor\" d=\"M3 4h18v2H3zm0 7h12v2H3zm0 7h18v2H3z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1280},"\u003Cpath fill=\"currentColor\" d=\"m13.172 12l-4.95-4.95l1.414-1.413L16 12l-6.364 6.364l-1.414-1.415z\"\u002F>",{"left":4,"top":4,"width":1282,"height":1282,"rotate":4,"vFlip":6,"hFlip":6,"body":1283},1024,"\u003Cpath fill=\"currentColor\" d=\"M768 832a128 128 0 0 1-128 128H192A128 128 0 0 1 64 832V384a128 128 0 0 1 128-128v64a64 64 0 0 0-64 64v448a64 64 0 0 0 64 64h448a64 64 0 0 0 64-64z\"\u002F>\u003Cpath fill=\"currentColor\" d=\"M384 128a64 64 0 0 0-64 64v448a64 64 0 0 0 64 64h448a64 64 0 0 0 64-64V192a64 64 0 0 0-64-64zm0-64h448a128 128 0 0 1 128 128v448a128 128 0 0 1-128 128H384a128 128 0 0 1-128-128V192A128 128 0 0 1 384 64\"\u002F>"]