【python qdrant 向量数据库 完整示例代码】

测试一下python版本的dqrant向量数据库的效果,完整代码如下:

安装库

!pip install qdrant-client>=1.1.1
!pip install -U sentence-transformers

导入

from qdrant_client import models, QdrantClient
from sentence_transformers import SentenceTransformer

encoder = SentenceTransformer("all-MiniLM-L6-v2", device="cuda")

准备测试数据集

documents = [
    {
        "name": "The Time Machine",
        "description": "A man travels through time and witnesses the evolution of humanity."
        * 8,
        "author": "H.G. Wells",
        "year": 1895,
    },
    {
        "name": "Ender's Game",
        "description": "A young boy is trained to become a military leader in a war against an alien race."
        * 4,
        "author": "Orson Scott Card",
        "year": 1985,
    },
    {
        "name": "Brave New World",
        "description": "A dystopian society where people are genetically engineered and conditioned to conform to a strict social hierarchy."
        * 6,
        "author": "Aldous Huxley",
        "year": 1932,
    },
] * 50000

print(len(documents))

创建存储库

qdrant = QdrantClient(":memory:")  # 内存中
# qdrant = QdrantClient(path='./qdrant')  # 存储到本地

在数据库中创建一个collection(类似一个存储桶)

qdrant.recreate_collection(
    collection_name="my_books",
    vectors_config=models.VectorParams(
        size=encoder.get_sentence_embedding_dimension(),  # Vector size is defined by used model
        distance=models.Distance.COSINE,
    ),
)

对文档进行向量化

import hashlib
from tqdm import tqdm

def sha256(text):

    hash_object = hashlib.sha256()
    hash_object.update(text.encode("utf-8"))
    hash_value = hash_object.hexdigest()
    return hash_value

records = []
bs = 256
for i in tqdm(range(0, len(documents), bs)):
    docs = documents[i : i + bs]
    vectors = encoder.encode(
        [doc["description"] for doc in docs], normalize_embeddings=True
    ).tolist()

    record = [
        models.Record(id=idx, vector=vec, payload=doc)  # sha256(doc['description'])
        for idx, vec, doc in zip(range(i, i + bs), vectors, docs)
    ]

    records.extend(record)

上传到向量数据库中指定的collection

qdrant.upload_points(
    collection_name="my_books", points=records, batch_size=128, parallel=12
)

语义搜索

query = "Aliens attack our planet"
hits = qdrant.search(
    collection_name="my_books",
    query_vector=encoder.encode(query).tolist(),
    limit=6,
)
for hit in hits:
    print(hit.payload, "score:", hit.score)

条件搜索

search only for books from 21st century

hits = qdrant.search(
    collection_name="my_books",
    query_vector=encoder.encode("Tyranic society").tolist(),
    query_filter=models.Filter(
        must=[models.FieldCondition(key="year", range=models.Range(gte=1980))]
    ),
    limit=3,
)
for hit in hits:
    print(hit.payload, "score:", hit.score)

参考官方GitHub

github

colab

相关推荐
好看资源平台1 小时前
网络爬虫——综合实战项目:多平台房源信息采集与分析系统
爬虫·python
进击的六角龙1 小时前
深入浅出:使用Python调用API实现智能天气预报
开发语言·python
檀越剑指大厂1 小时前
【Python系列】浅析 Python 中的字典更新与应用场景
开发语言·python
湫ccc1 小时前
Python简介以及解释器安装(保姆级教学)
开发语言·python
孤独且没人爱的纸鹤1 小时前
【深度学习】:从人工神经网络的基础原理到循环神经网络的先进技术,跨越智能算法的关键发展阶段及其未来趋势,探索技术进步与应用挑战
人工智能·python·深度学习·机器学习·ai
羊小猪~~1 小时前
tensorflow案例7--数据增强与测试集, 训练集, 验证集的构建
人工智能·python·深度学习·机器学习·cnn·tensorflow·neo4j
lzhlizihang1 小时前
python如何使用spark操作hive
hive·python·spark
q0_0p1 小时前
牛客小白月赛105 (Python题解) A~E
python·牛客
极客代码1 小时前
【Python TensorFlow】进阶指南(续篇三)
开发语言·人工智能·python·深度学习·tensorflow
庞传奇2 小时前
TensorFlow 的基本概念和使用场景
人工智能·python·tensorflow