From 6c0a39577ed8d5bcddad4771704b1f359e9272ac Mon Sep 17 00:00:00 2001 From: Jhin Lee Date: Fri, 9 Oct 2026 07:40:47 -0400 Subject: [PATCH 1/3] fix: put each media part's marker where the part is in its message, as llama-server does A chat template that reads both string and typed content was given typed image and audio parts, and its own placeholder was rewritten to the mtmd marker from a fixed table. A placeholder missing from the table (Qwen3.5, Qwen2.5-Omni) left no marker in the prompt, so the llama.cpp service put the markers before the whole prompt; GLM-OCR's wrapper tokens stayed around the marker and mtmd added them again. The render context now gives every template the marker in the message text, where the part was, and never the media part, which is what llama-server v0.6.0 renders. Closes #964 --- CHANGELOG.md | 6 + doc/litert_lm_templates.md | 6 +- .../core/template/chat_template_engine.dart | 9 +- .../core/template/chat_template_handler.dart | 18 +- .../core/template/jinja/jinja_analyzer.dart | 11 +- .../template/template_render_context.dart | 26 +- .../media_marker_render_upstream.json | 559 ++++++++++++++++++ test/fixtures/media_marker_render_upstream.md | 40 ++ test/support/fake_mtmd.dart | 26 +- .../llama_cpp_service_media_prompt_test.dart | 129 ++++ .../core/template/audio_transport_test.dart | 8 +- .../template/chat_template_engine_test.dart | 66 ++- .../handlers/gemma4_handler_test.dart | 6 +- .../media_marker_render_parity_test.dart | 102 ++++ .../template_render_context_test.dart | 72 ++- website/docs/changelog/recent-releases.md | 6 + .../docs/guides/template-engine-internals.md | 6 +- 17 files changed, 1038 insertions(+), 58 deletions(-) create mode 100644 test/fixtures/media_marker_render_upstream.json create mode 100644 test/fixtures/media_marker_render_upstream.md create mode 100644 test/unit/backends/llama_cpp/llama_cpp_service_media_prompt_test.dart create mode 100644 test/unit/core/template/media_marker_render_parity_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 213632ec7..b75b64925 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,12 @@ starting a driver llama.cpp cannot use; a load with other usable GPUs leaves that one out ([#782](https://github.com/leehack/llamadart/issues/782)). +- Fix an image or audio part sent to a GGUF model whose chat template writes + its own media placeholder (Qwen3.5, Qwen2.5-Omni, GLM-OCR) being attached + before the whole prompt, or wrapped in the image start and end tokens + twice. Each part now takes the place it has in its message, as llama.cpp's + `llama-server` renders it + ([#964](https://github.com/leehack/llamadart/issues/964)). * Aligned the default WebGPU bridge assets to `v0.1.59`: they embed llama.cpp `v0.6.0`, are qualified against native `v0.6.0`, and keep Web/native llama.cpp `v0.6.0@d81235049384534c167caea52b85a694f6103d14` parity; Web diff --git a/doc/litert_lm_templates.md b/doc/litert_lm_templates.md index 66e4d7a75..a5961628a 100644 --- a/doc/litert_lm_templates.md +++ b/doc/litert_lm_templates.md @@ -209,9 +209,9 @@ Templates are committed as jinja under `tool/litert_lm_templates/` and embedded into `lib/src/backends/litert_lm/litert_lm_chat_templates.dart` by a generator. Start from the canonical template llama.cpp uses. Keep any runtime content-shape adapter in this source and cover it with prompt-parity tests. -An adapter that reads content part lists either renders image and audio parts -or rejects both: the Dart renderer gives a template that reads part lists -media as typed parts unless it rejects both kinds. Pin the result in +The Dart renderer gives an adapter the media marker in the message text and +never an image or audio part. It still reports an adapter that reads content +part lists but rejects both kinds as reading strings only. Pin the result in `test/unit/core/template/template_caps_cache_fixtures_test.dart`. 1. Copy the canonical jinja into `tool/litert_lm_templates/.jinja` diff --git a/lib/src/core/template/chat_template_engine.dart b/lib/src/core/template/chat_template_engine.dart index 7d14ace77..a832763da 100644 --- a/lib/src/core/template/chat_template_engine.dart +++ b/lib/src/core/template/chat_template_engine.dart @@ -249,10 +249,11 @@ class ChatTemplateEngine { ); } - // Templates that also read string content get media as typed parts. - // Templates that read only typed parts (e.g. SmolVLM's - // `message['content'][0]['type']`) get the media marker in a text part, as - // llama.cpp gives them. + // No template gets a media part: each becomes the media marker in the + // message text, as llama.cpp renders it. Templates that also read string + // content take it through renderWithMultimodalContent. Templates that + // read only typed parts (e.g. SmolVLM's `message['content'][0]['type']`) + // get it in a text part. final hasMediaParts = effectiveMessages.any( (message) => message.parts.any( (part) => part is LlamaImageContent || part is LlamaAudioContent, diff --git a/lib/src/core/template/chat_template_handler.dart b/lib/src/core/template/chat_template_handler.dart index 6422107df..f11a6093f 100644 --- a/lib/src/core/template/chat_template_handler.dart +++ b/lib/src/core/template/chat_template_handler.dart @@ -50,6 +50,8 @@ abstract class ChatTemplateHandler { /// reads them as objects, and string content becomes a text part when it /// reads content only as parts, as llama.cpp does. A handler that builds /// typed parts itself passes `typedContent: false` to keep string content. + /// With [multimodal], each media part becomes the mtmd marker in the + /// message text. List> templateMessages( List messages, { bool multimodal = false, @@ -159,14 +161,12 @@ abstract class ChatTemplateHandler { }); } - /// Re-renders with content always in list-of-parts format. + /// Renders a request that carries media for a template that reads both + /// string and typed content. /// - /// Some templates (e.g. SmolVLM) expect `content` to be - /// `[{type: 'text', text: '...'}, {type: 'image'}]` rather than a string. - /// This method converts messages to multimodal format and re-renders. - /// - /// After rendering, replaces model-specific image placeholders with - /// the mtmd marker `<__media__>` so the native tokenizer can find them. + /// Each image, audio or video part becomes the mtmd marker `<__media__>` in + /// the message text, where the part was, as llama.cpp renders it. The + /// template's own placeholder for a typed media part is not written. LlamaChatTemplateResult renderWithMultimodalContent({ required String templateSource, required List messages, @@ -202,8 +202,8 @@ abstract class ChatTemplateHandler { } } - // Post-process: replace model-specific image placeholders with - // the mtmd marker so the native tokenizer can match bitmaps to markers. + // A model-specific placeholder written in the message text also counts + // as the mtmd marker. prompt = normalizeMediaPlaceholders(prompt); final hasTools = tools != null && tools.isNotEmpty; diff --git a/lib/src/core/template/jinja/jinja_analyzer.dart b/lib/src/core/template/jinja/jinja_analyzer.dart index 11c0d5a26..4b0838d92 100644 --- a/lib/src/core/template/jinja/jinja_analyzer.dart +++ b/lib/src/core/template/jinja/jinja_analyzer.dart @@ -24,8 +24,9 @@ class JinjaAnalyzer { /// /// The media-part probe is not llama.cpp's: a template that reads both /// strings and part lists is rendered with an image part and with an audio - /// part, and `supportsTypedContent` is cleared when it rejects both. The - /// only part lists `ChatTemplateEngine` gives such a template carry media. + /// part, and `supportsTypedContent` is cleared when it rejects both. + /// `ChatTemplateEngine` gives no template a media part; the probe only + /// selects the handler method that renders a request carrying media. /// /// A probe render that throws is one of llama.cpp's outcomes, not a /// failure: it is logged at debug level and read as llama.cpp reads it. @@ -105,9 +106,9 @@ class JinjaAnalyzer { } } - // A template that reads strings too is given a part list only to carry - // media. One that reads text part lists but rejects both media kinds is - // given strings, with the media marker in the text. + // A template that reads strings too and rejects both media kinds as + // typed parts renders a request carrying media as every string template + // does. Either way it gets the media marker in the message text. if (supportsTypedContent && supportsStringContent) { bool rejects(String type) => !_render( probe, diff --git a/lib/src/core/template/template_render_context.dart b/lib/src/core/template/template_render_context.dart index 9a34b8e70..d826301b6 100644 --- a/lib/src/core/template/template_render_context.dart +++ b/lib/src/core/template/template_render_context.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import '../models/chat/chat_message.dart'; import '../models/chat/chat_role.dart'; import '../models/chat/content_part.dart'; +import 'media_placeholders.dart'; import 'tool_call_parsing_utils.dart'; /// Tool-call serialization policy for template render contexts. @@ -81,6 +82,12 @@ class TemplateRenderContext { /// unchanged. A message with several tool results becomes one message per /// result, as [splitToolResults] describes. /// + /// With [multimodal], the text of a message holds [mtmdMediaMarker] where + /// each image, audio or video part was. llama.cpp also gives a template the + /// marker and never the media part: the placeholder a template writes for a + /// typed image part is not the marker mtmd matches, and mtmd adds the image + /// begin and end tokens itself. + /// /// When [typedContentOnly] marks a template that reads content only as a /// list of parts, every string `content` becomes a list holding one text /// part, as llama.cpp's `messages_inp_normalizer` does. @@ -99,9 +106,8 @@ class TemplateRenderContext { final renderedMessages = >[]; var hasToolCalls = false; for (final message in splitToolResults(messages)) { - final rendered = multimodal - ? message.toJsonMultimodal() - : message.toJson(); + final rendered = (multimodal ? _withMediaMarkers(message) : message) + .toJson(); rendered['content'] ??= ''; final toolResults = message.parts.whereType(); if (toolResults.isNotEmpty) { @@ -145,6 +151,20 @@ class TemplateRenderContext { return renderedMessages; } + static LlamaChatMessage _withMediaMarkers(LlamaChatMessage message) { + bool isMedia(LlamaContentPart part) => + part is LlamaImageContent || + part is LlamaAudioContent || + part is LlamaVideoContent; + if (!message.parts.any(isMedia)) return message; + return message.copyWith( + parts: [ + for (final part in message.parts) + isMedia(part) ? const LlamaTextContent(mtmdMediaMarker) : part, + ], + ); + } + static void _decodeObjectArguments(List> messages) { for (final message in messages) { final toolCalls = message['tool_calls']; diff --git a/test/fixtures/media_marker_render_upstream.json b/test/fixtures/media_marker_render_upstream.json new file mode 100644 index 000000000..9062bdb4e --- /dev/null +++ b/test/fixtures/media_marker_render_upstream.json @@ -0,0 +1,559 @@ +{ + "upstream_commit": "d81235049", + "endpoint": "POST /apply-template", + "media_marker": "<__media__>", + "vision_model": "LFM2-VL-450M-Q4_0.gguf", + "vision_model_sha256": "1fbf4797e8669cdeabd97fa1d31ab046907cb1fc78b1c3d4bda1ba879574e5d7", + "vision_mmproj": "mmproj-LFM2-VL-450M-Q8_0.gguf", + "vision_mmproj_sha256": "a6a813fae4ba3f90852a990fdd4786a017dba0e80e599a366853ba539ee2b5ef", + "audio_model": "Llama-3.2-1B-Instruct-Q4_K_M.gguf", + "audio_model_sha256": "6f85a640a97cf2bf5b8e764087b1e83da0fdb51d7c9fab7d0fece9385611df83", + "audio_mmproj": "mmproj-ultravox-v0_5-llama-3_2-1b-f16.gguf", + "audio_mmproj_sha256": "b34dde1835752949d6b960528269af93c92fec91c61ea0534fcc73f96c1ed8b2", + "image_png_base64": "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAAEklEQVR4nGP4z8CAFWEXHbQSACj/P8Fu7N9hAAAAAElFTkSuQmCC", + "audio_wav_base64": "UklGRqQMAABXQVZFZm10IBAAAAABAAEAgD4AAAB9AAACABAAZGF0YYAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==", + "conversations": { + "image_then_text": [ + { + "role": "user", + "parts": [ + { + "image": true + }, + { + "text": "What color is this image? Answer with one word." + } + ] + } + ], + "text_then_image": [ + { + "role": "user", + "parts": [ + { + "text": "What color is this image? Answer with one word." + }, + { + "image": true + } + ] + } + ], + "two_images_then_text": [ + { + "role": "user", + "parts": [ + { + "image": true + }, + { + "image": true + }, + { + "text": "What are the colors of the first and second image? Answer with two words." + } + ] + } + ], + "interleaved_two_images": [ + { + "role": "user", + "parts": [ + { + "text": "First:" + }, + { + "image": true + }, + { + "text": "Second:" + }, + { + "image": true + }, + { + "text": "What are the colors of the first and second image? Answer with two words." + } + ] + } + ], + "image_only": [ + { + "role": "user", + "parts": [ + { + "image": true + } + ] + } + ], + "image_in_later_turn": [ + { + "role": "user", + "parts": [ + { + "text": "Hello." + } + ] + }, + { + "role": "assistant", + "parts": [ + { + "text": "Hi! How can I help?" + } + ] + }, + { + "role": "user", + "parts": [ + { + "image": true + }, + { + "text": "What color is this image? Answer with one word." + } + ] + } + ], + "system_and_image": [ + { + "role": "system", + "parts": [ + { + "text": "You are a concise assistant." + } + ] + }, + { + "role": "user", + "parts": [ + { + "image": true + }, + { + "text": "What color is this image? Answer with one word." + } + ] + } + ], + "audio_then_text": [ + { + "role": "user", + "parts": [ + { + "audio": true + }, + { + "text": "What do you hear? Answer with one word." + } + ] + } + ], + "text_then_audio": [ + { + "role": "user", + "parts": [ + { + "text": "What do you hear? Answer with one word." + }, + { + "audio": true + } + ] + } + ] + }, + "cases": [ + { + "template": "test/fixtures/templates/Qwen3_5-0_8B.jinja", + "template_sha256": "273d8e0e683b885071fb17e08d71e5f2a5ddfb5309756181681de4f5a1822d80", + "conversation": "image_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|im_start|>user\n<__media__>What color is this image? Answer with one word.<|im_end|>\n<|im_start|>assistant\n\n" + }, + { + "template": "test/fixtures/templates/Qwen3_5-0_8B.jinja", + "template_sha256": "273d8e0e683b885071fb17e08d71e5f2a5ddfb5309756181681de4f5a1822d80", + "conversation": "text_then_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|im_start|>user\nWhat color is this image? Answer with one word.<__media__><|im_end|>\n<|im_start|>assistant\n\n" + }, + { + "template": "test/fixtures/templates/Qwen3_5-0_8B.jinja", + "template_sha256": "273d8e0e683b885071fb17e08d71e5f2a5ddfb5309756181681de4f5a1822d80", + "conversation": "two_images_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|im_start|>user\n<__media__><__media__>What are the colors of the first and second image? Answer with two words.<|im_end|>\n<|im_start|>assistant\n\n" + }, + { + "template": "test/fixtures/templates/Qwen3_5-0_8B.jinja", + "template_sha256": "273d8e0e683b885071fb17e08d71e5f2a5ddfb5309756181681de4f5a1822d80", + "conversation": "interleaved_two_images", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|im_start|>user\nFirst:<__media__>Second:<__media__>What are the colors of the first and second image? Answer with two words.<|im_end|>\n<|im_start|>assistant\n\n" + }, + { + "template": "test/fixtures/templates/Qwen3_5-0_8B.jinja", + "template_sha256": "273d8e0e683b885071fb17e08d71e5f2a5ddfb5309756181681de4f5a1822d80", + "conversation": "image_only", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|im_start|>user\n<__media__><|im_end|>\n<|im_start|>assistant\n\n" + }, + { + "template": "test/fixtures/templates/Qwen3_5-0_8B.jinja", + "template_sha256": "273d8e0e683b885071fb17e08d71e5f2a5ddfb5309756181681de4f5a1822d80", + "conversation": "image_in_later_turn", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|im_start|>user\nHello.<|im_end|>\n<|im_start|>assistant\nHi! How can I help?<|im_end|>\n<|im_start|>user\n<__media__>What color is this image? Answer with one word.<|im_end|>\n<|im_start|>assistant\n\n" + }, + { + "template": "test/fixtures/templates/Qwen3_5-0_8B.jinja", + "template_sha256": "273d8e0e683b885071fb17e08d71e5f2a5ddfb5309756181681de4f5a1822d80", + "conversation": "system_and_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|im_start|>system\nYou are a concise assistant.<|im_end|>\n<|im_start|>user\n<__media__>What color is this image? Answer with one word.<|im_end|>\n<|im_start|>assistant\n\n" + }, + { + "template": "test/fixtures/templates/gemma-3-4b-it.jinja", + "template_sha256": "7de1c58e208eda46e9c7f86397df37ec49883aeece39fb961e0a6b24088dd3c4", + "conversation": "image_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\n<__media__>What color is this image? Answer with one word.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3-4b-it.jinja", + "template_sha256": "7de1c58e208eda46e9c7f86397df37ec49883aeece39fb961e0a6b24088dd3c4", + "conversation": "text_then_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\nWhat color is this image? Answer with one word.<__media__>\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3-4b-it.jinja", + "template_sha256": "7de1c58e208eda46e9c7f86397df37ec49883aeece39fb961e0a6b24088dd3c4", + "conversation": "two_images_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\n<__media__><__media__>What are the colors of the first and second image? Answer with two words.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3-4b-it.jinja", + "template_sha256": "7de1c58e208eda46e9c7f86397df37ec49883aeece39fb961e0a6b24088dd3c4", + "conversation": "interleaved_two_images", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\nFirst:<__media__>Second:<__media__>What are the colors of the first and second image? Answer with two words.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3-4b-it.jinja", + "template_sha256": "7de1c58e208eda46e9c7f86397df37ec49883aeece39fb961e0a6b24088dd3c4", + "conversation": "image_only", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\n<__media__>\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3-4b-it.jinja", + "template_sha256": "7de1c58e208eda46e9c7f86397df37ec49883aeece39fb961e0a6b24088dd3c4", + "conversation": "image_in_later_turn", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\nHello.\nmodel\nHi! How can I help?\nuser\n<__media__>What color is this image? Answer with one word.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3-4b-it.jinja", + "template_sha256": "7de1c58e208eda46e9c7f86397df37ec49883aeece39fb961e0a6b24088dd3c4", + "conversation": "system_and_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\nYou are a concise assistant.\n\n<__media__>What color is this image? Answer with one word.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3n-E4B-it.jinja", + "template_sha256": "ac03dcb3b09726f1e50ac55ae58fc8d5930eadc3b3a12cb04286dc1d82ac8001", + "conversation": "image_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\n<__media__>What color is this image? Answer with one word.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3n-E4B-it.jinja", + "template_sha256": "ac03dcb3b09726f1e50ac55ae58fc8d5930eadc3b3a12cb04286dc1d82ac8001", + "conversation": "text_then_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\nWhat color is this image? Answer with one word.<__media__>\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3n-E4B-it.jinja", + "template_sha256": "ac03dcb3b09726f1e50ac55ae58fc8d5930eadc3b3a12cb04286dc1d82ac8001", + "conversation": "two_images_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\n<__media__><__media__>What are the colors of the first and second image? Answer with two words.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3n-E4B-it.jinja", + "template_sha256": "ac03dcb3b09726f1e50ac55ae58fc8d5930eadc3b3a12cb04286dc1d82ac8001", + "conversation": "interleaved_two_images", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\nFirst:<__media__>Second:<__media__>What are the colors of the first and second image? Answer with two words.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3n-E4B-it.jinja", + "template_sha256": "ac03dcb3b09726f1e50ac55ae58fc8d5930eadc3b3a12cb04286dc1d82ac8001", + "conversation": "image_only", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\n<__media__>\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3n-E4B-it.jinja", + "template_sha256": "ac03dcb3b09726f1e50ac55ae58fc8d5930eadc3b3a12cb04286dc1d82ac8001", + "conversation": "image_in_later_turn", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\nHello.\nmodel\nHi! How can I help?\nuser\n<__media__>What color is this image? Answer with one word.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3n-E4B-it.jinja", + "template_sha256": "ac03dcb3b09726f1e50ac55ae58fc8d5930eadc3b3a12cb04286dc1d82ac8001", + "conversation": "system_and_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\nYou are a concise assistant.\n\n<__media__>What color is this image? Answer with one word.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3n-E4B-it.jinja", + "template_sha256": "ac03dcb3b09726f1e50ac55ae58fc8d5930eadc3b3a12cb04286dc1d82ac8001", + "conversation": "audio_then_text", + "media_model": "audio", + "eos_token": "<|eot_id|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\n<__media__>What do you hear? Answer with one word.\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-3n-E4B-it.jinja", + "template_sha256": "ac03dcb3b09726f1e50ac55ae58fc8d5930eadc3b3a12cb04286dc1d82ac8001", + "conversation": "text_then_audio", + "media_model": "audio", + "eos_token": "<|eot_id|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "user\nWhat do you hear? Answer with one word.<__media__>\nmodel\n" + }, + { + "template": "test/fixtures/templates/gemma-4-E2B-it.jinja", + "template_sha256": "33204f1acb5bd0002713e16a593847f24ceeafe711ed88bda2a352dc996a3373", + "conversation": "image_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|turn>system\n<|think|>\n\n<|turn>user\n<__media__>What color is this image? Answer with one word.\n<|turn>model\n" + }, + { + "template": "test/fixtures/templates/gemma-4-E2B-it.jinja", + "template_sha256": "33204f1acb5bd0002713e16a593847f24ceeafe711ed88bda2a352dc996a3373", + "conversation": "text_then_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|turn>system\n<|think|>\n\n<|turn>user\nWhat color is this image? Answer with one word.<__media__>\n<|turn>model\n" + }, + { + "template": "test/fixtures/templates/gemma-4-E2B-it.jinja", + "template_sha256": "33204f1acb5bd0002713e16a593847f24ceeafe711ed88bda2a352dc996a3373", + "conversation": "two_images_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|turn>system\n<|think|>\n\n<|turn>user\n<__media__><__media__>What are the colors of the first and second image? Answer with two words.\n<|turn>model\n" + }, + { + "template": "test/fixtures/templates/gemma-4-E2B-it.jinja", + "template_sha256": "33204f1acb5bd0002713e16a593847f24ceeafe711ed88bda2a352dc996a3373", + "conversation": "interleaved_two_images", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|turn>system\n<|think|>\n\n<|turn>user\nFirst:<__media__>Second:<__media__>What are the colors of the first and second image? Answer with two words.\n<|turn>model\n" + }, + { + "template": "test/fixtures/templates/gemma-4-E2B-it.jinja", + "template_sha256": "33204f1acb5bd0002713e16a593847f24ceeafe711ed88bda2a352dc996a3373", + "conversation": "image_only", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|turn>system\n<|think|>\n\n<|turn>user\n<__media__>\n<|turn>model\n" + }, + { + "template": "test/fixtures/templates/gemma-4-E2B-it.jinja", + "template_sha256": "33204f1acb5bd0002713e16a593847f24ceeafe711ed88bda2a352dc996a3373", + "conversation": "image_in_later_turn", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|turn>system\n<|think|>\n\n<|turn>user\nHello.\n<|turn>model\nHi! How can I help?\n<|turn>user\n<__media__>What color is this image? Answer with one word.\n<|turn>model\n" + }, + { + "template": "test/fixtures/templates/gemma-4-E2B-it.jinja", + "template_sha256": "33204f1acb5bd0002713e16a593847f24ceeafe711ed88bda2a352dc996a3373", + "conversation": "system_and_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|turn>system\n<|think|>\nYou are a concise assistant.\n<|turn>user\n<__media__>What color is this image? Answer with one word.\n<|turn>model\n" + }, + { + "template": "test/fixtures/templates/gemma-4-E2B-it.jinja", + "template_sha256": "33204f1acb5bd0002713e16a593847f24ceeafe711ed88bda2a352dc996a3373", + "conversation": "audio_then_text", + "media_model": "audio", + "eos_token": "<|eot_id|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|turn>system\n<|think|>\n\n<|turn>user\n<__media__>What do you hear? Answer with one word.\n<|turn>model\n" + }, + { + "template": "test/fixtures/templates/gemma-4-E2B-it.jinja", + "template_sha256": "33204f1acb5bd0002713e16a593847f24ceeafe711ed88bda2a352dc996a3373", + "conversation": "text_then_audio", + "media_model": "audio", + "eos_token": "<|eot_id|>", + "supports_string_content": true, + "supports_typed_content": true, + "prompt": "<|turn>system\n<|think|>\n\n<|turn>user\nWhat do you hear? Answer with one word.<__media__>\n<|turn>model\n" + }, + { + "template": "test/fixtures/templates/SmolVLM-500M-Instruct.jinja", + "template_sha256": "830005085ddd94cec605b6e21b885b28ffad6e3f82ee6a212c523533e8f04f01", + "conversation": "image_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": false, + "supports_typed_content": true, + "prompt": "<|im_start|>User: <__media__>What color is this image? Answer with one word.\nAssistant:" + }, + { + "template": "test/fixtures/templates/SmolVLM-500M-Instruct.jinja", + "template_sha256": "830005085ddd94cec605b6e21b885b28ffad6e3f82ee6a212c523533e8f04f01", + "conversation": "text_then_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": false, + "supports_typed_content": true, + "prompt": "<|im_start|>User: What color is this image? Answer with one word.<__media__>\nAssistant:" + }, + { + "template": "test/fixtures/templates/SmolVLM-500M-Instruct.jinja", + "template_sha256": "830005085ddd94cec605b6e21b885b28ffad6e3f82ee6a212c523533e8f04f01", + "conversation": "two_images_then_text", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": false, + "supports_typed_content": true, + "prompt": "<|im_start|>User: <__media__><__media__>What are the colors of the first and second image? Answer with two words.\nAssistant:" + }, + { + "template": "test/fixtures/templates/SmolVLM-500M-Instruct.jinja", + "template_sha256": "830005085ddd94cec605b6e21b885b28ffad6e3f82ee6a212c523533e8f04f01", + "conversation": "interleaved_two_images", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": false, + "supports_typed_content": true, + "prompt": "<|im_start|>User: First:<__media__>Second:<__media__>What are the colors of the first and second image? Answer with two words.\nAssistant:" + }, + { + "template": "test/fixtures/templates/SmolVLM-500M-Instruct.jinja", + "template_sha256": "830005085ddd94cec605b6e21b885b28ffad6e3f82ee6a212c523533e8f04f01", + "conversation": "image_only", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": false, + "supports_typed_content": true, + "prompt": "<|im_start|>User: <__media__>\nAssistant:" + }, + { + "template": "test/fixtures/templates/SmolVLM-500M-Instruct.jinja", + "template_sha256": "830005085ddd94cec605b6e21b885b28ffad6e3f82ee6a212c523533e8f04f01", + "conversation": "image_in_later_turn", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": false, + "supports_typed_content": true, + "prompt": "<|im_start|>User: Hello.\nAssistant: Hi! How can I help?\nUser: <__media__>What color is this image? Answer with one word.\nAssistant:" + }, + { + "template": "test/fixtures/templates/SmolVLM-500M-Instruct.jinja", + "template_sha256": "830005085ddd94cec605b6e21b885b28ffad6e3f82ee6a212c523533e8f04f01", + "conversation": "system_and_image", + "media_model": "vision", + "eos_token": "<|im_end|>", + "supports_string_content": false, + "supports_typed_content": true, + "prompt": "<|im_start|>System: You are a concise assistant.\nUser: <__media__>What color is this image? Answer with one word.\nAssistant:" + } + ] +} diff --git a/test/fixtures/media_marker_render_upstream.md b/test/fixtures/media_marker_render_upstream.md new file mode 100644 index 000000000..9b8d56813 --- /dev/null +++ b/test/fixtures/media_marker_render_upstream.md @@ -0,0 +1,40 @@ +# Media marker render reference + +`media_marker_render_upstream.json` holds the prompts that an unmodified +`llama-server`, built from llama.cpp `d81235049` (tag `v0.6.0`, the pinned +native runtime), returned from `POST /apply-template` for conversations with +image and audio parts. It pins +[#964](https://github.com/leehack/llamadart/issues/964): llama-server gives a +chat template the media marker in the message text, where the image or audio +part was, and never the part itself. A template's own image placeholder +(`<|vision_start|><|image_pad|><|vision_end|>` in Qwen3.5, `` +in Gemma 3, `<|image|>` in Gemma 4) is therefore not in the prompt, and mtmd +adds the image begin and end tokens when it tokenizes the marker. + +`conversations` holds each conversation as ordered `parts` (`text`, `image` +or `audio`), the form the test builds typed `LlamaChatMessage`s from. The +request sent each image as an `image_url` part carrying `image_png_base64` and +each audio part as an `input_audio` part carrying `audio_wav_base64` (0.1 s of +16 kHz silence), and a message with a single text part as string content. + +For each template the server ran with +`--jinja --chat-template-file