Jina Python API Reference

Top-level module of Jina.

The primary function of this module is to import all of the public Jina interfaces into a single place. The interfaces themselves are located in sub-modules, as described below.

class jina.AsyncClient(args)[source]

Bases: jina.clients.base.BaseClient

AsyncClient is the asynchronous version of the Client.

They share the same interface, except in AsyncClient train(), index(), search() methods are coroutines (i.e. declared with the async/await syntax), simply calling them will not schedule them to be executed.

To actually run a coroutine, user need to put them in an event loop, e.g. via asyncio.run(), asyncio.create_task().

AsyncClient can be very useful in the integration settings, where Jina/Flow/Client is NOT the main logic, but rather served as a part of other program. In this case, users often do not want to let Jina control the asyncio.eventloop. On contrary, Client is controlling and wrapping the event loop internally, making the Client looks synchronous from outside.

For example, say you have the Flow running in remote. You want to use Client to connect to it do some index and search, but meanwhile you have some other IO-bounded jobs and want to do them concurrently. You can use AsyncClient,

from jina.clients.asyncio import AsyncClient

ac = AsyncClient(...)

async def jina_client_query():
    await ac.search(...)

async def heavylifting():
    await other_library.download_big_files(...)

async def concurrent_main():
    await asyncio.gather(jina_client_query(), heavylifting())


if __name__ == '__main__':
    # under python
    asyncio.run(concurrent_main())

One can think of Client as Jina-managed eventloop, whereas AsyncClient is self-managed eventloop.

train(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘train’ request to the Flow.

Parameters
  • input_fn (Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], Callable[…, Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]]]]]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

search(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘search’ request to the Flow.

Parameters
  • input_fn (Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], Callable[…, Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]]]]]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

index(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘index’ request to the Flow.

Parameters
  • input_fn (Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], Callable[…, Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]]]]]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

delete(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘delete’ request to the Flow.

Parameters
  • input_fn (Iterable[str]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

update(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘update’ request to the Flow.

Parameters
  • input_fn (Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], Callable[…, Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]]]]]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

class jina.AsyncFlow(args=None, env=None, **kwargs)[source]

Bases: jina.flow.mixin.async_crud.AsyncCRUDFlowMixin, jina.flow.base.BaseFlow

AsyncFlow is the asynchronous version of the Flow. They share the same interface, except in AsyncFlow train(), index(), search() methods are coroutines (i.e. declared with the async/await syntax), simply calling them will not schedule them to be executed. To actually run a coroutine, user need to put them in an eventloop, e.g. via asyncio.run(), asyncio.create_task().

AsyncFlow can be very useful in the integration settings, where Jina/Jina Flow is NOT the main logic, but rather served as a part of other program. In this case, users often do not want to let Jina control the asyncio.eventloop. On contrary, Flow is controlling and wrapping the eventloop internally, making the Flow looks synchronous from outside.

In particular, AsyncFlow makes Jina usage in Jupyter Notebook more natural and reliable. For example, the following code will use the eventloop that already spawned in Jupyter/ipython to run Jina Flow (instead of creating a new one).

from jina import AsyncFlow
import numpy as np

with AsyncFlow().add() as f:
    await f.index_ndarray(np.random.random([5, 4]), on_done=print)

Notice that the above code will NOT work in standard Python REPL, as only Jupyter/ipython implements “autoawait”.

Another example is when using Jina as an integration. Say you have another IO-bounded job heavylifting(), you can use this feature to schedule Jina index() and heavylifting() concurrently. For example,

async def run_async_flow_5s():
    # WaitDriver pause 5s makes total roundtrip ~5s
    with AsyncFlow().add(uses='- !WaitDriver {}') as f:
        await f.index_ndarray(np.random.random([5, 4]), on_done=validate)


async def heavylifting():
    # total roundtrip takes ~5s
    print('heavylifting other io-bound jobs, e.g. download, upload, file io')
    await asyncio.sleep(5)
    print('heavylifting done after 5s')


async def concurrent_main():
    # about 5s; but some dispatch cost, can't be just 5s, usually at <7s
    await asyncio.gather(run_async_flow_5s(), heavylifting())

One can think of Flow as Jina-managed eventloop, whereas AsyncFlow is self-managed eventloop.

