-
Notifications
You must be signed in to change notification settings - Fork 218
Mistral tool calling unary #3567
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b799c93
save
dkalinowski c033f88
Merge remote-tracking branch 'origin/main' into mistral
dkalinowski 17b361f
mistral
dkalinowski 5eed9ca
save
dkalinowski 9660fc5
save
dkalinowski eb9d1e6
save
dkalinowski a2249e4
save
dkalinowski fa66a41
save
dkalinowski ad2cb60
unit test
dkalinowski cf47ff2
Merge remote-tracking branch 'origin/main' into mistral
dkalinowski 5f02112
save
dkalinowski cca406b
skip special tokens false
dkalinowski 94597a1
save
dkalinowski 379bc68
save
dkalinowski 5b8e24a
Milosz
dkalinowski 360c5f3
prepare mistral tokenizer for unit tests on windows
dkalinowski 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
out | ||
demos/continuous_batching | ||
demos/embeddings | ||
demos/common/export_models/models |
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,88 @@ | ||
//***************************************************************************** | ||
// 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 <regex> | ||
|
||
#pragma warning(push) | ||
#pragma warning(disable : 6313) | ||
#include <rapidjson/document.h> | ||
#include <rapidjson/stringbuffer.h> | ||
#include <rapidjson/writer.h> | ||
#pragma warning(pop) | ||
|
||
#include "../../../logging.hpp" | ||
#include "tool_parser.hpp" | ||
#include "../utils.hpp" | ||
|
||
namespace ovms { | ||
|
||
void MistralToolParser::parse(ParsedOutput& parsedOutput, const std::vector<int64_t>& generatedTokens) { | ||
std::vector<std::string> tools; | ||
|
||
if (parsedOutput.content.empty() || generatedTokens.size() <= 0) { | ||
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "No content to parse for tool calls"); | ||
return; | ||
} | ||
|
||
if (generatedTokens[0] != this->botTokenId) { | ||
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Failed to parse functools content or extract tools array"); | ||
return; | ||
} | ||
|
||
rapidjson::Document toolsDoc; | ||
toolsDoc.Parse(parsedOutput.content.c_str()); | ||
|
||
if (!toolsDoc.HasParseError() && toolsDoc.IsArray()) { | ||
for (auto& toolVal : toolsDoc.GetArray()) { | ||
if (!toolVal.IsObject()) { | ||
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Tool call is not a valid JSON object"); | ||
continue; | ||
} | ||
ToolCall toolCall; | ||
if (toolVal.HasMember("name") && toolVal["name"].IsString()) { | ||
toolCall.name = toolVal["name"].GetString(); | ||
} else { | ||
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Tool call does not contain valid name field"); | ||
continue; | ||
} | ||
|
||
if (toolVal.HasMember("arguments") && toolVal["arguments"].IsObject()) { | ||
rapidjson::StringBuffer sb; | ||
rapidjson::Writer<rapidjson::StringBuffer> toolWriter(sb); | ||
toolVal["arguments"].Accept(toolWriter); | ||
toolCall.arguments = sb.GetString(); | ||
} else { | ||
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Tool call does not contain valid parameters object"); | ||
continue; | ||
} | ||
toolCall.id = generateRandomId(); // Generate a random ID for the tool call | ||
parsedOutput.toolCalls.push_back(toolCall); | ||
} | ||
parsedOutput.content.clear(); | ||
} else { | ||
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Failed to parse functools content or extract tools array"); | ||
} | ||
} | ||
|
||
std::optional<rapidjson::Document> MistralToolParser::parseChunk(const std::string& chunk) { | ||
// Not implemented | ||
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "MistralToolParser::parseChunk is not implemented"); | ||
return std::nullopt; | ||
} | ||
} // 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,53 @@ | ||
//***************************************************************************** | ||
// 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 <string> | ||
#include <optional> | ||
#include <vector> | ||
|
||
#pragma warning(push) | ||
#pragma warning(disable : 6313) | ||
#include <rapidjson/document.h> | ||
#include <rapidjson/stringbuffer.h> | ||
#include <rapidjson/writer.h> | ||
#pragma warning(pop) | ||
|
||
#include "../base_output_parser.hpp" | ||
|
||
namespace ovms { | ||
class MistralToolParser : public BaseOutputParser { | ||
const int64_t botTokenId = 5; // [TOOL_CALLS] | ||
|
||
public: | ||
MistralToolParser() = delete; | ||
explicit MistralToolParser(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) override; | ||
const std::string& getParsingStartTag() const override { | ||
static const std::string toolCallStartTag = "[TOOL_CALLS]"; | ||
return toolCallStartTag; | ||
} | ||
// Tools calls are expected to be the last part of the content, so we do not specify an end tag. | ||
const std::string& getParsingEndTag() const override { | ||
static const std::string toolCallEndTag = ""; | ||
return toolCallEndTag; | ||
} | ||
}; | ||
} // 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
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.
unrelated, but qwen3 should not be here
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.
this is already being changed in your PR, right? im rebased