大语言模型学习--向量数据库Milvus实践

Milvus是目前比较流行的开源向量数据库,其官网地址

Milvus 是什么? | Milvus 文档

1.Milvus简介

Milvus 是一种高性能、高扩展性的向量数据库。Milvus 提供强大的数据建模功能,能够将非结构化或多模式数据组织成结构化的 Collections。它支持多种数据类型,适用于不同的属性模型,包括常见的数字和字符类型、各种向量类型、数组、集合和 JSON。

Milvus 提供三种部署模式

  • Milvus Lite 是一个 Python 库,可以轻松集成到您的应用程序中。作为 Milvus 的轻量级版本,它非常适合在 Jupyter Notebooks 中进行快速原型开发,或在资源有限的边缘设备上运行。
  • Milvus Standalone 是单机服务器部署,所有组件都捆绑在一个 Docker 镜像中,方便部署。
  • Milvus Distributed 可部署在 Kubernetes 集群上,采用云原生架构,专为十亿规模甚至更大的场景而设计。该架构可确保关键组件的冗余。

Milvus 的云原生和高度解耦的系统架构

2.Milvus实践

推荐一个在线python运行环境(再也不用本地windows安装linux虚拟机了)

玻尔 | 全球科学家的 AI for Science 空间站

下面使用Milvus Lite本地实践一下,Milvus Lite,它是pymilvus 中包含的一个 python 库,可以嵌入到客户端应用程序中。

安装Milvus

复制代码
pip install -U pymilvus

设置向量数据库

复制代码
from pymilvus import MilvusClient
client = MilvusClient("milvus_demo.db")

创建Collections

复制代码
if client.has_collection(collection_name="demo_collection"):
    client.drop_collection(collection_name="demo_collection")
client.create_collection(
    collection_name="demo_collection",
    dimension=768,  # The vectors we will use in this demo has 768 dimensions
)

用向量表示文本

复制代码
import random

docs = [
    "Artificial intelligence was founded as an academic discipline in 1956.",
    "Alan Turing was the first person to conduct substantial research in AI.",
    "Born in Maida Vale, London, Turing was raised in southern England.",
]
vectors = [[random.uniform(-1, 1) for _ in range(768)] for _ in docs]
data = [
    {"id": i, "vector": vectors[i], "text": docs[i], "subject": "history"}
    for i in range(len(vectors))
]

print("Data has", len(data), "entities, each with fields: ", data[0].keys())
print("Vector dim:", len(data[0]["vector"]))

插入数据

复制代码
res = client.insert(collection_name="demo_collection", data=data)

print(res)

向量搜索

复制代码
# query_vectors = embedding_fn.encode_queries(["Who is Alan Turing?"])
# If you don't have the embedding function you can use a fake vector to finish the demo:
query_vectors = [ [ random.uniform(-1, 1) for _ in range(768) ] ]
res = client.search(
    collection_name="demo_collection",  # target collection
    data=query_vectors,  # query vectors
    limit=2,  # number of returned entities
    output_fields=["text", "subject"],  # specifies fields to be returned
)
print(res)

其他一些操作可以参考官网文档

管理数据库 | Milvus 文档

相关推荐
aqi0019 小时前
15天学会AI应用开发(八)使用向量数据库实现RAG功能
人工智能·python·大模型·ai编程·ai应用
aqi002 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
vivo互联网技术4 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
AndrewHZ5 天前
【LLM技术全景】大模型能力探秘:In-Context Learning与思维链(CoT)
人工智能·语言模型·大模型·llm·cot·思维链·icl
Vergelight5 天前
实战拆解|三类RAG架构差异:朴素、进阶、多轮RAG落地选型指南
架构·大模型·aigc·agent·ai产品经理·转行·ai后台设计
问道飞鱼5 天前
【大模型相关】意图识别实现方案行业分析报告
大模型·意图识别
DogDaoDao5 天前
【GitHub】CL4R1T4S:AI 系统提示词的透明革命
人工智能·python·ai·大模型·github·ai agent·cl4r1t4s
文艺倾年5 天前
【强化学习】数学推导专题,20W字总结(十五)
人工智能·分布式·大模型·强化学习·vibecoding
IRevers5 天前
【大模型】Gemma4在ROCm和vLLM部署
人工智能·pytorch·深度学习·大模型·datawhale·vllm·amdev
张彦峰ZYF5 天前
从嵌入、表征到潜空间:理解大模型向量世界的三种视角
人工智能·大模型·向量空间