jina.Classifier

alias of jina.executors.classifiers.BaseClassifier

class jina.Client(args)[source]

Bases: jina.clients.base.BaseClient

A simple Python client for connecting to the gRPC gateway.

It manages the asyncio event loop internally, so all interfaces are synchronous from the outside.

train(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘train’ request to the Flow.

Parameters
  • input_fn (Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], Callable[…, Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]]]]]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

search(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘search’ request to the Flow.

Parameters
  • input_fn (Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], Callable[…, Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]]]]]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

index(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘index’ request to the Flow.

Parameters
  • input_fn (Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], Callable[…, Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]]]]]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

update(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘update’ request to the Flow.

Parameters
  • input_fn (Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], Callable[…, Union[Document, Iterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]], AsyncIterable[Union[~DocumentContentType, ~DocumentSourceType, Document, Tuple[~DocumentContentType, ~DocumentContentType], Tuple[~DocumentSourceType, ~DocumentSourceType]]]]]]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

delete(input_fn, on_done=None, on_error=None, on_always=None, **kwargs)[source]

Issue ‘update’ request to the Flow.

Parameters
  • input_fn (Iterable[str]) – the input function that generates the content

  • on_done (Optional[Callable[…, None]]) – the function to be called when the Request object is resolved.

  • on_error (Optional[Callable[…, None]]) – the function to be called when the Request object is rejected.

  • on_always (Optional[Callable[…, None]]) – the function to be called when the Request object is is either resolved or rejected.

  • kwargs

Return type

None

Returns

jina.Crafter

alias of jina.executors.crafters.BaseCrafter

class jina.Document(document=None, field_resolver=None, copy=False, **kwargs)[source]

Bases: jina.types.mixin.ProtoTypeMixin

Document is one of the primitive data type in Jina.

It offers a Pythonic interface to allow users access and manipulate jina.jina_pb2.DocumentProto object without working with Protobuf itself.

To create a Document object, simply:

from jina import Document
d = Document()
d.text = 'abc'

Jina requires each Document to have a string id. You can set a custom one, or if non has been set a random one will be assigned.

Or you can use Document as a context manager:

with Document() as d:
    d.text = 'hello'

assert d.id  # now `id` has value

To access and modify the content of the document, you can use text, blob, and buffer. Each property is implemented with proper setter, to improve the integrity and user experience. For example, assigning doc.blob or doc.embedding can be simply done via:

import numpy as np

# to set as content
d.content = np.random.random([10, 5])

# to set as embedding
d.embedding = np.random.random([10, 5])

MIME type is auto set/guessed when setting content and uri

Document also provides multiple way to build from existing Document. You can build Document from jina_pb2.DocumentProto, bytes, str, and Dict. You can also use it as view (i.e. weak reference when building from an existing jina_pb2.DocumentProto). For example,

a = DocumentProto()
b = Document(a, copy=False)
a.text = 'hello'
assert b.text == 'hello'

You can leverage the convert_a_to_b() interface to convert between content forms.

Parameters
  • document (Optional[~DocumentSourceType]) – the document to construct from. If bytes is given then deserialize a DocumentProto; dict is given then parse a DocumentProto from it; str is given, then consider it as a JSON string and parse a DocumentProto from it; finally, one can also give DocumentProto directly, then depending on the copy, it builds a view or a copy from it.

  • copy (bool) – when document is given as a DocumentProto object, build a view (i.e. weak reference) from it or a deep copy from it.

  • field_resolver (Optional[Dict[str, str]]) – a map from field names defined in document (JSON, dict) to the field names defined in Protobuf. This is only used when the given document is a JSON string or a Python dict.

  • kwargs – other parameters to be set _after_ the document is constructed

Note

When document is a JSON string or Python dictionary object, the constructor will only map the values from known fields defined in Protobuf, all unknown fields are mapped to document.tags. For example,

d = Document({'id': '123', 'hello': 'world', 'tags': {'good': 'bye'}})

assert d.id == '123'  # true
assert d.tags['hello'] == 'world'  # true
assert d.tags['good'] == 'bye'  # true
property length

Get the length of of the document.

Return type

int

property weight

Return the weight of the document.

Return type

float

property modality

Get the modality of the document.

Return type

