Skip to content

Commit 14afa38

Browse files
committed
Style fixes
1 parent a83d122 commit 14afa38

10 files changed

+84
-336
lines changed

src/llm/apis/openai_completions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class OpenAIChatCompletionsHandler {
8686
endpoint(endpoint),
8787
created(creationTime),
8888
tokenizer(tokenizer) {
89-
// FIXME we should delay creating output parser until we have requiest with toolNameSchemaMap parsed
89+
// FIXME we should delay creating output parser until we have request with toolNameSchemaMap parsed
9090
if (!toolParserName.empty() || !reasoningParserName.empty()) {
9191
outputParser = std::make_unique<OutputParser>(tokenizer, toolParserName, reasoningParserName, this->request.toolNameSchemaMap);
9292
}

src/llm/io_processing/output_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ std::optional<rapidjson::Document> OutputParser::parseChunk(const std::string& c
232232
*/
233233

234234
bool reasoningParserExistsAndSupportsStreaming = reasoningParser && !reasoningParser->getParsingStartTag().empty() && !reasoningParser->getParsingEndTag().empty();
235-
bool toolParserExistsAndSupportsStreaming = toolParser && !toolParser->getParsingStartTag().empty(); // FIXME why not check for parsingEntTag not empty?
235+
bool toolParserExistsAndSupportsStreaming = toolParser && !toolParser->getParsingStartTag().empty(); // FIXME why not check for parsingEntTag not empty?
236236
bool applyToolParser = toolParserExistsAndSupportsStreaming && toolsAvailable;
237237

238238
if (applyToolParser && toolParser->isImmediateParsingEnabled() && processingPhase == UNKNOWN) {

src/llm/io_processing/qwen3coder/qwen3coder_tool_parser.cpp

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,13 @@ static std::string setCorrectValueType(std::string& inputValue, const std::strin
269269
}
270270

271271
#define RETURN_NULL_IF_NOT_FOUND(POS) \
272-
if (POS == std::string::npos) { \
273-
return std::nullopt; \
272+
if (POS == std::string::npos) { \
273+
return std::nullopt; \
274274
}
275275

276-
#define LOG_AND_DEFINE_POS_AND_RETURN_NULL_IF_NOT_FOUND(STATE, TAG) \
276+
#define LOG_AND_DEFINE_POS_AND_RETURN_NULL_IF_NOT_FOUND(STATE, TAG) \
277277
SPDLOG_TRACE("State: {}, current content:{} current pos:{}", #STATE, streamContent, this->lastStreamProcessedPosition); \
278-
auto pos = streamContent.find(TAG, this->lastStreamProcessedPosition); \
278+
auto pos = streamContent.find(TAG, this->lastStreamProcessedPosition); \
279279
RETURN_NULL_IF_NOT_FOUND(pos)
280280

281281
std::optional<ToolCalls> Parser::streamStep(const std::string& chunk) {
@@ -285,11 +285,11 @@ std::optional<ToolCalls> Parser::streamStep(const std::string& chunk) {
285285
this->streamContent += chunk;
286286
switch (this->currentState) {
287287
case State::Content: {
288-
LOG_AND_DEFINE_POS_AND_RETURN_NULL_IF_NOT_FOUND(Content, Qwen3CoderToolParser::toolsStartTag);
289-
this->lastStreamProcessedPosition = pos + Qwen3CoderToolParser::toolsStartTag.length();
290-
this->currentState = State::InsideToolCall;
291-
return std::nullopt;
292-
break;
288+
LOG_AND_DEFINE_POS_AND_RETURN_NULL_IF_NOT_FOUND(Content, Qwen3CoderToolParser::toolsStartTag);
289+
this->lastStreamProcessedPosition = pos + Qwen3CoderToolParser::toolsStartTag.length();
290+
this->currentState = State::InsideToolCall;
291+
return std::nullopt;
292+
break;
293293
}
294294
case State::InsideToolCall: {
295295
LOG_AND_DEFINE_POS_AND_RETURN_NULL_IF_NOT_FOUND(InsideToolCall, Qwen3CoderToolParser::toolPrefixTag);
@@ -341,7 +341,7 @@ std::optional<ToolCalls> Parser::streamStep(const std::string& chunk) {
341341
break;
342342
}
343343
case State::AfterParameter: {
344-
/*
344+
/*
345345
SPDLOG_TRACE("State: AfterParameter current content:{} current pos:{}", streamContent, this->lastStreamProcessedPosition);
346346
auto posToolEnd = streamContent.find(Qwen3CoderToolParser::toolEndTag, this->lastStreamProcessedPosition);
347347
auto posNewParameter = streamContent.find(Qwen3CoderToolParser::parameterPrefixTag, this->lastStreamProcessedPosition);
@@ -544,9 +544,11 @@ void Qwen3CoderToolParser::lazyFillInitToolParamatersTypsMap() {
544544
}
545545

546546
Qwen3CoderToolParser::Qwen3CoderToolParser(ov::genai::Tokenizer& tokenizer, const ToolsSchemas_t& toolSchemas) :
547-
BaseOutputParser(tokenizer), toolSchemas(toolSchemas), streamParser(NULL_STRING_CONTENT, this->toolsParametersTypes) {
548-
SPDLOG_DEBUG("Qwen3CoderToolParser created with {} tools", toolsParametersTypes.size());
549-
}
547+
BaseOutputParser(tokenizer),
548+
toolSchemas(toolSchemas),
549+
streamParser(NULL_STRING_CONTENT, this->toolsParametersTypes) {
550+
SPDLOG_DEBUG("Qwen3CoderToolParser created with {} tools", toolsParametersTypes.size());
551+
}
550552

551553
void Qwen3CoderToolParser::parse(ParsedOutput& parsedOutput, const std::vector<int64_t>& generatedTokens) {
552554
// there may be multiple parameters per function, there may be multiple linses per parameter value
@@ -589,7 +591,6 @@ void Qwen3CoderToolParser::parse(ParsedOutput& parsedOutput, const std::vector<i
589591
// then we will send only one delta with all arguments
590592
// we already have toJson functiont that turns all parameters into JSON string
591593

592-
593594
static std::string documentToString(const rapidjson::Document& doc) {
594595
rapidjson::StringBuffer buffer;
595596
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
@@ -630,60 +631,32 @@ std::optional<rapidjson::Document> Qwen3CoderToolParser::parseChunk(const std::s
630631
}
631632
if (toolCallsOpt.has_value()) {
632633
// TODO we assume we could only get one by one
633-
SPDLOG_ERROR("Has value but not insideFunction");
634-
/*
635-
Expected equality of these values:
636-
docStr
637-
Which is: "{\"arguments\":{\"delta\":{\"tool_calls\":[{\"\":0,\"function\":{\"arg1\":\"STRING_VALUE\"}}]}}}"
638-
expected
639-
Which is: "{\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":{\"arg1\":\"STRING_VALUE\"}}}]}}"
640-
*/
641-
auto& toolCalls = toolCallsOpt.value();
642-
if (toolCalls.size() != 1) {
643-
SPDLOG_WARN("Expected one tool call, got: {}", toolCalls.size());
644-
throw 42;
645-
}
646-
if (toolCalls.size() < 1) {
647-
return std::nullopt;
648-
}
649-
auto& toolCall = toolCalls[0];
650-
rapidjson::Document argsDelta;
651-
argsDelta.Parse(toolCall.arguments.c_str());
652-
this->returnedCompleteDeltas.insert(this->streamParser.toolCallIndex);
653-
// we currently have this fail so we need to wrap argsDelta in the arguments fielda
654-
/*
655-
Expected equality of these values:
656-
docStr
657-
Which is: "{\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arg1\":\"STRING_VALUE\"}}]}}"
658-
expected
659-
Which is: "{\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":{\"arg1\":\"STRING_VALUE\"}}}]}}"
660-
*/
661-
rapidjson::Document argumentsWrapper;
634+
SPDLOG_ERROR("Has value but not insideFunction");
635+
auto& toolCalls = toolCallsOpt.value();
636+
if (toolCalls.size() != 1) {
637+
SPDLOG_WARN("Expected one tool call, got: {}", toolCalls.size());
638+
throw 42;
639+
}
640+
if (toolCalls.size() < 1) {
641+
return std::nullopt;
642+
}
643+
auto& toolCall = toolCalls[0];
644+
rapidjson::Document argsDelta;
645+
argsDelta.Parse(toolCall.arguments.c_str());
646+
this->returnedCompleteDeltas.insert(this->streamParser.toolCallIndex);
647+
rapidjson::Document argumentsWrapper;
662648
argumentsWrapper.SetObject();
663649
rapidjson::Document::AllocatorType& allocator = argumentsWrapper.GetAllocator();
664650
// now we need to add string toolCall.arguments to argumentsWrapper under "arguments" key
665651
rapidjson::Value toolCallsString(rapidjson::kStringType);
666652
toolCallsString.SetString(toolCall.arguments.c_str(), allocator);
667653
argumentsWrapper.AddMember("arguments", toolCallsString, allocator);
668654

669-
670-
auto currentDelta = wrapDelta(argumentsWrapper, this->streamParser.toolCallIndex);
671-
// now we need wrap current delta in the required JSON structure
672-
// {"arguments" : currentDelta}
673-
/*
674-
rapidjson::Document returnedDelta;
675-
// create returned delta from scratch
676-
returnedDelta.SetObject();
677-
rapidjson::Document::AllocatorType& allocator = returnedDelta.GetAllocator();
678-
returnedDelta.AddMember("arguments", currentDelta, allocator);
679-
*/
680-
//return returnedDelta;
655+
auto currentDelta = wrapDelta(argumentsWrapper, this->streamParser.toolCallIndex);
681656
SPDLOG_DEBUG("First delta doc: {}", documentToString(currentDelta));
682657
return currentDelta;
683658
}
684-
685659
if (parserState != streamParser.currentState) {
686-
//SPDLOG_DEBUG("Parser state changed from {} to {}", static_cast<int>(parserState), static_cast<int>(streamParser.currentState));
687660
SPDLOG_DEBUG("Parser state changed from {} to {}", parserState, this->streamParser.currentState);
688661
}
689662
parserState = streamParser.currentState;

src/llm/io_processing/qwen3coder/qwen3coder_tool_parser.hpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
#include <openvino/genai/tokenizer.hpp>
1919
#include <optional>
20+
#include <set>
2021
#include <stack>
2122
#include <string>
2223
#include <unordered_set>
24+
#include <unordered_map>
2325
#include <utility>
2426
#include <vector>
2527

@@ -53,7 +55,7 @@ struct Parser {
5355
InsideParameterName, // (IPN) here we expect parameter end tag
5456
InsideParameter, // (IP) here we expect parameter end tag
5557
AfterParameter, // (AP) here we expect either next parameter or function & tools end
56-
AfterFunction, // (AF) here we expect either next parameter or function & tools end FIXME TODO different for streaming?
58+
AfterFunction, // (AF) here we expect either next parameter or function & tools end FIXME TODO different for streaming?
5759
ErrorEnd, // (EE) we reached the end with error
5860
End // (E) we reached the end successfully
5961
// C->ITC->IFN->IF->IPN->IP->AP->(IPN|E)
@@ -80,7 +82,7 @@ struct Parser {
8082
std::optional<ToolCalls> streamStep(const std::string& chunk);
8183
Parser(std::string& content, const ToolsParameterTypeMap_t& toolsParametersTypeMap);
8284
};
83-
static std::string NULL_STRING_CONTENT ="";
85+
static std::string NULL_STRING_CONTENT = "";
8486

8587
class Qwen3CoderToolParser : public BaseOutputParser {
8688
public:
@@ -91,9 +93,10 @@ class Qwen3CoderToolParser : public BaseOutputParser {
9193
static const std::string parameterPrefixTag;
9294
static const std::string parameterEndTag;
9395
static const std::string tagEnd;
96+
9497
private:
95-
const ToolsSchemas_t& toolSchemas; // we need to keep reference as this is not filled in OpenAIApiHandler during ToolParser creation, NOTE that its const here but it can change outside
96-
ToolsParameterTypeMap_t toolsParametersTypes; // FIXME do it once per request
98+
const ToolsSchemas_t& toolSchemas; // we need to keep reference as this is not filled in OpenAIApiHandler during ToolParser creation, NOTE that its const here but it can change outside
99+
ToolsParameterTypeMap_t toolsParametersTypes; // FIXME do it once per request
97100
bool filledParametersTypesMap{false};
98101
// streaming
99102
Parser streamParser;
@@ -120,30 +123,31 @@ class Qwen3CoderToolParser : public BaseOutputParser {
120123
const std::string& getParsingEndTag() const override {
121124
return toolsEndTag; // FIXME CHECK
122125
}
126+
123127
private:
124-
void lazyFillInitToolParamatersTypsMap();
128+
void lazyFillInitToolParamatersTypsMap();
125129
};
126130
} // namespace ovms
127-
template<>
131+
template <>
128132
struct fmt::formatter<ovms::Parser::State> : fmt::formatter<std::string> {
129-
auto format(const ovms::Parser::State& state, fmt::format_context& ctx) const {
133+
auto format(const ovms::Parser::State& state, fmt::format_context& ctx) const {
130134
// use unordered_map
131-
std::unordered_map<ovms::Parser::State, std::string> stateMap = {
132-
{ovms::Parser::State::Content, "Content"},
133-
{ovms::Parser::State::InsideToolCall, "InsideToolCall"},
134-
{ovms::Parser::State::InsideFunctionName, "InsideFunctionName"},
135-
{ovms::Parser::State::InsideFunction, "InsideFunction"},
136-
{ovms::Parser::State::InsideParameterName, "InsideParameterName"},
137-
{ovms::Parser::State::InsideParameter, "InsideParameter"},
138-
{ovms::Parser::State::AfterParameter, "AfterParameter"},
139-
{ovms::Parser::State::ErrorEnd, "ErrorEnd"},
140-
{ovms::Parser::State::End, "End"},
141-
};
142-
auto it = stateMap.find(state);
143-
if (it != stateMap.end()) {
144-
return fmt::formatter<std::string>::format(it->second, ctx);
145-
} else {
146-
return fmt::formatter<std::string>::format("Unknown", ctx);
147-
}
135+
std::unordered_map<ovms::Parser::State, std::string> stateMap = {
136+
{ovms::Parser::State::Content, "Content"},
137+
{ovms::Parser::State::InsideToolCall, "InsideToolCall"},
138+
{ovms::Parser::State::InsideFunctionName, "InsideFunctionName"},
139+
{ovms::Parser::State::InsideFunction, "InsideFunction"},
140+
{ovms::Parser::State::InsideParameterName, "InsideParameterName"},
141+
{ovms::Parser::State::InsideParameter, "InsideParameter"},
142+
{ovms::Parser::State::AfterParameter, "AfterParameter"},
143+
{ovms::Parser::State::ErrorEnd, "ErrorEnd"},
144+
{ovms::Parser::State::End, "End"},
145+
};
146+
auto it = stateMap.find(state);
147+
if (it != stateMap.end()) {
148+
return fmt::formatter<std::string>::format(it->second, ctx);
149+
} else {
150+
return fmt::formatter<std::string>::format("Unknown", ctx);
151+
}
148152
}
149153
};

src/test/llm/output_parsers/hermes3_output_parser_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const std::string tokenizerPath = "/ovms/src/test/llm_testing/NousResearch/Herme
3232
#endif
3333

3434
static std::unique_ptr<ov::genai::Tokenizer> hermes3Tokenizer;
35-
static const ToolsSchemas_t& EMPTY_TOOLS_SCHEMA = {}; // not used in hermes3
35+
static const ToolsSchemas_t& EMPTY_TOOLS_SCHEMA = {}; // not used in hermes3
3636

3737
class Hermes3OutputParserTest : public ::testing::Test {
3838
protected:

src/test/llm/output_parsers/llama3_output_parser_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const std::string tokenizerPath = getWindowsRepoRootPath() + "\\src\\test\\llm_t
3131
const std::string tokenizerPath = "/ovms/src/test/llm_testing/meta-llama/Llama-3.1-8B-Instruct";
3232
#endif
3333

34-
static const ovms::ToolsSchemas_t EMPTY_TOOL_SCHEMA = {}; // not used for llama3
34+
static const ovms::ToolsSchemas_t EMPTY_TOOL_SCHEMA = {}; // not used for llama3
3535
static std::unique_ptr<ov::genai::Tokenizer> llama3Tokenizer;
3636

3737
// Id of the <|python_tag|> which is a special token used to indicate the start of a tool calls

src/test/llm/output_parsers/mistral_output_parser_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const std::string tokenizerPath = getWindowsRepoRootPath() + "\\src\\test\\llm_t
3131
const std::string tokenizerPath = "/ovms/src/test/llm_testing/mistralai/Mistral-7B-Instruct-v0.3/";
3232
#endif
3333

34-
static ovms::ToolsSchemas_t EMPTY_TOOLS_SCHEMA = {}; // not used for mistral
34+
static ovms::ToolsSchemas_t EMPTY_TOOLS_SCHEMA = {}; // not used for mistral
3535
static std::unique_ptr<ov::genai::Tokenizer> mistralTokenizer;
3636

3737
class MistralOutputParserTest : public ::testing::Test {

src/test/llm/output_parsers/phi4_output_parser_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const std::string tokenizerPath = getWindowsRepoRootPath() + "\\src\\test\\llm_t
3131
const std::string tokenizerPath = "/ovms/src/test/llm_testing/microsoft/Phi-4-mini-instruct";
3232
#endif
3333

34-
static ovms::ToolsSchemas_t EMPTY_TOOLS_SCHEMA = {}; // not used for phi4
34+
static ovms::ToolsSchemas_t EMPTY_TOOLS_SCHEMA = {}; // not used for phi4
3535
static std::unique_ptr<ov::genai::Tokenizer> phi4Tokenizer;
3636

3737
class Phi4OutputParserTest : public ::testing::Test {

src/test/llm/output_parsers/qwen3_output_parser_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const std::string tokenizerPath = getWindowsRepoRootPath() + "\\src\\test\\llm_t
3131
const std::string tokenizerPath = "/ovms/src/test/llm_testing/Qwen/Qwen3-8B";
3232
#endif
3333

34-
static ovms::ToolsSchemas_t EMPTY_TOOLS_SCHEMA = {}; // not used for Qwen3
34+
static ovms::ToolsSchemas_t EMPTY_TOOLS_SCHEMA = {}; // not used for Qwen3
3535
static std::unique_ptr<ov::genai::Tokenizer> qwen3Tokenizer;
3636

3737
class Qwen3OutputParserTest : public ::testing::Test {

0 commit comments

Comments
 (0)