使用 FastEmbed 和 Qdrant 进行向量搜索
安装 Qdrant 客户端和 FastEmbed
pip install "qdrant-client[fastembed]>=1.14.2"
初始化客户端
Qdrant 客户端有一个简单的内存模式,可以让你在本地尝试语义搜索。
from qdrant_client import QdrantClient, models
client = QdrantClient(":memory:") # Qdrant is running from RAM.
添加数据
现在,你可以添加两个示例文档,它们相关的元数据,以及每个文档的id。
docs = [
"Qdrant has a LangChain integration for chatbots.",
"Qdrant has a LlamaIndex integration for agents.",
]
metadata = [
{"source": "langchain-docs"},
{"source": "llamaindex-docs"},
]
ids = [42, 2]
创建一个集合
Qdrant 将向量和相关元数据存储在集合中。集合在创建时需要设置向量参数。在本教程中,我们将使用BAAI/bge-small-en来计算嵌入。
model_name = "BAAI/bge-small-en"
client.create_collection(
collection_name="test_collection",
vectors_config=models.VectorParams(
size=client.get_embedding_size(model_name),
distance=models.Distance.COSINE
), # size and distance are model dependent
)
将文档 Upsert 到集合中
Qdrant 客户端可以通过 FastEmbed 集成在其方法中隐式地进行推理。它需要将你的数据封装在模型中,例如models.Document(如果你正在处理图像,则为models.Image)
metadata_with_docs = [
{"document": doc, "source": meta["source"]} for doc, meta in zip(docs, metadata)
]
client.upload_collection(
collection_name="test_collection",
vectors=[models.Document(text=doc, model=model_name) for doc in docs],
payload=metadata_with_docs,
ids=ids,
)
运行向量搜索
在这里,你将提出一个虚拟问题,它将允许你检索一个语义相关的结果。
search_result = client.query_points(
collection_name="test_collection",
query=models.Document(
text="Which integration is best for agents?",
model=model_name
)
).points
print(search_result)
语义搜索引擎将按相关性顺序检索最相似的结果。在这种情况下,关于 LlamaIndex 的第二个陈述更相关。
[
ScoredPoint(
id=2,
score=0.87491801319731,
payload={
"document": "Qdrant has a LlamaIndex integration for agents.",
"source": "llamaindex-docs",
},
...
),
ScoredPoint(
id=42,
score=0.8351846627714035,
payload={
"document": "Qdrant has a LangChain integration for chatbots.",
"source": "langchain-docs",
},
...
),
]