Skip to content

server : add bad input handling in embeddings #10866

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

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 12 additions & 2 deletions examples/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3649,20 +3649,30 @@ int main(int argc, char ** argv) {
oaicompat = true;
prompt = body.at("input");
} else if (body.count("content") != 0) {
// with "content", we only support single prompt
prompt = std::vector<std::string>{body.at("content")};
prompt = body.at("content");
} else {
res_error(res, format_error_response("\"input\" or \"content\" must be provided", ERROR_TYPE_INVALID_REQUEST));
return;
}

// with "content", we only support single prompt
if (!oaicompat && prompt.type() != json::value_t::string) {
res_error(res, format_error_response("\"content\" must be a string", ERROR_TYPE_INVALID_REQUEST));
return;
}

Comment on lines +3658 to +3663
Copy link
Member

@ggerganov ggerganov Dec 17, 2024

Choose a reason for hiding this comment

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

I wonder what is the reason to apply this restriction on the "content" field? Why don't we treat it as an alias to "input" and allow the same inputs for it? cc @ngxson

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm I have no idea why, seems like remnant from the past. It makes more sense to consider content as an alias of input as you said.

// create and queue the task
json responses = json::array();
bool error = false;
{
std::vector<server_task> tasks;
std::vector<llama_tokens> tokenized_prompts = tokenize_input_prompts(ctx_server.ctx, prompt, /* add_special */ false, true);
for (size_t i = 0; i < tokenized_prompts.size(); i++) {
if (tokenized_prompts[i].size() == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test should not be here. Also, empty string is still a valid input.

Please remove this check

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm ok sorry seems like OAI embedding does not accept empty string either. So this check is relevant, it's just not in the correct place.

Ref: https://platform.openai.com/docs/api-reference/embeddings/create

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should be fixed in my PR and it won't crash if the input is empty. We're now adding BOS token to the sequence.

res_error(res, format_error_response("input cannot be an empty string", ERROR_TYPE_INVALID_REQUEST));
return;
}

server_task task = server_task(SERVER_TASK_TYPE_EMBEDDING);
task.id = ctx_server.queue_tasks.get_new_id();
task.index = i;
Expand Down
36 changes: 36 additions & 0 deletions examples/server/tests/unit/test_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,39 @@ def test_same_prompt_give_same_result():
vi = res.body['data'][i]['embedding']
for x, y in zip(v0, vi):
assert abs(x - y) < EPSILON


@pytest.mark.parametrize("text", [
None,
True,
"",
42,
4.2,
{},
[],
[""],
["This is a test", ""],
])
def test_embedding_bad_input(text):
global server
server.start()
res = server.make_request("POST", "/embeddings", data={"input": text})
assert res.status_code >= 400


@pytest.mark.parametrize("text", [
None,
True,
"",
42,
4.2,
{},
[],
[""],
["This is a test"],
])
def test_embedding_content_bad_input(text):
global server
server.start()
res = server.make_request("POST", "/embeddings", data={"content": text})
assert res.status_code >= 400
Loading