8000 Use server sent events function for streaming completion · chabotsi/llama-cpp-python@dc39cc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc39cc0

Browse files
committed
Use server sent events function for streaming completion
1 parent f0ec6e6 commit dc39cc0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

llama_cpp/server/app.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,15 @@ def create_completion(
259259
)
260260
)
261261
if request.stream:
262+
263+
async def server_sent_events(
264+
chunks: Iterator[llama_cpp.CompletionChunk],
265+
):
266+
for chunk in chunks:
267+
yield dict(data=json.dumps(chunk))
268+
262269
chunks: Iterator[llama_cpp.CompletionChunk] = completion_or_chunks # type: ignore
263-
return EventSourceResponse(dict(data=json.dumps(chunk)) for chunk in chunks)
270+
return EventSourceResponse(server_sent_events(chunks))
264271
completion: llama_cpp.Completion = completion_or_chunks # type: ignore
265272
return completion
266273

0 commit comments

Comments
 (0)
0