SemaDB
來自 SemaFind 的 SemaDB 是一個簡單易用的向量相似度資料庫,用於建構 AI 應用程式。託管的
SemaDB Cloud
提供簡單易用的開發者體驗,讓您輕鬆入門。
完整的 API 文件以及範例和互動式遊樂場可在 RapidAPI 上取得。
此筆記本示範了 SemaDB Cloud
向量儲存的使用方法。
您需要安裝 langchain-community
,使用 pip install -qU langchain-community
才能使用此整合。
載入文件嵌入 (Load document embeddings)
為了在本地執行,我們使用 Sentence Transformers,它通常用於嵌入句子。您可以使用 LangChain 提供的任何嵌入模型。
%pip install --upgrade --quiet sentence_transformers
from langchain_huggingface import HuggingFaceEmbeddings
model_name = "sentence-transformers/all-mpnet-base-v2"
embeddings = HuggingFaceEmbeddings(model_name=model_name)
API 參考:HuggingFaceEmbeddings
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=400, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
print(len(docs))
API 參考:TextLoader | CharacterTextSplitter
114
連接到 SemaDB (Connect to SemaDB)
SemaDB Cloud 使用 RapidAPI 金鑰進行身份驗證。您可以建立一個免費的 RapidAPI 帳戶來取得您的金鑰。
import getpass
import os
if "SEMADB_API_KEY" not in os.environ:
os.environ["SEMADB_API_KEY"] = getpass.getpass("SemaDB API Key:")
SemaDB API Key: ········
from langchain_community.vectorstores import SemaDB
from langchain_community.vectorstores.utils import DistanceStrategy
API 參考:SemaDB | DistanceStrategy
SemaDB 向量儲存的參數直接反映了 API。
- "mycollection":是我們將儲存這些向量的集合名稱。
- 768:是向量的維度。在我們的例子中,sentence transformer 嵌入會產生 768 維向量。
- API_KEY:是您的 RapidAPI 金鑰。
- embeddings:對應於如何產生文件、文本和查詢的嵌入。
- DistanceStrategy:是使用的距離度量。如果使用 COSINE,包裝器會自動正規化向量。
db = SemaDB("mycollection", 768, embeddings, DistanceStrategy.COSINE)
# Create collection if running for the first time. If the collection
# already exists this will fail.
db.create_collection()
True
SemaDB 向量儲存包裝器會將文件文本作為點元數據添加,以便稍後收集。不建議儲存大量的文本塊。如果您要索引大量的集合,我們建議儲存對文件的參考,例如外部 ID。
db.add_documents(docs)[:2]
['813c7ef3-9797-466b-8afa-587115592c6c',
'fc392f7f-082b-4932-bfcc-06800db5e017']
相似度搜尋 (Similarity Search)
我們使用預設的 LangChain 相似度搜尋介面來搜尋最相似的句子。
query = "What did the president say about Ketanji Brown Jackson"
docs = db.similarity_search(query)
print(docs[0].page_content)
And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.
docs = db.similarity_search_with_score(query)
docs[0]
(Document(page_content='And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'source': '../../how_to/state_of_the_union.txt', 'text': 'And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.'}),
0.42369342)
清除 (Clean up)
您可以刪除集合以移除所有資料。
db.delete_collection()
True