Jina 轻松学 —— 三个基本元素

本文介绍了Jina中的三个核心元素:Document作为基本数据类型,Executor作为处理单元,Flow用于组合Executor构建搜索框架。Document支持文本、图片等多种类型,具有层次关系属性;Executor能实现数据预处理、向量化等功能,兼容多种机器学习框架;Flow允许组合Executor并在本地或云端作为服务运行。了解这些基础知识后,可以灵活构建Jina服务。

 Document、Executor、Flow是Jina中的三个基本元素

85b243a7692564e2d7e2805ba07dcbce.png

📄 Document —— Jina中的基本数据类型

⚙️ Executor —— Jina处理Document的基本单元

🔀 Flow —— 将Executor组合后得到的Jina搜索框架

本期教程将带来关于Document、Executor、Flow的简单使用介绍,让你快速熟悉Jina的工作原理!

 Document 和 DocumentArray 


⭐️ Document是Jina中的基本数据类型,可以是文字、图片、视频、音频、图像或3D mesh文件。

⭐️ DocumentArray由 Document的序列组成,也是Executor的直接处理对象,即Executor的输入和输出。

⭐️ Document相当于Jina中的np.float,而DocumentArray相当于Jina中的np.ndarray 。

 Document 


Document的创建

from jina import Document

d = Document()

Document的文本属性

一个Document 只可以拥有一种类型的文本属性。非常方便的是,当使用Document.content构造文本属性时,输入内容将自动归类为text,buffer,blob或uri中的一种。Document还有元数据、层次关系、相关性等其他类别的属性。

1cec4cf880460be32b13f1a466e7145e.png

Document的层次关系属性

可以为每个Document添加chunks和matches构成相应的层次关系,以便后续Executor在处理DocumentArray时进行纵向递归和横向递归。使用Document.plot( )画出层次关系图:

ee6c9f647f3fb7a0a4b06a3146ac6691.png

  DocumentArray 


DocumentArray由 Document的序列组成,可以像Python list一样对DocumentArray进行构造、切片、排序、删除、遍历等操作。

DocumentArray的创建

from jina import DocumentArray, Document
# from list
da1 = DocumentArray([Document(), Document()])
# from generator
da2 = DocumentArray((Document() for _ in range(10)))
# from another `DocumentArray`
da3 = DocumentArray(da2)

DocumentArray的元素遍历

根据DocumentArray中不同Document的层次关系,使用DocumentArray.traverse可以遍历自定义的层次路径,并返回与所提供的遍历路径匹配的DocumentArrays。

75e5d0c004847a89a4f0352b1ca39289.png

DocumentArray的元素匹配

DocumentArray.match可以在两个DocumentArray对象之间找到最接近的Document(要求每个Document有相同长度的embedding)

fc19e709795c7e14bf1aeac8e949ef5e.png

 Executor 


Executor是Jina中处理Document的基本单元,可以实现非常丰富多样的功能,包括数据预处理、多模态数据向量化、数据索引……并且支持Tensorflow, Pytorch, Pytorch Lightning, Fastai, Mindspore, PaddlePaddle, Scikit-learn等多种机器学习框架。

Executor的创建

  • Python代码

from jina import Executor, Flow, Document, requests

class MyExecutor(Executor):

    @requests
    def foo(self, **kwargs):
        print(kwargs)

f = Flow().add(uses=MyExecutor)

with f:
    f.post(on='/random_work', inputs=Document(), on_done=print)
  • Yaml文件+Python代码

    创建my.yml

jtype: MyExecutor
metas:
  py_modules:
    - foo.py
  name: awesomeness
  description: my first awesome executor
requests:
  /random_work: foo

                使用Yaml创建Executor

from jina import Flow, Document

f = Flow().add(uses='my.yml')

with f:
    f.post(on='/random_work', inputs=Document(), on_done=print)

 Flow 


Flow是将多个Executor组合后得到的Jina搜索框架。可以将Flow作为服务器运行在本地或云端,并允许多个客户端从公共/私有网络通过gRPC/REST/WebSocket等协议方式来访问。

Flow的创建

  • 服务端和客户端一起创建

from jina import Executor, Flow, Document, requests

class MyExecutor(Executor):

    @requests
    def foo(self, **kwargs):
        print(kwargs)

f = Flow().add(uses=MyExecutor)

with f:
    f.post(on='/random_work', inputs=Document(), on_done=print)
  • 服务端和客户端单独创建

    服务端

from jina import Flow, Executor, requests


class MyExecutor(Executor):

    @requests(on='/bar')
    def foo(self, docs, **kwargs):
        print(docs)


f = Flow(port_expose=12345).add(name='myexec1', uses=MyExecutor)

with f:
    f.block()

         客户端

from jina import Client, Document

c = Client(port_expose=12345)
c.post(on='/bar', inputs=Document(), on_done=print)

Flow的可视化——Flow.plot( )

6614078549016b599d3a7c944450935a.png

 Jina编程小技巧 


了解Document、Executor、Flow三个Jina中的基本元素后,你就可以搭建任意自己想要的Jina 服务了。

下面的9个tips可以帮你写出更简洁、更高效的Jina代码:

  1. 一行代码导入各Jina模块

  2. 使用 Python generator 作为Flow 的输入

  3. 使用request_size来决定每个请求中包含多少个Document

  4. 构造Executor时 __init__ 可省略

  5. 使用@request修饰函数时若没有指定on=参数,则此函数会响应所有的request

  6. 非必需的参数放在 **kwargs中

  7. 对Executor进行debug时 ,只需创建一个Executor实例并调用其功能即可

  8. 如果向Flow发送的请求中不包含Document ,则只需向Flow发送parameters

  9. 使用append将chunks添加到Document中,而不要在创建Document时写入chunks

点击下方 “阅读原文”,获得更详细的Jina 相关教程


👇 往期精彩

93fb589718f2807852527b37377e7f04.png

Jina·拥抱社区!即刻申请,丰富周边送上门

8179452ad79ae4c91f487a8d54ca79b3.png

Jina 2.0 快速入门指"北"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值