-
Notifications
You must be signed in to change notification settings - Fork 251
Gemma4 parser #4179
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
base: main
Are you sure you want to change the base?
Gemma4 parser #4179
Changes from all commits
4142c1f
a53a9bf
66cb9ac
7a9a4c9
461c65b
57a4946
bcaf372
5a7e40a
9d473fc
047cd5f
e92a335
80def30
16f3294
8a1b51b
0417f8b
0258121
a0ca438
01af9ad
d5c6f0e
9fc6306
2aab398
1b304f4
2352349
c6f3835
f63d78e
da43f5c
6eab061
8823f5a
7ce6041
c5f2094
a907b83
0faff45
894b794
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //***************************************************************************** | ||
| // Copyright 2025 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> | ||
|
|
||
| #include "src/port/rapidjson_document.hpp" | ||
|
|
||
| #include "../../../logging.hpp" | ||
| #include "gemma4_reasoning_parser.hpp" | ||
| #include "../utils.hpp" | ||
|
|
||
| namespace ovms { | ||
| void Gemma4ReasoningParser::parse(ParsedOutput& parsedOutput, const std::vector<int64_t>& generatedTokens) { | ||
| std::string startReasoningTag = getParsingStartTags()[0]; | ||
| std::string endReasoningTag = getParsingEndTag(); | ||
| size_t startPos = parsedOutput.content.find(startReasoningTag); | ||
| size_t endPos = parsedOutput.content.find(endReasoningTag); | ||
|
|
||
| if (startPos != std::string::npos && endPos != std::string::npos && startPos < endPos) { | ||
| size_t reasoningStart = startPos + startReasoningTag.length(); | ||
| std::string reasoningText = parsedOutput.content.substr(reasoningStart, endPos - reasoningStart); | ||
| parsedOutput.reasoning = reasoningText; | ||
| // Remove reasoning from content | ||
| parsedOutput.content.erase(startPos, endPos - startPos + endReasoningTag.length()); | ||
| } | ||
| } | ||
|
|
||
| std::optional<rapidjson::Document> Gemma4ReasoningParser::parseChunk(const std::string& chunk, ov::genai::GenerationFinishReason finishReason) { | ||
| if (chunk.empty()) { | ||
| SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Received empty chunk for Gemma4ReasoningParser"); | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| if (chunk.find(getParsingStartTags()[0]) != std::string::npos || chunk.find(getParsingEndTag()) != std::string::npos) { | ||
| 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; | ||
| } | ||
| return std::nullopt; | ||
| } | ||
| } // namespace ovms |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| //***************************************************************************** | ||
| // 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 <string> | ||
| #include <vector> | ||
|
|
||
| #include "../base_output_parser.hpp" | ||
|
|
||
| namespace ovms { | ||
| class Gemma4ReasoningParser : public BaseOutputParser { | ||
| protected: | ||
| // Tags used to identify the reasoning segment in the content | ||
| std::string parsingStartTag = "<|channel>thought\n"; | ||
| std::string parsingEndTag = "<channel|>"; | ||
|
|
||
| public: | ||
| Gemma4ReasoningParser() = delete; | ||
| explicit Gemma4ReasoningParser(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, ov::genai::GenerationFinishReason finishReason) override; | ||
| const std::vector<std::string>& getParsingStartTags() const override { | ||
| static const std::vector<std::string> parsingStartTags{this->parsingStartTag}; | ||
| 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 parsingEndTag; | ||
| } | ||
| }; | ||
| } // namespace ovms |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| //***************************************************************************** | ||
| // Copyright 2025 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> | ||
|
|
||
| #include "src/port/rapidjson_document.hpp" | ||
|
|
||
| #include "../../../logging.hpp" | ||
| #include "reasoning_parser.hpp" | ||
| #include "../utils.hpp" | ||
|
|
||
| namespace ovms { | ||
| void Gemma4ReasoningParser::parse(ParsedOutput& parsedOutput, const std::vector<int64_t>& generatedTokens) { | ||
| auto startPos = std::string::npos; | ||
| auto endPos = std::string::npos; | ||
|
|
||
| auto startIt = std::find(generatedTokens.begin(), generatedTokens.end(), reasoningTokenId); | ||
| auto endIt = std::find(generatedTokens.begin(), generatedTokens.end(), reasoningEndTokenId); | ||
|
|
||
| if (startIt != generatedTokens.end() && endIt != generatedTokens.end() && startIt < endIt) { | ||
| startPos = std::distance(generatedTokens.begin(), startIt); | ||
| endPos = std::distance(generatedTokens.begin(), endIt); | ||
| } | ||
|
|
||
| if (startPos != std::string::npos && endPos != std::string::npos && startPos < endPos) { | ||
| size_t reasoningStart = startPos + 3; // deleting "<|channel>thought\n" | ||
| std::string reasoningText = tokenizer.decode(std::vector<int64_t>(generatedTokens.begin() + reasoningStart, generatedTokens.begin() + endPos), ov::genai::skip_special_tokens(true)); | ||
| parsedOutput.reasoning = reasoningText; | ||
| // Remove reasoning from content | ||
| std::string contentWithoutReasoning = tokenizer.decode(std::vector<int64_t>(generatedTokens.begin() + endPos + 1, generatedTokens.end()), ov::genai::skip_special_tokens(true)); // content MUST never appear before reasoning | ||
| parsedOutput.content = contentWithoutReasoning; | ||
| } | ||
|
Collaborator
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. what if there was no reasoning? what will take care of content parsing?
Collaborator
Author
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. probably tool parser will take care of that |
||
| } | ||
| std::optional<rapidjson::Document> Gemma4ReasoningParser::parseChunk(const std::string& chunk, ov::genai::GenerationFinishReason finishReason) { | ||
| if (chunk.empty()) { | ||
| SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Received empty chunk for Gemma4ReasoningParser"); | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| if (chunk.find(getParsingStartTags()[0]) != std::string::npos || chunk.find(getParsingEndTag()) != std::string::npos) { | ||
| 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; | ||
| } | ||
| return std::nullopt; | ||
| } | ||
| } // namespace ovms | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| //***************************************************************************** | ||
| // Copyright 2025 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 <openvino/genai/tokenizer.hpp> | ||
| #include <vector> | ||
| #include <string> | ||
|
|
||
| #include "../qwen3/reasoning_parser.hpp" | ||
|
|
||
| namespace ovms { | ||
| class Gemma4ReasoningParser : public Qwen3ReasoningParser { | ||
| protected: | ||
| const int64_t reasoningTokenId = 100; // <|channel> | ||
| const int64_t reasoningEndTokenId = 101; // <channel|> | ||
|
|
||
| const std::string parsingStartTag = "<|channel>thought\n"; | ||
| const std::string parsingEndTag = "<channel|>"; | ||
|
|
||
| public: | ||
| Gemma4ReasoningParser() = delete; | ||
| explicit Gemma4ReasoningParser(ov::genai::Tokenizer& tokenizer) : | ||
| Qwen3ReasoningParser(tokenizer) {} | ||
| void parse(ParsedOutput& parsedOutput, const std::vector<int64_t>& generatedTokens) override; | ||
|
Collaborator
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. are we missing support for parseChunk? - does it work with streaming?
Collaborator
Author
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. I've added it yet |
||
| std::optional<rapidjson::Document> parseChunk(const std::string& chunk, ov::genai::GenerationFinishReason finishReason) override; | ||
|
|
||
| bool requiresStreamingWithSpecialTokens() const override { | ||
| return true; | ||
| } | ||
|
|
||
| const std::vector<std::string>& getParsingStartTags() const override { | ||
| static const std::vector<std::string> parsingStartTags{this->parsingStartTag}; | ||
| 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 parsingEndTag; | ||
| } | ||
| }; | ||
| } // namespace ovms | ||
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.
ensure these tokens were really the ones you assumed. otherwise we might not detect issues early