str

property content_hash

Get the content hash of the document.

update(source, exclude_fields=None, include_fields=None)[source]

Updates fields specified in include_fields from the source to current Document.

Parameters
  • source (Document) – source Document object.

  • exclude_fields (Optional[Tuple[str, …]]) – a tuple of field names that excluded from the current document, when not given the non-empty fields of the current document is considered as exclude_fields

  • include_fields (Optional[Tuple[str, …]]) – a tuple of field names that included from the source document

Note

*. destination will be modified in place, source will be unchanged

Return type

None

update_content_hash(exclude_fields=('id', 'chunks', 'matches', 'content_hash', 'parent_id'), include_fields=None)[source]

Update the document hash according to its content.

Parameters
  • exclude_fields (Optional[Tuple[str]]) – a tuple of field names that excluded when computing content hash

  • include_fields (Optional[Tuple[str]]) – a tuple of field names that included when computing content hash

Note

“exclude_fields” and “include_fields” are mutually exclusive, use one only

Return type

None

property id

The document id in hex string, for non-binary environment such as HTTP, CLI, HTML and also human-readable. it will be used as the major view.

Return type

str

property parent_id

The document’s parent id in hex string, for non-binary environment such as HTTP, CLI, HTML and also human-readable. it will be used as the major view.

Return type

str

property blob

Return blob, one of the content form of a Document.

Note

Use content to return the content of a Document

Return type

ndarray

property embedding

Return embedding of the content of a Document.

Return type

ndarray

property matches

Get all matches of the current document.

Return type

MatchSet

property chunks

Get all chunks of the current document.

Return type

ChunkSet

set_attrs(**kwargs)[source]

Bulk update Document fields with key-value specified in kwargs

See also

get_attrs() for bulk get attributes

get_attrs(*args)[source]

Bulk fetch Document fields and return a dict of the key-value pairs

See also

update() for bulk set/update attributes

Note

Arguments will be extracted using dunder_get .. highlight:: python .. code-block:: python

d = Document({‘id’: ‘123’, ‘hello’: ‘world’, ‘tags’: {‘id’: ‘external_id’, ‘good’: ‘bye’}})

assert d.id == ‘123’ # true assert d.tags[‘hello’] == ‘world’ # true assert d.tags[‘good’] == ‘bye’ # true assert d.tags[‘id’] == ‘external_id’ # true

res = d.get_attrs(*[‘id’, ‘tags__hello’, ‘tags__good’, ‘tags__id’])

assert res[‘id’] == ‘123’ # true assert res[‘tags__hello’] == ‘world’ # true assert res[‘tags__good’] == ‘bye’ # true assert res[‘tags__id’] == ‘external_id’ # true

Return type

Dict[str, Any]

property buffer

Return buffer, one of the content form of a Document.

Note

Use content to return the content of a Document

Return type

bytes

property text

Return text, one of the content form of a Document.

Note

Use content to return the content of a Document

property uri

Return the URI of the document.

Return type

str

property mime_type

Get MIME type of the document

Return type

str

property content_type

Return the content type of the document, possible values: text, blob, buffer

Return type

str

property content

Return the content of the document. It checks whichever field among blob, text, buffer has value and return it.

See also

blob, buffer, text

Return type

~DocumentContentType

property granularity

Return the granularity of the document.

property score

Return the score of the document.

convert_buffer_to_blob(**kwargs)[source]

Assuming the buffer is a _valid_ buffer of Numpy ndarray, set blob accordingly.

Parameters

kwargs – reserved for maximum compatibility when using with ConvertDriver

Note

One can only recover values not shape information from pure buffer.

convert_buffer_image_to_blob(color_axis=- 1, **kwargs)[source]

Convert an image buffer to blob

Parameters
  • color_axis (int) – the axis id of the color channel, -1 indicates the color channel info at the last axis

  • kwargs – reserved for maximum compatibility when using with ConvertDriver

convert_blob_to_uri(width, height, resize_method='BILINEAR', **kwargs)[source]

Assuming blob is a _valid_ image, set uri accordingly

convert_uri_to_blob(color_axis=- 1, uri_prefix=None, **kwargs)[source]

Convert uri to blob

