1:"$Sreact.fragment" 2:I[41596,["/_next/static/chunks/51cf3403df3ab8db.js"],"ThemeProvider"] 3:I[99623,["/_next/static/chunks/1f465ca74a71847d.js","/_next/static/chunks/6628e8793cd41c7c.js"],"default"] 4:I[1810,["/_next/static/chunks/1f465ca74a71847d.js","/_next/static/chunks/6628e8793cd41c7c.js"],"default"] 6:I[39271,["/_next/static/chunks/1f465ca74a71847d.js","/_next/static/chunks/6628e8793cd41c7c.js"],"OutletBoundary"] 7:"$Sreact.suspense" 9:I[39271,["/_next/static/chunks/1f465ca74a71847d.js","/_next/static/chunks/6628e8793cd41c7c.js"],"ViewportBoundary"] b:I[39271,["/_next/static/chunks/1f465ca74a71847d.js","/_next/static/chunks/6628e8793cd41c7c.js"],"MetadataBoundary"] d:I[79129,[],"default"] :HL["/_next/static/chunks/c2a0804a12c1a234.css","style"] :HL["/_next/static/media/1b99372b3eaef0c8-s.p.758e15a8.woff2","font",{"crossOrigin":"","type":"font/woff2"}] :HL["/_next/static/media/797e433ab948586e-s.p.dbea232f.woff2","font",{"crossOrigin":"","type":"font/woff2"}] :HL["/_next/static/media/caa3a2e1cccd8315-s.p.853070df.woff2","font",{"crossOrigin":"","type":"font/woff2"}] 0:{"P":null,"b":"3CCc4JxoD54WNKxdcWa8x","c":["","blog","rag-pipeline-optimization-healthcare"],"q":"","i":false,"f":[[["",{"children":["blog",{"children":[["slug","rag-pipeline-optimization-healthcare","d"],{"children":["__PAGE__",{}]}]}]},"$undefined","$undefined",true],[["$","$1","c",{"children":[[["$","link","0",{"rel":"stylesheet","href":"/_next/static/chunks/c2a0804a12c1a234.css","precedence":"next","crossOrigin":"$undefined","nonce":"$undefined"}],["$","script","script-0",{"src":"/_next/static/chunks/51cf3403df3ab8db.js","async":true,"nonce":"$undefined"}]],["$","html",null,{"lang":"en","suppressHydrationWarning":true,"children":[["$","head",null,{"children":["$","script",null,{"type":"application/ld+json","dangerouslySetInnerHTML":{"__html":"{\"@context\":\"https://schema.org\",\"@type\":\"Person\",\"name\":\"Raad Kasem\",\"url\":\"https://raadkasem.dev\",\"jobTitle\":\"Senior Software Engineer & Team Lead\",\"worksFor\":{\"@type\":\"Organization\",\"name\":\"CanGuru\"},\"alumniOf\":[{\"@type\":\"CollegeOrUniversity\",\"name\":\"Syrian Virtual University\"},{\"@type\":\"CollegeOrUniversity\",\"name\":\"Tishreen University\"}],\"knowsAbout\":[\"Artificial Intelligence\",\"Machine Learning\",\"LangChain\",\"LangGraph\",\"FastAPI\",\"Python\",\"PHP\",\"Laravel\",\"RAG Pipelines\"],\"sameAs\":[\"https://github.com/raadkasem\",\"https://linkedin.com/in/raad-kasem\"]}"}}]}],["$","body",null,{"className":"geist_a71539c9-module__T19VSG__variable geist_mono_8d43a2aa-module__8Li5zG__variable outfit_a0e3ceb4-module__1hgF0q__variable antialiased","children":["$","$L2",null,{"children":["$","$L3",null,{"parallelRouterKey":"children","error":"$undefined","errorStyles":"$undefined","errorScripts":"$undefined","template":["$","$L4",null,{}],"templateStyles":"$undefined","templateScripts":"$undefined","notFound":[[["$","title",null,{"children":"404: This page could not be found."}],["$","div",null,{"style":{"fontFamily":"system-ui,\"Segoe UI\",Roboto,Helvetica,Arial,sans-serif,\"Apple Color Emoji\",\"Segoe UI Emoji\"","height":"100vh","textAlign":"center","display":"flex","flexDirection":"column","alignItems":"center","justifyContent":"center"},"children":["$","div",null,{"children":[["$","style",null,{"dangerouslySetInnerHTML":{"__html":"body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}"}}],["$","h1",null,{"className":"next-error-h1","style":{"display":"inline-block","margin":"0 20px 0 0","padding":"0 23px 0 0","fontSize":24,"fontWeight":500,"verticalAlign":"top","lineHeight":"49px"},"children":404}],["$","div",null,{"style":{"display":"inline-block"},"children":["$","h2",null,{"style":{"fontSize":14,"fontWeight":400,"lineHeight":"49px","margin":0},"children":"This page could not be found."}]}]]}]}]],[]],"forbidden":"$undefined","unauthorized":"$undefined"}]}]}]]}]]}],{"children":[["$","$1","c",{"children":[null,["$","$L3",null,{"parallelRouterKey":"children","error":"$undefined","errorStyles":"$undefined","errorScripts":"$undefined","template":["$","$L4",null,{}],"templateStyles":"$undefined","templateScripts":"$undefined","notFound":"$undefined","forbidden":"$undefined","unauthorized":"$undefined"}]]}],{"children":[["$","$1","c",{"children":[null,["$","$L3",null,{"parallelRouterKey":"children","error":"$undefined","errorStyles":"$undefined","errorScripts":"$undefined","template":["$","$L4",null,{}],"templateStyles":"$undefined","templateScripts":"$undefined","notFound":"$undefined","forbidden":"$undefined","unauthorized":"$undefined"}]]}],{"children":[["$","$1","c",{"children":["$L5",[["$","script","script-0",{"src":"/_next/static/chunks/e1faea17a9e9ed14.js","async":true,"nonce":"$undefined"}]],["$","$L6",null,{"children":["$","$7",null,{"name":"Next.MetadataOutlet","children":"$@8"}]}]]}],{},null,false,false]},null,false,false]},null,false,false]},null,false,false],["$","$1","h",{"children":[null,["$","$L9",null,{"children":"$La"}],["$","div",null,{"hidden":true,"children":["$","$Lb",null,{"children":["$","$7",null,{"name":"Next.Metadata","children":"$Lc"}]}]}],["$","meta",null,{"name":"next-size-adjust","content":""}]]}],false]],"m":"$undefined","G":["$d",[]],"S":true} e:I[97333,["/_next/static/chunks/51cf3403df3ab8db.js","/_next/static/chunks/e1faea17a9e9ed14.js"],""] 5:["$","div",null,{"className":"min-h-screen bg-[var(--bg)]","children":["$","article",null,{"className":"section-container pt-32 max-w-3xl","children":[["$","$Le",null,{"href":"/blog","className":"inline-flex items-center gap-2 text-sm text-muted hover:text-accent transition-colors mb-8","children":[["$","svg",null,{"ref":"$undefined","xmlns":"http://www.w3.org/2000/svg","width":24,"height":24,"viewBox":"0 0 24 24","fill":"none","stroke":"currentColor","strokeWidth":2,"strokeLinecap":"round","strokeLinejoin":"round","className":"lucide lucide-arrow-left h-4 w-4","aria-hidden":"true","children":[["$","path","1l729n",{"d":"m12 19-7-7 7-7"}],["$","path","x3x0zl",{"d":"M19 12H5"}],"$undefined"]}],"Back to Blog"]}],["$","header",null,{"className":"mb-10","children":[["$","div",null,{"className":"flex flex-wrap items-center gap-3 text-sm text-muted mb-4","children":[["$","span",null,{"className":"inline-flex items-center gap-1.5","children":[["$","svg",null,{"ref":"$undefined","xmlns":"http://www.w3.org/2000/svg","width":24,"height":24,"viewBox":"0 0 24 24","fill":"none","stroke":"currentColor","strokeWidth":2,"strokeLinecap":"round","strokeLinejoin":"round","className":"lucide lucide-calendar h-3.5 w-3.5","aria-hidden":"true","children":[["$","path","1cmpym",{"d":"M8 2v4"}],["$","path","4m81vk",{"d":"M16 2v4"}],["$","rect","1hopcy",{"width":"18","height":"18","x":"3","y":"4","rx":"2"}],["$","path","8toen8",{"d":"M3 10h18"}],"$undefined"]}],"November 28, 2024"]}],["$","span",null,{"className":"inline-flex items-center gap-1.5","children":[["$","svg",null,{"ref":"$undefined","xmlns":"http://www.w3.org/2000/svg","width":24,"height":24,"viewBox":"0 0 24 24","fill":"none","stroke":"currentColor","strokeWidth":2,"strokeLinecap":"round","strokeLinejoin":"round","className":"lucide lucide-clock h-3.5 w-3.5","aria-hidden":"true","children":[["$","circle","1mglay",{"cx":"12","cy":"12","r":"10"}],["$","path","mmk7yg",{"d":"M12 6v6l4 2"}],"$undefined"]}],"7 min read"]}]]}],["$","h1",null,{"className":"font-[family-name:var(--font-outfit)] text-3xl font-bold tracking-tight text-foreground sm:text-4xl lg:text-5xl","children":"Optimizing RAG Pipelines for Healthcare: From 2s Latency to 200ms"}],["$","div",null,{"className":"mt-4 flex flex-wrap gap-2","children":[["$","span","RAG",{"className":"rounded-full border border-[var(--card-border)] bg-[var(--card-bg)] px-3 py-1 text-xs text-muted","children":"RAG"}],["$","span","Healthcare AI",{"className":"rounded-full border border-[var(--card-border)] bg-[var(--card-bg)] px-3 py-1 text-xs text-muted","children":"Healthcare AI"}],["$","span","Vector Databases",{"className":"rounded-full border border-[var(--card-border)] bg-[var(--card-bg)] px-3 py-1 text-xs text-muted","children":"Vector Databases"}],["$","span","Qdrant",{"className":"rounded-full border border-[var(--card-border)] bg-[var(--card-bg)] px-3 py-1 text-xs text-muted","children":"Qdrant"}],["$","span","Performance",{"className":"rounded-full border border-[var(--card-border)] bg-[var(--card-bg)] px-3 py-1 text-xs text-muted","children":"Performance"}],["$","span","LangChain",{"className":"rounded-full border border-[var(--card-border)] bg-[var(--card-bg)] px-3 py-1 text-xs text-muted","children":"LangChain"}]]}]]}],["$","div",null,{"className":"prose-custom","children":"$Lf"}]]}]}] a:[["$","meta","0",{"charSet":"utf-8"}],["$","meta","1",{"name":"viewport","content":"width=device-width, initial-scale=1"}]] 10:I[39406,["/_next/static/chunks/1f465ca74a71847d.js","/_next/static/chunks/6628e8793cd41c7c.js"],"IconMark"] 8:null c:[["$","title","0",{"children":"Optimizing RAG Pipelines for Healthcare: From 2s Latency to 200ms | Raad Kasem"}],["$","meta","1",{"name":"description","content":"The techniques we used to 10x our RAG pipeline performance in a medical AI system — covering hybrid search, intelligent chunking, reranking strategies, and caching at every layer."}],["$","link","2",{"rel":"author","href":"https://raadkasem.dev"}],["$","meta","3",{"name":"author","content":"Raad Kasem"}],["$","meta","4",{"name":"keywords","content":"Raad Kasem,Software Engineer,AI Engineer,LangChain,LangGraph,RAG,FastAPI,Full Stack Developer,Team Lead,LLM,Machine Learning"}],["$","meta","5",{"name":"creator","content":"Raad Kasem"}],["$","meta","6",{"name":"robots","content":"index, follow"}],["$","meta","7",{"name":"googlebot","content":"index, follow, max-video-preview:-1, max-image-preview:large, max-snippet:-1"}],["$","meta","8",{"property":"og:title","content":"Raad Kasem — AI-focused Senior Software Engineer"}],["$","meta","9",{"property":"og:description","content":"Senior Software Engineer & Team Lead with 7+ years building production-ready AI/LLM-powered solutions."}],["$","meta","10",{"property":"og:url","content":"https://raadkasem.dev"}],["$","meta","11",{"property":"og:site_name","content":"Raad Kasem"}],["$","meta","12",{"property":"og:locale","content":"en_US"}],["$","meta","13",{"property":"og:image:alt","content":"Raad Kasem — AI-focused Senior Software Engineer"}],["$","meta","14",{"property":"og:image:type","content":"image/png"}],["$","meta","15",{"property":"og:image","content":"https://raadkasem.dev/opengraph-image?6524c747f4eb7f2a"}],["$","meta","16",{"property":"og:image:width","content":"1200"}],["$","meta","17",{"property":"og:image:height","content":"630"}],["$","meta","18",{"property":"og:type","content":"website"}],["$","meta","19",{"name":"twitter:card","content":"summary_large_image"}],["$","meta","20",{"name":"twitter:title","content":"Raad Kasem — AI-focused Senior Software Engineer"}],["$","meta","21",{"name":"twitter:description","content":"Senior Software Engineer & Team Lead with 7+ years building production-ready AI/LLM-powered solutions."}],["$","meta","22",{"name":"twitter:image:alt","content":"Raad Kasem — AI-focused Senior Software Engineer"}],["$","meta","23",{"name":"twitter:image:type","content":"image/png"}],["$","meta","24",{"name":"twitter:image","content":"https://raadkasem.dev/opengraph-image?6524c747f4eb7f2a"}],["$","meta","25",{"name":"twitter:image:width","content":"1200"}],["$","meta","26",{"name":"twitter:image:height","content":"630"}],["$","link","27",{"rel":"icon","href":"/icon?51d30ca368f9894e","alt":"$undefined","type":"image/png","sizes":"32x32"}],["$","$L10","28",{}]] f:[["$","h1",null,{"className":"font-[family-name:var(--font-outfit)] text-3xl font-bold tracking-tight text-foreground mt-10 mb-4 first:mt-0","children":"Optimizing RAG Pipelines for Healthcare: From 2s Latency to 200ms"}],"\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"When we launched Genio Pulse — our medical AI assistant serving 5,000+ doctors — the initial RAG pipeline took 2-3 seconds per query. For a tool that doctors use mid-consultation, that was unacceptable. Over six months of iterative optimization, we brought it down to under 200ms while improving retrieval relevance from 78% to 95%."}],"\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"This post documents every optimization we made, in the order we implemented them. If you're building RAG for production, these lessons will save you months."}],"\n",["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"The Starting Point"}],"\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"Our initial architecture was textbook:"}],"\n",["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","children":"User Query → Embed → Vector Search (Qdrant) → Top-K → LLM → Response\n"}]}],"\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":["$","strong",null,{"className":"font-semibold text-foreground","children":"Baseline metrics:"}]}],"\n",["$","ul",null,{"className":"list-disc list-inside space-y-2 text-muted mb-5","children":["\n",["$","li",null,{"className":"text-base leading-relaxed","children":"Embedding latency: ~100ms"}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":"Vector search: ~150ms"}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":"LLM generation: ~1500ms"}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":"Total: ~2000ms"}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":"Retrieval relevance (manual eval): 78%"}],"\n"]}],"\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"The LLM generation time is mostly out of our control (model provider latency), so we focused on everything else — and discovered that better retrieval actually reduces generation time too, because the LLM gets more relevant context and generates faster, more focused answers."}],"\n",["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"Optimization 1: Chunking Strategy (78% → 84% relevance)"}],"\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"Our first mistake was using a naive text splitter with fixed chunk sizes. Medical literature has structure — headings, sections, bullet points, references — and splitting mid-paragraph destroys context."}],"\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":["$","strong",null,{"className":"font-semibold text-foreground","children":"Before:"}]}],"\n",["$","figure",null,{"data-rehype-pretty-code-figure":"","children":["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","style":{"backgroundColor":"#22272e","color":"#adbac7"},"tabIndex":"0","data-language":"python","data-theme":"github-dark-dimmed","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","data-language":"python","data-theme":"github-dark-dimmed","style":{"display":"grid"},"children":["$L11","\n","$L12","\n","$L13","\n","$L14","\n","$L15"]}]}]}],"\n","$L16","\n","$L17","\n","$L18","\n","$L19","\n","$L1a","\n","$L1b","\n","$L1c","\n","$L1d","\n","$L1e","\n","$L1f","\n","$L20","\n","$L21","\n","$L22","\n","$L23","\n","$L24","\n","$L25","\n","$L26","\n","$L27","\n","$L28","\n","$L29","\n","$L2a","\n","$L2b","\n","$L2c","\n","$L2d","\n","$L2e","\n","$L2f","\n","$L30","\n","$L31","\n","$L32","\n","$L33","\n","$L34","\n","$L35","\n","$L36","\n","$L37","\n","$L38","\n","$L39","\n","$L3a","\n","$L3b"] 11:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":"# Naive: fixed 1000-char chunks with 200-char overlap"}]}] 12:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":"text_splitter "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" RecursiveCharacterTextSplitter("}]]}] 13:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" chunk_size"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"1000"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}] 14:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" chunk_overlap"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"200"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}] 15:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":")"}]}] 16:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":["$","strong",null,{"className":"font-semibold text-foreground","children":"After:"}]}] 17:["$","figure",null,{"data-rehype-pretty-code-figure":"","children":["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","style":{"backgroundColor":"#22272e","color":"#adbac7"},"tabIndex":"0","data-language":"python","data-theme":"github-dark-dimmed","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","data-language":"python","data-theme":"github-dark-dimmed","style":{"display":"grid"},"children":[["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":"# Semantic chunking that respects document structure"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"class"}],["$","span",null,{"style":{"color":"#F69D50"},"children":" MedicalDocumentChunker"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":":"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" def"}],["$","span",null,{"style":{"color":"#DCBDFB"},"children":" chunk"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(self, document: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"str"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", metadata: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"dict"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":") -> list[Document]:"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" sections "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".split_by_headers(document)"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" chunks "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" []"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" for"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" section "}],["$","span",null,{"style":{"color":"#F47067"},"children":"in"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" sections:"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" if"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" len"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(section.content) "}],["$","span",null,{"style":{"color":"#F47067"},"children":"<"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" 512"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":":"}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Small sections stay intact"}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" chunks.append(Document("}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" content"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"section.content,"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" metadata"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"{"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" **"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"metadata,"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#96D0FF"},"children":" \"section\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":": section.header,"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#96D0FF"},"children":" \"chunk_type\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":": "}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"full_section\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" }"}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" ))"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" else"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":":"}]]}],"\n","$L3c","\n","$L3d","\n","$L3e","\n","$L3f","\n","$L40","\n","$L41","\n","$L42","\n","$L43","\n","$L44","\n","$L45","\n","$L46","\n","$L47","\n","$L48","\n","$L49","\n","$L4a","\n","$L4b","\n","$L4c","\n","$L4d"]}]}]}] 18:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":["$","strong",null,{"className":"font-semibold text-foreground","children":"Key changes:"}]}] 19:["$","ul",null,{"className":"list-disc list-inside space-y-2 text-muted mb-5","children":["\n",["$","li",null,{"className":"text-base leading-relaxed","children":"Chunk size: 1000 → 512 tokens (smaller chunks = more precise retrieval)"}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":"Overlap: character-based → sentence-based (2 sentences overlap)"}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":"Structure-aware: respect section boundaries"}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":"Prepend section header to each chunk (preserves context)"}],"\n"]}] 1a:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Impact:"}]," Relevance jumped from 78% to 84%. Latency unchanged."]}] 1b:["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"Optimization 2: Hybrid Search (84% → 90% relevance)"}] 1c:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"Pure vector similarity misses exact medical terms. If a doctor searches for \"metformin 500mg side effects,\" pure semantic search might return results about diabetes medications generally. We need exact keyword matching too."}] 1d:["$","figure",null,{"data-rehype-pretty-code-figure":"","children":["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","style":{"backgroundColor":"#22272e","color":"#adbac7"},"tabIndex":"0","data-language":"python","data-theme":"github-dark-dimmed","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","data-language":"python","data-theme":"github-dark-dimmed","style":{"display":"grid"},"children":[["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"from"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" qdrant_client.models "}],["$","span",null,{"style":{"color":"#F47067"},"children":"import"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" SparseVector"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"class"}],["$","span",null,{"style":{"color":"#F69D50"},"children":" HybridSearcher"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":":"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" def"}],["$","span",null,{"style":{"color":"#DCBDFB"},"children":" search"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(self, query: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"str"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", limit: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"int"}],["$","span",null,{"style":{"color":"#F47067"},"children":" ="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" 10"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":") -> list[SearchResult]:"}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Dense vector search (semantic understanding)"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" dense_results "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".qdrant.search("}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" collection_name"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"medical_docs\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" query_vector"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"("}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"dense\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".embed(query)),"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" limit"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"limit "}],["$","span",null,{"style":{"color":"#F47067"},"children":"*"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" 2"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", "}],["$","span",null,{"style":{"color":"#768390"},"children":"# Fetch more for fusion"}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Sparse vector search (keyword matching via BM25)"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" sparse_vector "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".bm25_encoder.encode(query)"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" sparse_results "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".qdrant.search("}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" collection_name"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"medical_docs\""}],"$L4e"]}],"\n","$L4f","\n","$L50","\n","$L51","\n","$L52","\n","$L53","\n","$L54","\n","$L55","\n","$L56","\n","$L57","\n","$L58","\n","$L59","\n","$L5a","\n","$L5b","\n","$L5c","\n","$L5d","\n","$L5e","\n","$L5f","\n","$L60","\n","$L61","\n","$L62"]}]}]}] 1e:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Why RRF over weighted scoring:"}]," Reciprocal Rank Fusion doesn't require tuning weights between dense and sparse scores. It's robust and works well out of the box."]}] 1f:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Impact:"}]," Relevance improved from 84% to 90%. Search latency increased slightly (~20ms) due to dual search, but the quality improvement was worth it."]}] 20:["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"Optimization 3: Metadata Filtering (latency: 150ms → 40ms)"}] 21:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"Most queries have implicit constraints that we can filter on before vector search:"}] 22:["$","figure",null,{"data-rehype-pretty-code-figure":"","children":["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","style":{"backgroundColor":"#22272e","color":"#adbac7"},"tabIndex":"0","data-language":"python","data-theme":"github-dark-dimmed","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","data-language":"python","data-theme":"github-dark-dimmed","style":{"display":"grid"},"children":[["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"def"}],["$","span",null,{"style":{"color":"#DCBDFB"},"children":" build_filters"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(self, query: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"str"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", doctor_profile: DoctorProfile):"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" filters "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" []"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Filter by medical specialty"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" if"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" doctor_profile.specialty:"}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" filters.append("}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" FieldCondition("}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" key"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"specialty\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" match"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"MatchAny("}],["$","span",null,{"style":{"color":"#F69D50"},"children":"any"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".related_specialties("}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" doctor_profile.specialty"}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )),"}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Filter by recency (prefer recent medical literature)"}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" filters.append("}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" FieldCondition("}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" key"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"publication_year\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" range"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"Range("}],["$","span",null,{"style":{"color":"#F69D50"},"children":"gte"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"2019"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"), "}],["$","span",null,{"style":{"color":"#768390"},"children":"# Last 5 years"}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Filter by evidence level (prefer high-quality sources)"}]}],"\n","$L63","\n","$L64","\n","$L65","\n","$L66","\n","$L67","\n","$L68","\n","$L69","\n","$L6a"]}]}]}] 23:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Impact:"}]," By filtering before vector search, Qdrant searches a much smaller subset. Latency dropped from 150ms to 40ms, and relevance improved to 92% (irrelevant specialties no longer pollute results)."]}] 24:["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"Optimization 4: Reranking (90% → 95% relevance)"}] 25:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"Vector similarity is a rough first pass. A cross-encoder reranker scores each query-document pair more accurately:"}] 26:["$","figure",null,{"data-rehype-pretty-code-figure":"","children":["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","style":{"backgroundColor":"#22272e","color":"#adbac7"},"tabIndex":"0","data-language":"python","data-theme":"github-dark-dimmed","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","data-language":"python","data-theme":"github-dark-dimmed","style":{"display":"grid"},"children":[["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"from"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" sentence_transformers "}],["$","span",null,{"style":{"color":"#F47067"},"children":"import"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" CrossEncoder"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"class"}],["$","span",null,{"style":{"color":"#F69D50"},"children":" Reranker"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":":"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" def"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" __init__"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(self):"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".model "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" CrossEncoder("}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#96D0FF"},"children":" \"cross-encoder/ms-marco-MiniLM-L-12-v2\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" max_length"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"512"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" def"}],["$","span",null,{"style":{"color":"#DCBDFB"},"children":" rerank"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(self, query: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"str"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", documents: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"list"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", top_k: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"int"}],["$","span",null,{"style":{"color":"#F47067"},"children":" ="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" 5"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"):"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" pairs "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" [(query, doc.content) "}],["$","span",null,{"style":{"color":"#F47067"},"children":"for"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" doc "}],["$","span",null,{"style":{"color":"#F47067"},"children":"in"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" documents]"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" scores "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".model.predict(pairs)"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" scored_docs "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" list"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"("}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"zip"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(documents, scores))"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" scored_docs.sort("}],["$","span",null,{"style":{"color":"#F69D50"},"children":"key"}],["$","span",null,{"style":{"color":"#F47067"},"children":"=lambda"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" x: x["}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"1"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"], "}],"$L6b","$L6c","$L6d","$L6e"]}],"\n","$L6f","\n","$L70"]}]}]}] 27:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":["$","strong",null,{"className":"font-semibold text-foreground","children":"Pipeline after reranking:"}]}] 28:["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","children":"Query → Hybrid Search (top 20) → Rerank (top 5) → LLM\n"}]}] 29:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Impact:"}]," Relevance went from 92% to 95%. Reranking adds ~30ms but the LLM gets much better context, which actually reduced generation time by ~200ms (shorter, more focused responses)."]}] 2a:["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"Optimization 5: Embedding Cache (100ms → 5ms for repeated queries)"}] 2b:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"Doctors often ask similar questions. We cache embeddings in Redis:"}] 2c:["$","figure",null,{"data-rehype-pretty-code-figure":"","children":["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","style":{"backgroundColor":"#22272e","color":"#adbac7"},"tabIndex":"0","data-language":"python","data-theme":"github-dark-dimmed","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","data-language":"python","data-theme":"github-dark-dimmed","style":{"display":"grid"},"children":[["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"class"}],["$","span",null,{"style":{"color":"#F69D50"},"children":" CachedEmbedder"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":":"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" def"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" __init__"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(self, redis_client, embedder, ttl"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"3600"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"):"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".redis "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" redis_client"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".embedder "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" embedder"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".ttl "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" ttl"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" def"}],["$","span",null,{"style":{"color":"#DCBDFB"},"children":" embed"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(self, text: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"str"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":") -> list["}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"float"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"]:"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" cache_key "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#F47067"},"children":" f"}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"emb:"}],["$","span",null,{"style":{"color":"#F47067"},"children":"{"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"hashlib.md5(text.encode()).hexdigest()"}],["$","span",null,{"style":{"color":"#F47067"},"children":"}"}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\""}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" cached "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".redis.get(cache_key)"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" if"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" cached:"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" return"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" json.loads(cached)"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" embedding "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".embedder.embed_query(text)"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".redis.setex(cache_key, "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".ttl, json.dumps(embedding))"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" return"}],"$L71"]}]]}]}]}] 2d:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Impact:"}]," For cache hits (~40% of queries), embedding latency drops from 100ms to 5ms."]}] 2e:["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"Optimization 6: Streaming + Parallel Processing"}] 2f:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"Instead of sequential execution, we parallelize independent steps:"}] 30:["$","figure",null,{"data-rehype-pretty-code-figure":"","children":["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","style":{"backgroundColor":"#22272e","color":"#adbac7"},"tabIndex":"0","data-language":"python","data-theme":"github-dark-dimmed","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","data-language":"python","data-theme":"github-dark-dimmed","style":{"display":"grid"},"children":[["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"import"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" asyncio"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"async"}],["$","span",null,{"style":{"color":"#F47067"},"children":" def"}],["$","span",null,{"style":{"color":"#DCBDFB"},"children":" rag_query"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(query: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"str"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", doctor: DoctorProfile):"}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Run embedding and filter building in parallel"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" embedding_task "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" asyncio.create_task(embedder.aembed(query))"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" filters "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" build_filters(query, doctor)"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" embedding "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#F47067"},"children":" await"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" embedding_task"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Hybrid search (dense + sparse in parallel internally)"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" results "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#F47067"},"children":" await"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" hybrid_searcher.asearch("}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" embedding"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"embedding,"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" filters"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"filters,"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" limit"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"20"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Rerank"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" top_docs "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" reranker.rerank(query, results, "}],["$","span",null,{"style":{"color":"#F69D50"},"children":"top_k"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"5"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":")"}]]}],"\n",["$","span",null,{"data-line":"","children":" "}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Stream LLM response (don't wait for full generation)"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" async"}],["$","span",null,{"style":{"color":"#F47067"},"children":" for"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" chunk "}],"$L72","$L73"]}],"\n","$L74","\n","$L75","\n","$L76"]}]}]}] 31:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Impact:"}]," Total perceived latency dropped to ~200ms for first token (streaming means the doctor sees text appearing immediately)."]}] 32:["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"The Final Architecture"}] 33:["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","children":"Query\n ├─ [Parallel] Cache Check → Embed (5-100ms)\n ├─ [Parallel] Build Filters (1ms)\n │\n ├─ Hybrid Search (dense + sparse) with Filters (40ms)\n ├─ Rerank Top 20 → Top 5 (30ms)\n │\n └─ Stream LLM Response (first token: ~150ms)\n\nTotal to first token: ~200ms\nTotal to complete response: ~800ms\n"}]}] 34:["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"Monitoring in Production"}] 35:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"We track these metrics continuously:"}] 36:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":"| Metric | Target | Alert Threshold |\n|--------|--------|----------------|\n| P50 latency (to first token) | under 200ms | over 500ms |\n| P99 latency (to first token) | under 500ms | over 1500ms |\n| Retrieval relevance (weekly eval) | above 93% | below 88% |\n| Cache hit rate | above 35% | below 20% |\n| Embedding API errors | below 0.1% | above 1% |\n| Cost per query | under $0.03 | over $0.10 |"}] 37:["$","figure",null,{"data-rehype-pretty-code-figure":"","children":["$","pre",null,{"className":"overflow-x-auto rounded-xl border border-[var(--card-border)] bg-[var(--surface)] p-4 text-sm mb-5","style":{"backgroundColor":"#22272e","color":"#adbac7"},"tabIndex":"0","data-language":"python","data-theme":"github-dark-dimmed","children":["$","code",null,{"className":"rounded bg-[var(--card-bg)] px-1.5 py-0.5 text-sm font-mono text-accent","data-language":"python","data-theme":"github-dark-dimmed","style":{"display":"grid"},"children":[["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":"# We log every query for continuous evaluation"}]}],"\n",["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#DCBDFB"},"children":"@dataclass"}]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":"class"}],["$","span",null,{"style":{"color":"#F69D50"},"children":" QueryLog"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":":"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" query: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"str"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" doctor_specialty: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"str"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" retrieved_doc_ids: list["}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"str"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"]"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" reranker_scores: list["}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"float"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"]"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" embedding_latency_ms: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"float"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" search_latency_ms: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"float"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" rerank_latency_ms: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"float"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" generation_latency_ms: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"float"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" total_latency_ms: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"float"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" cache_hit: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"bool"}]]}],"\n",["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" token_usage: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"dict"}]]}]]}]}]}] 38:["$","h2",null,{"className":"font-[family-name:var(--font-outfit)] text-2xl font-semibold tracking-tight text-foreground mt-10 mb-3","children":"Lessons Learned"}] 39:["$","ol",null,{"className":"list-decimal list-inside space-y-2 text-muted mb-5","children":["\n",["$","li",null,{"className":"text-base leading-relaxed","children":["\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Chunking quality > embedding model quality."}]," We got a bigger relevance boost from better chunking than from upgrading embedding models."]}],"\n"]}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":["\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Filter before you search."}]," Metadata filtering is free performance. Use every signal you have (specialty, recency, document type) to narrow the search space."]}],"\n"]}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":["\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Reranking is almost always worth it."}]," The 30ms cost pays for itself by giving the LLM better context (faster, more accurate responses)."]}],"\n"]}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":["\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Cache everything you can."}]," Embeddings, search results, even full responses for common queries. In healthcare, many questions are variations of the same few hundred queries."]}],"\n"]}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":["\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Measure retrieval quality separately from generation quality."}]," A bad retrieval + good LLM still produces a bad answer. Track retrieval relevance independently."]}],"\n"]}],"\n",["$","li",null,{"className":"text-base leading-relaxed","children":["\n",["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":[["$","strong",null,{"className":"font-semibold text-foreground","children":"Stream, don't wait."}]," Perceived latency matters more than total latency. First-token-time is the metric that determines user satisfaction."]}],"\n"]}],"\n"]}] 3a:["$","hr",null,{"className":"my-8 border-[var(--card-border)]"}] 3b:["$","p",null,{"className":"text-base leading-relaxed text-muted mb-5","children":["$","em",null,{"children":"These optimizations didn't happen overnight — they were spread across six months of iterative improvement, driven by user feedback and continuous monitoring. The key is having good observability so you know where to focus next."}]}] 3c:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Large sections get sentence-aware splitting"}]}] 3d:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" sub_chunks "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".sentence_split("}]]}] 3e:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" section.content,"}]}] 3f:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" target_size"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"512"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}] 40:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" overlap_sentences"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"2"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}] 41:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}] 42:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" for"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" i, sub "}],["$","span",null,{"style":{"color":"#F47067"},"children":"in"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" enumerate"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(sub_chunks):"}]]}] 43:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" chunks.append(Document("}]}] 44:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" content"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#F47067"},"children":"f"}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"Section: "}],["$","span",null,{"style":{"color":"#F47067"},"children":"{"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"section.header"}],["$","span",null,{"style":{"color":"#F47067"},"children":"}\\n\\n{"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"sub"}],["$","span",null,{"style":{"color":"#F47067"},"children":"}"}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}] 45:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" metadata"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"{"}]]}] 46:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" **"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"metadata,"}]]}] 47:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#96D0FF"},"children":" \"section\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":": section.header,"}]]}] 48:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#96D0FF"},"children":" \"chunk_index\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":": i,"}]]}] 49:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#96D0FF"},"children":" \"chunk_type\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":": "}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"sub_section\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}] 4a:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" }"}]}] 4b:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" ))"}]}] 4c:["$","span",null,{"data-line":"","children":" "}] 4d:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" return"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" chunks"}]]}] 4e:["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}] 4f:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" query_vector"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"("}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"sparse\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", SparseVector("}]]}] 50:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" indices"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"sparse_vector.indices,"}]]}] 51:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" values"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"sparse_vector.values,"}]]}] 52:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )),"}]}] 53:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" limit"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"limit "}],["$","span",null,{"style":{"color":"#F47067"},"children":"*"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" 2"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}] 54:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}] 55:["$","span",null,{"data-line":"","children":" "}] 56:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#768390"},"children":" # Reciprocal Rank Fusion"}]}] 57:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" return"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" self"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":".rrf_fusion(dense_results, sparse_results, "}],["$","span",null,{"style":{"color":"#F69D50"},"children":"limit"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"limit)"}]]}] 58:["$","span",null,{"data-line":"","children":" "}] 59:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" def"}],["$","span",null,{"style":{"color":"#DCBDFB"},"children":" rrf_fusion"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(self, "}],["$","span",null,{"style":{"color":"#F47067"},"children":"*"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"result_lists, limit: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"int"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", k: "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"int"}],["$","span",null,{"style":{"color":"#F47067"},"children":" ="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" 60"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"):"}]]}] 5a:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#96D0FF"},"children":" \"\"\"Combine rankings using Reciprocal Rank Fusion.\"\"\""}]}] 5b:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" scores "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" {}"}]]}] 5c:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" for"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" results "}],["$","span",null,{"style":{"color":"#F47067"},"children":"in"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" result_lists:"}]]}] 5d:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" for"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" rank, result "}],["$","span",null,{"style":{"color":"#F47067"},"children":"in"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" enumerate"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(results):"}]]}] 5e:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" doc_id "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" result.id"}]]}] 5f:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" scores[doc_id] "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" scores.get(doc_id, "}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"0"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":") "}],["$","span",null,{"style":{"color":"#F47067"},"children":"+"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" 1.0"}],["$","span",null,{"style":{"color":"#F47067"},"children":" /"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" (k "}],["$","span",null,{"style":{"color":"#F47067"},"children":"+"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" rank "}],["$","span",null,{"style":{"color":"#F47067"},"children":"+"}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" 1"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":")"}]]}] 60:["$","span",null,{"data-line":"","children":" "}] 61:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#ADBAC7"},"children":" sorted_docs "}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":" sorted"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"(scores.items(), "}],["$","span",null,{"style":{"color":"#F69D50"},"children":"key"}],["$","span",null,{"style":{"color":"#F47067"},"children":"=lambda"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" x: x["}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"1"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"], "}],["$","span",null,{"style":{"color":"#F69D50"},"children":"reverse"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#6CB6FF"},"children":"True"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":")"}]]}] 62:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" return"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" sorted_docs[:limit]"}]]}] 63:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" filters.append("}]}] 64:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" FieldCondition("}]}] 65:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" key"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"evidence_level\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":","}]]}] 66:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" match"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"MatchAny("}],["$","span",null,{"style":{"color":"#F69D50"},"children":"any"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"["}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"systematic_review\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", "}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"rct\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":", "}],["$","span",null,{"style":{"color":"#96D0FF"},"children":"\"meta_analysis\""}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"]),"}]]}] 67:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}] 68:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" )"}]}] 69:["$","span",null,{"data-line":"","children":" "}] 6a:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" return"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" Filter("}],["$","span",null,{"style":{"color":"#F69D50"},"children":"must"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"filters)"}]]}] 6b:["$","span",null,{"style":{"color":"#F69D50"},"children":"reverse"}] 6c:["$","span",null,{"style":{"color":"#F47067"},"children":"="}] 6d:["$","span",null,{"style":{"color":"#6CB6FF"},"children":"True"}] 6e:["$","span",null,{"style":{"color":"#ADBAC7"},"children":")"}] 6f:["$","span",null,{"data-line":"","children":" "}] 70:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" return"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" [doc "}],["$","span",null,{"style":{"color":"#F47067"},"children":"for"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" doc, score "}],["$","span",null,{"style":{"color":"#F47067"},"children":"in"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" scored_docs[:top_k]]"}]]}] 71:["$","span",null,{"style":{"color":"#ADBAC7"},"children":" embedding"}] 72:["$","span",null,{"style":{"color":"#F47067"},"children":"in"}] 73:["$","span",null,{"style":{"color":"#ADBAC7"},"children":" llm.astream("}] 74:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F69D50"},"children":" prompt"}],["$","span",null,{"style":{"color":"#F47067"},"children":"="}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":"build_prompt(query, top_docs),"}]]}] 75:["$","span",null,{"data-line":"","children":["$","span",null,{"style":{"color":"#ADBAC7"},"children":" ):"}]}] 76:["$","span",null,{"data-line":"","children":[["$","span",null,{"style":{"color":"#F47067"},"children":" yield"}],["$","span",null,{"style":{"color":"#ADBAC7"},"children":" chunk"}]]}]