-
Notifications
You must be signed in to change notification settings - Fork 262
MiniCPM5 tool parser #4373
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
Merged
Merged
MiniCPM5 tool parser #4373
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
288ade9
Add minicpm5 tool_parser for MiniCPM5 XML tool calls (prototype)
exzile 0d45d35
minicpm5 parser: add streaming split-chunk and edge-case tests
exzile 5c6e4a7
Merge branch 'main' into feature/minicpm5-tool-parser
exzile e9a9d86
Merge branch 'main' into feature/minicpm5-tool-parser
exzile 869484c
Address review: share parser helpers, split state machine, separate r…
exzile b77edee
minicpm5: stream special tokens for tool tags + matching reasoning pa…
exzile 0ceda28
test(minicpm5): clarify why the Qwen3-8B tokenizer is reused in tests
exzile e1bea89
minicpm5: implement native token-based reasoning parser
exzile 2243ea4
Merge branch 'main' into feature/minicpm5-tool-parser
exzile 7a3e1da
minicpm5: preserve special tokens when decoding content, not just rea…
exzile ff1694a
test(minicpm5): clarify streaming test scope, add multi-call delta co…
exzile b77dd79
Merge remote-tracking branch 'minifork/feature/minicpm5-tool-parser' …
przepeck d7a00ec
save
przepeck 00c099a
fix content with reasoning and without tool calls
przepeck dc115a9
streaming tests
przepeck 4376744
fixing array and object parsing
przepeck db2abda
styles
przepeck 2f966cd
fix reasoning, tests and special tokens in content
przepeck 9c4cf6c
reasoning fixes
przepeck 941b157
styles
przepeck baf4831
Merge branch 'main' into przepeck/minicpm5
przepeck a60410e
copilot review
przepeck 88bfa43
style
przepeck a02c8dd
typos
przepeck 52706ea
copilot review
przepeck 59ad38d
styles
przepeck 053ff35
Merge branch 'main' into przepeck/minicpm5
przepeck 645a29e
windows model download
przepeck 24ca369
autodetection of tool and reasoning parser
przepeck 3ee10e9
copilot's review
przepeck c2a2ece
review changes
przepeck 93fc51f
style
przepeck 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
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
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
81 changes: 81 additions & 0 deletions
81
src/llm/io_processing/minicpm5/minicpm5_reasoning_parser.cpp
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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| //***************************************************************************** | ||
| // Copyright 2026 Intel Corporation | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| //***************************************************************************** | ||
| #include <openvino/genai/tokenizer.hpp> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
|
przepeck marked this conversation as resolved.
|
||
| #include "src/port/rapidjson_document.hpp" | ||
|
|
||
| #include "src/logging.hpp" | ||
| #include "minicpm5_reasoning_parser.hpp" | ||
| #include "src/llm/io_processing/utils.hpp" | ||
|
|
||
| namespace ovms { | ||
| void Minicpm5ReasoningParser::parse(ParsedOutput& parsedOutput, const std::vector<int64_t>& generatedTokens) { | ||
| auto startReasoningIt = std::find(generatedTokens.begin(), generatedTokens.end(), reasoningStartTokenId); | ||
| auto endReasoningIt = std::find(generatedTokens.begin(), generatedTokens.end(), reasoningEndTokenId); | ||
|
|
||
| if ((startReasoningIt == generatedTokens.end() && endReasoningIt == generatedTokens.end())) { | ||
| SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Minicpm5ReasoningParser: Reasoning start or end token not found in the generated tokens. Start token found: {}, End token found: {}, Start position: {}, End position: {}", | ||
| startReasoningIt != generatedTokens.end(), endReasoningIt != generatedTokens.end(), std::distance(generatedTokens.begin(), startReasoningIt), std::distance(generatedTokens.begin(), endReasoningIt)); | ||
| return; | ||
| } | ||
|
|
||
| auto startPos = 0; | ||
| if (startReasoningIt != generatedTokens.end()) { | ||
| startPos = std::distance(generatedTokens.begin(), startReasoningIt) + 1; | ||
| } else { | ||
| SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Minicpm5ReasoningParser: Reasoning start token not found in the generated tokens. Start position: {}", startPos); | ||
| } | ||
| auto endPos = std::distance(generatedTokens.begin(), endReasoningIt); | ||
|
|
||
| std::string reasoningContent = tokenizer.decode(std::vector<int64_t>(startPos + generatedTokens.begin(), endPos + generatedTokens.begin()), ov::genai::skip_special_tokens(true)); | ||
|
|
||
| parsedOutput.reasoning = reasoningContent; | ||
|
|
||
| if (endReasoningIt != generatedTokens.end()) { | ||
| endPos += 1; | ||
| } | ||
|
|
||
| std::string contentWithoutReasoning = tokenizer.decode(std::vector<int64_t>(endPos + generatedTokens.begin(), generatedTokens.end()), ov::genai::skip_special_tokens(true)); | ||
| parsedOutput.content = contentWithoutReasoning; | ||
| } | ||
|
przepeck marked this conversation as resolved.
|
||
|
|
||
| std::optional<rapidjson::Document> Minicpm5ReasoningParser::parseChunk(const std::string& chunk, const std::vector<int64_t>& tokens, ov::genai::GenerationFinishReason finishReason) { | ||
| if (tokens.empty()) { | ||
| SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Received empty tokens for Minicpm5ReasoningParser"); | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| if (std::find(tokens.begin(), tokens.end(), reasoningStartTokenId) != tokens.end() || | ||
| std::find(tokens.begin(), tokens.end(), reasoningEndTokenId) != tokens.end()) { | ||
| return std::nullopt; | ||
| } else { | ||
| rapidjson::StringBuffer buffer; | ||
| rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); | ||
| writer.StartObject(); | ||
| writer.String("delta"); | ||
| writer.StartObject(); | ||
| writer.String("reasoning_content"); | ||
| writer.String(chunk.c_str()); | ||
| writer.EndObject(); | ||
| writer.EndObject(); | ||
| rapidjson::Document doc; | ||
| doc.Parse(buffer.GetString()); | ||
| return doc; | ||
| } | ||
| } | ||
| } // namespace ovms | ||
53 changes: 53 additions & 0 deletions
53
src/llm/io_processing/minicpm5/minicpm5_reasoning_parser.hpp
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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| //***************************************************************************** | ||
| // Copyright 2026 Intel Corporation | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| //***************************************************************************** | ||
| #pragma once | ||
| #include "src/llm/io_processing/base_output_parser.hpp" | ||
| #include <vector> | ||
| #include <string> | ||
|
|
||
| namespace ovms { | ||
| class Minicpm5ReasoningParser : public BaseOutputParser { | ||
| public: | ||
| static inline const std::string reasoningStartTag = "<think>"; | ||
| static inline const std::string reasoningEndTag = "</think>"; | ||
|
|
||
| static constexpr int64_t reasoningStartTokenId = 8; | ||
| static constexpr int64_t reasoningEndTokenId = 9; | ||
|
|
||
| public: | ||
| Minicpm5ReasoningParser() = delete; | ||
| explicit Minicpm5ReasoningParser(ov::genai::Tokenizer& tokenizer) : | ||
| BaseOutputParser(tokenizer) {} | ||
|
|
||
| void parse(ParsedOutput& parsedOutput, const std::vector<int64_t>& generatedTokens) override; | ||
| std::optional<rapidjson::Document> parseChunk(const std::string& chunk, const std::vector<int64_t>& tokens, ov::genai::GenerationFinishReason finishReason) override; | ||
| const std::vector<std::string>& getParsingStartTags() const override { | ||
| static const std::vector<std::string> parsingStartTags{this->reasoningStartTag}; | ||
| return parsingStartTags; | ||
| } | ||
| const std::vector<std::string>& getSpecialParsingStartTags() const override { | ||
| static const std::vector<std::string> specialParsingStartTags{}; | ||
| return specialParsingStartTags; | ||
| } | ||
| const std::string& getParsingEndTag() const override { | ||
| return reasoningEndTag; | ||
| } | ||
|
|
||
| bool requiresStreamingWithSpecialTokens() const override { | ||
| return true; | ||
| } | ||
| }; | ||
| } // namespace ovms |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.