Parameters
  • color_axis (int) – the axis id of the color channel, -1 indicates the color channel info at the last axis

  • kwargs – reserved for maximum compatibility when using with ConvertDriver

convert_data_uri_to_blob(color_axis=- 1, **kwargs)[source]

Convert data URI to image blob

Parameters
  • color_axis (int) – the axis id of the color channel, -1 indicates the color channel info at the last axis

  • kwargs – reserved for maximum compatibility when using with ConvertDriver

convert_uri_to_buffer(**kwargs)[source]

Convert uri to buffer Internally it downloads from the URI and set buffer.

Parameters

kwargs – reserved for maximum compatibility when using with ConvertDriver

convert_uri_to_data_uri(charset='utf-8', base64=False, **kwargs)[source]

Convert uri to data uri. Internally it reads uri into buffer and convert it to data uri

Parameters
  • charset (str) – charset may be any character set registered with IANA

  • base64 (bool) – used to encode arbitrary octet sequences into a form that satisfies the rules of 7bit. Designed to be efficient for non-text 8 bit and binary data. Sometimes used for text data that frequently uses non-US-ASCII characters.

  • kwargs – reserved for maximum compatibility when using with ConvertDriver

convert_buffer_to_uri(charset='utf-8', base64=False, **kwargs)[source]

Convert buffer to data uri. Internally it first reads into buffer and then converts it to data URI.

Parameters
  • charset (str) – charset may be any character set registered with IANA

  • base64 (bool) – used to encode arbitrary octet sequences into a form that satisfies the rules of 7bit. Designed to be efficient for non-text 8 bit and binary data. Sometimes used for text data that frequently uses non-US-ASCII characters.

  • kwargs – reserved for maximum compatibility when using with ConvertDriver

convert_text_to_uri(charset='utf-8', base64=False, **kwargs)[source]

Convert text to data uri.

Parameters
  • charset (str) – charset may be any character set registered with IANA

  • base64 (bool) – used to encode arbitrary octet sequences into a form that satisfies the rules of 7bit.

Designed to be efficient for non-text 8 bit and binary data. Sometimes used for text data that frequently uses non-US-ASCII characters. :param kwargs: reserved for maximum compatibility when using with ConvertDriver

convert_uri_to_text(**kwargs)[source]

Assuming URI is text, convert it to text

Parameters

kwargs – reserved for maximum compatibility when using with ConvertDriver

convert_content_to_uri(**kwargs)[source]

Convert content in URI with best effort

Parameters

kwargs – reserved for maximum compatibility when using with ConvertDriver

MergeFrom(doc)[source]

Merge the content of target :param:doc into current document.

CopyFrom(doc)[source]

Copy the content of target :param:doc into current document.

plot(output=None, inline_display=False)[source]

Visualize the Document recursively.

Parameters
  • output (Optional[str]) – a filename specifying the name of the image to be created, the suffix svg/jpg determines the file type of the output image

  • inline_display (bool) – show image directly inside the Jupyter Notebook

Return type

None

property non_empty_fields

Return the set fields of the curren

Return type

Tuple[str]

class jina.DocumentSet(docs_proto)[source]

Bases: collections.abc.MutableSequence

DocumentSet is a mutable sequence of Document. It gives an efficient view of a list of Document. One can iterate over it like a generator but ALSO modify it, count it, get item, or union two ‘DocumentSet’s using the ‘+’ and ‘+=’ operators.

Parameters

docs_proto (Union['RepeatedContainer', Sequence['Document']]) – A list of Document

Set constructor method.

insert(index, doc)[source]

