Skip to content
Merged
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: 13 additions & 1 deletion app/forward_serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ void handle_generate(ForwardEngine& e, const httplib::Request& req, httplib::Res
}
return true;
};
run(emit_pieces);
const std::string finish = run(emit_pieces);
{
std::vector<std::pair<bool, std::string>> tail;
stream.finish(tail);
Expand All @@ -613,6 +613,18 @@ void handle_generate(ForwardEngine& e, const httplib::Request& req, httplib::Res
chunk["choices"] = json::array({{{"index", 0}, {"delta", delta}, {"finish_reason", "tool_calls"}}});
chunks->push_back("data: " + chunk.dump(-1, ' ', false, json::error_handler_t::replace) + "\n\n");
}
// OpenAI's stream ends with one choice that carries an empty delta and the reason the
// generation ended; without it a streaming client cannot tell a `length` cut from a
// natural `stop` (the per-delta chunks all say null, and the usage tail has no
// choices at all). The tool-call chunk above already reports `tool_calls`, so this is
// sent exactly once, and only for the text path.
if (stream.calls().empty()) {
json chunk = chunk_tpl;
chunk["choices"] = json::array({{{"index", 0},
{"delta", json::object()},
{"finish_reason", finish}}});
chunks->push_back("data: " + chunk.dump(-1, ' ', false, json::error_handler_t::replace) + "\n\n");
}
}
{
json tail = chunk_tpl;
Expand Down
Loading