@@ -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