Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/llm/io_processing/mistral/tool_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ void MistralToolParser::parse(ParsedOutput& parsedOutput, const std::vector<int6
// In regular parsing, the parser will consume entire model output only if the first generated token is the beginning of tools token.
// In immediate parsing, the parser will consume entire model output regardless of the first token.
if (generatedTokens[0] != this->botTokenId && !immediateParsingEnabled) {
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Begin of tools token has not been found in the model output. Exiting parser.");
return;
if (parsedOutput.content.size() >= 2 && parsedOutput.content[0] == '[' && parsedOutput.content[1] == '{') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we rely on generatedTokens? We already base on checking it in condition above, so why not keep it continuous?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think its good idea, [{ is regular text (non special token)
Do you want me to handle all cases where:

  • [ and { are separate tokens? x, y
  • [{ is glued together into 1 token? x

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, in that case I think it's fine.

SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Model output starts with '[{' but begin of tools token is missing. Proceeding with parsing.");
} else {
SPDLOG_LOGGER_DEBUG(llm_calculator_logger, "Begin of tools token or '[{' has not been found in the model output. Exiting parser.");
return;
}
}

rapidjson::Document toolsDoc;
Expand Down
13 changes: 13 additions & 0 deletions src/test/llm/output_parsers/mistral_output_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ TEST_F(MistralOutputParserTest, ParseToolCallOutputWithSingleToolCall) {
}
}

TEST_F(MistralOutputParserTest, ParseToolCallOutputWithSingleToolCall_MissingToolCallStartTag) {
std::string testInput = "[{\"name\": \"example_tool\", \"arguments\": {\"arg1\": \"value1\", \"arg2\": 42}}]</s>";
auto generatedTensor = mistralTokenizer->encode(testInput, ov::genai::add_special_tokens(false)).input_ids;
std::vector<int64_t> generatedTokens(generatedTensor.data<int64_t>(), generatedTensor.data<int64_t>() + generatedTensor.get_size());
ParsedOutput parsedOutput = outputParserWithRegularToolParsing->parse(generatedTokens, true);
EXPECT_EQ(parsedOutput.content, "");
EXPECT_EQ(parsedOutput.reasoning, "");
ASSERT_EQ(parsedOutput.toolCalls.size(), 1);
EXPECT_EQ(parsedOutput.toolCalls[0].name, "example_tool");
EXPECT_EQ(parsedOutput.toolCalls[0].arguments, "{\"arg1\":\"value1\",\"arg2\":42}");
EXPECT_EQ(parsedOutput.toolCalls[0].id.empty(), false);
}

TEST_F(MistralOutputParserTest, ParseToolCallOutputWithThreeToolCalls) {
std::string input = "[TOOL_CALLS][{\"name\": \"example_tool\", \"arguments\": {\"arg1\": \"value1\", \"arg2\": 42}},"
"{\"name\": \"another_tool\", \"arguments\": {\"param1\": \"data\", \"param2\": true}},"
Expand Down