Repository navigation
server: stream /v1/tasks/stream events as SSE on request - #849
Merged
Merged
Conversation
/v1/tasks/stream collects every event of a run and sends them in one
JSON body after the run is over, so a client gets the first audio of a
reply only once the whole reply is done. With "stream_format": "sse" at
the top level of the body, both server runtimes now answer with
server-sent events instead: each event as the model produces it, as
{"type":"task.stream.event","event":E}, where E is the object the JSON
response puts in "events", then {"type":"task.stream.done","result":R},
where R is the object it puts under "result", artifacts included, then
[DONE].
Without the field, or with null, the response is the JSON body as
before. This route used to ignore the field, so a body that carried
any value, "sse" included, got the JSON body; a value other than "sse"
or null is now a 400. Errors found before the stream starts keep the
status the JSON request gets, including 503 for a busy model with
--parallel-jobs, whose slot is bound before the headers. Without
--parallel-jobs the model lock is taken inside the stream, so a busy
model, like any later failure, ends the stream with the transport's
error event, as on the other SSE routes.
Both paths pass the same request to run_streaming_model and write each
event and the result with the same stream_event_json and
streaming_task_result_json, so a run with the same output gives the
bytes the JSON response holds, apart from ttft_ms. A header shared by
both runtimes checks the field and builds the two kinds of line. The
README gets a section for the route, which had none, and the help text
lists it. server_parallel_lifecycle_test covers the SSE path with 1, 2
and 4 slots; server_runtime_selection_test covers the legacy route's
opt-in, its load failure inside the stream and the 400s.
With stream_format sse on /v1/tasks/stream, a reply's text and audio come as events while it is generated, and the reply artifact for the next turn comes in the closing task.stream.done event. Say so in Conversations, with what a client needs to know: the entry needs mode streaming, the done event repeats the reply's audio on one line, a stream closed early returns no reply artifact, the route does not read audio_base64, and a turn the model turns away, such as one past the step limit, is an error event before any audio. The Limitations entry on conversations names the route's SSE form too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Lets
POST /v1/tasks/streamsend each event to the client as soon as the model makes it, instead of all at once at the end. A client opts in with"stream_format": "sse". For lfm2_audio conversations (#838, #844, #845), a reply then starts playing about 1.4 s into turn 10 instead of after the whole reply is done (about 6 s).Problem
/v1/tasks/streamwaits until the model has finished, then sends every event in one JSON body. The client hears nothing until the whole reply is ready/v1/audio/speech/livedoes stream, but it cannot take a conversation's history, so the later turns of a conversation have no way to start playing earlyWhat this PR changes
"stream_format": "sse"in the body, both server runtimes answer with server-sent events:app/server/task_stream_format.h, and oneif (sse)block in each runtime's handler, reusing the existing SSE helpers. Plus tests, a README section for this route (it had none), and a note in the lfm2_audio Conversations sectionWhat changes for users
stream_format: no changestream_formatother than"sse"ornullnow gets a 400; before, this route ignored the field. This catches typos and matches/v1/audio/speech. Nothing in this repo sends the field to this route, and the WebUI does not use it. If you would rather keep ignoring other values, it is a one-line change--parallel-jobs). Errors after streaming starts arrive as anerrorevent, so a client knows the run succeeded only when it getstask.stream.doneaudiopath, notaudio_base64. Would you likeaudio_base64here too, in this PR or a follow-up?Testing
On a Xeon 8358, CPU only, the English LFM2.5-Audio checkpoint at Q4_0, 8 threads, both server runtimes. Tested on main bd88e6e; the branch now sits on #837, which only changes CUDA files.
stream_format: responses are byte for byte main's apart from the timing value, including the error responses and ten-turn conversationsThe whole response takes about the same time either way
Build and run
with a
server.jsonsuch as{"models": [{"id": "lfm2-audio-s2s-stream", "family": "lfm2_audio", "task": "s2s", "mode": "streaming", "path": "models/LFM2.5-Audio-1.5B-GGUF", "session_options": {"lfm2_audio.model_gguf": "LFM2.5-Audio-1.5B-Q4_0.gguf"}}]}The test runs used gcc 12 (
-DCMAKE_C_COMPILER=gcc-12 -DCMAKE_CXX_COMPILER=g++-12). For a conversation's later turns, send the earlier questions and replies inrequest.artifacts, as in Conversations in docs/community_models/lfm2_audio.md.