11k Star!登上 Github 熱榜前三的神經(jīng)搜索框架!
【導語】:云原生的神經(jīng)搜索框架。
簡介
Jina 是一個神經(jīng)搜索框架,簡單快速,可以在幾分鐘內(nèi)構(gòu)建 SOTA 和可擴展的深度學習搜索應用程序。
支持幾乎所有的數(shù)據(jù)類型-支持對任何類型的非結(jié)構(gòu)化數(shù)據(jù)進行大規(guī)模索引和查詢,比如視頻、圖片、文本、音樂、源代碼、PDF等。 快速和云原生-分布式架構(gòu),具有可擴展性,支持云原生、容器化、并行、分片、異步調(diào)度、HTTP/gRPC/WebSocket 協(xié)議。 高效快速-幾分鐘內(nèi)就可以構(gòu)建一個深度學習搜索系統(tǒng)。
項目地址:
https://github.com/jina-ai/jina
安裝
通過 PyPI 安裝:
pip install jina
通過 Conda 安裝:
conda install jina -c conda-forge
通過 Docker 安裝:
docker run jinaai/jina:latest
官方例子
圖片搜索
構(gòu)建命令:
jina hello fashion

這個過程會下載 Fashion-MNIST 訓練和測試數(shù)據(jù)集,并訓練集中索引 60,000 張圖像。然后從測試集中隨機抽取圖像作為查詢,整個過程大約需要1分鐘。

聊天機器人
構(gòu)建命令:
pip install "jina[demo]"
jina hello chatbot

這會下載 CovidQA 數(shù)據(jù)集并告訴 Jina 使用 MPNet 索引 418 個問答集。索引過程在 CPU 上大約需要 1 分鐘。然后它會打開一個網(wǎng)頁,可以在其中輸入問題并詢問 Jina。

構(gòu)建
Document、Executor 和 Flow 是 Jina 中的三個基本概念。利用這三個組件,就可以構(gòu)建一個應用程序。
?? Document 是 Jina 中的基本數(shù)據(jù)類型; ?? 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 創(chuàng)建客戶端:
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)
打印如下結(jié)果:
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"
還不過癮?試試它們
▲CPython 有 GIL 是因為當年設(shè)計的人偷懶嗎?
▲一個小破網(wǎng)站,居然比 Python 官網(wǎng)還牛逼
▲Schedule—比 Celery 更輕量級的周期任務調(diào)度工具
評論
圖片
表情