Insert :param:`doc.proto` at :param:`index` into the list of :class:`DocumentSet .

Parameters
  • index (int) – Position of the insertion.

  • doc (Document) – The doc needs to be inserted.

Return type

None

append(doc)[source]

Append :param:`doc` in DocumentSet.

Parameters

doc (Document) – The doc needs to be appended.

Return type

Document

Returns

Appended list.

add(doc)[source]

Shortcut to append(), do not override this method.

Return type

Document

extend(iterable)[source]

Extend an iterable to DocumentSet.

Return type

None

clear()[source]

Clear the data of DocumentSet

reverse()[source]

In-place reverse the sequence.

build()[source]

Build a doc_id to doc mapping so one can later index a Document using doc_id as string key.

sort(*args, **kwargs)[source]

Sort the list of DocumentSet.

traverse(traversal_paths)[source]

Return a DocumentSet that traverses this DocumentSet object according to the traversal_paths.

Parameters

traversal_paths (Iterable[str]) – a list of string that represents the traversal path

Example on traversal_paths:

  • [r]: docs in this DocumentSet

  • [m]: all match-documents at adjacency 1

  • [c]: all child-documents at granularity 1

  • [cc]: all child-documents at granularity 2

  • [mm]: all match-documents at adjacency 2

  • [cm]: all match-document at adjacency 1 and granularity 1

  • [r, c]: docs in this DocumentSet and all child-documents at granularity 1

Return type

DocumentSet

property all_embeddings

Return all embeddings from every document in this set as a ndarray

Returns

The corresponding documents in a DocumentSet, and the documents have no embedding in a DocumentSet.

Return type

A tuple of embedding in np.ndarray

property all_contents

Return all embeddings from every document in this set as a ndarray

Returns

The corresponding documents in a DocumentSet, and the documents have no contents in a DocumentSet.

Return type

A tuple of embedding in np.ndarray

new()[source]

Create a new empty document appended to the end of the set.

Return type

Document

jina.Encoder

alias of jina.executors.encoders.BaseEncoder

jina.Evaluator

alias of jina.executors.evaluators.BaseEvaluator

jina.Executor

alias of jina.executors.BaseExecutor

class jina.Flow(args=None, env=None, **kwargs)[source]

Bases: jina.flow.mixin.crud.CRUDFlowMixin, jina.flow.base.BaseFlow

The synchronous version of AsyncFlow.

For proper usage see this guide <https://docs.jina.ai/chapters/flow/index.html>

jina.Indexer

alias of jina.executors.indexers.BaseIndexer

class jina.Message(envelope, request, *args, **kwargs)[source]

Bases: object

Message is one of the primitive data type in Jina.

It offers a Pythonic interface to allow users access and manipulate jina.jina_pb2.MessageProto object without working with Protobuf itself.

A container class for jina_pb2.MessageProto. Note, the Protobuf version of jina_pb2.MessageProto contains a jina_pb2.EnvelopeProto and jina_pb2.RequestProto. Here, it contains:

  • a jina_pb2.EnvelopeProto object

  • and one of:
    • a Request object wrapping jina_pb2.RequestProto

    • a jina_pb2.RequestProto object

It provide a generic view of as jina_pb2.MessageProto, allowing one to access its member, request and envelope as if using jina_pb2.MessageProto object directly.

This class also collected all helper functions related to jina_pb2.MessageProto into one place.

Parameters
  • envelope (Union[bytes, EnvelopeProto, None]) – Represents a Envelope, a part of the Message.

  • request (Union[bytes, RequestProto]) – Represents a Request

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Set constructor method.

property request

Get the request.

Return type

Request

property proto

Get the RequestProto.

Return type

MessageProto

property is_data_request

check if the request is not a control request

Warning

If request change the type, e.g. by leveraging the feature of oneof, this property wont be updated. This is not considered as a good practice.

Return type

bool

dump()[source]

Get the message in a list of bytes.

Return type

List[bytes]

property colored_route

Get the string representation of the routes in a message.

Return type

str

Returns

add_route(name, identity)[source]

Add a route to the envelope.

Parameters
  • name (str) – the name of the pod service

  • identity (str) – the identity of the pod service

property size

Get the size in bytes.

To get the latest size, use it after dump()

update_timestamp()[source]

Update the timestamp of the last route

property response

Get the response of the message in protobuf.

Note

This should be only called at Gateway

Return type

Request

merge_envelope_from(msgs)[source]

Extend the current envelope routes with :param: msgs.

Parameters

msgs (List[Message]) – List of msgs.

add_exception(ex=None, executor=None)[source]

Add exception to the last route in the envelope

Parameters

ex (Optional[ForwardRef]) – Exception to be added

Return type

None

Returns

property is_error

Return if the envelope status is ERROR.

Return type

bool

property is_ready

Return if the envelope status is READY.

Return type

bool

class jina.MultimodalDocument(document=None, chunks=None, modality_content_map=None, copy=False, **kwargs)[source]

Bases: jina.types.document.Document

MultimodalDocument is a data type created based on Jina primitive data type Document.

It shares the same methods and properties with Document, while it focus on modality at chunk level.

Warning

  • It assumes that every chunk of a document belongs to a different modality.

  • It assumes that every MultimodalDocument have at least two chunks.

Parameters
  • document (Optional[~DocumentSourceType]) – the document to construct from. If bytes is given then deserialize a DocumentProto; dict is given then parse a DocumentProto from it; str is given, then consider it as a JSON string and parse a DocumentProto from it; finally, one can also give DocumentProto directly, then depending on the copy, it builds a view or a copy from it.

  • chunks (Optional[Sequence[Document]]) – the chunks of the multimodal document to initialize with. Expected to received a list of Document, with different modalities.

  • copy (bool) – when document is given as a DocumentProto object, build a view (i.e. weak reference) from it or a deep copy from it.

  • kwargs – other parameters to be set

Param

modality_content_mapping: A Python dict, the keys are the modalities and the values are the content of the Document

Warning

property is_valid

A valid MultimodalDocument should meet the following requirements:

  • Document should consist at least 2 chunks.

  • Length of modality is not identical to length of chunks.

Return type

bool

property modality_content_map

Get the mapping of modality and content, the mapping is represented as a dict, the keys are the modalities of the chunks, the values are the corresponded content of the chunks.

Return type

Dict

Returns

the mapping of modality and content extracted from chunks.

property modalities

Get all modalities of the MultimodalDocument.

Return type

List[str]

Returns

List of modalities extracted from chunks of the document.

update_content_hash(exclude_fields=('id', 'matches', 'content_hash'))[source]

Update content hash of the document by including chunks when computing the hash

Return type

None

class jina.NdArray(proto=None, is_sparse=False, dense_cls=<class 'jina.types.ndarray.dense.numpy.DenseNdArray'>, sparse_cls=<class 'jina.types.ndarray.sparse.scipy.SparseNdArray'>, *args, **kwargs)[source]

Bases: jina.types.ndarray.BaseNdArray

NdArray is one of the primitive data type in Jina.

It offers a Pythonic interface to allow users access and manipulate jina.jina_pb2.NdArrayProto object without working with Protobuf itself.

A generic view of the Protobuf NdArray, unifying the view of DenseNdArray and SparseNdArray

This class should be used in nearly all the Jina context.

Simple usage:

# start from empty proto
a = NdArray()

# start from an existig proto
a = NdArray(doc.embedding)

# set value
a.value = np.random.random([10, 5])

# get value
print(a.value)

# set value to a TF sparse tensor
a.is_sparse = True
a.value = SparseTensor(...)
print(a.value)

Advanced usage:

NdArray also takes a dense NdArray and a sparse NdArray constructor as arguments. You can consider them as the backend for dense and sparse NdArray. The combination is your choice, it could be:

# numpy (dense) + scipy (sparse)
from .dense.numpy import DenseNdArray
from .sparse.scipy import SparseNdArray
NdArray(dense_cls=DenseNdArray, sparse_cls=SparseNdArray)

# numpy (dense) + pytorch (sparse)
from .dense.numpy import DenseNdArray
from .sparse.pytorch import SparseNdArray
NdArray(dense_cls=DenseNdArray, sparse_cls=SparseNdArray)

# numpy (dense) + tensorflow (sparse)
from .dense.numpy import DenseNdArray
from .sparse.tensorflow import SparseNdArray
NdArray(dense_cls=DenseNdArray, sparse_cls=SparseNdArray)

Once you set sparse_cls, it will only accept the data type in that particular type. That is, you can not use a NdArray equipped with Tensorflow sparse to set/get Pytorch or Scipy sparse matrices.

Parameters
  • proto (Optional[NdArrayProto]) – the protobuf message, when not given then create a new one via get_null_proto()

  • is_sparse (bool) – if the ndarray is sparse, can be changed later

  • dense_cls (Type[BaseDenseNdArray]) – the to-be-used class for DenseNdArray when is_sparse=False

  • sparse_cls (Type[BaseSparseNdArray]) – the to-be-used class for SparseNdArray when is_sparse=True

  • args

  • kwargs

null_proto()[source]

Get the new protobuf representation.

property value

Get the value of protobuf and return in corresponding type.

class jina.QueryLang(querylang=None, copy=False)[source]

Bases: jina.types.mixin.ProtoTypeMixin

QueryLang is one of the primitive data type in Jina.

It offers a Pythonic interface to allow users access and manipulate jina.jina_pb2.QueryLangProto object without working with Protobuf itself.

To create a QueryLang object from a Dict containing the name of a BaseDriver,

and the parameters to override, simply:

from jina import QueryLang
ql = QueryLang({name: 'SliceQL', priority: 1, parameters: {'start': 3, 'end': 1}})

Warning

The BaseDriver needs to be a QuerySetReader to be able to read the QueryLang

One can also build a :class`QueryLang` from JSON string, bytes, dict or directly from a protobuf object.

A QueryLang object (no matter how it is constructed) can be converted to protobuf object by using:

# to protobuf object
ql.as_pb_object
Parameters
  • querylang (Optional[QueryLangSourceType]) – the query language source to construct from, acceptable types include: jina_pb2.QueryLangProto, bytes, str, Dict, Tuple.

  • copy (bool) – when querylang is given as a QueryLangProto object, build a view (i.e. weak reference) from it or a deep copy from it.

Set constructor method.

property priority

Get the priority of this query language. The query language only takes effect when if it has a higher priority than the internal one with the same name

Return type

int

property name

Get the name of the driver that the query language attached to.

Return type

str

class jina.QueryLangSet(querylang_protos)[source]

Bases: collections.abc.MutableSequence

QueryLangSet is a mutable sequence of QueryLang. It gives an efficient view of a list of Document. One can iterate over it like a generator but ALSO modify it, count it, get item.

Parameters

querylang_protos (RepeatedCompositeContainer) – A list of QueryLangProto

Set constructor method.

insert(index, ql)[source]

Insert :param:`ql` at :param:`index` into _querylangs_proto.

Return type

None

append(value)[source]

Append :param:`value` in _querylangs_proto.

extend(iterable)[source]

Extend an iterable to :class:QueryLangSet.

Return type

None

clear()[source]

Clear _querylangs_proto set.

reverse()[source]

Reverse order of _querylangs_proto set.

build()[source]

Build a name to QueryLang mapping so one can later index a QueryLang using name as string key.

jina.Ranker

alias of jina.executors.rankers.BaseRanker

class jina.Request(request=None, envelope=None, copy=False)[source]

Bases: jina.types.mixin.ProtoTypeMixin

Request is one of the primitive data type in Jina.

It offers a Pythonic interface to allow users access and manipulate jina.jina_pb2.RequestProto object without working with Protobuf itself.

A container for serialized jina_pb2.RequestProto that only triggers deserialization and decompression when receives the first read access to its member.

It overrides __getattr__() to provide the same get/set interface as an jina_pb2.RequestProto object.

Parameters
  • request (Union[bytes, dict, str, RequestProto, None]) – The request.

  • envelope (Optional[EnvelopeProto]) – EnvelopeProto object.

  • copy (bool) – Copy the request if copy is True.

Set constructor method.

is_used

Return True when request has been r/w at least once

property body

Return the request type, raise ValueError if request_type not set.

as_typed_request(request_type)[source]

Change the request class according to the one_of value in body.

property request_type

Return the request body type, when not set yet, return None.

Return type

Optional[str]

property proto

Cast self to a jina_pb2.RequestProto. This will trigger is_used. Laziness will be broken and serialization will be recomputed when calling SerializeToString().

Return type

RequestProto

SerializeToString()[source]

Convert serialized data to string.

Return type

bytes

property queryset

Get the queryset in QueryLangSet type.

Return type

QueryLangSet

as_response()[source]

Return a weak reference of this object but as Response object. It gives a more consistent semantics on the client.

class jina.Response[source]

Bases: object

Response is the Request object returns from the flow. Right now it shares the same representation as Request. At 0.8.12, Response is a simple alias. But it does give a more consistent semantic on the client API: send a Request and receive a Response.

jina.Segmenter

alias of jina.executors.segmenters.BaseSegmenter