Intel 的可视化数据管理系统 (VDMS)
本 Notebook 介绍了如何开始使用 VDMS 作为向量存储。
Intel 的 可视化数据管理系统 (VDMS) 是一个用于高效访问大数据“视觉”数据的存储解决方案,它旨在通过以图形式存储的视觉元数据进行相关视觉数据的搜索,并支持机器友好的视觉数据增强以加快访问速度,从而实现云规模。VDMS 在 MIT 许可下发布。有关
VDMS的更多信息,请访问 此页面,并在此处查找 LangChain API 参考 here。
VDMS 支持:
- K 近邻搜索
- 欧几里得距离 (L2) 和内积 (IP)
- 用于索引和计算距离的库:FaissFlat (默认), FaissHNSWFlat, FaissIVFFlat, Flinng, TileDBDense, TileDBSparse
- 文本、图像和视频的嵌入
- 向量和元数据搜索
设置
要访问 VDMS 向量存储,您需要安装 langchain-vdms 集成包,并通过公开可用的 Docker 镜像部署 VDMS 服务器。
为简便起见,本笔记本将在本地主机上使用端口 55555 部署 VDMS 服务器。
%pip install -qU "langchain-vdms>=0.1.3"
!docker run --no-healthcheck --rm -d -p 55555:55555 --name vdms_vs_test_nb intellabs/vdms:latest
!sleep 5
Note: you may need to restart the kernel to use updated packages.
c464076e292613df27241765184a673b00c775cecb7792ef058591c2cbf0bde8
凭据
您可以直接使用 VDMS,无需任何凭证。
要启用对模型调用的自动跟踪,请设置您的 LangSmith API 密钥:
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"
初始化
使用 VDMS Client 连接到 VDMS vectorstore,使用 FAISS IndexFlat 索引(默认)和欧氏距离(默认)作为相似性搜索的距离度量。
Select embeddings model:
pip install -qU langchain-openai
import getpass
import os
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
from langchain_vdms.vectorstores import VDMS, VDMS_Client
collection_name = "test_collection_faiss_L2"
vdms_client = VDMS_Client(host="localhost", port=55555)
vector_store = VDMS(
client=vdms_client,
embedding=embeddings,
collection_name=collection_name,
engine="FaissFlat",
distance_strategy="L2",
)
管理向量存储
向向量存储添加条目
import logging
logging.basicConfig()
logging.getLogger("langchain_vdms.vectorstores").setLevel(logging.INFO)
from langchain_core.documents import Document
document_1 = Document(
page_content="I had chocolate chip pancakes and scrambled eggs for breakfast this morning.",
metadata={"source": "tweet"},
id=1,
)
document_2 = Document(
page_content="The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees.",
metadata={"source": "news"},
id=2,
)
document_3 = Document(
page_content="Building an exciting new project with LangChain - come check it out!",
metadata={"source": "tweet"},
id=3,
)
document_4 = Document(
page_content="Robbers broke into the city bank and stole $1 million in cash.",
metadata={"source": "news"},
id=4,
)
document_5 = Document(
page_content="Wow! That was an amazing movie. I can't wait to see it again.",
metadata={"source": "tweet"},
id=5,
)
document_6 = Document(
page_content="Is the new iPhone worth the price? Read this review to find out.",
metadata={"source": "website"},
id=6,
)
document_7 = Document(
page_content="The top 10 soccer players in the world right now.",
metadata={"source": "website"},
id=7,
)
document_8 = Document(
page_content="LangGraph is the best framework for building stateful, agentic applications!",
metadata={"source": "tweet"},
id=8,
)
document_9 = Document(
page_content="The stock market is down 500 points today due to fears of a recession.",
metadata={"source": "news"},
id=9,
)
document_10 = Document(
page_content="I have a bad feeling I am going to get deleted :(",
metadata={"source": "tweet"},
id=10,
)
documents = [
document_1,
document_2,
document_3,
document_4,
document_5,
document_6,
document_7,
document_8,
document_9,
document_10,
]
doc_ids = [str(i) for i in range(1, 11)]
vector_store.add_documents(documents=documents, ids=doc_ids)
API Reference:Document
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
如果一个 id 被多次提供,add_documents 不会检查 id 是否唯一。因此,请使用 upsert 在添加之前删除现有的 id 条目。
vector_store.upsert(documents, ids=doc_ids)
{'succeeded': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
'failed': []}
更新向量存储中的项目
updated_document_1 = Document(
page_content="I had chocolate chip pancakes and fried eggs for breakfast this morning.",
metadata={"source": "tweet"},
id=1,
)
updated_document_2 = Document(
page_content="The weather forecast for tomorrow is sunny and warm, with a high of 82 degrees.",
metadata={"source": "news"},
id=2,
)
vector_store.update_documents(
ids=doc_ids[:2],
documents=[updated_document_1, updated_document_2],
batch_size=2,
)