-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix streaming handling for builtin assistants #462
Changes from 5 commits
3441b55
649fef3
11cebec
efd4c69
0b9211e
389463d
4dc4cab
4b4fbae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import json | ||
import random | ||
|
||
import sse_starlette | ||
from fastapi import FastAPI, Request, Response, status | ||
from fastapi.responses import StreamingResponse | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/health") | ||
async def health(): | ||
return Response(b"", status_code=status.HTTP_200_OK) | ||
|
||
|
||
@app.post("/sse") | ||
async def sse(request: Request): | ||
data = await request.json() | ||
|
||
async def stream(): | ||
for obj in data: | ||
yield sse_starlette.ServerSentEvent(json.dumps(obj)) | ||
|
||
return sse_starlette.EventSourceResponse(stream()) | ||
|
||
|
||
@app.post("/jsonl") | ||
async def jsonl(request: Request): | ||
data = await request.json() | ||
|
||
async def stream(): | ||
for obj in data: | ||
yield f"{json.dumps(obj)}\n" | ||
|
||
return StreamingResponse(stream()) | ||
|
||
|
||
@app.post("/json") | ||
async def json_(request: Request): | ||
data = await request.body() | ||
|
||
async def stream(): | ||
start = 0 | ||
while start < len(data): | ||
end = start + random.randint(1, 10) | ||
yield data[start:end] | ||
start = end | ||
|
||
return StreamingResponse(stream()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import socket | ||
import subprocess | ||
import sys | ||
import time | ||
|
@@ -11,12 +10,7 @@ | |
from ragna._utils import timeout_after | ||
from ragna.deploy import Config | ||
from tests.deploy.utils import TestAssistant | ||
|
||
|
||
def get_available_port(): | ||
with socket.socket() as s: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved it to the generic test utils as this is no longer just needed for deploy. |
||
s.bind(("", 0)) | ||
return s.getsockname()[1] | ||
from tests.utils import get_available_port | ||
|
||
|
||
@pytest.fixture | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
import contextlib | ||
import platform | ||
import socket | ||
import subprocess | ||
import sys | ||
|
||
import pytest | ||
|
||
skip_on_windows = pytest.mark.skipif( | ||
platform.system() == "Windows", reason="Test is broken skipped on Windows" | ||
) | ||
|
||
|
||
@contextlib.contextmanager | ||
def background_subprocess(*args, stdout=sys.stdout, stderr=sys.stdout, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had this before, but it was removed in #322. Probably can also be used by the UI tests, but we can do that in a follow-up. |
||
process = subprocess.Popen(args, stdout=stdout, stderr=stderr, **kwargs) | ||
try: | ||
yield process | ||
finally: | ||
process.kill() | ||
process.communicate() | ||
|
||
|
||
def get_available_port(): | ||
with socket.socket() as s: | ||
s.bind(("", 0)) | ||
return s.getsockname()[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nenb This test fails when I revert my patch in this PR. See the CI runs for 0b9211e.