Vald
Vald 是一个高度可扩展的分布式快速近似最近邻 (ANN) 密集向量搜索引擎。
此笔记本展示了如何使用与 Vald
数据库相关的功能。
要运行此笔记本,您需要一个正在运行的 Vald 集群。查看 入门 以获取更多信息。
请参阅 安装说明。
%pip install --upgrade --quiet vald-client-python langchain-community
基本示例
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import Vald
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_text_splitters import CharacterTextSplitter
raw_documents = TextLoader("state_of_the_union.txt").load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
documents = text_splitter.split_documents(raw_documents)
model_name = "sentence-transformers/all-mpnet-base-v2"
embeddings = HuggingFaceEmbeddings(model_name=model_name)
db = Vald.from_documents(documents, embeddings, host="localhost", port=8080)
query = "What did the president say about Ketanji Brown Jackson"
docs = db.similarity_search(query)
docs[0].page_content
基于向量的相似性搜索
embedding_vector = embeddings.embed_query(query)
docs = db.similarity_search_by_vector(embedding_vector)
docs[0].page_content
带分数的相似性搜索
docs_and_scores = db.similarity_search_with_score(query)
docs_and_scores[0]
最大边缘相关性搜索 (MMR)
除了在检索器对象中使用相似性搜索外,您还可以使用 mmr
作为检索器。
retriever = db.as_retriever(search_type="mmr")
retriever.invoke(query)
或直接使用 max_marginal_relevance_search
db.max_marginal_relevance_search(query, k=2, fetch_k=10)
使用安全连接的示例
为了运行此笔记本,需要使用安全连接运行 Vald 集群。
以下是一个使用 Athenz 身份验证的 Vald 集群示例,其配置如下。
ingress(TLS) -> authorization-proxy(在 grpc 元数据中检查 athenz-role-auth) -> vald-lb-gateway
import grpc
with open("test_root_cacert.crt", "rb") as root:
credentials = grpc.ssl_channel_credentials(root_certificates=root.read())
# Refresh is required for server use
with open(".ztoken", "rb") as ztoken:
token = ztoken.read().strip()
metadata = [(b"athenz-role-auth", token)]
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import Vald
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_text_splitters import CharacterTextSplitter
raw_documents = TextLoader("state_of_the_union.txt").load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
documents = text_splitter.split_documents(raw_documents)
model_name = "sentence-transformers/all-mpnet-base-v2"
embeddings = HuggingFaceEmbeddings(model_name=model_name)
db = Vald.from_documents(
documents,
embeddings,
host="localhost",
port=443,
grpc_use_secure=True,
grpc_credentials=credentials,
grpc_metadata=metadata,
)
query = "What did the president say about Ketanji Brown Jackson"
docs = db.similarity_search(query, grpc_metadata=metadata)
docs[0].page_content
基于向量的相似性搜索
embedding_vector = embeddings.embed_query(query)
docs = db.similarity_search_by_vector(embedding_vector, grpc_metadata=metadata)
docs[0].page_content
带分数的相似性搜索
docs_and_scores = db.similarity_search_with_score(query, grpc_metadata=metadata)
docs_and_scores[0]
最大边缘相关性搜索 (MMR)
retriever = db.as_retriever(
search_kwargs={"search_type": "mmr", "grpc_metadata": metadata}
)
retriever.invoke(query, grpc_metadata=metadata)
或者
db.max_marginal_relevance_search(query, k=2, fetch_k=10, grpc_metadata=metadata)