共计 2635 个字符,预计需要花费 7 分钟才能阅读完成。
【导语】:云原生的神经搜寻框架。
简介
Jina 是一个神经搜寻框架,简略疾速,能够在几分钟内构建 SOTA 和可扩大的深度学习搜寻应用程序。
- 反对简直所有的数据类型 - 反对对任何类型的非结构化数据进行大规模索引和查问,比方视频、图片、文本、音乐、源代码、PDF 等。
- 疾速和云原生 - 分布式架构,具备可扩展性,反对云原生、容器化、并行、分片、异步调度、HTTP/gRPC/WebSocket 协定。
- 高效疾速 - 几分钟内就能够构建一个深度学习搜寻零碎。
我的项目地址:
https://github.com/jina-ai/jina
装置
- 通过 PyPI 装置:
pip install jina
- 通过 Conda 装置:
conda install jina -c conda-forge
- 通过 Docker 装置:
docker run jinaai/jina:latest
官网例子
图片搜寻
构建命令:
jina hello fashion
这个过程会下载 Fashion-MNIST 训练和测试数据集,并训练集中索引 60,000 张图像。而后从测试集中随机抽取图像作为查问,整个过程大概须要 1 分钟。
聊天机器人
构建命令:
pip install "jina[demo]"
jina hello chatbot
这会下载 CovidQA 数据集并通知 Jina 应用 MPNet 索引 418 个问答集。索引过程在 CPU 上大概须要 1 分钟。而后它会关上一个网页,能够在其中输出问题并询问 Jina。
构建
Document、Executor 和 Flow 是 Jina 中的三个基本概念。利用这三个组件,就能够构建一个应用程序。
- 📄 Document 是 Jina 中的根本数据类型;
- ⚙️ Executor 是 Jina 解决 Documents 的形式;
- 🔀 Flow 是 Jina 精简和调配 Executor 的形式。
1. 复制以下最小示例并运行
import numpy as np
from jina import Document, DocumentArray, Executor, Flow, requests
class CharEmbed(Executor): # a simple character embedding with mean-pooling
offset = 32 # letter `a`
dim = 127 - offset + 1 # last pos reserved for `UNK`
char_embd = np.eye(dim) * 1 # one-hot embedding for all chars
@requests
def foo(self, docs: DocumentArray, **kwargs):
for d in docs:
r_emb = [ord(c) - self.offset if self.offset <= ord(c) <= 127 else (self.dim - 1) for c in d.text]
d.embedding = self.char_embd[r_emb, :].mean(axis=0) # average pooling
class Indexer(Executor):
_docs = DocumentArray() # for storing all documents in memory
@requests(on='/index')
def foo(self, docs: DocumentArray, **kwargs):
self._docs.extend(docs) # extend stored `docs`
@requests(on='/search')
def bar(self, docs: DocumentArray, **kwargs):
docs.match(self._docs, metric='euclidean')
f = Flow(port_expose=12345, protocol='http', cors=True).add(uses=CharEmbed, parallel=2).add(uses=Indexer) # build a Flow, with 2 parallel CharEmbed, tho unnecessary
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
2. 在浏览器中关上(http://localhost:12345/docs),点击 /search 并输出以下文本,点击 Execute 按钮:
{"data": [{"text": "@requests(on=something)"}]}
3. 不喜爱 GUI 也能够应用 Python 命令行来做。放弃步骤 1 的服务运行,通过 Python 创立客户端:
from jina import Client, Document
from jina.types.request import Response
def print_matches(resp: Response): # the callback function invoked when task is done
for idx, d in enumerate(resp.docs[0].matches[:3]): # print top-3 matches
print(f'[{idx}]{d.scores["euclidean"].value:2f}:"{d.text}"')
c = Client(protocol='http', port=12345) # connect to localhost:12345
c.post('/search', Document(text='request(on=something)'), on_done=print_matches)
打印如下后果:
Client@1608[S]:connected to the gateway at localhost:12345!
[0]0.168526: "@requests(on='/index')"
[1]0.181676: "@requests(on='/search')"
[2]0.218218: "from jina import Document, DocumentArray, Executor, Flow, requests"
开源前哨
日常分享热门、乏味和实用的开源我的项目。参加保护 10 万 + Star 的开源技术资源库,包含:Python、Java、C/C++、Go、JS、CSS、Node.js、PHP、.NET 等。
正文完