-
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
Changes from 14 commits
b799c93
c033f88
17b361f
5eed9ca
9660fc5
eb9d1e6
a2249e4
fa66a41
ad2cb60
cf47ff2
5f02112
cca406b
94597a1
379bc68
5b8e24a
360c5f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
//***************************************************************************** | ||
// 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()) { | ||
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "No content to parse for tool calls"); | ||
return; | ||
} | ||
|
||
std::string decoded = tokenizer.decode(generatedTokens, {ov::genai::skip_special_tokens(false)}); | ||
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. With that line we do double decoding since parsedOutput already contains decoded content. I suppose it's the 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. theres no double decoding anymore, i simply check first token for BOT and treat entire response as JSON as you requested, and it works |
||
|
||
const std::string toolsStartString = getParsingStartTag(); | ||
const std::string toolsStartEnd = getParsingEndTag(); | ||
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. StartEnd? |
||
|
||
size_t toolsStartPos = decoded.find(toolsStartString); | ||
size_t toolsEndPos = decoded.find(toolsStartEnd); | ||
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. Do mistral have a dedicated tools end tag? 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. apparently no - the we saw was EOS |
||
|
||
if (toolsStartPos != std::string::npos && toolsEndPos != std::string::npos) { | ||
std::string remaining = decoded.substr(0, toolsStartPos) + decoded.substr(toolsEndPos + toolsStartEnd.length()); | ||
|
||
size_t toolsStartPos2 = remaining.find(toolsStartString); | ||
size_t toolsEndPos2 = remaining.find(toolsStartEnd); | ||
bool hasMoreSpecialTags = !(toolsStartPos2 == std::string::npos && toolsEndPos2 == std::string::npos); | ||
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. Does it make sense to make such extraction? Did you see real example where there are more special tokens in the generated output? 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. not relevant anymore with new approach |
||
|
||
std::string toolsString = decoded.substr( | ||
toolsStartPos + toolsStartString.length(), | ||
toolsEndPos - toolsStartPos - toolsStartString.length()); | ||
|
||
rapidjson::Document toolsDoc; | ||
toolsDoc.Parse(toolsString.c_str()); | ||
|
||
if (!toolsDoc.HasParseError() && toolsDoc.IsArray() && !hasMoreSpecialTags) { | ||
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. what is going to happen when json is incomplete for example with max_tokens too short? I guess we should still return the tool_call response even if incomplete or invalid. 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. as with other tool parsers - when max tokens is reached and message is not valid JSON, it will not be put into tools, but content |
||
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 = remaining; | ||
} 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//***************************************************************************** | ||
// 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 std::string toolCallStartTag = "[TOOL_CALLS]"; | ||
const std::string toolCallEndTag = "</s>"; | ||
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. Isn't it just a regular EOS token? 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. yes, good catch |
||
|
||
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 { | ||
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 { | ||
return toolCallEndTag; | ||
} | ||
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. Implementation does not match the comment. If the comment is true for mistral, we should return empty string here. 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. done |
||
}; | ||
} // namespace ovms |
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