跳到主要內容

SingleStoreDB

SingleStoreDB 是一個高效能分散式 SQL 資料庫,支援雲端和本地部署。它提供向量儲存和向量函數,包括 dot_producteuclidean_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.
if "OPENAI_API_KEY" not in os.environ:
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)

此頁面是否對您有幫助?