近期整理了关于向量数据库入门的分享,并把测试代码封装成了一个demo项目,涵盖了一下内容:
以下截取项目部分代码,对向量数据库进行简单查询
from qdrant_client import QdrantClient, AsyncQdrantClient
from sentence_transformers import SentenceTransformer
class NeuralSearcher:
def __init__(self, collection_name):
self.collection_name = collection_name
self.model = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
self.qdrant_client = QdrantClient("http://localhost:6333")
# 查询
def search(self, text: str):
vector = self.model.encode(text).tolist()
search_result = self.qdrant_client.search(
collection_name=self.collection_name,
query_vector=vector,
query_filter=None,
limit=5
)
payloads = [hit.payload for hit in search_result]
return payloads
以下是我实现的示例项目,可参考和star一下下哈!
https://github.com/chengxs1994/qdrant-fast-api
后面我会对向量数据库做详细的分享!