-
Notifications
You must be signed in to change notification settings - Fork 261
Reasoning and tool parser for LFM2.5 #4307
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
przepeck
wants to merge
35
commits into
main
Choose a base branch
from
przepeck/lfm2_reasoning_parser
base: main
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.
Open
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
52f4d6a
use parser
przepeck 677ca74
adding reasoning parser
przepeck e46fc6c
style
przepeck d1c5c7b
year
przepeck 87f4bd0
fixes
przepeck 628497e
explicit constructor
przepeck df2756d
docs updated~
przepeck 0096992
save
przepeck a06abec
usual reasoning parser approach
przepeck d6f1911
new parser for lfm25, potential solution
przepeck 851ec7a
Merge branch 'main' into przepeck/lfm2_reasoning_parser
przepeck 26ce24f
save
przepeck 600e7e2
save
przepeck 218a008
fix reasoning
przepeck f676fb6
minor fixes
przepeck c6c52de
minor fixes
przepeck e8ee7bb
prams docs
przepeck 9f55fdb
spelling
przepeck f49d53a
styles
przepeck 8b330ed
cpplint
przepeck 4a8b862
build fixes
przepeck f4a2d0c
fixing reasoning in content
przepeck 6bba4ee
styles
przepeck 9292a9a
remove lfm2.5 parser and add changed chat template
przepeck cba7bc6
export model param description
przepeck 6f76889
review changes
przepeck 277cbde
Merge branch 'main' into przepeck/lfm2_reasoning_parser
przepeck 3dc268b
fixing reasoning+content without tool calls, minor fixes
przepeck 77edc5e
Merge branch 'main' into przepeck/lfm2_reasoning_parser
przepeck 043712a
handling missing square brackets
przepeck 4eacbd0
style
przepeck ce4a133
streaming
przepeck 2761269
autodetection of parser for lfm2 and reasoning parser for lfm2.5
przepeck f35a2d6
workaround for preserve thinking for lfm2.5
przepeck eab69cc
windows model download
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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| //***************************************************************************** | ||
| // 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> | ||
|
|
||
| #include "src/port/rapidjson_document.hpp" | ||
|
|
||
| #include "../../../logging.hpp" | ||
| #include "lfm25_reasoning_parser.hpp" | ||
| #include "../utils.hpp" | ||
|
|
||
| namespace ovms { | ||
| void Lfm25ReasoningParser::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() || startReasoningIt >= endReasoningIt) { | ||
| SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Lfm25ReasoningParser: Reasoning start or end token not found in the generated tokens, or in wrong order. 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 = std::distance(generatedTokens.begin(), startReasoningIt); | ||
| auto endPos = std::distance(generatedTokens.begin(), endReasoningIt); | ||
|
|
||
| std::string reasoningContent = tokenizer.decode(std::vector<int64_t>(startPos + generatedTokens.begin() + 1, endPos + generatedTokens.begin()), ov::genai::skip_special_tokens(true)); | ||
|
|
||
| parsedOutput.reasoning = reasoningContent; | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| std::optional<rapidjson::Document> Lfm25ReasoningParser::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 Lfm25ReasoningParser"); | ||
| 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 |
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,54 @@ | ||
| //***************************************************************************** | ||
| // 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 "../base_output_parser.hpp" | ||
| #include <vector> | ||
| #include <string> | ||
|
|
||
| namespace ovms { | ||
| class Lfm25ReasoningParser : public BaseOutputParser { | ||
| protected: | ||
| const std::string parsingStartTag = "<think>"; | ||
| const std::string parsingEndTag = "</think>"; | ||
|
|
||
| const int64_t reasoningStartTokenId = 124901; // <think> | ||
| const int64_t reasoningEndTokenId = 124902; // </think> | ||
|
|
||
| public: | ||
| Lfm25ReasoningParser() = delete; | ||
| explicit Lfm25ReasoningParser(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->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; | ||
| } | ||
|
|
||
| // It may be removed after changing logic in Lfm2ToolParser to use tokens in streaming instead of chunk content, both tool parser and reasoning parser need to have the same value for this function | ||
| 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.
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.
for consistency it might be better to name reasoning and tool parsers with the same name lfm2