Skip to content
Open
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
38 changes: 37 additions & 1 deletion src/test/tokenize_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ TEST(TokenizeDeserialization, invalidTokenizeMaxLengthType) {
auto status = ovms::TokenizeParser::parseTokenizeRequest(d, request);
ASSERT_NE(status, absl::OkStatus());
auto error = status.message();
ASSERT_EQ(error, "max_length should be integer");
ASSERT_EQ(error, "max_length should be unsigned integer");
}

TEST(TokenizeDeserialization, invalidTokenizePadToMaxLengthType) {
Expand Down Expand Up @@ -228,3 +228,39 @@ TEST(TokenizeDeserialization, invalidTokenizePaddingSideValue) {
auto error = status.message();
ASSERT_EQ(error, "padding_side should be either left or right");
}

TEST(TokenizeDeserialization, invalidTokenizeMaxLengthNegative) {
std::string requestBody = R"(
{
"model": "embeddings",
"text": ["one", "two", "three"],
"max_length": -10
}
)";
rapidjson::Document d;
rapidjson::ParseResult ok = d.Parse(requestBody.c_str());
ovms::TokenizeRequest request;
ASSERT_EQ(ok.Code(), 0);
auto status = ovms::TokenizeParser::parseTokenizeRequest(d, request);
ASSERT_NE(status, absl::OkStatus());
auto error = status.message();
ASSERT_EQ(error, "max_length should be unsigned integer");
}

TEST(TokenizeDeserialization, invalidTokenizeMaxLengthZero) {
std::string requestBody = R"(
{
"model": "embeddings",
"text": ["one", "two", "three"],
"max_length": 0
}
)";
rapidjson::Document d;
rapidjson::ParseResult ok = d.Parse(requestBody.c_str());
ovms::TokenizeRequest request;
ASSERT_EQ(ok.Code(), 0);
auto status = ovms::TokenizeParser::parseTokenizeRequest(d, request);
ASSERT_NE(status, absl::OkStatus());
auto error = status.message();
ASSERT_EQ(error, "max_length should be greater than 0");
}
10 changes: 7 additions & 3 deletions src/tokenize/tokenize_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@ std::variant<TokenizeRequest, std::string> TokenizeParser::validateTokenizeReque

auto it = parsedJson.FindMember("max_length");
if (it != parsedJson.MemberEnd()) {
if (it->value.IsInt()) {
size_t max_length = it->value.GetInt();
if (it->value.IsUint()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

is zero allowed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yes, I will add additional condition for 0

size_t max_length = it->value.GetUint();
if (max_length == 0) {
return "max_length should be greater than 0";
}

request.parameters["max_length"] = max_length;
// Keep OVMS tokenize API contract: max_length implies truncation.
request.parameters["truncation"] = true;
Comment on lines +84 to 92
} else {
return "max_length should be integer";
return "max_length should be unsigned integer";
}
}
it = parsedJson.FindMember("pad_to_max_length");
Expand Down