-
Notifications
You must be signed in to change notification settings - Fork 264
Input processor for removing empty toolcall list from input #4383
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
5
commits into
main
Choose a base branch
from
przepeck/empty_toolcalls_rm
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
5 commits
Select commit
Hold shift + click to select a range
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
42 changes: 42 additions & 0 deletions
42
src/llm/io_processing/input_processors/empty_tool_calls_array_removing_processor.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,42 @@ | ||
| //***************************************************************************** | ||
| // 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 "empty_tool_calls_array_removing_processor.hpp" | ||
|
|
||
| #include <variant> | ||
|
|
||
| namespace ovms { | ||
|
|
||
| absl::Status EmptyToolCallsArrayRemovingProcessor::process(InputRequest& req) { | ||
| if (!std::holds_alternative<ov::genai::ChatHistory>(req.input)) { | ||
| return absl::Status(absl::StatusCode::kInternal, | ||
| "EmptyToolCallsArrayRemovingProcessor received input that is not a ChatHistory"); | ||
| } | ||
| ov::genai::ChatHistory& chatHistory = std::get<ov::genai::ChatHistory>(req.input); | ||
| for (size_t i = 0; i < chatHistory.size(); i++) { | ||
| if (!chatHistory[i].contains("tool_calls")) { | ||
| continue; | ||
| } | ||
|
|
||
| const auto toolCalls = chatHistory[i]["tool_calls"]; | ||
| if (toolCalls.is_array() && toolCalls.size() == 0) { | ||
| chatHistory[i].erase("tool_calls"); | ||
| } | ||
| } | ||
| return absl::OkStatus(); | ||
| } | ||
|
|
||
| } // namespace ovms |
30 changes: 30 additions & 0 deletions
30
src/llm/io_processing/input_processors/empty_tool_calls_array_removing_processor.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,30 @@ | ||
| //***************************************************************************** | ||
| // 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_input_processor.hpp" | ||
|
|
||
| namespace ovms { | ||
|
|
||
| // Removes empty tool_calls arrays ("tool_calls": []) from ChatHistory messages. | ||
| // Runs for all chat paths (LM and VLM) and must execute before ChatTemplateProcessor | ||
| // so downstream processors and chat templates do not render an empty tool_calls list. | ||
| class EmptyToolCallsArrayRemovingProcessor : public BaseInputProcessor { | ||
| public: | ||
| absl::Status process(InputRequest& req) override; | ||
| }; | ||
|
|
||
| } // namespace ovms |
73 changes: 73 additions & 0 deletions
73
src/test/llm/input_processing/empty_tool_calls_array_removing_processor_test.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,73 @@ | ||
| //***************************************************************************** | ||
| // 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 <string> | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <openvino/genai/chat_history.hpp> | ||
|
|
||
| #include "../../../llm/io_processing/input_processors/empty_tool_calls_array_removing_processor.hpp" | ||
| #include "../../../llm/io_processing/input_request.hpp" | ||
|
|
||
| using namespace ovms; | ||
|
|
||
| // Helpers ---------------------------------------------------------------- | ||
|
|
||
| static InputRequest makeChatRequest(ov::genai::ChatHistory chatHistory) { | ||
| InputRequest req; | ||
| req.input = std::move(chatHistory); | ||
| return req; | ||
| } | ||
|
|
||
| // Tests ------------------------------------------------------------------ | ||
|
|
||
| TEST(EmptyToolCallsArrayRemovingProcessorTest, EmptyToolCallsArrayRemoved) { | ||
| ov::genai::ChatHistory history; | ||
| ov::AnyMap msg = {{"role", std::string("user")}}; | ||
| msg["content"] = ov::genai::JsonContainer::from_json_string("\"What is the weather in Szczecin?\""); | ||
| msg["tool_calls"] = ov::genai::JsonContainer::from_json_string("[]"); | ||
| history.push_back(msg); | ||
|
|
||
| InputRequest req = makeChatRequest(history); | ||
| EmptyToolCallsArrayRemovingProcessor processor; | ||
| const auto status = processor.process(req); | ||
|
|
||
| EXPECT_TRUE(status.ok()); | ||
| const auto& result = std::get<ov::genai::ChatHistory>(req.input); | ||
| EXPECT_FALSE(result[0].contains("tool_calls")); | ||
| EXPECT_TRUE(result[0]["content"].is_string()); | ||
| EXPECT_EQ(result[0]["content"].as_string().value_or(""), "What is the weather in Szczecin?"); | ||
| } | ||
|
|
||
| TEST(EmptyToolCallsArrayRemovingProcessorTest, NonEmptyArrayPreserved) { | ||
| ov::genai::ChatHistory history; | ||
| ov::AnyMap msg = {{"role", std::string("assistant")}}; | ||
| msg["tool_calls"] = ov::genai::JsonContainer::from_json_string( | ||
| R"([{"name":"get_weather","parameters":{"city":"Szczecin"}}])"); | ||
|
|
||
| history.push_back(msg); | ||
|
|
||
| InputRequest req = makeChatRequest(history); | ||
| EmptyToolCallsArrayRemovingProcessor processor; | ||
| const auto status = processor.process(req); | ||
|
|
||
| EXPECT_TRUE(status.ok()); | ||
| const auto& result = std::get<ov::genai::ChatHistory>(req.input); | ||
| ASSERT_TRUE(result[0]["tool_calls"].is_array()); | ||
| EXPECT_EQ(result[0]["tool_calls"].size(), 1u); | ||
| EXPECT_EQ(result[0]["tool_calls"][0]["name"].as_string().value_or(""), "get_weather"); | ||
| ASSERT_TRUE(result[0]["tool_calls"][0].contains("parameters")); | ||
| EXPECT_EQ(result[0]["tool_calls"][0]["parameters"]["city"].as_string().value_or(""), "Szczecin"); | ||
| } | ||
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.