[{"data":1,"prerenderedAt":591},["ShallowReactive",2],{"\u002Farticles\u002Frag-media-as-text":3},{"id":4,"title":5,"body":6,"date":576,"description":577,"extension":578,"head":579,"image":580,"meta":581,"navigation":196,"ogImage":579,"path":582,"rating":146,"readingTime":583,"robots":579,"schemaOrg":579,"seo":584,"sitemap":585,"stem":586,"tags":587,"__hash__":590},"articles_en\u002Farticles\u002Frag-media-as-text.md","Teaching an AI Support Agent to Send Photos: Index Media as Text, Skip Multimodal Embeddings",{"type":7,"value":8,"toc":568},"minimark",[9,13,16,19,26,31,34,37,40,43,99,102,107,111,114,156,159,165,171,177,180,255,258,261,265,268,271,274,362,365,372,421,424,427,433,437,442,445,448,453,456,459,462,466,469,472,475,505,508,512,518,524,530,533,536,539,561,564],[10,11,12],"p",{},"A customer types into the chat window: \"What does your venue look like?\"",[10,14,15],{},"The AI support agent answers with a paragraph. The space is bright and roomy, there is a separate lounge, and there is a lawn outside. Nothing wrong with it, but it is plainly not what the customer wanted. A human agent would have sent a photo.",[10,17,18],{},"The merchant has photos, and videos too. The AI simply cannot reach them. The knowledge base only accepts documents, so everything the AI can retrieve is text.",[10,20,21,22],{},"This project fills that gap: ",[23,24,25],"strong",{},"merchants upload images and videos to the knowledge base, the AI decides when one is worth sending, and then it gets sent.",[27,28,30],"h2",{"id":29},"two-routes-multimodal-embeddings-or-translate-to-text-first","Two routes: multimodal embeddings, or translate to text first",[10,32,33],{},"There are two ways to make an image retrievable.",[10,35,36],{},"The first is multimodal embeddings. Use an embedding model that understands both images and text, place the image and the question in the same vector space, and search for images with the question directly. This is the textbook answer.",[10,38,39],{},"The second is to translate first and retrieve second. A vision model looks at the file and writes a text description. That description is embedded like any other text. Retrieval finds the description, and what gets sent is the file behind it.",[10,41,42],{},"I chose the second, for three reasons.",[44,45,46,62],"table",{},[47,48,49],"thead",{},[50,51,52,56,59],"tr",{},[53,54,55],"th",{},"Consideration",[53,57,58],{},"Multimodal embeddings",[53,60,61],{},"Translate to text first",[63,64,65,77,88],"tbody",{},[50,66,67,71,74],{},[68,69,70],"td",{},"Changes to the existing system",[68,72,73],{},"New embedding model, and every existing document must be re-embedded",[68,75,76],{},"Vector store schema untouched, retrieval logic reused as is",[50,78,79,82,85],{},[68,80,81],{},"What you do when retrieval misses",[68,83,84],{},"Hard to explain why it missed; you can only tune the model",[68,86,87],{},"The description is readable text; edit a sentence and it is fixed",[50,89,90,93,96],{},[68,91,92],{},"Text inside the image",[68,94,95],{},"Price lists and timetables contain numbers that a vector represents poorly",[68,97,98],{},"The vision model transcribes the text, so it can be matched exactly",[10,100,101],{},"The second row decided it. When a merchant uploads a photo and it cannot be found, the multimodal route leaves me saying \"that is how the model sees it\". With text, the merchant opens the description, notices the lawn is not mentioned, and adds the word.",[10,103,104],{},[23,105,106],{},"Replace an unexplainable step with an intermediate product people can read and edit, and the system becomes something you can operate.",[27,108,110],{"id":109},"indexing-one-file-one-chunk","Indexing: one file, one chunk",[10,112,113],{},"The flow is short:",[115,116,121],"pre",{"className":117,"code":118,"language":119,"meta":120,"style":120},"language-mermaid shiki shiki-themes github-light github-dark github-dark","flowchart LR\n    A[\"Merchant picks an image \u002F video\"] --> B[\"Upload to object storage\u003Cbr>get a file link\"]\n    B --> C[\"Vision model reads the file by link\u003Cbr>writes a draft description\"]\n    C --> D[\"Merchant confirms or edits it\"]\n    D --> E[\"Embed the description\u003Cbr>store it with the file link\"]\n","mermaid","",[122,123,124,132,138,144,150],"code",{"__ignoreMap":120},[125,126,129],"span",{"class":127,"line":128},"line",1,[125,130,131],{},"flowchart LR\n",[125,133,135],{"class":127,"line":134},2,[125,136,137],{},"    A[\"Merchant picks an image \u002F video\"] --> B[\"Upload to object storage\u003Cbr>get a file link\"]\n",[125,139,141],{"class":127,"line":140},3,[125,142,143],{},"    B --> C[\"Vision model reads the file by link\u003Cbr>writes a draft description\"]\n",[125,145,147],{"class":127,"line":146},4,[125,148,149],{},"    C --> D[\"Merchant confirms or edits it\"]\n",[125,151,153],{"class":127,"line":152},5,[125,154,155],{},"    D --> E[\"Embed the description\u003Cbr>store it with the file link\"]\n",[10,157,158],{},"A few details are worth pointing out.",[10,160,161,164],{},[23,162,163],{},"The file never passes through our service."," To generate a description, the file link goes straight to the vision model, which fetches it on its own. The server does no downloading and no transcoding, and videos need no frame extraction.",[10,166,167,170],{},[23,168,169],{},"The merchant sees the description first."," Generating a description and indexing are two separate endpoints. The first only returns a draft. After the merchant confirms or edits it, the draft is submitted with the indexing request. That extra step means a person has read every description.",[10,172,173,176],{},[23,174,175],{},"One file is always one chunk."," Documents get split into chunks; media does not. The full description is the only retrieval text, and the media id maps to exactly one chunk. This convention simplifies a lot downstream: editing a description means updating one record and recomputing one vector, and a media id is enough to locate a file when sending.",[10,178,179],{},"Indexing gains a single branch:",[115,181,185],{"className":182,"code":183,"language":184,"meta":120,"style":120},"language-py shiki shiki-themes github-light github-dark github-dark","MEDIA_KINDS = {\"jpg\": \"image\", \"jpeg\": \"image\", \"png\": \"image\", \"mp4\": \"video\"}\n\ndef insert(req):\n    kind = MEDIA_KINDS.get(req.file_type)\n    if kind:\n        # Media: no file parsing, the description is the retrieval text\n        text = req.description.strip() or describe_media(req.url, kind)\n        chunks = [text]\n    else:\n        # Documents: the existing parse + split path\n        chunks = split(parse(req.url))\n    save(chunks, embed(chunks), url=req.url, file_type=req.file_type)\n","py",[122,186,187,192,198,203,208,213,219,225,231,237,243,249],{"__ignoreMap":120},[125,188,189],{"class":127,"line":128},[125,190,191],{},"MEDIA_KINDS = {\"jpg\": \"image\", \"jpeg\": \"image\", \"png\": \"image\", \"mp4\": \"video\"}\n",[125,193,194],{"class":127,"line":134},[125,195,197],{"emptyLinePlaceholder":196},true,"\n",[125,199,200],{"class":127,"line":140},[125,201,202],{},"def insert(req):\n",[125,204,205],{"class":127,"line":146},[125,206,207],{},"    kind = MEDIA_KINDS.get(req.file_type)\n",[125,209,210],{"class":127,"line":152},[125,211,212],{},"    if kind:\n",[125,214,216],{"class":127,"line":215},6,[125,217,218],{},"        # Media: no file parsing, the description is the retrieval text\n",[125,220,222],{"class":127,"line":221},7,[125,223,224],{},"        text = req.description.strip() or describe_media(req.url, kind)\n",[125,226,228],{"class":127,"line":227},8,[125,229,230],{},"        chunks = [text]\n",[125,232,234],{"class":127,"line":233},9,[125,235,236],{},"    else:\n",[125,238,240],{"class":127,"line":239},10,[125,241,242],{},"        # Documents: the existing parse + split path\n",[125,244,246],{"class":127,"line":245},11,[125,247,248],{},"        chunks = split(parse(req.url))\n",[125,250,252],{"class":127,"line":251},12,[125,253,254],{},"    save(chunks, embed(chunks), url=req.url, file_type=req.file_type)\n",[10,256,257],{},"The instructions for the vision model differ by type. For an image: say what it shows in a sentence or two, and if it contains text (a price list, a timetable, a poster), transcribe all of it faithfully. For a video: describe it in two to four sentences and include any on-screen text that carries information.",[10,259,260],{},"On cost, the vision model is called once at upload. A single call costs well under a cent, and an image takes a few seconds. It is never called during a conversation.",[27,262,264],{"id":263},"choosing-the-model-says-which-one-and-never-touches-the-link","Choosing: the model says which one, and never touches the link",[10,266,267],{},"Indexing makes media findable. The next question is how the AI sends what it found.",[10,269,270],{},"The obvious approach is to put the file link in the retrieval result and let the model write it into the reply. I did not do that. Once a link is in the model's context, two things can go wrong. The model may paste a long raw link to the customer, or it may assemble a link from memory that does not exist.",[10,272,273],{},"The actual design separates choosing from sending:",[115,275,277],{"className":117,"code":276,"language":119,"meta":120,"style":120},"sequenceDiagram\n    autonumber\n    participant V as Customer\n    participant M as Main agent\n    participant K as Knowledge retrieval\n    participant C as Code (end of turn)\n    participant I as Messaging channel\n\n    V->>M: Do you have photos of the venue?\n    M->>K: Retrieve \"venue photo\"\n    K-->>M: Text content + media list (id \u002F type \u002F short description)\n    Note over K,C: Full media records (with links)\u003Cbr>are kept in this turn's media pool\n    M-->>C: Reply text + [media:812]\n    C->>C: Strip the tag, check 812 is in the pool\n    C->>I: Send the photo by its pooled link, then the text\n    I-->>V: Photo + \"Here is our venue...\"\n",[122,278,279,284,289,294,299,304,309,314,318,323,328,333,338,344,350,356],{"__ignoreMap":120},[125,280,281],{"class":127,"line":128},[125,282,283],{},"sequenceDiagram\n",[125,285,286],{"class":127,"line":134},[125,287,288],{},"    autonumber\n",[125,290,291],{"class":127,"line":140},[125,292,293],{},"    participant V as Customer\n",[125,295,296],{"class":127,"line":146},[125,297,298],{},"    participant M as Main agent\n",[125,300,301],{"class":127,"line":152},[125,302,303],{},"    participant K as Knowledge retrieval\n",[125,305,306],{"class":127,"line":215},[125,307,308],{},"    participant C as Code (end of turn)\n",[125,310,311],{"class":127,"line":221},[125,312,313],{},"    participant I as Messaging channel\n",[125,315,316],{"class":127,"line":227},[125,317,197],{"emptyLinePlaceholder":196},[125,319,320],{"class":127,"line":233},[125,321,322],{},"    V->>M: Do you have photos of the venue?\n",[125,324,325],{"class":127,"line":239},[125,326,327],{},"    M->>K: Retrieve \"venue photo\"\n",[125,329,330],{"class":127,"line":245},[125,331,332],{},"    K-->>M: Text content + media list (id \u002F type \u002F short description)\n",[125,334,335],{"class":127,"line":251},[125,336,337],{},"    Note over K,C: Full media records (with links)\u003Cbr>are kept in this turn's media pool\n",[125,339,341],{"class":127,"line":340},13,[125,342,343],{},"    M-->>C: Reply text + [media:812]\n",[125,345,347],{"class":127,"line":346},14,[125,348,349],{},"    C->>C: Strip the tag, check 812 is in the pool\n",[125,351,353],{"class":127,"line":352},15,[125,354,355],{},"    C->>I: Send the photo by its pooled link, then the text\n",[125,357,359],{"class":127,"line":358},16,[125,360,361],{},"    I-->>V: Photo + \"Here is our venue...\"\n",[10,363,364],{},"The media information returned by the retrieval tool is split in two. The copy the model sees has only an id, a type and a short excerpt of the description. The full copy, with the link, stays in a per-turn media pool that the model cannot see.",[10,366,367,368,371],{},"To send something, the model writes a tag line in its reply, such as ",[122,369,370],{},"[media:812]",". At the end of the turn, code does three things:",[115,373,375],{"className":182,"code":374,"language":184,"meta":120,"style":120},"_TAG = re.compile(r\"\\[\\s*media\\s*[:=]\\s*([^\\]]*)\\]\", re.IGNORECASE)\n_MAX_PER_TURN = 2\n\ndef attach_media(answer: str, pool: dict) -> tuple[str, list]:\n    ids = [int(x) for m in _TAG.finditer(answer) for x in re.findall(r\"\\d+\", m.group(1))]\n    answer = _TAG.sub(\"\", answer).strip()          # 1. strip the tag wherever it appears\n    picked = [pool[i] for i in dict.fromkeys(ids)  # 2. the id must be in this turn's pool\n              if i in pool][:_MAX_PER_TURN]        # 3. at most 2 per turn\n    return answer, picked\n",[122,376,377,382,387,391,396,401,406,411,416],{"__ignoreMap":120},[125,378,379],{"class":127,"line":128},[125,380,381],{},"_TAG = re.compile(r\"\\[\\s*media\\s*[:=]\\s*([^\\]]*)\\]\", re.IGNORECASE)\n",[125,383,384],{"class":127,"line":134},[125,385,386],{},"_MAX_PER_TURN = 2\n",[125,388,389],{"class":127,"line":140},[125,390,197],{"emptyLinePlaceholder":196},[125,392,393],{"class":127,"line":146},[125,394,395],{},"def attach_media(answer: str, pool: dict) -> tuple[str, list]:\n",[125,397,398],{"class":127,"line":152},[125,399,400],{},"    ids = [int(x) for m in _TAG.finditer(answer) for x in re.findall(r\"\\d+\", m.group(1))]\n",[125,402,403],{"class":127,"line":215},[125,404,405],{},"    answer = _TAG.sub(\"\", answer).strip()          # 1. strip the tag wherever it appears\n",[125,407,408],{"class":127,"line":221},[125,409,410],{},"    picked = [pool[i] for i in dict.fromkeys(ids)  # 2. the id must be in this turn's pool\n",[125,412,413],{"class":127,"line":227},[125,414,415],{},"              if i in pool][:_MAX_PER_TURN]        # 3. at most 2 per turn\n",[125,417,418],{"class":127,"line":233},[125,419,420],{},"    return answer, picked\n",[10,422,423],{},"If the model invents an id, it is not in the pool, so it is dropped and logged. If the model puts the tag mid-sentence, the regex still removes it and the customer never sees it.",[10,425,426],{},"The idea is to treat model output as untrusted input. The model has the right to decide which file to send, but all it hands over is a number. What is actually sent, and whether it can be sent, is decided by code.",[428,429,430],"tip",{},[10,431,432],{},"The prompt has to spell out when to attach: only when the media directly shows what the customer asked about. For something loosely related, the agent can offer (\"want to see it?\") and wait for a yes. Also state it outright: never say \"here's a photo\" without the tag.",[27,434,436],{"id":435},"two-details-that-are-easy-to-miss","Two details that are easy to miss",[10,438,439],{},[23,440,441],{},"Media that was sent has to be written back into the conversation history.",[10,443,444],{},"History stores text. The AI sends a photo this turn, and on the next turn all it sees in history is that it said \"here is our venue\". It does not see the photo. If the customer follows up with \"where's the photo?\", the model assumes it failed to send, and either apologises or sends it again.",[10,446,447],{},"The fix is to attach an internal note to the reply at the moment the media goes out, recording which media the customer was shown and what each one is. The note is not shown to the customer, but it travels with the history into the next turn.",[10,449,450],{},[23,451,452],{},"Media can only be chosen from this turn's retrieval results.",[10,454,455],{},"The pool is rebuilt every turn. Media retrieved last turn is no longer in the pool.",[10,457,458],{},"This matters for a common two-turn exchange. The customer asks \"do you have a photo of the lawn?\", the AI says \"yes, want to see it?\", and the customer says \"sure\". On the second turn the AI must retrieve again to get the media. It cannot write last turn's id from memory. This rule also goes into the prompt. I ran this exact two-turn exchange repeatedly to confirm the model does not change its answer on the second turn and claim there is no photo.",[10,460,461],{},"Why not keep the pool across turns? Because a merchant may delete or disable a file between two turns. Retrieving every turn guarantees that what comes back is valid right now.",[27,463,465],{"id":464},"sending-video-is-slower-than-text","Sending: video is slower than text",[10,467,468],{},"The reply finally goes out on whatever chat channel the customer is using. We support several IM channels, plus the web. Web is the easy one: the media information goes in the response and the frontend renders it. Each IM channel needs its ordering handled.",[10,470,471],{},"Video is where it goes wrong. When an IM platform receives a video message, it fetches the file and validates it before delivering it to the customer, which is far slower than a text message. If the code sends the video and then immediately sends the text, the customer sees them reversed: first \"here is our training video\", then, seconds later, the video.",[10,473,474],{},"There are two ways to handle it, depending on what the channel supports:",[44,476,477,487],{},[47,478,479],{},[50,480,481,484],{},[53,482,483],{},"Situation",[53,485,486],{},"What we do",[63,488,489,497],{},[50,490,491,494],{},[68,492,493],{},"The channel can combine media and text, there is one file, and the text is short",[68,495,496],{},"Send the text as the caption, as a single message",[50,498,499,502],{},[68,500,501],{},"Several files, or the text exceeds the caption limit",[68,503,504],{},"Send the media first, wait for the platform's delivery receipt on the video, then send the text",[10,506,507],{},"Format and size limits also differ between channels. Rather than handle each one at send time, I constrain uploads to the strictest channel: images must be jpg or png, videos must be mp4, and size is capped at the smallest limit. Anything that passes upload can be sent by link on every channel, so the sending code needs no compatibility checks.",[27,509,511],{"id":510},"where-this-approach-stops-working","Where this approach stops working",[10,513,514,517],{},[23,515,516],{},"Quality is capped by the description."," Anything the description leaves out cannot be found. If the photo shows a lawn and the description does not say so, the photo will not appear when a customer asks about the lawn. The upside is that the merchant can fix this without us.",[10,519,520,523],{},[23,521,522],{},"Whether to send is governed by the prompt, not a hard rule."," Sometimes it will not send when it should, or will send something only loosely relevant. The cap of two per turn is insurance against the worst case.",[10,525,526,529],{},[23,527,528],{},"It searches what the image says, not what the image looks like."," If a customer sends a picture and asks \"do you have something similar?\", this design cannot answer. Search by image and visual similarity is where multimodal embeddings belong.",[10,531,532],{},"So the test is simple. If you have tens to hundreds of files, maintained by people, and customers ask in text, translating to text is enough. If you have thousands of product images that nobody can describe one by one, and customers ask with pictures, look at multimodal embeddings.",[534,535],"hr",{},[10,537,538],{},"Looking back, this feature uses no new technology and not much code. It works because of three choices:",[540,541,542,549,555],"ol",{},[543,544,545,548],"li",{},[23,546,547],{},"Reuse the existing pipeline before adding a new one."," Media disguised as a piece of text gets retrieval, permissions, enable\u002Fdisable and deletion for free.",[543,550,551,554],{},[23,552,553],{},"Make the intermediate product readable and editable."," The description is there for the model to retrieve, and for the merchant to correct.",[543,556,557,560],{},[23,558,559],{},"The model chooses; code executes and validates."," The model hands over a number. The link, the cap and validity are all enforced by code.",[10,562,563],{},"The third one keeps coming up elsewhere. Wherever a model's output turns into a real action, it is worth asking which part of that action does not need to be left to the model.",[565,566,567],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":120,"searchDepth":134,"depth":134,"links":569},[570,571,572,573,574,575],{"id":29,"depth":134,"text":30},{"id":109,"depth":134,"text":110},{"id":263,"depth":134,"text":264},{"id":435,"depth":134,"text":436},{"id":464,"depth":134,"text":465},{"id":510,"depth":134,"text":511},"2026-11-20","Merchants wanted their AI support agent to send photos and videos during a chat. Instead of multimodal embeddings, I had a vision model write a text description of each file and reused the existing text retrieval. This post walks through the approach, and the design that lets the model choose while code does the sending.","md",null,"\u002Farticles\u002Frag-media-as-text.svg",{},"\u002Farticles\u002Frag-media-as-text","9",{"title":5,"description":577},{"loc":582},"articles\u002Frag-media-as-text",[588,589],"LLM Apps","RAG","YH7YcLTHWwETWrCI2vHHxVmO2Aw7DG4t3tuyu-SAZKM",1795761651298]