-
Notifications
You must be signed in to change notification settings - Fork 259
GenAI servables - refactor input processing #4318
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
mzegla
wants to merge
7
commits into
main
Choose a base branch
from
servables_input_flow_refactor_1
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
7 commits
Select commit
Hold shift + click to select a range
f5b6037
init
mzegla 8ae413b
copilot comments
mzegla a811b0e
drop modelsPath
mzegla 5b2f535
bring back non const tokenizer
mzegla b8ac775
minor suggestions
mzegla 8e56150
openai handler tests cleanup
mzegla 46b790f
copilot
mzegla 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| #include "../../logging.hpp" | ||
| #include "../../profiler.hpp" | ||
| #include "../../filesystem/filesystem.hpp" | ||
| #include "../io_processing/generation_config_builder.hpp" | ||
| #pragma warning(push) | ||
| #pragma warning(disable : 6001 4324 6385 6386) | ||
| #include "absl/strings/escaping.h" | ||
|
|
@@ -286,7 +287,7 @@ absl::Status OpenAIApiHandler::parseResponseFormat() { | |
|
|
||
| // --- Shared parsing methods --- | ||
|
|
||
| absl::Status OpenAIApiHandler::ensureArgumentsInToolCalls(Value& messageObj, bool& jsonChanged) { | ||
| absl::Status OpenAIApiHandler::ensureArgumentsInToolCalls(Value& messageObj) { | ||
| auto& allocator = doc.GetAllocator(); | ||
| auto toolCallsIt = messageObj.FindMember("tool_calls"); | ||
| if (toolCallsIt != messageObj.MemberEnd() && toolCallsIt->value.IsArray()) { | ||
|
|
@@ -307,7 +308,6 @@ absl::Status OpenAIApiHandler::ensureArgumentsInToolCalls(Value& messageObj, boo | |
| rapidjson::Value argumentsValue; | ||
| argumentsValue.SetString("{}", allocator); | ||
| functionIt->value.GetObject().AddMember(argumentsKey, argumentsValue, allocator); | ||
| jsonChanged = true; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -348,11 +348,9 @@ absl::Status OpenAIApiHandler::parseTools() { | |
| return absl::InvalidArgumentError("tool_choice is not a valid JSON object or string"); | ||
| } | ||
| } | ||
| bool jsonChanged = false; | ||
| if (toolChoice == "none") { | ||
| // remove tools from the request | ||
| doc.RemoveMember("tools"); | ||
| jsonChanged = true; | ||
| } | ||
| auto it = doc.FindMember("tools"); | ||
| if (it != doc.MemberEnd() && !it->value.IsNull()) { | ||
|
|
@@ -405,7 +403,6 @@ absl::Status OpenAIApiHandler::parseTools() { | |
| // If toolChoice is set to a specific function name, we keep only that tool | ||
| if (toolChoice != "auto" && toolChoice != "required" && toolChoice != functionName) { | ||
| it->value.Erase(&obj); | ||
| jsonChanged = true; | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -430,12 +427,6 @@ absl::Status OpenAIApiHandler::parseTools() { | |
| } | ||
|
|
||
| request.toolChoice = toolChoice; | ||
| if (jsonChanged) { | ||
| StringBuffer buffer; | ||
| Writer<StringBuffer> writer(buffer); | ||
| doc.Accept(writer); | ||
| request.processedJson = buffer.GetString(); | ||
| } | ||
| return absl::OkStatus(); | ||
| } | ||
|
|
||
|
|
@@ -492,18 +483,48 @@ const OpenAIRequest& OpenAIApiHandler::getRequest() const { | |
| return request; | ||
| } | ||
|
|
||
| const std::string& OpenAIApiHandler::getProcessedJson() const { | ||
| return request.processedJson; | ||
| } | ||
|
|
||
| const ImageHistory& OpenAIApiHandler::getImageHistory() const { | ||
| return request.imageHistory; | ||
| } | ||
|
|
||
| ov::genai::ChatHistory& OpenAIApiHandler::getChatHistory() { | ||
| return request.chatHistory; | ||
| } | ||
|
|
||
| absl::StatusOr<InputRequest> OpenAIApiHandler::extractInputRequest(GenerationConfigBuilder& configBuilder) { | ||
| configBuilder.parseConfigFromRequest(request); | ||
| configBuilder.adjustConfigForDecodingMethod(); | ||
| try { | ||
| configBuilder.validateStructuredOutputConfig(tokenizer); | ||
| } catch (const std::exception& e) { | ||
| SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Tool guided generation will not be applied due to JSON schema validation failure: {}", e.what()); | ||
| configBuilder.unsetStructuredOutputConfig(); | ||
| } | ||
| InputRequest req; | ||
| req.generationConfig = configBuilder.getConfig(); | ||
| if (endpoint == Endpoint::COMPLETIONS) { | ||
| req.input = request.prompt.value_or(""); | ||
| } else { | ||
| // CHAT_COMPLETIONS and RESPONSES both use ChatHistory. | ||
| // Copied (not moved) so the handler retains its own copy for response serialization. | ||
| req.input = request.chatHistory; | ||
| // Populate tools and chat_template_kwargs on the copied ChatHistory so | ||
| // ChatTemplateProcessor can access them via get_tools()/get_extra_context(). | ||
| auto& chatHistory = std::get<ov::genai::ChatHistory>(req.input); | ||
|
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. it can throw an exception if req.input is not of type ChatHistory. can we try catch that and return error if it happens? |
||
| auto toolsResult = parseToolsToJsonContainer(); | ||
| if (!toolsResult.ok()) { | ||
| return toolsResult.status(); | ||
| } | ||
| if (toolsResult.value().has_value()) { | ||
| chatHistory.set_tools(toolsResult.value().value()); | ||
| } | ||
| auto kwargsResult = parseChatTemplateKwargsToJsonContainer(); | ||
| if (!kwargsResult.ok()) { | ||
| return kwargsResult.status(); | ||
| } | ||
| if (kwargsResult.value().has_value()) { | ||
| chatHistory.set_extra_context(kwargsResult.value().value()); | ||
| } | ||
| } | ||
|
mzegla marked this conversation as resolved.
|
||
| return req; | ||
| } | ||
|
|
||
| std::optional<int> OpenAIApiHandler::getMaxTokens() const { | ||
| return request.maxTokens; | ||
| } | ||
|
|
||
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.
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.
whats this warning?