Skip to main content
NeuralDB 是由 ThirdAI 开发的一款对 CPU 友好且支持微调的向量存储。

初始化

有两种初始化方式:
  • 从零开始:使用基础模型
  • 从检查点加载:加载之前保存的模型
对于以下所有初始化方式,如果已设置 THIRDAI_KEY 环境变量,则可以省略 thirdai_key 参数。 ThirdAI API 密钥可在 www.thirdai.com/try-bolt/ 获取。 使用此集成需要先通过 pip install -qU langchain-community 安装 langchain-community
from langchain_community.vectorstores import NeuralDBVectorStore

# From scratch
vectorstore = NeuralDBVectorStore.from_scratch(thirdai_key="your-thirdai-key")

# From checkpoint
vectorstore = NeuralDBVectorStore.from_checkpoint(
    # Path to a NeuralDB checkpoint. For example, if you call
    # vectorstore.save("/path/to/checkpoint.ndb") in one script, then you can
    # call NeuralDBVectorStore.from_checkpoint("/path/to/checkpoint.ndb") in
    # another script to load the saved model.
    checkpoint="/path/to/checkpoint.ndb",
    thirdai_key="your-thirdai-key",
)

插入文档来源

vectorstore.insert(
    # If you have PDF, DOCX, or CSV files, you can directly pass the paths to the documents
    sources=["/path/to/doc.pdf", "/path/to/doc.docx", "/path/to/doc.csv"],
    # When True this means that the underlying model in the NeuralDB will
    # undergo unsupervised pretraining on the inserted files. Defaults to True.
    train=True,
    # Much faster insertion with a slight drop in performance. Defaults to True.
    fast_mode=True,
)

from thirdai import neural_db as ndb

vectorstore.insert(
    # If you have files in other formats, or prefer to configure how
    # your files are parsed, then you can pass in NeuralDB document objects
    # like this.
    sources=[
        ndb.PDF(
            "/path/to/doc.pdf",
            version="v2",
            chunk_size=100,
            metadata={"published": 2022},
        ),
        ndb.Unstructured("/path/to/deck.pptx"),
    ]
)

相似度搜索

要查询向量存储,可以使用 LangChain 标准向量存储方法 similarity_search,它返回一个 LangChain Document 对象列表。每个文档对象代表已索引文件中的一段文本,例如来自某个已索引 PDF 文件的一个段落。除文本内容外,文档的 metadata 字段还包含文档 ID、文档来源(来自哪个文件)以及文档分数等信息。
# This returns a list of LangChain Document objects
documents = vectorstore.similarity_search("query", k=10)

微调

NeuralDBVectorStore 可以针对用户行为和特定领域知识进行微调,支持以下两种微调方式:
  1. 关联(Association):向量存储将源短语与目标短语关联起来。当向量存储遇到源短语时,也会考虑与目标短语相关的结果。
  2. 点赞(Upvoting):向量存储对特定查询的某篇文档提升评分权重。当你希望根据用户行为对向量存储进行微调时非常有用。例如,如果用户搜索”汽车是如何制造的”并喜欢返回的 id 为 52 的文档,则可以对该查询的 id 52 文档进行点赞。
vectorstore.associate(source="source phrase", target="target phrase")
vectorstore.associate_batch(
    [
        ("source phrase 1", "target phrase 1"),
        ("source phrase 2", "target phrase 2"),
    ]
)

vectorstore.upvote(query="how is a car manufactured", document_id=52)
vectorstore.upvote_batch(
    [
        ("query 1", 52),
        ("query 2", 20),
    ]
)