-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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: use body for streaming instead of params #6098
Changes from 24 commits
94348f6
b645afb
72037b5
99cdd7d
cc315c9
8bc5124
804b665
3ce6e1e
a5376af
025413f
60c9b5e
03cc4c4
b89c01a
cf2f145
fd30b0e
f1f9a88
3aa6534
57af50a
5f27f0a
a72a797
ee77d13
524aba4
5cfb423
3d1b07f
ca3a707
92405db
2c626bf
38a577b
c4afd99
c654c14
bb90d14
08bfd6b
3eff072
46af5fa
bf3cbe2
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import inspect | ||
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Union | ||
|
||
from jina import DocumentArray, Document | ||
from jina import Document, DocumentArray | ||
from jina._docarray import docarray_v2 | ||
from jina.importer import ImportExtensions | ||
from jina.serve.networking.sse import EventSourceResponse | ||
|
@@ -11,15 +11,15 @@ | |
from jina.logging.logger import JinaLogger | ||
|
||
if docarray_v2: | ||
from docarray import DocList, BaseDoc | ||
from docarray import BaseDoc, DocList | ||
|
||
|
||
def get_fastapi_app( | ||
request_models_map: Dict, | ||
caller: Callable, | ||
logger: 'JinaLogger', | ||
cors: bool = False, | ||
**kwargs, | ||
request_models_map: Dict, | ||
caller: Callable, | ||
logger: 'JinaLogger', | ||
cors: bool = False, | ||
**kwargs, | ||
): | ||
""" | ||
Get the app from FastAPI as the REST interface. | ||
|
@@ -35,15 +35,18 @@ | |
from fastapi import FastAPI, Response, HTTPException | ||
import pydantic | ||
from fastapi.middleware.cors import CORSMiddleware | ||
import os | ||
|
||
from pydantic import BaseModel, Field | ||
from pydantic.config import BaseConfig, inherit_config | ||
|
||
from jina.proto import jina_pb2 | ||
from jina.serve.runtimes.gateway.models import _to_camel_case | ||
import os | ||
|
||
class Header(BaseModel): | ||
request_id: Optional[str] = Field(description='Request ID', example=os.urandom(16).hex()) | ||
request_id: Optional[str] = Field( | ||
description='Request ID', example=os.urandom(16).hex() | ||
) | ||
|
||
class Config(BaseConfig): | ||
alias_generator = _to_camel_case | ||
|
@@ -66,11 +69,11 @@ | |
logger.warning('CORS is enabled. This service is accessible from any website!') | ||
|
||
def add_post_route( | ||
endpoint_path, | ||
input_model, | ||
output_model, | ||
input_doc_list_model=None, | ||
output_doc_list_model=None, | ||
endpoint_path, | ||
input_model, | ||
output_model, | ||
input_doc_list_model=None, | ||
output_doc_list_model=None, | ||
): | ||
app_kwargs = dict( | ||
path=f'/{endpoint_path.strip("/")}', | ||
|
@@ -123,8 +126,8 @@ | |
return ret | ||
|
||
def add_streaming_routes( | ||
endpoint_path, | ||
input_doc_model=None, | ||
endpoint_path, | ||
input_doc_model=None, | ||
): | ||
from fastapi import Request | ||
|
||
|
@@ -133,26 +136,14 @@ | |
methods=['GET'], | ||
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. The following code works, but I did not allow get since it doesn't work with gateway deployments. methods=['GET', 'POST'], |
||
summary=f'Streaming Endpoint {endpoint_path}', | ||
) | ||
async def streaming_get(request: Request): | ||
query_params = dict(request.query_params) | ||
req = DataRequest() | ||
req.header.exec_endpoint = endpoint_path | ||
if not docarray_v2: | ||
req.data.docs = DocumentArray([Document.from_dict(query_params)]) | ||
else: | ||
req.document_array_cls = DocList[input_doc_model] | ||
req.data.docs = DocList[input_doc_model]( | ||
[input_doc_model(**query_params)] | ||
async def streaming_get(request: Request, body: input_doc_model = None): | ||
if not body: | ||
query_params = dict(request.query_params) | ||
body = ( | ||
input_doc_model.parse_obj(query_params) | ||
if docarray_v2 | ||
else Document.from_dict(query_params) | ||
) | ||
event_generator = _gen_dict_documents(await caller(req)) | ||
return EventSourceResponse(event_generator) | ||
|
||
@app.api_route( | ||
path=f'/{endpoint_path.strip("/")}', | ||
methods=['POST'], | ||
summary=f'Streaming Endpoint {endpoint_path}', | ||
) | ||
async def streaming_post(body: input_doc_model, request: Request): | ||
req = DataRequest() | ||
req.header.exec_endpoint = endpoint_path | ||
if not docarray_v2: | ||
|
@@ -169,7 +160,9 @@ | |
output_doc_model = input_output_map['output']['model'] | ||
is_generator = input_output_map['is_generator'] | ||
parameters_model = input_output_map['parameters']['model'] or Optional[Dict] | ||
default_parameters = ... if input_output_map['parameters']['model'] else None | ||
default_parameters = ( | ||
... if input_output_map['parameters']['model'] else None | ||
) | ||
|
||
if docarray_v2: | ||
_config = inherit_config(InnerConfig, BaseDoc.__config__) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,19 +143,17 @@ async def test_streaming_delay(protocol, include_gateway): | |
): | ||
client = Client(port=port, protocol=protocol, asyncio=True) | ||
i = 0 | ||
stream = client.stream_doc( | ||
start_time = time.time() | ||
async for doc in client.stream_doc( | ||
on='/hello', | ||
inputs=MyDocument(text='hello world', number=i), | ||
return_type=MyDocument, | ||
) | ||
start_time = None | ||
async for doc in stream: | ||
start_time = start_time or time.time() | ||
): | ||
assert doc.text == f'hello world {i}' | ||
i += 1 | ||
delay = time.time() - start_time | ||
|
||
# 0.5 seconds between each request + 0.5 seconds tolerance interval | ||
assert delay < (0.5 * i), f'Expected delay to be less than {0.5 * i}, got {delay} on iteration {i}' | ||
assert time.time() - start_time < (0.5 * i) + 0.5 | ||
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. At the very least, we should merge this and fix the gateway. |
||
|
||
|
||
@pytest.mark.asyncio | ||
|
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.
We can
and request.method == 'GET'
to the condition here.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.
I think we may not need
POST
at all then.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.
That's the current state of this PR, and it fixes everything. Can't tell if tests need to be re-run or if they're actually failing.
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.
you can see a test failing here https://github.com/jina-ai/jina/actions/runs/6647328275/job/18084538375?pr=6098
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.
I fixed that issue, I think the current failures just need re-runs, can you take a look?
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.
Nevermind, found one that's not flakyness