-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix issue with same doc type in spawned processes (#6062)
- Loading branch information
Showing
14 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM jinaai/jina:test-pip | ||
|
||
COPY . /executor_root/ | ||
|
||
WORKDIR /executor_root | ||
|
||
ENTRYPOINT ["jina", "executor", "--uses", "config.yml"] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
jtype: Encoder | ||
metas: | ||
name: EncoderPrivate | ||
py_modules: | ||
- executor.py |
23 changes: 23 additions & 0 deletions
23
tests/integration/docarray_v2/docker/executor1/executor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Optional | ||
from docarray import DocList, BaseDoc | ||
from docarray.typing import NdArray | ||
from jina import Executor, requests | ||
import numpy as np | ||
|
||
class MyDoc(BaseDoc): | ||
text: str | ||
embedding: Optional[NdArray] = None | ||
|
||
|
||
class Encoder(Executor): | ||
def __init__( | ||
self, | ||
*args, | ||
**kwargs, | ||
): | ||
super().__init__(*args, **kwargs) | ||
|
||
@requests | ||
def encode(self, docs: DocList[MyDoc], **kwargs) -> DocList[MyDoc]: | ||
for doc in docs: | ||
doc.embedding = np.random.random(128) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM jinaai/jina:test-pip | ||
|
||
COPY . /executor_root/ | ||
|
||
WORKDIR /executor_root | ||
|
||
ENTRYPOINT ["jina", "executor", "--uses", "config.yml"] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
jtype: Indexer | ||
metas: | ||
name: IndexerPrivate | ||
py_modules: | ||
- executor.py |
39 changes: 39 additions & 0 deletions
39
tests/integration/docarray_v2/docker/executor2/executor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Optional, List | ||
from docarray import DocList, BaseDoc | ||
from docarray.typing import NdArray | ||
from docarray.index import InMemoryExactNNIndex | ||
from jina import Executor, requests | ||
|
||
|
||
class MyDoc(BaseDoc): | ||
text: str | ||
embedding: Optional[NdArray] = None | ||
|
||
|
||
class MyDocWithMatches(MyDoc): | ||
matches: DocList[MyDoc] = [] | ||
scores: List[float] = [] | ||
|
||
|
||
class Indexer(Executor): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._indexer = InMemoryExactNNIndex[MyDoc]() | ||
|
||
@requests(on='/index') | ||
def index(self, docs: DocList[MyDoc], **kwargs) -> DocList[MyDoc]: | ||
self._indexer.index(docs) | ||
return docs | ||
|
||
@requests(on='/search') | ||
def search(self, docs: DocList[MyDoc], **kwargs) -> DocList[MyDocWithMatches]: | ||
res = DocList[MyDocWithMatches]() | ||
ret = self._indexer.find_batched(docs, search_field='embedding') | ||
matched_documents = ret.documents | ||
matched_scores = ret.scores | ||
for query, matches, scores in zip(docs, matched_documents, matched_scores): | ||
output_doc = MyDocWithMatches(**query.dict()) | ||
output_doc.matches = matches | ||
output_doc.scores = scores.tolist() | ||
res.append(output_doc) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import time | ||
|
||
import pytest | ||
import requests as general_requests | ||
|
||
from jina import Flow | ||
|
||
cur_dir = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
|
||
@pytest.fixture | ||
def executor_images_built(): | ||
import docker | ||
|
||
client = docker.from_env() | ||
client.images.build(path=os.path.join(cur_dir, 'executor1'), tag='encoder-executor') | ||
client.images.build(path=os.path.join(cur_dir, 'executor2'), tag='indexer-executor') | ||
client.close() | ||
yield | ||
time.sleep(2) | ||
client = docker.from_env() | ||
client.containers.prune() | ||
|
||
|
||
@pytest.mark.parametrize('protocol', ['http', 'grpc']) | ||
def test_flow_with_docker(executor_images_built, protocol): | ||
from docarray import BaseDoc, DocList | ||
from typing import Optional, List | ||
from docarray.typing import NdArray | ||
|
||
class MyDoc(BaseDoc): | ||
text: str | ||
embedding: Optional[NdArray] = None | ||
|
||
class MyDocWithMatches(MyDoc): | ||
matches: DocList[MyDoc] = [] | ||
scores: List[float] = [] | ||
|
||
f = Flow(protocol=protocol).add(uses='docker://encoder-executor').add(uses='docker://indexer-executor') | ||
|
||
with f: | ||
if protocol == 'http': | ||
resp = general_requests.get(f'http://localhost:{f.port}/openapi.json') | ||
resp.json() | ||
|
||
sentences = ['This framework generates embeddings for each input sentence', | ||
'Sentences are passed as a list of string.', | ||
'The quick brown fox jumps over the lazy dog.'] | ||
|
||
inputs = DocList[MyDoc]([MyDoc(text=sentence) for sentence in sentences]) | ||
f.post(on='/index', inputs=inputs) | ||
queries = inputs[0:2] | ||
search_results = f.post(on='/search', inputs=queries, return_type=DocList[MyDocWithMatches]) | ||
|
||
assert len(search_results) == len(queries) | ||
for result in search_results: | ||
assert result.text in sentences | ||
assert len(result.matches) == len(sentences) | ||
for m in result.matches: | ||
assert m.text in sentences |