未提供项目描述
项目描述
naan
目录
什么是Naan?
- Naan是围绕FAISS索引的包装器,为添加到索引中的向量提供元数据存储和检索。
- Naan的工作是消除在编码并添加到索引之前保留原始内容的繁琐任务。
- Naan不是一个向量数据库。所有向量搜索操作都需要FAISS。
安装
pip install naan
索引数据
要查看Naan的实际应用,我们首先需要一些数据来嵌入
from io import StringIO
import requests
import json
res = requests.get("https://raw.githubusercontent.com/masci/naan/main/example/sentences.json")
sentences = json.load(StringIO(res.text))
Naan不会干扰你管理FAISS索引,因此第一步总是设置FAISS方面的事情
from sentence_transformers import SentenceTransformer
import faiss
model = SentenceTransformer("bert-base-nli-mean-tokens")
sentence_embeddings = model.encode(sentences)
dim = sentence_embeddings.shape[1]
index = faiss.IndexFlatL2(dim)
现在使用Naan包装FAISS索引并使用它来索引数据的时候到了
from naan import NaanDB
# Create a Naan database from scratch
db = NaanDB("db.naan", index, force_recreate=True)
db.add(sentence_embeddings, sentences)
Naan会将向量嵌入添加到FAISS索引中,并存储原始句子。这样,向量搜索将看起来像这样
# Reopen an existing Naan database
db = NaanDB("db.naan")
query_embeddings = model.encode(["The book is on the table"])
# Naan's search API is the same as FAISS, let's get the 3 closest vectors
results = db.search(query_embeddings, 3)
for result in results:
print(result)
# Document(vector_id=11451, content='A group of people sitting around a desk.', embeddings=None)
# Document(vector_id=2754, content='A close-up picture of a desk with a computer and papers on it.', embeddings=None)
# Document(vector_id=11853, content='A computer on a desk.', embeddings=None)
许可证
naan
是在MIT许可证下分发的。
项目详情
下载文件
下载适合您平台文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。
源代码分发
naan-0.0.4.tar.gz (6.4 kB 查看散列值)
构建发行版
naan-0.0.4-py3-none-any.whl (6.5 kB 查看哈希)
关闭
naan-0.0.4.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8e8d4b32e1c96ab6dea15e86df891f6a56f721e17085e5020ac6bb123b92b984 |
|
MD5 | 3018c3aaf27b99c03e7c80b105af5895 |
|
BLAKE2b-256 | 3153d2182f75c917b40fe8679f2ff9f847f5a2400bce534b4546ed7953737397 |
关闭
naan-0.0.4-py3-none-any.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cedc458dfc35a5d3cf9c1e53751698858a829d9407a19e9493b9e09d6a66a5bf |
|
MD5 | ddd61c1950de5cff25b8bcd8bf5ac993 |
|
BLAKE2b-256 | 8618ba235729e63f180e6f483953630955ccbc5c676f7de0607f17d5aa029581 |