-
Notifications
You must be signed in to change notification settings - Fork 339
fix(server): flatten OpenAI multimodal content before chat template #1387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fuheaven
wants to merge
2
commits into
ModelTC:neo_plus_clean
Choose a base branch
from
fuheaven:neo_plus_clean
base: neo_plus_clean
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+40
−14
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -79,11 +79,37 @@ def _alias_reasoning_to_reasoning_content(messages: list) -> None: | |||||||||||||||||||||||||||||
| msg["reasoning_content"] = reasoning | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def _flatten_multimodal_content(messages: list) -> None: | ||||||||||||||||||||||||||||||
| # OpenAI-style chat requests may send content as a list of parts | ||||||||||||||||||||||||||||||
| # ({"type": "text", ...}, {"type": "image_url", ...}). Most HF chat | ||||||||||||||||||||||||||||||
| # templates (Qwen, LLaMA, ...) call string methods on `content` directly | ||||||||||||||||||||||||||||||
| # and crash on lists. Flatten into a plain string with placeholder tokens | ||||||||||||||||||||||||||||||
| # so the template can render; the image data itself is already extracted | ||||||||||||||||||||||||||||||
| # upstream via `_get_images_and_audios` and fed through MultimodalParams. | ||||||||||||||||||||||||||||||
| for msg in messages: | ||||||||||||||||||||||||||||||
| content = msg.get("content") | ||||||||||||||||||||||||||||||
| if not isinstance(content, list): | ||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||
| parts: list = [] | ||||||||||||||||||||||||||||||
| for item in content: | ||||||||||||||||||||||||||||||
| if not isinstance(item, dict): | ||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||
| t = item.get("type") | ||||||||||||||||||||||||||||||
| if t == "text": | ||||||||||||||||||||||||||||||
| parts.append(item.get("text", "") or "") | ||||||||||||||||||||||||||||||
| elif t == "image_url": | ||||||||||||||||||||||||||||||
| parts.append("<image>") | ||||||||||||||||||||||||||||||
| elif t == "audio_url": | ||||||||||||||||||||||||||||||
| parts.append("<audio>") | ||||||||||||||||||||||||||||||
|
Comment on lines
+97
to
+103
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改进建议
Suggested change
|
||||||||||||||||||||||||||||||
| msg["content"] = "\n".join(p for p in parts if p != "") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async def build_prompt(request, tools) -> str: | ||||||||||||||||||||||||||||||
| # pydantic格式转成dict, 否则,当根据tokenizer_config.json拼template时,Jinja判断无法识别 | ||||||||||||||||||||||||||||||
| messages = [m.model_dump(by_alias=True, exclude_none=True) for m in request.messages] | ||||||||||||||||||||||||||||||
| _normalize_tool_call_arguments(messages) | ||||||||||||||||||||||||||||||
| _alias_reasoning_to_reasoning_content(messages) | ||||||||||||||||||||||||||||||
| _flatten_multimodal_content(messages) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| kwargs = {"conversation": messages} | ||||||||||||||||||||||||||||||
| if request.character_settings: | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an OpenAI-style
image_url/audio_urlrequest is served by Qwen2/3-VL or Qwen3-Omni, this replaces the content with generic<image>/<audio>text while_get_images_and_audiosstill attaches the media. Those tokenizers do not count these generic strings: Qwen2/3-VL looks forvision_start_idimmediately followed byvision_end_idand then raisesinvalid image tag numwhen the attached image count is nonzero (lightllm/models/qwen2_vl/model.py:66-85), and Omni does the same for audio start/end ids (lightllm/models/qwen3_omni_moe_thinker/model.py:126-144). This means multimodal chat completions for those models still fail after prompt building; preserve the list for templates that know how to render it or emit the tokenizer-specific sentinels instead.Useful? React with 👍 / 👎.