Auto parser detect / auto input-workaround detection#4312
Open
dkalinowski wants to merge 23 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds chat-template capability detection plus request “input workarounds” so OVMS can (a) auto-pick tool/reasoning parsers from the template and (b) normalize incoming requests to match template expectations (e.g., Gemma4 object arguments, Llama3 non-null content) across both Python/Jinja and GenAI C++ paths.
Changes:
- Introduces
ChatTemplateAnalyzer+ChatTemplateCapsand wires analysis into servable initialization for auto-detecting model family/tool parser/reasoning parser. - Adds
input_workaroundsmodule and applies it before chat template rendering (JSON for Jinja;ChatHistoryfor GenAI). - Adds unit tests + Bazel targets for analyzer/workaround behavior, and updates example templates/docs.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/test/llm/input_workarounds_test.cpp | New unit tests for JSON-path workarounds (object args, non-null content, applyToJson). |
| src/test/llm/chat_template_analyzer_test.cpp | New unit tests for template-family detection and inferred caps/parsers. |
| src/llm/visual_language_model/legacy/servable.cpp | Applies input workarounds to Jinja JSON path and GenAI ChatHistory path in VLM legacy flow. |
| src/llm/visual_language_model/legacy/servable_initializer.cpp | Updates chat-template mode selection (but currently duplicated). |
| src/llm/visual_language_model/continuous_batching/servable.cpp | Applies input workarounds to both Jinja JSON and GenAI ChatHistory paths in VLM CB flow. |
| src/llm/servable.hpp | Stores analyzed template caps + detected model family in GenAiServableProperties. |
| src/llm/servable.cpp | Applies workarounds before template application on both Jinja and GenAI paths. |
| src/llm/servable_initializer.cpp | Runs chat template analysis at init and auto-selects tool/reasoning parser when not explicitly configured. |
| src/llm/llm_calculator.proto | Removes an outdated TODO comment for chat template mode. |
| src/llm/language_model/legacy/servable_initializer.cpp | Updates chat-template mode selection (but currently duplicated). |
| src/llm/language_model/continuous_batching/servable_initializer.cpp | Updates chat-template mode selection (but currently duplicated). |
| src/llm/input_workarounds.hpp | Declares JSON + ChatHistory workaround APIs and aggregate apply functions. |
| src/llm/input_workarounds.cpp | Implements workarounds for JSON and ChatHistory paths (but ChatHistory mutation currently broken). |
| src/llm/chat_template_caps.hpp | New capability struct describing template requirements/support. |
| src/llm/chat_template_analyzer.hpp | Declares analyzer result + analyzer entrypoint. |
| src/llm/chat_template_analyzer.cpp | Implements Tier-1 pattern matching for family/caps/parser detection. |
| src/llm/BUILD | Adds Bazel libraries for analyzer + workarounds and links them into genai servables. |
| src/BUILD | Adds a new test library for analyzer/workaround tests into the main test target. |
| extras/chat_template_examples/chat_template_qwen36.jinja | Updates example template to from_json tool-call arguments before iterating. |
| extras/chat_template_examples/chat_template_qwen35.jinja | Adds a new example template variant with the same from_json workaround. |
| docs/plan_chat_template_input_workarounds.md | Adds a design/plan document describing detection/workaround strategy and roadmap. |
Comment on lines
+91
to
+96
| auto message = chatHistory[msgIdx]; | ||
| if (!message.contains("tool_calls")) { | ||
| continue; | ||
| } | ||
| auto toolCalls = message["tool_calls"]; | ||
| if (!toolCalls.is_array()) { |
Comment on lines
+100
to
+105
| auto toolCall = toolCalls[i]; | ||
| if (!toolCall.is_object() || !toolCall.contains("function")) { | ||
| continue; | ||
| } | ||
| auto function = toolCall["function"]; | ||
| if (!function.is_object() || !function.contains("arguments")) { |
Comment on lines
+108
to
+112
| auto args = function["arguments"]; | ||
| if (!args.is_string()) { | ||
| continue; | ||
| } | ||
| std::string argsStr = args.get_string(); |
|
|
||
| void ensureNonNullContentHistory(ov::genai::ChatHistory& chatHistory) { | ||
| for (size_t msgIdx = 0; msgIdx < chatHistory.size(); ++msgIdx) { | ||
| auto message = chatHistory[msgIdx]; |
Comment on lines
+49
to
+51
| if (argsDoc.HasParseError()) { | ||
| continue; | ||
| } |
Comment on lines
+63
to
+65
| if (!message.IsObject() || !message.HasMember("tool_calls")) { | ||
| continue; | ||
| } |
Comment on lines
+89
to
+90
| void funcArgsToObjectHistory(ov::genai::ChatHistory& chatHistory) { | ||
| for (size_t msgIdx = 0; msgIdx < chatHistory.size(); ++msgIdx) { |
Comment on lines
+188
to
+192
| // Apply input workarounds based on detected chat template capabilities. | ||
| // This modifies the request JSON before chat template application. | ||
| // Currently effective only on the Python Jinja path; GenAI C++ path workarounds | ||
| // will be added during the pre-processing refactoring. | ||
| auto applyInputWorkarounds = [this](const std::string& jsonBody) -> std::string { |
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.