Skip to content

Commit

Permalink
fix: add readme snippet to CI (#2847)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxiao committed Jul 4, 2021
1 parent 1df5472 commit 634662a
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 177 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Document, Executor, and Flow are the three fundamental concepts in Jina.

<img src="https://github.com/jina-ai/jina/blob/master/.github/2.0/simple-arch.svg" alt="The architecture of a simple neural search system powered by Jina">

<!-- README-SERVER-START -->
```python
import numpy as np
from jina import Document, DocumentArray, Executor, Flow, requests
Expand Down Expand Up @@ -113,6 +114,7 @@ with f:
f.post('/index', (Document(text=t.strip()) for t in open(__file__) if t.strip())) # index all lines of _this_ file
f.block() # block for listening request
```
<!-- README-SERVER-END -->

2️⃣ Open `http://localhost:12345/docs` (an extended Swagger UI) in your browser, click <kbd>/search</kbd> tab and input:

Expand All @@ -127,6 +129,8 @@ Here `@requests(on=something)` is our textual query, **we want to find the lines

3️⃣ Not a GUI guy? Let's query it from Python then! Keep the above server running and start a simple client:


<!-- README-CLIENT-START -->
```python
from jina import Client, Document
from jina.types.request import Response
Expand All @@ -141,6 +145,8 @@ c = Client(protocol='http', port_expose=12345) # connect to localhost:12345
c.post('/search', Document(text='request(on=something)'), on_done=print_matches)
```

<!-- README-CLIENT-END -->

, which prints the following results:

```text
Expand Down
5 changes: 3 additions & 2 deletions jina/clients/base/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async def _get_results(

try:
cm1 = ProgressBar() if self.show_progress else nullcontext()
url = f'http://{self.args.host}:{self.args.port_expose}/post'

with cm1 as p_bar:
all_responses = []
Expand All @@ -73,7 +74,7 @@ async def _get_results(
asyncio.create_task(
self._get_http_response(
session,
f'http://{self.args.host}:{self.args.port_expose}/post',
url,
req_dict,
)
)
Expand All @@ -82,7 +83,7 @@ async def _get_results(
for resp in asyncio.as_completed(all_responses):
r_status, r_str = await resp
if r_status == 404:
raise BadClient('no such endpoint on the server')
raise BadClient(f'no such endpoint {url}')
elif r_status < 200 or r_status > 300:
raise ValueError(r_str)

Expand Down
20 changes: 12 additions & 8 deletions jina/peapods/runtimes/gateway/http/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CustomConfig(BaseConfig):

def _get_oneof_validator(oneof_fields: List, oneof_key: str) -> Callable:
"""
Pydantic root validator (pre) classmethod generator to confirm only one oneof field is passed
Pydantic root validator (pre) classmethod generator to confirm only one oneof field is passed
:param oneof_fields: list of field names for oneof
:type oneof_fields: List
Expand All @@ -77,7 +77,7 @@ def oneof_validator(cls, values):

def _get_oneof_setter(oneof_fields: List, oneof_key: str) -> Callable:
"""
Pydantic root validator (post) classmethod generator to set the oneof key
Pydantic root validator (post) classmethod generator to set the oneof key
:param oneof_fields: list of field names for oneof
:type oneof_fields: List
Expand All @@ -101,7 +101,7 @@ def oneof_setter(cls, values):

def _get_tags_updater() -> Callable:
"""
Pydantic root validator (pre) classmethod generator to update tags
Pydantic root validator (pre) classmethod generator to update tags
:return: classmethod for updating tags in DocumentProto Pydantic model
"""
Expand Down Expand Up @@ -166,17 +166,14 @@ def protobuf_to_pydantic_model(
field_type = Enum(f.enum_type.name, enum_dict)

if f.message_type:

if f.message_type.name == 'Struct':
# Proto Field Type: google.protobuf.Struct
field_type = Dict
default_value = {}

elif f.message_type.name == 'Timestamp':
# Proto Field Type: google.protobuf.Timestamp
field_type = datetime
default_value = datetime.now()

else:
# Proto field type: Proto message defined in jina.proto
if f.message_type.name == model_name:
Expand All @@ -185,13 +182,20 @@ def protobuf_to_pydantic_model(
else:
# This field_type itself a Pydantic model
field_type = protobuf_to_pydantic_model(f.message_type)
PROTO_TO_PYDANTIC_MODELS.__setattr__(model_name, field_type)
PROTO_TO_PYDANTIC_MODELS.model_name = field_type

if f.label == FieldDescriptor.LABEL_REPEATED:
field_type = List[field_type]

all_fields[field_name] = (field_type, Field(default=default_value))

# some fixes on Doc.scores and Doc.evaluations
if field_name in ('scores', 'evaluations'):
all_fields[field_name] = (
Dict[str, PROTO_TO_PYDANTIC_MODELS.NamedScoreProto],
Field(default={}),
)

# Post-processing (Handle oneof fields)
for oneof_k, oneof_v_list in oneof_fields.items():
oneof_field_validators[f'oneof_validator_{oneof_k}'] = _get_oneof_validator(
Expand All @@ -217,10 +221,10 @@ def protobuf_to_pydantic_model(


for proto in (
NamedScoreProto,
DenseNdArrayProto,
NdArrayProto,
SparseNdArrayProto,
NamedScoreProto,
DocumentProto,
RouteProto,
EnvelopeProto,
Expand Down
10 changes: 2 additions & 8 deletions jina/proto/jina.proto
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ message NamedScoreProto {
string ref_id = 5; // the score is computed between doc `id` and `ref_id`
}

/**
* Represents the mapping of score by keys
*/
message NamedScoreMappingProto {
map<string, NamedScoreProto> values = 1;
}

/**
* Represents a Graph
Expand Down Expand Up @@ -144,13 +138,13 @@ message DocumentProto {
NdArrayProto embedding = 19;

// Scores performed on the document, each element corresponds to a metric
NamedScoreMappingProto scores = 28;
map<string, NamedScoreProto> scores = 28;

// modality, an identifier to the modality this document belongs to. In the scope of multi/cross modal search
string modality = 21;

// Evaluations performed on the document, each element corresponds to a metric
NamedScoreMappingProto evaluations = 29;
map<string, NamedScoreProto> evaluations = 29;
}

/**
Expand Down

0 comments on commit 634662a

Please sign in to comment.