From e421757a3ab2ab80eb251599d45a1f70cd81164d Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Wed, 16 Jun 2021 09:23:45 +0800 Subject: [PATCH 1/4] fix: clarify typehint for callback function --- jina/clients/base.py | 7 +++---- jina/helloworld/chatbot/app.py | 4 ++-- jina/types/request/__init__.py | 4 ++-- tests/system/chatbot/test_chatbot.py | 4 ++-- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/jina/clients/base.py b/jina/clients/base.py index 72f2c0adacff0..b6284bab5ea40 100644 --- a/jina/clients/base.py +++ b/jina/clients/base.py @@ -5,7 +5,7 @@ import inspect import os from contextlib import nullcontext -from typing import Callable, Union, Optional, Iterator, Iterable, AsyncIterator +from typing import Callable, Union, Optional, Iterator, AsyncIterator import grpc @@ -18,11 +18,10 @@ from ..logging.profile import TimeContext, ProgressBar from ..parsers import set_client_cli_parser from ..proto import jina_pb2_grpc -from ..types.request import Request +from ..types.request import Request, Response InputType = Union[GeneratorSourceType, Callable[..., GeneratorSourceType]] -InputDeleteType = Union[str, Iterable[str], Callable[..., Iterable[str]]] -CallbackFnType = Optional[Callable[..., None]] +CallbackFnType = Optional[Callable[[Response, ...], None]] class BaseClient: diff --git a/jina/helloworld/chatbot/app.py b/jina/helloworld/chatbot/app.py index 8b3ebe85ba137..1f690af6087b6 100644 --- a/jina/helloworld/chatbot/app.py +++ b/jina/helloworld/chatbot/app.py @@ -16,7 +16,7 @@ from .my_executors import MyTransformer, MyIndexer -def create_chatbot_flow(args): +def _get_flow(args): """Ensure the same flow is used in hello world example and system test.""" return ( Flow() @@ -55,7 +55,7 @@ def hello_world(args): # now comes the real work # load index flow from a YAML file - f = create_chatbot_flow(args) + f = _get_flow(args) # index it! with f, open(targets['covid-csv']['filename']) as fp: diff --git a/jina/types/request/__init__.py b/jina/types/request/__init__.py index 99e68803f5351..db4fcd6dde549 100644 --- a/jina/types/request/__init__.py +++ b/jina/types/request/__init__.py @@ -31,7 +31,7 @@ ) -class Request(ProtoTypeMixin): +class Request(ProtoTypeMixin, DocsPropertyMixin, GroundtruthPropertyMixin): """ :class:`Request` is one of the **primitive data type** in Jina. @@ -254,7 +254,7 @@ def parameters(self, value: Dict): self._pb_body.parameters.update(value) -class Response(Request, DocsPropertyMixin, GroundtruthPropertyMixin): +class Response(Request): """ Response is the :class:`Request` object returns from the flow. Right now it shares the same representation as :class:`Request`. At 0.8.12, :class:`Response` is a simple alias. But it does give a more consistent semantic on diff --git a/tests/system/chatbot/test_chatbot.py b/tests/system/chatbot/test_chatbot.py index 1bc0b88f338ba..b5dcd2746f3ce 100644 --- a/tests/system/chatbot/test_chatbot.py +++ b/tests/system/chatbot/test_chatbot.py @@ -1,7 +1,7 @@ import pytest from jina import Document -from jina.helloworld.chatbot.app import hello_world, create_chatbot_flow +from jina.helloworld.chatbot.app import hello_world, _get_flow from jina.parsers.helloworld import set_hw_chatbot_parser from tests import validate_callback @@ -39,7 +39,7 @@ def validate_response(resp): mock_on_done = mocker.Mock() mock_on_fail = mocker.Mock() - flow = create_chatbot_flow(helloworld_args) + flow = _get_flow(helloworld_args) with flow as f: f.index( inputs=Document( From e36c381bf9e9e1dcdaea026a90da3779e7e6679c Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Wed, 16 Jun 2021 09:39:18 +0800 Subject: [PATCH 2/4] fix: clarify typehint for callback function --- jina/clients/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jina/clients/base.py b/jina/clients/base.py index b6284bab5ea40..534c3b590e899 100644 --- a/jina/clients/base.py +++ b/jina/clients/base.py @@ -21,7 +21,7 @@ from ..types.request import Request, Response InputType = Union[GeneratorSourceType, Callable[..., GeneratorSourceType]] -CallbackFnType = Optional[Callable[[Response, ...], None]] +CallbackFnType = Optional[Callable[[Response], None]] class BaseClient: From 532d47a322c8a705db9b8e3d10168dd0a2ba18d8 Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Wed, 16 Jun 2021 09:55:17 +0800 Subject: [PATCH 3/4] fix: move gateway hint to flow --- jina/flow/base.py | 28 +++++++++++++++++++---- jina/peapods/runtimes/asyncio/rest/app.py | 7 ------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/jina/flow/base.py b/jina/flow/base.py index 794e9aa0a3aef..1eb252f3dc529 100644 --- a/jina/flow/base.py +++ b/jina/flow/base.py @@ -1062,6 +1062,7 @@ def __iter__(self): return self._pod_nodes.items().__iter__() def _show_success_message(self): + if self._pod_nodes['gateway'].args.restful: header = 'http://' protocol = 'REST' @@ -1069,8 +1070,12 @@ def _show_success_message(self): header = 'tcp://' protocol = 'gRPC' + self.logger.success( + f'šŸŽ‰ Flow is ready to use, accepting {colored(protocol + " request", attrs="bold")}' + ) + address_table = [ - f'\tšŸ–„ļø Local access:\t' + f'\tšŸ  Local access:\t' + colored( f'{header}{self.host}:{self.port_expose}', 'cyan', attrs='underline' ), @@ -1090,9 +1095,24 @@ def _show_success_message(self): attrs='underline', ) ) - self.logger.success( - f'šŸŽ‰ Flow is ready to use, accepting {colored(protocol + " request", attrs="bold")}' - ) + if self.args.restful: + address_table.append( + f'\tšŸ’¬ Swagger UI:\t\t' + + colored( + f'http://localhost:{self.port_expose}/docs', + 'cyan', + attrs='underline', + ) + ) + address_table.append( + f'\tšŸ“š Redoc:\t\t' + + colored( + f'http://localhost:{self.port_expose}/redoc', + 'cyan', + attrs='underline', + ) + ) + self.logger.info('\n' + '\n'.join(address_table)) def block(self): diff --git a/jina/peapods/runtimes/asyncio/rest/app.py b/jina/peapods/runtimes/asyncio/rest/app.py index 5d412c3b4b5d5..65540cb884075 100644 --- a/jina/peapods/runtimes/asyncio/rest/app.py +++ b/jina/peapods/runtimes/asyncio/rest/app.py @@ -74,13 +74,6 @@ def _shutdown(): @app.on_event('startup') async def startup(): """Log the host information when start the server.""" - default_logger.info( - f''' - Jina REST interface - šŸ’¬ Swagger UI:\thttp://localhost:{args.port_expose}/docs - šŸ“š Redoc :\thttp://localhost:{args.port_expose}/redoc - ''' - ) from jina import __ready_msg__ default_logger.success(__ready_msg__) From 11a3da315f9cf18e43c4d5efd058c5fecf2679d6 Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Wed, 16 Jun 2021 10:11:16 +0800 Subject: [PATCH 4/4] fix: move gateway hint to flow --- .github/2.0/cookbooks/Flow.md | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/.github/2.0/cookbooks/Flow.md b/.github/2.0/cookbooks/Flow.md index 63a16b3278f56..e9ce96d810275 100644 --- a/.github/2.0/cookbooks/Flow.md +++ b/.github/2.0/cookbooks/Flow.md @@ -542,8 +542,21 @@ with f, open('my.csv') as fp: #### Callback Functions -Once a request is made, callback functions are fired. Jina Flow implements a Promise-like interface. You can add -callback functions `on_done`, `on_error`, `on_always` to hook different events. In the example below, our Flow passes +Once a request is returned, callback functions are fired. Jina Flow implements a Promise-like interface. You can add +callback functions `on_done`, `on_error`, `on_always` to hook different events. + +In Jina, callback function's first argument is a `jina.types.request.Response` object. Hence, you can annotate the callback function via: + +```python +from jina.types.request import Response + +def my_callback(rep: Response): + ... +``` + +`Response` object has many attributes, probably the most popular one is `Response.docs`, where you can access all `Document` as an `DocumentArray`. + +In the example below, our Flow passes the message then prints the result when successful. If something goes wrong, it beeps. Finally, the result is written to `output.txt`. @@ -553,8 +566,8 @@ from jina import Document, Flow def beep(*args): # make a beep sound - import os - os.system('echo -n "\a";') + import sys + sys.stdout.write('\a') with Flow().add() as f, open('output.txt', 'w') as fp: @@ -562,7 +575,7 @@ with Flow().add() as f, open('output.txt', 'w') as fp: Document(), on_done=print, on_error=beep, - on_always=lambda x: fp.write(x.json())) + on_always=lambda x: x.docs.save(fp)) ``` #### Send Parameters