跳转到主要内容

未提供项目描述

项目描述

naan

PyPI - Version PyPI - Python Version


目录

什么是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 查看哈希)

上传于 Python 3

由以下支持