Skip to content

Commit d25cd7f

Browse files
committed
refactor
1 parent 5f06d37 commit d25cd7f

File tree

2 files changed

+42
-47
lines changed

2 files changed

+42
-47
lines changed

examples/server/server.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,9 +3069,14 @@ int main(int argc, char ** argv) {
30693069
}
30703070
json body = json::parse(req.body);
30713071

3072-
if (body.contains("tools") && ctx_server.tool_format != LLAMA_TOOL_FORMAT_NOT_SUPPORTED) {
3073-
body["prompt"] = format_chat_with_tool(ctx_server.tool_format, body.at("messages"), body.at("tools"));
3074-
body.erase(body.find("tools"));
3072+
if (body.contains("tools")) {
3073+
if (ctx_server.tool_format != LLAMA_TOOL_FORMAT_NOT_SUPPORTED) {
3074+
body["prompt"] = format_chat_with_tool(ctx_server.tool_format, body.at("messages"), body.at("tools"));
3075+
body.erase(body.find("tools"));
3076+
} else {
3077+
res_error(res, format_error_response("This server does not support tool calls. Start it with `--tool-calls`", ERROR_TYPE_NOT_SUPPORTED));
3078+
return;
3079+
}
30753080
}
30763081

30773082
json data = oaicompat_completion_params_parse(ctx_server.model, body, params.chat_template);

examples/server/utils.hpp

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,15 @@ static std::vector<json> format_partial_response_oaicompat(json result, const st
499499
finish_reason = "length";
500500
}
501501

502-
std::time_t t = std::time(0);
502+
auto wrap_choices = [&completion_id, &modelname](json choices) -> json {
503+
return json{
504+
{"choices", choices},
505+
{"created", std::time(0)},
506+
{"id", completion_id},
507+
{"model", modelname},
508+
{"object", "chat.completion.chunk"}
509+
};
510+
};
503511

504512
json choices;
505513
json delta = has_tool_calls
@@ -519,22 +527,14 @@ static std::vector<json> format_partial_response_oaicompat(json result, const st
519527
{"delta", json::object()}}});
520528
if (has_tool_calls) {
521529
// tool call must be send as two updates
522-
json initial_ret = json{{"choices", json::array({json{
523-
{"finish_reason", nullptr},
524-
{"index", 0},
525-
{"delta", delta}}})},
526-
{"created", t},
527-
{"id", completion_id},
528-
{"model", modelname},
529-
{"object", "chat.completion.chunk"}};
530-
531-
json second_ret = json{
532-
{"choices", choices},
533-
{"created", t},
534-
{"id", completion_id},
535-
{"model", modelname},
536-
{"object", "chat.completion.chunk"}};
537-
530+
json initial_ret = wrap_choices(json::array({
531+
json{
532+
{"finish_reason", nullptr},
533+
{"index", 0},
534+
{"delta", delta},
535+
}
536+
}));
537+
json second_ret = wrap_choices(choices);
538538
return std::vector<json>({initial_ret, second_ret});
539539
}
540540
} else {
@@ -545,26 +545,22 @@ static std::vector<json> format_partial_response_oaicompat(json result, const st
545545
{"delta", json{{"role", "assistant"}}}}});
546546
} else {
547547
// We have to send this as two updates to conform to openai behavior
548-
json initial_ret = json{{"choices", json::array({json{
549-
{"finish_reason", nullptr},
550-
{"index", 0},
551-
{"delta", json{
552-
{"role", "assistant"}
553-
}}}})},
554-
{"created", t},
555-
{"id", completion_id},
556-
{"model", modelname},
557-
{"object", "chat.completion.chunk"}};
558-
559-
json second_ret = json{
560-
{"choices", json::array({json{{"finish_reason", nullptr},
561-
{"index", 0},
562-
{"delta", delta}}})},
563-
{"created", t},
564-
{"id", completion_id},
565-
{"model", modelname},
566-
{"object", "chat.completion.chunk"}};
567-
548+
json initial_ret = wrap_choices(json::array({
549+
json{
550+
{"finish_reason", nullptr},
551+
{"index", 0},
552+
{"delta", json{
553+
{"role", "assistant"},
554+
}},
555+
}
556+
}));
557+
json second_ret = wrap_choices(json::array({
558+
json{
559+
{"finish_reason", nullptr},
560+
{"index", 0},
561+
{"delta", delta},
562+
}
563+
}));
568564
return std::vector<json>({initial_ret, second_ret});
569565
}
570566
} else {
@@ -582,13 +578,7 @@ static std::vector<json> format_partial_response_oaicompat(json result, const st
582578
}
583579
}
584580

585-
json ret = json {
586-
{"choices", choices},
587-
{"created", t},
588-
{"id", completion_id},
589-
{"model", modelname},
590-
{"object", "chat.completion.chunk"}
591-
};
581+
json ret = wrap_choices(choices);
592582
if (!finish_reason.empty()) {
593583
int num_tokens_predicted = json_value(result, "tokens_predicted", 0);
594584
int num_prompt_tokens = json_value(result, "tokens_evaluated", 0);

0 commit comments

Comments
 (0)