SingleStoreDB
SingleStoreDB 是一款高性能分布式 SQL 数据库,支持在 云端 和本地部署。它提供向量存储和向量函数,包括 点积 (dot_product) 和 欧氏距离 (euclidean_distance),从而支持需要文本相似性匹配的 AI 应用。
此笔记本展示了如何使用使用 SingleStoreDB
的检索器。
# Establishing a connection to the database is facilitated through the singlestoredb Python connector.
# Please ensure that this connector is installed in your working environment.
%pip install --upgrade --quiet singlestoredb
从向量存储创建检索器
import getpass
import os
# We want to use OpenAIEmbeddings so we have to get the OpenAI API Key.
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import SingleStoreDB
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
# Setup connection url as environment variable
os.environ["SINGLESTOREDB_URL"] = "root:pass@localhost:3306/db"
# Load documents to the store
docsearch = SingleStoreDB.from_documents(
docs,
embeddings,
table_name="notebook", # use table with a custom name
)
# create retriever from the vector store
retriever = docsearch.as_retriever(search_kwargs={"k": 2})
使用检索器进行搜索
result = retriever.invoke("What did the president say about Ketanji Brown Jackson")
print(docs[0].page_content)