%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%3D%3D0.23.13%22%2C%0A%23%20%20%20%20%20%22ollama%3E%3D0.4.0%22%2C%0A%23%20%20%20%20%20%22numpy%3E%3D1.26%22%2C%0A%23%20%20%20%20%20%22pandas%3E%3D2.2%22%2C%0A%23%20%20%20%20%20%22scikit-learn%3E%3D1.4%22%2C%0A%23%20%20%20%20%20%22plotly%3E%3D5.20%22%2C%0A%23%20%20%20%20%20%22langchain-text-splitters%3D%3D1.1.2%22%2C%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.23.13%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Introduction%20to%20RAG%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20ollama%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.express%20as%20px%0A%20%20%20%20from%20sklearn.decomposition%20import%20PCA%0A%0A%20%20%20%20return%20PCA%2C%20np%2C%20ollama%2C%20pd%2C%20px%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(np%2C%20ollama)%3A%0A%20%20%20%20EMBED_MODEL%20%3D%20%22mxbai-embed-large%22%20%20%23%201024-dim%2C%20local%3B%20separates%20short%20concepts%20more%20cleanly%20than%20nomic%0A%0A%20%20%20%20def%20embed(texts)%3A%0A%20%20%20%20%20%20%20%20vecs%20%3D%20np.array(ollama.embed(model%3DEMBED_MODEL%2C%20input%3Dlist(texts))%5B%22embeddings%22%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20%20%20%20%20%23%20normalize%20so%20cosine%20similarity%20%3D%3D%20dot%20product%0A%20%20%20%20%20%20%20%20return%20vecs%20%2F%20np.linalg.norm(vecs%2C%20axis%3D1%2C%20keepdims%3DTrue)%0A%0A%20%20%20%20return%20(embed%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Introduction%20to%20RAG%0A%20%20%20%20*Retrieval-Augmented%20Generation%20%E2%80%94%20what%20it%20actually%20is.*%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22**This%20notebook%20is%20fully%20interactive%20only%20when%20you%20run%20it%20locally.**%20It%20uses%20local%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22models%20via%20%5BOllama%5D(https%3A%2F%2Follama.com)%2C%20so%20in%20a%20static%20web%20preview%20the%20inputs%20below%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22are%20frozen.%20To%20type%20your%20own%20words%20and%20queries%2C%20clone%20the%20repo%20and%20run%20%60.%2Frun.sh%60.%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20kind%3D%22warn%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20RAG%20you%20already%20use%0A%20%20%20%20-%20Right%20now%20I'm%20talking%20to%20**Claude%20Code**%20%E2%80%94%20and%20it%20answers%20from%20my%20**memory**%2C%20my%20notes%20(**Obsidian**)%2C%20my%20tickets%20(**Linear**).%0A%20%20%20%20-%20That%20*is*%20RAG%3A%20**retrieve**%20the%20relevant%20bits%20%E2%86%92%20**feed%20them%20to%20the%20model**%20%E2%86%92%20get%20a%20**grounded**%20answer.%0A%20%20%20%20-%20You%20already%20use%20it%20every%20day.%20The%20rest%20of%20this%20talk%20is%20*what%20it%20is*%20and%20*how%20it%20works*.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20What%20is%20RAG%3F%0A%20%20%20%20Give%20the%20model%20the%20**right%20context%20at%20query%20time**%2C%20then%20let%20it%20generate.%0A%0A%20%20%20%20-%20**Ingestion**%20%E2%80%94%20parse%20sources%20(docs%20%C2%B7%20APIs)%20%E2%86%92%20select%20%E2%86%92%20process%20%E2%86%92%20store%20%26%20index%0A%20%20%20%20-%20**Retrieval**%20%E2%80%94%20rewrite%20the%20query%20%E2%86%92%20(agentic)%20search%20%E2%86%92%20pull%20from%20the%20sources%0A%20%20%20%20-%20**Augmentation**%20%E2%80%94%20filter%20%2F%20select%20findings%20%E2%86%92%20push%20into%20context%20%E2%86%92%20**LLM%20generates**%20%E2%86%92%20result%0A%0A%20%20%20%20%23%23%20Formal%20definition%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20mo.image(str(mo.notebook_dir()%20%2F%20%22assets%22%20%2F%20%22RAG-formal-definition.png%22)%2C%20width%3D460)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22%23%23%20Ingestion%20%26mdash%3B%20build%20the%20knowledge%20base%20*(ahead%20of%20time)*%22)%2C%0A%20%20%20%20%20%20%20%20mo.Html(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%3Csvg%20viewBox%3D%220%200%20820%20256%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20style%3D%22max-width%3A100%25%3Bheight%3Aauto%22%20font-family%3D%22ui-sans-serif%2C%20system-ui%2C%20sans-serif%22%3E%0A%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%3Cmarker%20id%3D%22ig%22%20markerWidth%3D%229%22%20markerHeight%3D%229%22%20refX%3D%226%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L8%2C4%20L0%2C8%20Z%22%20fill%3D%22%236366f1%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%3Cmarker%20id%3D%22igm%22%20markerWidth%3D%228%22%20markerHeight%3D%228%22%20refX%3D%225%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L7%2C4%20L0%2C8%20Z%22%20fill%3D%22currentColor%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%2290%22%20y%3D%2222%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.6%22%20font-size%3D%2212%22%3Esources%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cg%20font-size%3D%2213%22%20fill%3D%22currentColor%22%3E%0A%20%20%20%20%3Crect%20x%3D%2214%22%20y%3D%2230%22%20width%3D%22152%22%20height%3D%2234%22%20rx%3D%228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.4%22%2F%3E%0A%20%20%20%20%3Cpath%20d%3D%22M28%2038%20h12%20l4%204%20v14%20h-16%20z%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.55%22%20stroke-width%3D%221.2%22%2F%3E%0A%20%20%20%20%3Ctext%20x%3D%2256%22%20y%3D%2252%22%3EPDF%3C%2Ftext%3E%0A%20%20%20%20%3Crect%20x%3D%2214%22%20y%3D%2272%22%20width%3D%22152%22%20height%3D%2234%22%20rx%3D%228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.4%22%2F%3E%0A%20%20%20%20%3Cpath%20d%3D%22M28%2080%20h12%20l4%204%20v14%20h-16%20z%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.55%22%20stroke-width%3D%221.2%22%2F%3E%0A%20%20%20%20%3Ctext%20x%3D%2256%22%20y%3D%2294%22%3EDOC%3C%2Ftext%3E%0A%20%20%20%20%3Crect%20x%3D%2214%22%20y%3D%22114%22%20width%3D%22152%22%20height%3D%2234%22%20rx%3D%228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.4%22%2F%3E%0A%20%20%20%20%3Ctext%20x%3D%2226%22%20y%3D%22136%22%20font-family%3D%22ui-monospace%2C%20monospace%22%20opacity%3D%220.6%22%3E%7B%20%7D%3C%2Ftext%3E%0A%20%20%20%20%3Ctext%20x%3D%2256%22%20y%3D%22136%22%3EJSON%3C%2Ftext%3E%0A%20%20%20%20%3Crect%20x%3D%2214%22%20y%3D%22156%22%20width%3D%22152%22%20height%3D%2234%22%20rx%3D%228%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.4%22%2F%3E%0A%20%20%20%20%3Crect%20x%3D%2226%22%20y%3D%22164%22%20width%3D%2218%22%20height%3D%2216%22%20rx%3D%222%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.55%22%20stroke-width%3D%221.2%22%2F%3E%0A%20%20%20%20%3Ccircle%20cx%3D%2231%22%20cy%3D%22169%22%20r%3D%222%22%20fill%3D%22currentColor%22%20opacity%3D%220.55%22%2F%3E%0A%20%20%20%20%3Cpath%20d%3D%22M27%20179%20l6%20-6%20l4%204%20l4%20-3%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.55%22%20stroke-width%3D%221.2%22%2F%3E%0A%20%20%20%20%3Ctext%20x%3D%2256%22%20y%3D%22178%22%3EImages%3C%2Ftext%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Cg%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%20fill%3D%22none%22%3E%0A%20%20%20%20%3Cpath%20d%3D%22M166%2047%20C%20190%2047%2C%20196%20118%2C%20214%20128%22%20marker-end%3D%22url(%23ig)%22%2F%3E%0A%20%20%20%20%3Cpath%20d%3D%22M166%2089%20C%20192%2089%2C%20198%20124%2C%20214%20132%22%20marker-end%3D%22url(%23ig)%22%2F%3E%0A%20%20%20%20%3Cpath%20d%3D%22M166%20131%20L214%20136%22%20marker-end%3D%22url(%23ig)%22%2F%3E%0A%20%20%20%20%3Cpath%20d%3D%22M166%20173%20C%20192%20173%2C%20198%20148%2C%20214%20140%22%20marker-end%3D%22url(%23ig)%22%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22216%22%20y%3D%22108%22%20width%3D%2286%22%20height%3D%2254%22%20rx%3D%2210%22%20fill%3D%22none%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.5%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22259%22%20y%3D%22132%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2214%22%20font-weight%3D%22600%22%3EParse%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22259%22%20y%3D%22150%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.65%22%20font-size%3D%2211%22%3E%26%238594%3B%20text%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M302%20135%20L330%20135%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ig)%22%2F%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22332%22%20y%3D%2296%22%20width%3D%22150%22%20height%3D%2292%22%20rx%3D%2210%22%20fill%3D%22none%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.5%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22407%22%20y%3D%22114%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2214%22%20font-weight%3D%22600%22%3EChunk%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22344%22%20y%3D%22124%22%20width%3D%2234%22%20height%3D%2252%22%20rx%3D%222%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.5%22%2F%3E%0A%20%20%20%20%20%20%3Cline%20x1%3D%22344%22%20y1%3D%22141%22%20x2%3D%22378%22%20y2%3D%22141%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.5%22%20stroke-dasharray%3D%223%202%22%2F%3E%0A%20%20%20%20%20%20%3Cline%20x1%3D%22344%22%20y1%3D%22158%22%20x2%3D%22378%22%20y2%3D%22158%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.5%22%20stroke-dasharray%3D%223%202%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M384%20148%20l14%200%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.5%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ig)%22%2F%3E%0A%20%20%20%20%20%20%3Cg%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.14%22%20stroke%3D%22%236366f1%22%20stroke-opacity%3D%220.7%22%3E%0A%20%20%20%20%3Crect%20x%3D%22414%22%20y%3D%22126%22%20width%3D%2256%22%20height%3D%2214%22%20rx%3D%223%22%2F%3E%0A%20%20%20%20%3Crect%20x%3D%22414%22%20y%3D%22145%22%20width%3D%2256%22%20height%3D%2214%22%20rx%3D%223%22%2F%3E%0A%20%20%20%20%3Crect%20x%3D%22414%22%20y%3D%22164%22%20width%3D%2256%22%20height%3D%2214%22%20rx%3D%223%22%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M420%2096%20C%20440%2058%2C%20470%2052%2C%20500%2050%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ig)%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22452%22%20y%3D%2276%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.7%22%20font-size%3D%2211%22%3Echunks%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22504%22%20y%3D%2216%22%20width%3D%22192%22%20height%3D%2266%22%20rx%3D%2212%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.05%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.6%22%20stroke-dasharray%3D%226%204%22%2F%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22520%22%20y%3D%2234%22%20width%3D%2226%22%20height%3D%2226%22%20rx%3D%225%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.18%22%20stroke%3D%22%236366f1%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22533%22%20y%3D%2252%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2212%22%3E%26%238776%3B%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22622%22%20y%3D%2244%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2213%22%20font-weight%3D%22600%22%3EEmbedding%20model%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22622%22%20y%3D%2262%22%20text-anchor%3D%22middle%22%20fill%3D%22%236366f1%22%20font-size%3D%2211%22%3Eexternal%20service%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M654%2082%20C%20676%20114%2C%20704%20126%2C%20726%20138%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ig)%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22692%22%20y%3D%22110%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.75%22%20font-size%3D%2211%22%3Evector%20%3D%20key%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M472%20170%20C%20560%20212%2C%20650%20208%2C%20712%20186%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.55%22%20stroke-width%3D%221.6%22%20fill%3D%22none%22%20marker-end%3D%22url(%23igm)%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22586%22%20y%3D%22228%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.7%22%20font-size%3D%2211%22%3Echunk%20%3D%20value%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M710%20148%20v44%20a44%2012%200%200%200%2088%200%20v-44%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.1%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%2F%3E%0A%20%20%20%20%20%20%3Cellipse%20cx%3D%22754%22%20cy%3D%22148%22%20rx%3D%2244%22%20ry%3D%2212%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.2%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%2F%3E%0A%20%20%20%20%20%20%3Cg%3E%0A%20%20%20%20%3Ccircle%20cx%3D%22724%22%20cy%3D%22166%22%20r%3D%223%22%20fill%3D%22%236366f1%22%2F%3E%3Cline%20x1%3D%22729%22%20y1%3D%22166%22%20x2%3D%22744%22%20y2%3D%22166%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.2%22%2F%3E%3Crect%20x%3D%22746%22%20y%3D%22161%22%20width%3D%2212%22%20height%3D%2210%22%20rx%3D%222%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.6%22%2F%3E%0A%20%20%20%20%3Ccircle%20cx%3D%22724%22%20cy%3D%22182%22%20r%3D%223%22%20fill%3D%22%236366f1%22%2F%3E%3Cline%20x1%3D%22729%22%20y1%3D%22182%22%20x2%3D%22744%22%20y2%3D%22182%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.2%22%2F%3E%3Crect%20x%3D%22746%22%20y%3D%22177%22%20width%3D%2212%22%20height%3D%2210%22%20rx%3D%222%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.6%22%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22754%22%20y%3D%22218%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2213%22%20font-weight%3D%22600%22%3EKnowledge%20base%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22754%22%20y%3D%22234%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.6%22%20font-size%3D%2211%22%3Evector%20%26%238594%3B%20chunk%3C%2Ftext%3E%0A%20%20%20%20%3C%2Fsvg%3E%22%22%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22%23%23%20Retrieval%20%26mdash%3B%20find%20the%20relevant%20bits%20*(at%20query%20time)*%22)%2C%0A%20%20%20%20%20%20%20%20mo.Html(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%3Csvg%20viewBox%3D%220%200%20820%20320%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20style%3D%22max-width%3A100%25%3Bheight%3Aauto%22%20font-family%3D%22ui-sans-serif%2C%20system-ui%2C%20sans-serif%22%3E%0A%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%3Cmarker%20id%3D%22ga%22%20markerWidth%3D%229%22%20markerHeight%3D%229%22%20refX%3D%226%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L8%2C4%20L0%2C8%20Z%22%20fill%3D%22%236366f1%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%3Cmarker%20id%3D%22gw%22%20markerWidth%3D%229%22%20markerHeight%3D%229%22%20refX%3D%226%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L8%2C4%20L0%2C8%20Z%22%20fill%3D%22%23e0813a%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%3Cmarker%20id%3D%22gm%22%20markerWidth%3D%228%22%20markerHeight%3D%228%22%20refX%3D%225%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L7%2C4%20L0%2C8%20Z%22%20fill%3D%22currentColor%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%2212%22%20y%3D%2228%22%20width%3D%22164%22%20height%3D%2256%22%20rx%3D%2210%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.4%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M26%2044%20h20%20v12%20h-14%20l-6%206%20z%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.5%22%20stroke-width%3D%221.2%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%2296%22%20y%3D%2252%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2213%22%3EConversation%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%2296%22%20y%3D%2270%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2213%22%3Ehistory%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%2212%22%20y%3D%22130%22%20width%3D%22164%22%20height%3D%2256%22%20rx%3D%2210%22%20fill%3D%22none%22%20stroke%3D%22%236366f1%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%2296%22%20y%3D%22153%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2214%22%20font-weight%3D%22600%22%3EUser%20query%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%2296%22%20y%3D%22172%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.7%22%20font-size%3D%2212%22%20font-style%3D%22italic%22%3E%22what%20is%20it%3F%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cg%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%20fill%3D%22none%22%3E%0A%20%20%20%20%3Cpath%20d%3D%22M176%2060%20C%20196%2060%2C%20200%2096%2C%20220%20104%22%20marker-end%3D%22url(%23ga)%22%2F%3E%0A%20%20%20%20%3Cpath%20d%3D%22M176%20158%20C%20196%20158%2C%20200%20124%2C%20220%20116%22%20marker-end%3D%22url(%23ga)%22%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22222%22%20y%3D%2280%22%20width%3D%22150%22%20height%3D%2260%22%20rx%3D%2210%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.06%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.6%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22297%22%20y%3D%22105%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2214%22%20font-weight%3D%22600%22%3EContextualize%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22297%22%20y%3D%22124%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.65%22%20font-size%3D%2211%22%3E%26%238594%3B%20self-contained%20query%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M372%20110%20L402%20110%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ga)%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M404%2082%20h96%20a10%2010%200%200%201%2010%2010%20v36%20a10%2010%200%200%201%20-10%2010%20h-96%20a10%2010%200%200%201%20-10%20-10%20v-36%20a10%2010%200%200%201%2010%20-10%20z%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.1%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.8%22%2F%3E%0A%20%20%20%20%20%20%3Ccircle%20cx%3D%22428%22%20cy%3D%22112%22%20r%3D%229%22%20fill%3D%22none%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.5%22%2F%3E%0A%20%20%20%20%20%20%3Ccircle%20cx%3D%22425%22%20cy%3D%22110%22%20r%3D%221.4%22%20fill%3D%22%236366f1%22%2F%3E%3Ccircle%20cx%3D%22431%22%20cy%3D%22110%22%20r%3D%221.4%22%20fill%3D%22%236366f1%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22472%22%20y%3D%22117%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2215%22%20font-weight%3D%22700%22%3EAgent%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M512%20100%20C%20552%2092%2C%20578%2092%2C%20600%2096%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ga)%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22556%22%20y%3D%2286%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.75%22%20font-size%3D%2211%22%3Eretrieve%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M600%20132%20C%20578%20138%2C%20552%20138%2C%20514%20132%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.55%22%20stroke-width%3D%221.5%22%20fill%3D%22none%22%20marker-end%3D%22url(%23gm)%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22556%22%20y%3D%22152%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.6%22%20font-size%3D%2211%22%3Eresults%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M470%2078%20C%20470%2040%2C%20630%2040%2C%20636%2080%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.5%22%20stroke-dasharray%3D%225%204%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ga)%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22552%22%20y%3D%2234%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.7%22%20font-size%3D%2211%22%3Eexplore%20%26%238594%3B%20retrieve%20more%20%20(multi-step)%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M602%2096%20v44%20a40%2012%200%200%200%2080%200%20v-44%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.1%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%2F%3E%0A%20%20%20%20%20%20%3Cellipse%20cx%3D%22642%22%20cy%3D%2296%22%20rx%3D%2240%22%20ry%3D%2212%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.2%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22642%22%20y%3D%22176%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2213%22%20font-weight%3D%22600%22%3EKnowledge%20base%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M462%20142%20C%20452%20200%2C%20470%20220%2C%20512%20236%22%20stroke%3D%22%23e0813a%22%20stroke-width%3D%222%22%20fill%3D%22none%22%20marker-end%3D%22url(%23gw)%22%2F%3E%0A%20%20%20%20%20%20%3Cg%20stroke%3D%22%23e0813a%22%20fill%3D%22none%22%20stroke-width%3D%221.5%22%3E%0A%20%20%20%20%3Cpath%20d%3D%22M528%20214%20h20%20l6%206%20v30%20h-26%20z%22%2F%3E%0A%20%20%20%20%3Cpath%20d%3D%22M536%20224%20h20%20l6%206%20v30%20h-26%20z%22%2F%3E%0A%20%20%20%20%3Cpath%20d%3D%22M544%20234%20h20%20l6%206%20v30%20h-26%20z%22%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22560%22%20y%3D%22298%22%20text-anchor%3D%22middle%22%20fill%3D%22%23e0813a%22%20font-size%3D%2213%22%3Erelevant%20pieces%3C%2Ftext%3E%0A%20%20%20%20%3C%2Fsvg%3E%22%22%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22%23%23%20Augmentation%20%26mdash%3B%20assemble%20the%20context%2C%20then%20generate%22)%2C%0A%20%20%20%20%20%20%20%20mo.Html(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%3Csvg%20viewBox%3D%220%200%20780%20296%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20style%3D%22max-width%3A100%25%3Bheight%3Aauto%22%20font-family%3D%22ui-sans-serif%2C%20system-ui%2C%20sans-serif%22%3E%0A%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%3Cmarker%20id%3D%22aA%22%20markerWidth%3D%229%22%20markerHeight%3D%229%22%20refX%3D%226%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L8%2C4%20L0%2C8%20Z%22%20fill%3D%22%236366f1%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%3Cmarker%20id%3D%22aW%22%20markerWidth%3D%229%22%20markerHeight%3D%229%22%20refX%3D%226%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L8%2C4%20L0%2C8%20Z%22%20fill%3D%22%23e0813a%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%3Cmarker%20id%3D%22aI%22%20markerWidth%3D%229%22%20markerHeight%3D%229%22%20refX%3D%226%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L8%2C4%20L0%2C8%20Z%22%20fill%3D%22currentColor%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%2212%22%20y%3D%2230%22%20width%3D%22176%22%20height%3D%2244%22%20rx%3D%229%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.4%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22100%22%20y%3D%2257%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2214%22%3ESystem%20instructions%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%2212%22%20y%3D%22116%22%20width%3D%22176%22%20height%3D%2244%22%20rx%3D%229%22%20fill%3D%22none%22%20stroke%3D%22%236366f1%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22100%22%20y%3D%22143%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2214%22%3EUser%20query%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%2212%22%20y%3D%22202%22%20width%3D%22176%22%20height%3D%2244%22%20rx%3D%229%22%20fill%3D%22none%22%20stroke%3D%22%23e0813a%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22100%22%20y%3D%22229%22%20text-anchor%3D%22middle%22%20fill%3D%22%23e0813a%22%20font-size%3D%2214%22%3ERetrieved%20corpus%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22100%22%20y%3D%22262%22%20text-anchor%3D%22middle%22%20fill%3D%22%23e0813a%22%20font-size%3D%2211%22%3E%E2%86%91%20what%20RAG%20adds%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M192%2052%20L296%2068%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.5%22%20stroke-width%3D%221.6%22%20fill%3D%22none%22%20marker-end%3D%22url(%23aI)%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M192%20138%20L296%20130%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.6%22%20fill%3D%22none%22%20marker-end%3D%22url(%23aA)%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M192%20224%20L296%20190%22%20stroke%3D%22%23e0813a%22%20stroke-width%3D%222%22%20fill%3D%22none%22%20marker-end%3D%22url(%23aW)%22%2F%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22300%22%20y%3D%2234%22%20width%3D%22176%22%20height%3D%22200%22%20rx%3D%2210%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.06%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%222%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22388%22%20y%3D%2262%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2216%22%20font-weight%3D%22700%22%3ECONTEXT%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22388%22%20y%3D%2280%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.6%22%20font-size%3D%2211%22%3Ethe%20prompt%20the%20model%20sees%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cg%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.25%22%20stroke-width%3D%221.4%22%3E%0A%20%20%20%20%3Cline%20x1%3D%22320%22%20y1%3D%22102%22%20x2%3D%22456%22%20y2%3D%22102%22%2F%3E%3Cline%20x1%3D%22320%22%20y1%3D%22120%22%20x2%3D%22456%22%20y2%3D%22120%22%2F%3E%0A%20%20%20%20%3Cline%20x1%3D%22320%22%20y1%3D%22138%22%20x2%3D%22456%22%20y2%3D%22138%22%2F%3E%3Cline%20x1%3D%22320%22%20y1%3D%22156%22%20x2%3D%22440%22%20y2%3D%22156%22%2F%3E%0A%20%20%20%20%3Cline%20x1%3D%22320%22%20y1%3D%22174%22%20x2%3D%22456%22%20y2%3D%22174%22%2F%3E%3Cline%20x1%3D%22320%22%20y1%3D%22192%22%20x2%3D%22430%22%20y2%3D%22192%22%2F%3E%0A%20%20%20%20%3Cline%20x1%3D%22320%22%20y1%3D%22210%22%20x2%3D%22456%22%20y2%3D%22210%22%2F%3E%0A%20%20%20%20%20%20%3C%2Fg%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M476%20134%20L522%20134%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.8%22%20fill%3D%22none%22%20marker-end%3D%22url(%23aA)%22%2F%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22524%22%20y%3D%22110%22%20width%3D%2286%22%20height%3D%2248%22%20rx%3D%229%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.12%22%20stroke%3D%22%236366f1%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22567%22%20y%3D%22139%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2215%22%20font-weight%3D%22700%22%3ELLM%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M610%20134%20L650%20134%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.8%22%20fill%3D%22none%22%20marker-end%3D%22url(%23aA)%22%2F%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22652%22%20y%3D%22113%22%20width%3D%22112%22%20height%3D%2242%22%20rx%3D%2221%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.4%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22708%22%20y%3D%22139%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2214%22%3EAnswer%3C%2Ftext%3E%0A%20%20%20%20%3C%2Fsvg%3E%22%22%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20What%20*is*%20a%20vector%20(semantic)%20search%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20The%20toy%3A%20axes%20*I*%20chose%0A%20%20%20%20Three%20axes%20I%20can%20name.%20Similar%20fruits%20sit%20close.%20This%20is%20only%20the%20intuition.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(px)%3A%0A%20%20%20%20_fruits%20%3D%20%5B%22lemon%22%2C%20%22lime%22%2C%20%22orange%22%2C%20%22apple%22%2C%20%22banana%22%2C%20%22watermelon%22%5D%0A%20%20%20%20%23%20hand-picked%2C%20human-readable%20axes%20(0..1)%3A%20sweetness%2C%20sourness%2C%20juiciness%0A%20%20%20%20_sweet%20%3D%20%5B0.10%2C%200.10%2C%200.70%2C%200.65%2C%200.80%2C%200.75%5D%0A%20%20%20%20_sour%20%3D%20%5B0.95%2C%200.90%2C%200.50%2C%200.35%2C%200.10%2C%200.10%5D%0A%20%20%20%20_juicy%20%3D%20%5B0.60%2C%200.55%2C%200.90%2C%200.60%2C%200.30%2C%200.98%5D%0A%20%20%20%20_fig%20%3D%20px.scatter_3d(x%3D_sweet%2C%20y%3D_sour%2C%20z%3D_juicy%2C%20text%3D_fruits)%0A%20%20%20%20_fig.update_traces(marker_size%3D6%2C%20textposition%3D%22top%20center%22)%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20scene%3Ddict(xaxis_title%3D%22sweetness%22%2C%20yaxis_title%3D%22sourness%22%2C%20zaxis_title%3D%22juiciness%22)%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D0%2C%20r%3D0%2C%20t%3D0%2C%20b%3D0)%2C%0A%20%20%20%20)%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Reality%3A%20axes%20the%20model%20chose%2C%20and%20won't%20explain%0A%20%20%20%20One%20word%20%E2%86%92%20**1024%20numbers**%2C%20signed%2C%20unlabeled.%20There%20is%20no%20%22sweetness%22%20axis%2C%20and%20a%0A%20%20%20%20*different*%20model%20would%20produce%20different%20numbers%20for%20the%20same%20word.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20words%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22apple%22%2C%20%22orange%22%2C%20%22banana%22%2C%20%22lemon%22%2C%20%20%20%20%20%20%20%20%20%20%23%20fruit%0A%20%20%20%20%20%20%20%20%22car%22%2C%20%22truck%22%2C%20%22bicycle%22%2C%20%22airplane%22%2C%20%20%20%20%20%20%20%20%20%23%20vehicle%0A%20%20%20%20%20%20%20%20%22guitar%22%2C%20%22violin%22%2C%20%22piano%22%2C%20%22drum%22%2C%20%20%20%20%20%20%20%20%20%20%20%23%20instrument%0A%20%20%20%20%20%20%20%20%22democracy%22%2C%20%22monarchy%22%2C%20%22justice%22%2C%20%22freedom%22%2C%20%20%23%20concept%0A%20%20%20%20%5D%0A%20%20%20%20groups%20%3D%20%5B%22fruit%22%5D%20*%204%20%2B%20%5B%22vehicle%22%5D%20*%204%20%2B%20%5B%22instrument%22%5D%20*%204%20%2B%20%5B%22concept%22%5D%20*%204%0A%20%20%20%20return%20groups%2C%20words%0A%0A%0A%40app.cell%0Adef%20_(embed%2C%20words)%3A%0A%20%20%20%20E%20%3D%20embed(words)%20%20%23%20(16%2C%201024)%2C%20unit%20vectors%0A%20%20%20%20return%20(E%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(E%2C%20mo%2C%20words)%3A%0A%20%20%20%20_peek%20%3D%20%22%2C%20%22.join(f%22%7Bx%3A%2B.3f%7D%22%20for%20x%20in%20E%5B0%5D%5B%3A8%5D)%0A%20%20%20%20mo.md(f%22%60%7Bwords%5B0%5D%7D%60%20%3D%20a%20point%20in%20**%7BE.shape%5B1%5D%7D-D**.%20First%208%20of%20its%20coordinates%3A%20%60%5B%7B_peek%7D%2C%20%E2%80%A6%5D%60%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Seeing%20it%3A%20a%201024-D%20*shadow*%0A%20%20%20%20We%20can't%20picture%201024-D%2C%20so%20we%20flatten%20it%20onto%20three%20axes%20(PCA)%20and%20look%20at%20the%20*shadow*.%0A%20%20%20%20Clusters%20are%20real%3B%20the%20axes%20and%20exact%20distances%20are%20not.%20Type%20any%20words%20(comma-separated)%0A%20%20%20%20and%20watch%20where%20their%20meaning%20lands%20%E2%80%94%20same%20space%2C%20projected%20the%20same%20way.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(E%2C%20PCA)%3A%0A%20%20%20%20pca%20%3D%20PCA(n_components%3D3).fit(E)%0A%20%20%20%20var_kept%20%3D%20float(pca.explained_variance_ratio_.sum())%0A%20%20%20%20return%20pca%2C%20var_kept%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20concept%20%3D%20mo.ui.text(value%3D%22strawberry%22%2C%20label%3D%22Embed%20word(s)%20%E2%80%94%20comma-separated%22%2C%20full_width%3DTrue)%0A%20%20%20%20concept%0A%20%20%20%20return%20(concept%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(E%2C%20concept%2C%20embed%2C%20groups%2C%20pca%2C%20px%2C%20var_kept%2C%20words)%3A%0A%20%20%20%20_term%20%3D%20concept.value.strip()%20or%20%22strawberry%22%0A%20%20%20%20_EP%20%3D%20pca.transform(E)%0A%20%20%20%20_QP%20%3D%20pca.transform(embed(%5B_term%5D))%0A%20%20%20%20_x%20%3D%20list(_EP%5B%3A%2C%200%5D)%20%2B%20list(_QP%5B%3A%2C%200%5D)%0A%20%20%20%20_y%20%3D%20list(_EP%5B%3A%2C%201%5D)%20%2B%20list(_QP%5B%3A%2C%201%5D)%0A%20%20%20%20_z%20%3D%20list(_EP%5B%3A%2C%202%5D)%20%2B%20list(_QP%5B%3A%2C%202%5D)%0A%20%20%20%20_labels%20%3D%20list(words)%20%2B%20%5B_term%5B%3A15%5D%5D%0A%20%20%20%20_color%20%3D%20list(groups)%20%2B%20%5B%22%E2%98%85%20your%20word%22%5D%0A%20%20%20%20_fig%20%3D%20px.scatter_3d(%0A%20%20%20%20%20%20%20%20x%3D_x%2C%20y%3D_y%2C%20z%3D_z%2C%20text%3D_labels%2C%20color%3D_color%2C%0A%20%20%20%20%20%20%20%20title%3Df%22a%20lossy%20shadow%20%E2%80%94%203%20axes%20keep%20%7Bvar_kept%3A.0%25%7D%20of%20the%20variance%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.update_traces(marker_size%3D6%2C%20textposition%3D%22top%20center%22)%0A%20%20%20%20_fig.update_layout(margin%3Ddict(l%3D0%2C%20r%3D0%2C%20t%3D30%2C%20b%3D0))%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Search%3A%20find%20by%20meaning%2C%20not%20keywords%0A%20%20%20%20Type%20a%20question.%20We%20embed%20it%20and%20rank%20a%20small%20corpus%20by%20*nearness*%20%E2%80%94%20no%20shared%20words%20needed.%0A%20%20%20%20The%20**%E2%98%85%20query**%20lands%20next%20to%20its%20answers%3B%20the%20**red-ringed**%20points%20are%20the%20top-K%20we%20return.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20corpus%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22Knead%20the%20dough%20and%20let%20it%20rise%20before%20baking.%22%2C%0A%20%20%20%20%20%20%20%20%22A%20pinch%20of%20salt%20sharpens%20almost%20any%20dish.%22%2C%0A%20%20%20%20%20%20%20%20%22Let%20the%20loaf%20cool%20on%20a%20rack%20before%20slicing.%22%2C%0A%20%20%20%20%20%20%20%20%22The%20telescope%20captured%20a%20distant%20spiral%20galaxy.%22%2C%0A%20%20%20%20%20%20%20%20%22Astronauts%20train%20for%20months%20before%20a%20launch.%22%2C%0A%20%20%20%20%20%20%20%20%22A%20black%20hole's%20gravity%20traps%20even%20light.%22%2C%0A%20%20%20%20%20%20%20%20%22Refactor%20the%20function%20to%20remove%20the%20nested%20loops.%22%2C%0A%20%20%20%20%20%20%20%20%22The%20unit%20test%20failed%20on%20a%20tricky%20edge%20case.%22%2C%0A%20%20%20%20%20%20%20%20%22Cache%20the%20results%20so%20you%20never%20recompute%20them.%22%2C%0A%20%20%20%20%20%20%20%20%22The%20fox%20hunts%20mice%20in%20the%20tall%20grass%20at%20dusk.%22%2C%0A%20%20%20%20%20%20%20%20%22Whales%20migrate%20thousands%20of%20miles%20every%20year.%22%2C%0A%20%20%20%20%20%20%20%20%22Bees%20tell%20each%20other%20where%20the%20flowers%20are.%22%2C%0A%20%20%20%20%5D%0A%20%20%20%20corpus_topic%20%3D%20%5B%22cooking%22%5D%20*%203%20%2B%20%5B%22space%22%5D%20*%203%20%2B%20%5B%22code%22%5D%20*%203%20%2B%20%5B%22nature%22%5D%20*%203%0A%20%20%20%20return%20corpus%2C%20corpus_topic%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(PCA%2C%20corpus%2C%20embed)%3A%0A%20%20%20%20CE%20%3D%20embed(corpus)%20%20%23%20corpus%20embeddings%0A%20%20%20%20search_pca%20%3D%20PCA(n_components%3D2).fit(CE)%20%20%23%202-D%20map%20so%20%22near%22%20is%20obvious%0A%20%20%20%20return%20CE%2C%20search_pca%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20query%20%3D%20mo.ui.text(value%3D%22how%20do%20I%20make%20bread%3F%22%2C%20label%3D%22Search%20query%22%2C%20full_width%3DTrue)%0A%20%20%20%20top_k%20%3D%20mo.ui.slider(1%2C%206%2C%20value%3D3%2C%20label%3D%22Top-K%20results%22)%0A%20%20%20%20return%20query%2C%20top_k%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20query%2C%20top_k)%3A%0A%20%20%20%20mo.vstack(%5Bquery%2C%20top_k%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20CE%2C%0A%20%20%20%20corpus%2C%0A%20%20%20%20corpus_topic%2C%0A%20%20%20%20embed%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20pd%2C%0A%20%20%20%20px%2C%0A%20%20%20%20query%2C%0A%20%20%20%20search_pca%2C%0A%20%20%20%20top_k%2C%0A)%3A%0A%20%20%20%20_q%20%3D%20embed(%5Bquery.value%20or%20%22how%20do%20I%20make%20bread%3F%22%5D)%5B0%5D%0A%20%20%20%20_sims%20%3D%20CE%20%40%20_q%0A%20%20%20%20_hits%20%3D%20list(np.argsort(-_sims)%5B%3A%20top_k.value%5D)%0A%0A%20%20%20%20_lines%20%3D%20%5B%0A%20%20%20%20%20%20%20%20f%22%7B_r%20%2B%201%7D.%20**%7B_sims%5B_i%5D%3A.2f%7D**%20%20%C2%B7%20%20%7Bcorpus%5B_i%5D%7D%20%20%E2%80%94%20%20_%7Bcorpus_topic%5B_i%5D%7D_%22%0A%20%20%20%20%20%20%20%20for%20_r%2C%20_i%20in%20enumerate(_hits)%0A%20%20%20%20%5D%0A%0A%20%20%20%20_CP%20%3D%20search_pca.transform(CE)%0A%20%20%20%20_qp%20%3D%20search_pca.transform(_q.reshape(1%2C%20-1))%5B0%5D%0A%20%20%20%20_df%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22x%22%3A%20_CP%5B%3A%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%22y%22%3A%20_CP%5B%3A%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22topic%22%3A%20corpus_topic%2C%0A%20%20%20%20%20%20%20%20%22text%22%3A%20%5Bc%5B%3A20%5D%20%2B%20%22%E2%80%A6%22%20for%20c%20in%20corpus%5D%2C%20%20%23%20hover%20shows%20the%20first%2020%20chars%0A%20%20%20%20%7D)%0A%20%20%20%20_fig%20%3D%20px.scatter(%0A%20%20%20%20%20%20%20%20_df%2C%20x%3D%22x%22%2C%20y%3D%22y%22%2C%20color%3D%22topic%22%2C%0A%20%20%20%20%20%20%20%20hover_name%3D%22text%22%2C%20hover_data%3D%7B%22x%22%3A%20False%2C%20%22y%22%3A%20False%7D%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.update_traces(marker_size%3D11)%0A%20%20%20%20_fig.add_scatter(%0A%20%20%20%20%20%20%20%20x%3D_CP%5B_hits%2C%200%5D%2C%20y%3D_CP%5B_hits%2C%201%5D%2C%20mode%3D%22markers%22%2C%20name%3D%22top-K%22%2C%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%20%20%20%20marker%3Ddict(size%3D20%2C%20color%3D%22rgba(0%2C0%2C0%2C0)%22%2C%20line%3Ddict(width%3D2.5%2C%20color%3D%22%23d62728%22))%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.add_scatter(%0A%20%20%20%20%20%20%20%20x%3D%5B_qp%5B0%5D%5D%2C%20y%3D%5B_qp%5B1%5D%5D%2C%20mode%3D%22markers%2Btext%22%2C%20name%3D%22query%22%2C%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%20%20%20%20marker%3Ddict(size%3D22%2C%20symbol%3D%22star%22%2C%20color%3D%22%23d62728%22)%2C%0A%20%20%20%20%20%20%20%20text%3D%5B%22query%22%5D%2C%20textposition%3D%22bottom%20center%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.update_layout(margin%3Ddict(l%3D0%2C%20r%3D0%2C%20t%3D0%2C%20b%3D0)%2C%20height%3D430%2C%20legend_title_text%3D%22%22)%0A%0A%20%20%20%20mo.vstack(%5Bmo.md(%22**Results%20%E2%80%94%20nearest%20by%20meaning%3A**%5Cn%5Cn%22%20%2B%20%22%5Cn%22.join(_lines))%2C%20_fig%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20The%20space%20belongs%20to%20the%20*model*%2C%20not%20the%20world%0A%20%20%20%20Pick%20an%20embedding%20model%20and%20you're%20married%20to%20it%3A%20every%20document%20you%20store%20must%20be%20embedded%0A%20%20%20%20with%20the%20**same**%20model%2C%20or%20the%20numbers%20don't%20line%20up.%20Switch%20models%20%E2%86%92%20re-embed%20everything.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20np%2C%20ollama)%3A%0A%20%20%20%20def%20_vec(model%2C%20text)%3A%0A%20%20%20%20%20%20%20%20v%20%3D%20np.array(ollama.embed(model%3Dmodel%2C%20input%3D%5Btext%5D)%5B%22embeddings%22%5D%5B0%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20%20%20%20%20return%20v%20%2F%20np.linalg.norm(v)%0A%0A%20%20%20%20_models%20%3D%20%5B%22mxbai-embed-large%22%2C%20%22nomic-embed-text%22%5D%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_m%20in%20_models%3A%0A%20%20%20%20%20%20%20%20_v%20%3D%20_vec(_m%2C%20%22apple%22)%0A%20%20%20%20%20%20%20%20_ao%20%3D%20float(_v%20%40%20_vec(_m%2C%20%22orange%22))%0A%20%20%20%20%20%20%20%20_ad%20%3D%20float(_v%20%40%20_vec(_m%2C%20%22democracy%22))%0A%20%20%20%20%20%20%20%20_rows.append(f%22%7C%20%60%7B_m%7D%60%20%7C%20%7B_v.shape%5B0%5D%7D%20%7C%20%7B_ao%3A.3f%7D%20%7C%20%7B_ad%3A.3f%7D%20%7C%22)%0A%0A%20%20%20%20_table%20%3D%20(%0A%20%20%20%20%20%20%20%20%22%7C%20model%20%7C%20dims%20%7C%20cos(apple%2C%20orange)%20%7C%20cos(apple%2C%20democracy)%20%7C%5Cn%22%0A%20%20%20%20%20%20%20%20%22%7C---%7C---%7C---%7C---%7C%5Cn%22%20%2B%20%22%5Cn%22.join(_rows)%0A%20%20%20%20)%0A%20%20%20%20_raw%20%3D%20%22%5Cn%22.join(%0A%20%20%20%20%20%20%20%20f%22-%20%60%7B_m%7D%60%20%E2%86%92%20%60%5B%7B'%2C%20'.join(f'%7Bx%3A%2B.2f%7D'%20for%20x%20in%20_vec(_m%2C%20'apple')%5B%3A10%5D)%7D%2C%20%E2%80%A6%5D%60%22%0A%20%20%20%20%20%20%20%20for%20_m%20in%20_models%0A%20%20%20%20)%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20_table%0A%20%20%20%20%20%20%20%20%2B%20%22%5Cn%5CnSame%20word%20**%60apple%60**%2C%20first%2010%20numbers%20from%20each%20model%20%E2%80%94%20different%20length%2C%20%22%0A%20%20%20%20%20%20%20%20%22different%20values%2C%20**not%20interchangeable**%3A%5Cn%5Cn%22%20%2B%20_raw%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Making%20it%20work%20on%20real%20documents%0A%20%20%20%20Real%20documents%20are%20long%2C%20so%20we%20cut%20them%20into%20**chunks**.%20But%20a%20chunk%20pulled%20out%20of%20its%20page%0A%20%20%20%20loses%20the%20context%20that%20told%20you%20what%20it%20was%20about%20%E2%80%94%20and%20then%20search%20can't%20find%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20problem%3A%20a%20chunk%20forgets%20where%20it%20came%20from%0A%20%20%20%20A%20spec%20sheet%20reads%20*%22up%20to%2034%20hours%20of%20video%20playback.%22*%20**Which%20phone%3F**%20The%20sentence%20never%0A%20%20%20%20says.%20Embed%20that%20chunk%20on%20its%20own%20and%20a%20search%20for%20**%22Nimbus%20N1%20battery%20life%22**%20can%20rank%20a%0A%20%20%20%20*different*%20phone's%20battery%20section%20above%20the%20right%20one%20%E2%80%94%20the%20answer%20is%20in%20the%20corpus%2C%20but%0A%20%20%20%20unfindable.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20import%20glob%20as%20_glob%0A%20%20%20%20import%20pathlib%20as%20_pathlib%0A%20%20%20%20from%20langchain_text_splitters%20import%20RecursiveCharacterTextSplitter%20as%20_Splitter%0A%0A%20%20%20%20CHUNK_SIZE%2C%20CHUNK_OVERLAP%20%3D%20500%2C%2080%0A%20%20%20%20_dir%20%3D%20_pathlib.Path(mo.notebook_dir())%20%2F%20%22docs%22%0A%20%20%20%20_splitter%20%3D%20_Splitter(chunk_size%3DCHUNK_SIZE%2C%20chunk_overlap%3DCHUNK_OVERLAP)%0A%0A%20%20%20%20c4_docs%20%3D%20%7B%7D%0A%20%20%20%20c4_chunks%20%3D%20%5B%5D%0A%20%20%20%20c4_origin%20%3D%20%5B%5D%0A%20%20%20%20for%20_p%20in%20sorted(_glob.glob(str(_dir%20%2F%20%22*.md%22)))%3A%0A%20%20%20%20%20%20%20%20_stem%20%3D%20_pathlib.Path(_p).stem%0A%20%20%20%20%20%20%20%20_text%20%3D%20_pathlib.Path(_p).read_text()%0A%20%20%20%20%20%20%20%20c4_docs%5B_stem%5D%20%3D%20_text%0A%20%20%20%20%20%20%20%20for%20_c%20in%20_splitter.split_text(_text)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20c4_chunks.append(_c)%0A%20%20%20%20%20%20%20%20%20%20%20%20c4_origin.append(_stem)%0A%20%20%20%20return%20CHUNK_OVERLAP%2C%20CHUNK_SIZE%2C%20c4_chunks%2C%20c4_docs%2C%20c4_origin%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(CHUNK_OVERLAP%2C%20CHUNK_SIZE%2C%20c4_chunks%2C%20c4_docs%2C%20mo)%3A%0A%20%20%20%20_example%20%3D%20c4_chunks%5B0%5D.strip()%0A%20%20%20%20_full%20%3D%20%7Bf%22%F0%9F%93%84%20%7B_k%7D%22%3A%20mo.md(_v)%20for%20_k%2C%20_v%20in%20c4_docs.items()%7D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(c4_chunks)%7D%20chunks**%20from%20**%7Blen(c4_docs)%7D%20documents**%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22(size%20%E2%89%88%20%7BCHUNK_SIZE%7D%20chars%2C%20overlap%20%7BCHUNK_OVERLAP%7D).%20One%20chunk%20looks%20like%3A%22)%2C%0A%20%20%20%20%20%20%20%20mo.Html(f'%3Cdiv%20style%3D%22padding%3A8px%2010px%3Bborder%3A1px%20solid%20%236366f1%3Bborder-radius%3A6px%3Bopacity%3A.9%22%3E%7B_example%7D%3C%2Fdiv%3E')%2C%0A%20%20%20%20%20%20%20%20mo.md(%22*The%20full%20documents%20%E2%80%94%20click%20to%20expand%3A*%22)%2C%0A%20%20%20%20%20%20%20%20mo.accordion(_full)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Multiple%20ways%20to%20ingest%20the%20*same*%20chunks%0A%20%20%20%20**Na%C3%AFve**%20%E2%80%94%20embed%20each%20chunk%20exactly%20as%20in%20the%20last%20chapter%3B%20nothing%20added.%0A%0A%20%20%20%20**Contextual%20Retrieval**%20*(%5BAnthropic%2C%202024%5D(https%3A%2F%2Fwww.anthropic.com%2Fengineering%2Fcontextual-retrieval))*%20%E2%80%94%20first%20an%20LLM%20writes%20a%20summary%20that%20*situates%20the%20chunk%20inside%20its%20document*%2C%20and%20we%20**prepend%20it%20before%20embedding**.%20The%0A%20%20%20%20embedding%20model%20is%20untouched%20%E2%80%94%20**we%20fix%20the%20input**.%0A%0A%20%20%20%20%3E%20Prompt%20(the%20whole%20document%20%2B%20the%20chunk)%3A%20*%22%E2%80%A6give%20a%20short%20succinct%20context%20to%20situate%20this%0A%20%20%20%20%3E%20chunk%20within%20the%20overall%20document%20for%20the%20purposes%20of%20improving%20search%20retrieval%20of%20the%0A%20%20%20%20%3E%20chunk.%20Answer%20only%20with%20the%20succinct%20context%20and%20nothing%20else.%22*%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22%23%23%20In%20one%20picture%22)%2C%0A%20%20%20%20%20%20%20%20mo.Html(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%3Csvg%20viewBox%3D%220%200%20840%20300%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20style%3D%22max-width%3A100%25%3Bheight%3Aauto%22%20font-family%3D%22ui-sans-serif%2C%20system-ui%2C%20sans-serif%22%20role%3D%22img%22%3E%0A%20%20%20%20%20%20%3Ctitle%3ENa%C3%AFve%20vs%20Contextual%20ingestion%3C%2Ftitle%3E%0A%20%20%20%20%20%20%3Cdesc%3ENa%C3%AFve%20embeds%20the%20chunk%20alone%3B%20Contextual%20has%20an%20LLM%20write%20a%20situating%20blurb%2C%20prepends%20it%20to%20the%20chunk%2C%20then%20embeds%20that%20%E2%80%94%20same%20embedding%20model.%3C%2Fdesc%3E%0A%20%20%20%20%20%20%3Cdefs%3E%0A%20%20%20%20%3Cmarker%20id%3D%22ci%22%20markerWidth%3D%229%22%20markerHeight%3D%229%22%20refX%3D%226%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L8%2C4%20L0%2C8%20Z%22%20fill%3D%22%236366f1%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%3Cmarker%20id%3D%22ca%22%20markerWidth%3D%229%22%20markerHeight%3D%229%22%20refX%3D%226%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L8%2C4%20L0%2C8%20Z%22%20fill%3D%22%23e0813a%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%3Cmarker%20id%3D%22cn%22%20markerWidth%3D%228%22%20markerHeight%3D%228%22%20refX%3D%225%22%20refY%3D%224%22%20orient%3D%22auto%22%3E%3Cpath%20d%3D%22M0%2C0%20L7%2C4%20L0%2C8%20Z%22%20fill%3D%22currentColor%22%2F%3E%3C%2Fmarker%3E%0A%20%20%20%20%20%20%3C%2Fdefs%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%2216%22%20y%3D%2260%22%20fill%3D%22currentColor%22%20opacity%3D%220.55%22%20font-size%3D%2212%22%20font-weight%3D%22600%22%3ENA%C3%8FVE%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%2216%22%20y%3D%22212%22%20fill%3D%22%23e0813a%22%20font-size%3D%2212%22%20font-weight%3D%22600%22%3ECONTEXTUAL%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%2272%22%20y%3D%2248%22%20width%3D%22132%22%20height%3D%2248%22%20rx%3D%229%22%20fill%3D%22none%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.5%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22138%22%20y%3D%2268%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2213%22%20font-weight%3D%22600%22%3Echunk%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22138%22%20y%3D%2285%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.6%22%20font-size%3D%2210.5%22%20font-style%3D%22italic%22%3E%22%E2%80%A634h%20video%E2%80%A6%22%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M204%2072%20L548%2072%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ci)%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22374%22%20y%3D%2263%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.5%22%20font-size%3D%2210.5%22%3Ejust%20the%20chunk%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22550%22%20y%3D%2244%22%20width%3D%22150%22%20height%3D%2256%22%20rx%3D%2210%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.05%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.6%22%20stroke-dasharray%3D%226%204%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22625%22%20y%3D%2268%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2213%22%20font-weight%3D%22600%22%3EEmbedding%20model%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22625%22%20y%3D%2285%22%20text-anchor%3D%22middle%22%20fill%3D%22%236366f1%22%20font-size%3D%2210.5%22%3Eexternal%20%C2%B7%20unchanged%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M700%2072%20L740%2072%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.7%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ci)%22%2F%3E%0A%20%20%20%20%20%20%3Ccircle%20cx%3D%22760%22%20cy%3D%2272%22%20r%3D%224%22%20fill%3D%22%236366f1%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22774%22%20y%3D%2260%22%20fill%3D%22currentColor%22%20opacity%3D%220.55%22%20font-size%3D%2210%22%3Ewhich%20phone%3F%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22780%22%20y%3D%2290%22%20fill%3D%22currentColor%22%20opacity%3D%220.55%22%20font-size%3D%2210%22%3E%E2%80%94%20lost%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%2224%22%20y%3D%22178%22%20width%3D%2240%22%20height%3D%2252%22%20rx%3D%224%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.55%22%20stroke-width%3D%221.3%22%2F%3E%0A%20%20%20%20%20%20%3Cline%20x1%3D%2232%22%20y1%3D%22190%22%20x2%3D%2256%22%20y2%3D%22190%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.5%22%2F%3E%0A%20%20%20%20%20%20%3Cline%20x1%3D%2232%22%20y1%3D%22200%22%20x2%3D%2256%22%20y2%3D%22200%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.5%22%2F%3E%0A%20%20%20%20%20%20%3Cline%20x1%3D%2232%22%20y1%3D%22210%22%20x2%3D%2256%22%20y2%3D%22210%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.5%22%2F%3E%0A%20%20%20%20%20%20%3Cline%20x1%3D%2232%22%20y1%3D%22220%22%20x2%3D%2248%22%20y2%3D%22220%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.5%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%2244%22%20y%3D%22246%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.6%22%20font-size%3D%2210.5%22%3Ewhole%20doc%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%2282%22%20y%3D%22188%22%20width%3D%2272%22%20height%3D%2234%22%20rx%3D%227%22%20fill%3D%22none%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.4%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22118%22%20y%3D%22209%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2211.5%22%20font-weight%3D%22600%22%3Echunk%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M64%20202%20C%20150%20202%2C%20158%20205%2C%20214%20208%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.55%22%20stroke-width%3D%221.4%22%20fill%3D%22none%22%20marker-end%3D%22url(%23cn)%22%2F%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M154%20205%20L214%20207%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.5%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ci)%22%2F%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22216%22%20y%3D%22182%22%20width%3D%22104%22%20height%3D%2252%22%20rx%3D%2210%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.1%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.6%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22268%22%20y%3D%22205%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2213%22%20font-weight%3D%22700%22%3ELLM%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22268%22%20y%3D%22222%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.65%22%20font-size%3D%2210%22%3Ewrites%20context%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M320%20208%20L360%20208%22%20stroke%3D%22%23e0813a%22%20stroke-width%3D%221.8%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ca)%22%2F%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22362%22%20y%3D%22176%22%20width%3D%22150%22%20height%3D%2264%22%20rx%3D%2210%22%20fill%3D%22%23e0813a%22%20fill-opacity%3D%220.08%22%20stroke%3D%22%23e0813a%22%20stroke-width%3D%221.6%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22437%22%20y%3D%22197%22%20text-anchor%3D%22middle%22%20fill%3D%22%23e0813a%22%20font-size%3D%2211%22%20font-weight%3D%22600%22%3Econtext%20(50%E2%80%93100%20tok)%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cline%20x1%3D%22376%22%20y1%3D%22205%22%20x2%3D%22498%22%20y2%3D%22205%22%20stroke%3D%22%23e0813a%22%20stroke-opacity%3D%220.4%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22437%22%20y%3D%22224%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.8%22%20font-size%3D%2212%22%3E%E2%8A%95%20chunk%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M512%20208%20L548%20208%22%20stroke%3D%22%23e0813a%22%20stroke-width%3D%221.8%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ca)%22%2F%3E%0A%20%20%20%20%20%20%3Crect%20x%3D%22550%22%20y%3D%22180%22%20width%3D%22150%22%20height%3D%2256%22%20rx%3D%2210%22%20fill%3D%22%236366f1%22%20fill-opacity%3D%220.05%22%20stroke%3D%22%236366f1%22%20stroke-width%3D%221.6%22%20stroke-dasharray%3D%226%204%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22625%22%20y%3D%22204%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20font-size%3D%2213%22%20font-weight%3D%22600%22%3EEmbedding%20model%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22625%22%20y%3D%22221%22%20text-anchor%3D%22middle%22%20fill%3D%22%236366f1%22%20font-size%3D%2210.5%22%3Eexternal%20%C2%B7%20unchanged%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M700%20208%20L740%20208%22%20stroke%3D%22%23e0813a%22%20stroke-width%3D%221.8%22%20fill%3D%22none%22%20marker-end%3D%22url(%23ca)%22%2F%3E%0A%20%20%20%20%20%20%3Ccircle%20cx%3D%22760%22%20cy%3D%22208%22%20r%3D%224%22%20fill%3D%22%23e0813a%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22774%22%20y%3D%22205%22%20fill%3D%22%23e0813a%22%20font-size%3D%2210%22%3Ecarries%20the%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22777%22%20y%3D%22218%22%20fill%3D%22%23e0813a%22%20font-size%3D%2210%22%3Econtext%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Cpath%20d%3D%22M712%20100%20C%20728%20100%2C%20728%20180%2C%20712%20180%22%20stroke%3D%22currentColor%22%20stroke-opacity%3D%220.4%22%20stroke-width%3D%221.2%22%20fill%3D%22none%22%2F%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22735%22%20y%3D%22144%22%20fill%3D%22currentColor%22%20opacity%3D%220.6%22%20font-size%3D%2210.5%22%3Esame%20model%3C%2Ftext%3E%0A%20%20%20%20%20%20%3Ctext%20x%3D%22410%22%20y%3D%22284%22%20text-anchor%3D%22middle%22%20fill%3D%22currentColor%22%20opacity%3D%220.75%22%20font-size%3D%2212%22%3EWe%20didn't%20touch%20the%20embedding%20model%20%E2%80%94%20we%20changed%20the%20%3Ctspan%20fill%3D%22%23e0813a%22%20font-weight%3D%22600%22%3Einput%3C%2Ftspan%3E.%3C%2Ftext%3E%0A%20%20%20%20%3C%2Fsvg%3E%22%22%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20c4_model%20%3D%20mo.ui.text(value%3D%22gemma4%3Ae4b-mlx%22%2C%20label%3D%22Context-writing%20model%20(Ollama)%22%2C%20full_width%3DTrue)%0A%20%20%20%20c4_model%0A%20%20%20%20return%20(c4_model%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(c4_chunks%2C%20embed)%3A%0A%20%20%20%20c4_naiveE%20%3D%20embed(c4_chunks)%0A%20%20%20%20return%20(c4_naiveE%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(c4_chunks%2C%20c4_docs%2C%20c4_model%2C%20c4_origin%2C%20mo%2C%20ollama)%3A%0A%20%20%20%20import%20hashlib%20as%20_hashlib%0A%20%20%20%20import%20json%20as%20_json%0A%20%20%20%20import%20pathlib%20as%20_pathlib%0A%0A%20%20%20%20_cache_path%20%3D%20_pathlib.Path(mo.notebook_dir())%20%2F%20%22__marimo__%22%20%2F%20%22ctx_cache.json%22%0A%20%20%20%20_cache_path.parent.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20_cache%20%3D%20_json.loads(_cache_path.read_text())%20if%20_cache_path.exists()%20else%20%7B%7D%0A%0A%20%20%20%20def%20_key(model%2C%20doc%2C%20chunk)%3A%0A%20%20%20%20%20%20%20%20return%20_hashlib.sha1(f%22%7Bmodel%7D%5Cx1f%7Bdoc%7D%5Cx1f%7Bchunk%7D%22.encode()).hexdigest()%0A%0A%20%20%20%20def%20_blurb(model%2C%20doc%2C%20chunk)%3A%0A%20%20%20%20%20%20%20%20_k%20%3D%20_key(model%2C%20doc%2C%20chunk)%0A%20%20%20%20%20%20%20%20if%20_k%20in%20_cache%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20_cache%5B_k%5D%0A%20%20%20%20%20%20%20%20_p%20%3D%20(f%22%3Cdocument%3E%5Cn%7Bdoc%7D%5Cn%3C%2Fdocument%3E%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Here%20is%20the%20chunk%20we%20want%20to%20situate%20within%20the%20whole%20document%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cchunk%3E%5Cn%7Bchunk%7D%5Cn%3C%2Fchunk%3E%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Please%20give%20a%20short%20succinct%20context%20to%20situate%20this%20chunk%20within%20the%20overall%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22document%20for%20the%20purposes%20of%20improving%20search%20retrieval%20of%20the%20chunk.%20Answer%20only%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22with%20the%20succinct%20context%20and%20nothing%20else.%22)%0A%20%20%20%20%20%20%20%20_out%20%3D%20ollama.generate(model%3Dmodel%2C%20prompt%3D_p%2C%20options%3D%7B%22temperature%22%3A%200%7D)%5B%22response%22%5D.strip()%0A%20%20%20%20%20%20%20%20_cache%5B_k%5D%20%3D%20_out%0A%20%20%20%20%20%20%20%20return%20_out%0A%0A%20%20%20%20_model%20%3D%20c4_model.value.strip()%20or%20%22gemma4%3Ae4b-mlx%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20c4_blurbs%20%3D%20%5B_blurb(_model%2C%20c4_docs%5B_o%5D%2C%20_ch)%20for%20_ch%2C%20_o%20in%20zip(c4_chunks%2C%20c4_origin)%5D%0A%20%20%20%20%20%20%20%20_cache_path.write_text(_json.dumps(_cache))%0A%20%20%20%20%20%20%20%20c4_ctx_chunks%20%3D%20%5Bf%22%7B_b%7D%5Cn%5Cn%7B_ch%7D%22%20for%20_b%2C%20_ch%20in%20zip(c4_blurbs%2C%20c4_chunks)%5D%0A%20%20%20%20%20%20%20%20c4_ctx_error%20%3D%20None%0A%20%20%20%20except%20Exception%20as%20_e%3A%0A%20%20%20%20%20%20%20%20c4_blurbs%20%3D%20None%0A%20%20%20%20%20%20%20%20c4_ctx_chunks%20%3D%20None%0A%20%20%20%20%20%20%20%20c4_ctx_error%20%3D%20str(_e)%0A%20%20%20%20return%20c4_blurbs%2C%20c4_ctx_chunks%2C%20c4_ctx_error%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(c4_ctx_chunks%2C%20embed)%3A%0A%20%20%20%20c4_ctxE%20%3D%20embed(c4_ctx_chunks)%20if%20c4_ctx_chunks%20else%20None%0A%20%20%20%20return%20(c4_ctxE%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Same%20query%2C%20two%20indexes%20%E2%80%94%20watch%20it%20move%0A%20%20%20%20One%20search%20box.%20Left%20ranks%20the%20**na%C3%AFve**%20chunks%2C%20right%20ranks%20the%20**contextualized**%20ones%20%E2%80%94%20the%0A%20%20%20%20*same*%20chunks%2C%20embedded%20differently.%20Colour%20%3D%20which%20phone%20the%20chunk%20came%20from.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20c4_query%20%3D%20mo.ui.text(value%3D%22Does%20the%20Nimbus%20N1%20have%20a%20telephoto%20camera%3F%22%2C%20label%3D%22Search%20query%22%2C%20full_width%3DTrue)%0A%20%20%20%20c4_topk%20%3D%20mo.ui.slider(1%2C%205%2C%20value%3D3%2C%20label%3D%22Top-K%22)%0A%20%20%20%20mo.vstack(%5Bc4_query%2C%20c4_topk%5D)%0A%20%20%20%20return%20c4_query%2C%20c4_topk%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20c4_blurbs%2C%0A%20%20%20%20c4_chunks%2C%0A%20%20%20%20c4_ctxE%2C%0A%20%20%20%20c4_ctx_error%2C%0A%20%20%20%20c4_naiveE%2C%0A%20%20%20%20c4_origin%2C%0A%20%20%20%20c4_query%2C%0A%20%20%20%20c4_topk%2C%0A%20%20%20%20embed%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A)%3A%0A%20%20%20%20_q%20%3D%20c4_query.value.strip()%20or%20%22Does%20the%20Nimbus%20N1%20have%20a%20telephoto%20camera%3F%22%0A%20%20%20%20_qv%20%3D%20embed(%5B_q%5D)%5B0%5D%0A%0A%20%20%20%20_names%20%3D%20sorted(set(c4_origin))%0A%20%20%20%20_palette%20%3D%20%5B%22%236366f1%22%2C%20%22%23e0813a%22%2C%20%22%2314b8a6%22%2C%20%22%23a855f7%22%2C%20%22%23ec4899%22%5D%0A%20%20%20%20_color%20%3D%20%7B_n%3A%20_palette%5B_i%20%25%20len(_palette)%5D%20for%20_i%2C%20_n%20in%20enumerate(_names)%7D%0A%0A%20%20%20%20def%20_pretty(_stem)%3A%0A%20%20%20%20%20%20%20%20return%20_stem.replace(%22-%22%2C%20%22%20%22).title()%0A%0A%20%20%20%20def%20_clean(_text)%3A%0A%20%20%20%20%20%20%20%20return%20%22%20%22.join(_text.replace(%22%23%22%2C%20%22%20%22).split())%0A%0A%20%20%20%20def%20_clamp2(_inner%2C%20_extra)%3A%0A%20%20%20%20%20%20%20%20return%20(f'%3Cdiv%20style%3D%22%7B_extra%7Ddisplay%3A-webkit-box%3B-webkit-line-clamp%3A2%3B'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f'-webkit-box-orient%3Avertical%3Boverflow%3Ahidden%22%3E%7B_inner%7D%3C%2Fdiv%3E')%0A%0A%20%20%20%20def%20_rows(_E%2C%20_blurbs)%3A%0A%20%20%20%20%20%20%20%20_sims%20%3D%20_E%20%40%20_qv%0A%20%20%20%20%20%20%20%20_idx%20%3D%20list(np.argsort(-_sims)%5B%3A%20c4_topk.value%5D)%0A%20%20%20%20%20%20%20%20_html%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20_r%2C%20_i%20in%20enumerate(_idx)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_n%20%3D%20c4_origin%5B_i%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_ctx%20%3D%20%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20_blurbs%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_ctx%20%3D%20_clamp2(%22%2B%20context%3A%20%22%20%2B%20_clean(_blurbs%5B_i%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22color%3A%23e0813a%3Bfont-size%3A.8em%3Bmargin%3A3px%200%3B%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20_html.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f'%3Cdiv%20style%3D%22margin%3A6px%200%3Bpadding%3A8px%2010px%3Bborder%3A1px%20solid%20%7B_color%5B_n%5D%7D44%3B'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f'border-left%3A3px%20solid%20%7B_color%5B_n%5D%7D%3Bborder-radius%3A6px%22%3E'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f'%3Cspan%20style%3D%22opacity%3A.5%22%3E%7B_r%20%2B%201%7D.%3C%2Fspan%3E%20'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f'%3Cb%3E%7B_sims%5B_i%5D%3A.2f%7D%3C%2Fb%3E%20%26nbsp%3B'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f'%3Cspan%20style%3D%22color%3A%7B_color%5B_n%5D%7D%3Bfont-weight%3A600%22%3E%7B_pretty(_n)%7D%3C%2Fspan%3E'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f'%7B_ctx%7D'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20_clamp2(_clean(c4_chunks%5B_i%5D)%2C%20%22opacity%3A.75%3Bfont-size%3A.9em%3Bmargin-top%3A2px%3B%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20%22%3C%2Fdiv%3E%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20mo.Html(%22%22.join(_html))%0A%0A%20%20%20%20_left%20%3D%20mo.vstack(%5Bmo.md(%22%23%23%23%20Na%C3%AFve%20chunks%22)%2C%20_rows(c4_naiveE%2C%20None)%5D)%0A%20%20%20%20if%20c4_ctx_error%3A%0A%20%20%20%20%20%20%20%20_right%20%3D%20mo.vstack(%5Bmo.md(%22%23%23%23%20Contextualized%20chunks%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mo.callout(mo.md(%22Contextual%20index%20unavailable%20%E2%80%94%20see%20the%20model%20box%20above.%22)%2C%20kind%3D%22warn%22)%5D)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_right%20%3D%20mo.vstack(%5Bmo.md(%22%23%23%23%20Contextualized%20chunks%22)%2C%20_rows(c4_ctxE%2C%20c4_blurbs)%5D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.hstack(%5B_left%2C%20_right%5D%2C%20widths%3D%22equal%22%2C%20gap%3D1.5)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22*Score%20%3D%20cosine%20similarity%20(1.0%20%3D%20identical%20direction).%20The%20right%20column%20shows%20the%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22LLM-added%20context%20in%20amber%20%E2%80%94%20the%20blurb%20**and**%20the%20chunk%20together%20are%20what%20we%20embed.*%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Semantic%20search%20alone%20isn't%20the%20whole%20answer%0A%20%20%20%20Contextual%20**Embeddings**%20are%20only%20step%20one.%20Real%20systems%20stack%20more%3A%0A%0A%20%20%20%20-%20**Contextual%20BM25**%20%E2%80%94%20keyword%20%2F%20exact-match%20retrieval%20(catches%20%60error%20code%20TS-999%60%20that%0A%20%20%20%20%20%20embeddings%20gloss%20over)%2C%20using%20the%20*same*%20prepended%20context.%0A%20%20%20%20-%20**Reranking**%20%E2%80%94%20vector%20search%20is%20fast%20but%20rough%2C%20so%20it%20grabs%20a%20wide%20net%20of%20candidates%20(say%0A%20%20%20%20%20%20the%20top%20150).%20A%20slower%2C%20more%20accurate%20**cross-encoder**%20then%20re-reads%20each%20candidate%0A%20%20%20%20%20%20*together%20with%20the%20query*%20%E2%80%94%20not%20as%20separate%20vectors%20%E2%80%94%20and%20keeps%20only%20the%20best%20handful%20to%0A%20%20%20%20%20%20hand%20to%20the%20LLM.%0A%0A%20%20%20%20Anthropic's%20measured%20drop%20in%20retrieval%20failures%3A%20**%E2%88%9235%25**%20(contextual%20embeddings)%20%E2%86%92%0A%20%20%20%20**%E2%88%9249%25**%20(%2B%20contextual%20BM25)%20%E2%86%92%20**%E2%88%9267%25**%20(%2B%20reranking).%20*We%20fixed%20the%20**input**%2C%20not%20the%20model.*%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Final%20words%20%E2%80%94%20what%20to%20explore%20next%0A%20%20%20%20Everything%20so%20far%20%E2%80%94%20semantic%20search%2C%20then%20Contextual%20Retrieval%20%E2%80%94%20is%20**one%20slice**.%20Two%0A%20%20%20%20directions%20worth%20clicking%20into%3A%0A%0A%20%20%20%20%23%23%23%20Retrieval%20beyond%20chunks%20%E2%86%92%20**graph%20RAG**%0A%20%20%20%20**%5BLightRAG%5D(https%3A%2F%2Farxiv.org%2Fabs%2F2410.05779)**%20%C2%B7%20%5Bcode%5D(https%3A%2F%2Fgithub.com%2FHKUDS%2FLightRAG)%20%E2%80%94%0A%20%20%20%20an%20LLM%20builds%20a%20**knowledge%20graph**%20(entities%20%2B%20how%20they%20relate)%20over%20the%20corpus%2C%20so%20retrieval%0A%20%20%20%20reasons%20over%20how%20things%20*connect*%2C%20not%20just%20which%20chunks%20look%20alike.%20Two%20levels%3A%20precise%20entity%0A%20%20%20%20lookups%20**and**%20broad%20themes.%20*(Its%20own%20line%20of%20work%20%E2%80%94%20not%20Microsoft's%20GraphRAG.)*%0A%0A%20%20%20%20%23%23%23%20Retrieval%20beyond%20text%20%E2%86%92%20**other%20modalities**%0A%20%20%20%20**%5BVideoRAG%5D(https%3A%2F%2Farxiv.org%2Fabs%2F2501.05874)**%20%C2%B7%20%5Bcode%5D(https%3A%2F%2Fgithub.com%2Fstarsuzi%2FVideoRAG)%20%E2%80%94%0A%20%20%20%20RAG%20over%20a%20**video**%20corpus%3A%20retrieve%20the%20relevant%20clips%20and%20feed%20the%20model%20both%20the%20**visual**%0A%20%20%20%20frames%20*and*%20the%20text.%20Every%20modality%20needs%20its%20**own%20ingestion**.%20Same%20problem%2C%20new%20medium.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20%E2%80%A6and%20it%20depends%0A%20%20%20%20We%20only%20scratched%20the%20surface.%20There's%20a%20whole%20menu%20we%20skipped%20%E2%80%94%20hybrid%20(keyword%20%2B%20vector)%0A%20%20%20%20search%2C%20reranking%2C%20agentic%20%2F%20multi-step%20retrieval%2C%20multimodal%20embeddings%2C%20graph%20RAG.%0A%0A%20%20%20%20Which%20of%20these%20you%20actually%20need%20is%20a%20**trade-off**%3A%20use%20case%2C%20requirements%2C%20scale%2C%20cost%2C%0A%20%20%20%20latency%2C%20freshness%2C%20data%20types.%20**Start%20simple%3B%20add%20only%20what%20your%20evals%20tell%20you%20to.**%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
740dcf54f8f235f046ba618982d71a42