fix(vl): forward tools to multimodal chat templates#4759
Open
lvhan028 wants to merge 1 commit into
Open
Conversation
Collaborator
Author
|
cc @littlegy |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the vision-language (VL) prompt rendering path to forward OpenAI-style tools into multimodal chat template rendering, aligning VL behavior with the text-only serving pipeline.
Changes:
- Thread
toolsthrough VL modelproc_messages/to_pytorch/to_turbomindflows and pass it intochat_template.messages2prompt(...). - Extend
VisionModel.get_input_prompt(...)to accepttoolsand forward them to the chat template renderer. - Pass
toolsintoget_input_prompt(...)from the multimodal serving processor for the “new preprocess” path.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| lmdeploy/vl/model/qwen2.py | Forward tools into template rendering for Qwen2-VL prompt construction. |
| lmdeploy/vl/model/minicpmv.py | Forward tools into template rendering for MiniCPM-V prompt construction. |
| lmdeploy/vl/model/llava_hf.py | Forward tools into template rendering for LLaVA-HF prompt construction. |
| lmdeploy/vl/model/llama4.py | Forward tools into template rendering for Llama4-VL prompt construction. |
| lmdeploy/vl/model/glm4_v.py | Forward tools into template rendering for GLM-4V prompt construction. |
| lmdeploy/vl/model/gemma3_vl.py | Forward tools into template rendering for Gemma3-VL prompt construction. |
| lmdeploy/vl/model/deepseek.py | Forward tools into template rendering for DeepSeek-VL prompt construction. |
| lmdeploy/vl/model/deepseek_vl2.py | Forward tools into template rendering for DeepSeek-VL2 prompt construction. |
| lmdeploy/vl/model/cogvlm.py | Forward tools into template rendering for CogVLM prompt construction (per-segment rendering). |
| lmdeploy/vl/model/base.py | Add tools to get_input_prompt(...) and forward it to messages2prompt. |
| lmdeploy/serve/processors/multimodal.py | Pass tools into VL get_input_prompt(...) for the new preprocess path. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| message = dict(role=role, content=''.join(_content)) | ||
| prompt_messages.append(message) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start, tools=tools, **chat_template_kwargs) |
| prompt = ''.join(prompts) + content[0] | ||
| prompt_messages.append(dict(role='user', content=prompt)) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start, tools=tools, **chat_template_kwargs) |
| prompt = (IMAGE_TOKEN + '\n') * n_images + content[0] | ||
| prompt_messages.append(dict(role='user', content=prompt)) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start, tools=tools, **chat_template_kwargs) |
| prompt = f'{IMAGE_TOKEN * n_images}' + prompt | ||
| prompt_messages.append(dict(role='user', content=prompt)) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start, tools=tools, **chat_template_kwargs) |
| prompt = ''.join([f'{IMAGE_TOKEN}\n'] * n_images) + prompt[0] | ||
| prompt_messages.append(dict(role='user', content=prompt)) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start, tools=tools, **chat_template_kwargs) |
| content = ''.join([f'{IMAGE_TOKEN} is Figure {str(i)}.\n' for i in range(n_image)]) + content | ||
| prompt_messages.append(dict(role='user', content=content)) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start, tools=tools, **chat_template_kwargs) |
| continue | ||
| prompt_messages.append(dict(role='user', content=content)) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start) | ||
| prompt = chat_template.messages2prompt(prompt_messages, sequence_start, tools=tools, **chat_template_kwargs) |
Comment on lines
+81
to
+83
| render_kwargs = dict(chat_template_kwargs) if i == 0 else {} | ||
| render_kwargs['tools'] = tools if i == 0 else None | ||
| prompt_i = chat_template.messages2prompt([msg], sequence_start and i == 0, **render_kwargs) |
| if VisionModel.has_input_ids(messages): | ||
| return messages[0]['content'][0]['text'] | ||
| return chat_template.messages2prompt(messages, sequence_start, **(chat_template_kwargs or {})) | ||
| return chat_template.messages2prompt(messages, sequence_start, tools=tools, **(chat_template_kwargs or {})) |
Comment on lines
265
to
267
| def get_input_prompt(messages: list[dict], chat_template, sequence_start: bool, | ||
| tools: list[object] | None = None, | ||
| chat_template_kwargs: dict | None = None) -> str | list[int]: |
CUHKSZzxy
approved these changes
Jul 17, 2026
CUHKSZzxy
approved these changes
Jul 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.