跳至主要內容

ThirdAI NeuralDB

NeuralDB 是一個由 ThirdAI 開發,對 CPU 友善且可微調的向量儲存區。

初始化

有兩種初始化方法

  • 從頭開始:基本模型
  • 從檢查點:載入先前儲存的模型

對於以下所有初始化方法,如果設定了 THIRDAI_KEY 環境變數,則可以省略 thirdai_key 參數。

ThirdAI API 金鑰可以在 https://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",
)
API 參考:NeuralDBVectorStore

插入文件來源

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 文件之一的段落。除了文字之外,文檔的元數據字段還包含諸如文檔的 ID、此文檔的來源(來自哪個文件)以及文檔的分數等信息。

# This returns a list of LangChain Document objects
documents = vectorstore.similarity_search("query", k=10)

微調

NeuralDBVectorStore 可以根據使用者行為和特定領域的知識進行微調。它可以通過兩種方式進行微調

  1. 關聯:向量儲存區將源短語與目標短語相關聯。當向量儲存區看到源短語時,它也會考慮與目標短語相關的結果。
  2. 投票:向量儲存區會提高特定查詢的文檔分數。當您想要根據使用者行為微調向量儲存區時,這非常有用。例如,如果使用者搜尋「汽車是如何製造的」並且喜歡返回的 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),
]
)

此頁面有幫助嗎?