-
Notifications
You must be signed in to change notification settings - Fork 288
Add parsing #2772
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
Draft
pavel-esir
wants to merge
6
commits into
openvinotoolkit:master
Choose a base branch
from
pavel-esir:add_parsing
base: master
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.
+1,013
−16
Draft
Add parsing #2772
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4b357ff
tests are green
pavel-esir 525a4d8
python api added
pavel-esir 3ab9757
refactored
pavel-esir 5624fc2
add parsing at the end of generate()
pavel-esir 3407d8f
hide map with predefined initialized parsers; add TODOs
pavel-esir 219827a
use JsonContainer
pavel-esir 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (C) 2023-2025 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "openvino/genai/llm_pipeline.hpp" | ||
#include "openvino/genai/parsers.hpp" | ||
#include "openvino/genai/text_streamer.hpp" | ||
|
||
|
||
class CurrentStreamer : public ov::genai::TextParserStreamer { | ||
private: | ||
public: | ||
CurrentStreamer(const ov::genai::Tokenizer& tokenizer) | ||
: ov::genai::TextParserStreamer(tokenizer) {} | ||
ov::genai::StreamingStatus write(ov::genai::ParsedMessage& message) { | ||
std::cout << message["content"].get_string() << std::flush; | ||
return ov::genai::StreamingStatus::RUNNING; | ||
} | ||
}; | ||
|
||
|
||
int main(int argc, char* argv[]) try { | ||
if (argc < 2 || argc > 3) { | ||
throw std::runtime_error(std::string{"Usage: "} + argv[0] + " <MODEL_DIR> <DEVICE>"); | ||
} | ||
// std::string prompt = "<|begin▁of▁sentence|><|User|>Please think of a dificcult task to solve x**2 + y**2 = 1<|Assistant|><think>"; | ||
std::string prompt = "<|begin▁of▁sentence|><|User|>Why is the Sky blue?<|Assistant|><think>"; | ||
std::string models_path = argv[1]; | ||
|
||
// Default device is CPU; can be overridden by the second argument | ||
std::string device = (argc == 3) ? argv[2] : "CPU"; // GPU, NPU can be used as well | ||
ov::genai::LLMPipeline pipe(models_path, device); | ||
|
||
ov::genai::GenerationConfig config; | ||
config.max_new_tokens = 1000; | ||
|
||
auto tok = pipe.get_tokenizer(); | ||
std::shared_ptr<CurrentStreamer> streamer = std::make_shared<CurrentStreamer>(tok); | ||
|
||
pipe.generate(prompt, config, streamer); | ||
|
||
|
||
} catch (const std::exception& error) { | ||
try { | ||
std::cerr << error.what() << '\n'; | ||
} catch (const std::ios_base::failure&) {} | ||
return EXIT_FAILURE; | ||
} catch (...) { | ||
try { | ||
std::cerr << "Non-exception object thrown\n"; | ||
} catch (const std::ios_base::failure&) {} | ||
return EXIT_FAILURE; | ||
} |
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,111 @@ | ||
// Copyright (C) 2023-2025 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
#include <string> | ||
#include <memory> | ||
#include <variant> | ||
#include <map> | ||
#include <functional> | ||
#include <optional> | ||
#include <vector> | ||
#include "openvino/genai/json_container.hpp" | ||
|
||
namespace ov { | ||
namespace genai { | ||
|
||
// TODO: will be converted to JSONLike object | ||
// using ParsedMessage = std::map<std::string, std::string>; | ||
using ParsedMessage = JsonContainer; | ||
|
||
class IncrementalParserBase { | ||
public: | ||
IncrementalParserBase() = default; | ||
|
||
// We return string which with filtered text to be added to content. | ||
virtual std::string parse( | ||
ParsedMessage& msg, | ||
const std::string& previous_text, | ||
std::string& delta_text, | ||
const std::optional<std::vector<int64_t>>& previous_tokens = std::nullopt, | ||
const std::optional<std::vector<int64_t>>& delta_tokens = std::nullopt | ||
) = 0; | ||
|
||
virtual bool is_active() const = 0; | ||
static std::shared_ptr<IncrementalParserBase> get_parser(std::string name); | ||
}; | ||
|
||
// Forward declaration | ||
class ReasoningParserImpl; | ||
|
||
class ReasoningParser : public IncrementalParserBase { | ||
private: | ||
std::shared_ptr<ReasoningParserImpl> m_impl; | ||
public: | ||
ReasoningParser(bool starts_with_thinking = true, | ||
bool keep_original_content = true); | ||
|
||
std::string parse( | ||
ParsedMessage& msg, | ||
const std::string& previous_text, | ||
std::string& delta_text, | ||
const std::optional<std::vector<int64_t>>& previous_tokens = std::nullopt, | ||
const std::optional<std::vector<int64_t>>& delta_tokens = std::nullopt | ||
) override; | ||
bool is_active() const override; | ||
}; | ||
|
||
class DeepSeekR1ReasoningParser : public ReasoningParser { | ||
public: | ||
DeepSeekR1ReasoningParser(bool starts_with_thinking = true) : ReasoningParser(starts_with_thinking) {}; | ||
static std::string name() { return "DeepSeekR1ReasoningParser"; } | ||
}; | ||
|
||
class Phi4ReasoningParser : public ReasoningParser { | ||
public: | ||
Phi4ReasoningParser(bool starts_with_thinking = false) : ReasoningParser(starts_with_thinking) {}; | ||
static std::string name() { return "Phi4ReasoningParser"; } | ||
}; | ||
|
||
class ParserBase { | ||
public: | ||
ParserBase() = default; | ||
|
||
virtual ParsedMessage parse(ParsedMessage& text) = 0; | ||
static std::shared_ptr<ParserBase> get_parser(std::string name); | ||
}; | ||
|
||
using ParserVariant = std::variant<std::shared_ptr<IncrementalParserBase>, std::string>; | ||
pavel-esir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class Llama32PythonicToolParser : public ParserBase { | ||
// Does not modify original content, only extracts and adds tool calls | ||
public: | ||
// TODO: Check that vLLM has the same default. | ||
Llama32PythonicToolParser(bool keep_original_content = true) : m_keep_original_content(keep_original_content) {} | ||
|
||
ParsedMessage parse(ParsedMessage& input) override; | ||
static std::string name() { return "Llama32PythonicToolParser"; } | ||
private: | ||
bool m_keep_original_content = true; | ||
}; | ||
|
||
class BaseReasoningParser : public ParserBase{ | ||
public: | ||
BaseReasoningParser(bool expect_open_tag = true, bool keep_original_content = true, std::string open_tag = "<think>", std::string close_tag = "</think>") : | ||
m_expect_open_tag(expect_open_tag), | ||
m_keep_original_content(keep_original_content), | ||
m_open_tag(open_tag), | ||
m_close_tag(close_tag) {} | ||
|
||
ParsedMessage parse(ParsedMessage& input) override; | ||
|
||
private: | ||
bool m_expect_open_tag = true; | ||
bool m_keep_original_content = true; | ||
std::string m_open_tag = "<think>"; | ||
std::string m_close_tag = "</think>"; | ||
}; | ||
|
||
|
||
} // namespace genai | ||
} // namespace ov |
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
Oops, something went wrong.
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.
The
ParsingState
type is used in a using declaration but is not defined in any of the included headers. This will result in a compilation error.Copilot uses AI. Check for mistakes.