Skip to content

Commit d9da0e4

Browse files
authored
server : improve "prompt" handling (ggml-org#7847)
1 parent 1f0dabd commit d9da0e4

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

examples/server/server.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct server_slot {
147147
int32_t n_prompt_tokens = 0;
148148
int32_t n_prompt_tokens_processed = 0;
149149

150-
json prompt;
150+
std::string prompt;
151151

152152
// when a task is submitted, we first tokenize the prompt and store it here
153153
std::vector<llama_token> prompt_tokens;
@@ -822,13 +822,8 @@ struct server_context {
822822
continue;
823823
}
824824

825-
// skip the slot if it does not contains prompt
826-
if (!slot.prompt.is_string()) {
827-
continue;
828-
}
829-
830825
// current slot's prompt
831-
std::string slot_prompt = slot.prompt.get<std::string>();
826+
std::string slot_prompt = slot.prompt;
832827

833828
// length of the current slot's prompt
834829
int slot_prompt_len = slot_prompt.size();
@@ -958,13 +953,16 @@ struct server_context {
958953
if (!task.infill) {
959954
const auto & prompt = data.find("prompt");
960955
if (prompt == data.end()) {
961-
send_error(task, "Either \"prompt\" or \"messages\" must be provided", ERROR_TYPE_INVALID_REQUEST);
956+
send_error(task, "\"prompt\" must be provided", ERROR_TYPE_INVALID_REQUEST);
962957
return false;
963-
} else {
964-
slot.prompt = *prompt;
965958
}
966-
if (slot.prompt.is_array() && slot.prompt.size() == 0) {
967-
send_error(task, "\"prompt\" cannot be an empty array", ERROR_TYPE_INVALID_REQUEST);
959+
960+
if (prompt->is_string()) {
961+
slot.prompt = prompt->get<std::string>();
962+
} else if (prompt->is_array() && prompt->size() == 1 && prompt->at(0).is_string()) {
963+
slot.prompt = prompt->at(0).get<std::string>();
964+
} else {
965+
send_error(task, "\"prompt\" must be a string or an array of strings", ERROR_TYPE_INVALID_REQUEST);
968966
return false;
969967
}
970968
}
@@ -1582,14 +1580,18 @@ struct server_context {
15821580
switch (task.type) {
15831581
case SERVER_TASK_TYPE_COMPLETION:
15841582
{
1585-
int id_slot = json_value(task.data, "id_slot", -1);
1586-
std::string prompt = json_value(task.data, "prompt", std::string());
1583+
const int id_slot = json_value(task.data, "id_slot", -1);
15871584

15881585
server_slot * slot;
15891586

15901587
if (id_slot != -1) {
15911588
slot = get_slot_by_id(id_slot);
15921589
} else {
1590+
std::string prompt;
1591+
if (task.data.contains("prompt") && task.data.at("prompt").is_string()) {
1592+
json_value(task.data, "prompt", std::string());
1593+
}
1594+
15931595
slot = get_available_slot(prompt);
15941596
}
15951597

0 commit comments

Comments
 (0)