大语言模型学习--向量数据库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 文档

相关推荐
AllData公司负责人1 天前
实战一:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 File 到 Doris 高效数据同步链路,批量文件离线入仓
数据库·人工智能·分布式·大模型·数据传输·数据同步·连接器
aicat_cn2 天前
AI Studio 做的安卓 App 有后端了,Gemini 密钥不用塞进 APK
ai·大模型
JonaHu2 天前
AceDataCloud Coding Plan 多模型编程:从 Chat Completions 迁移到 Responses API 与工具调用
python·大模型·api·ai编程·acedatacloud
吴名氏.3 天前
从零手写一个 AI Agent 框架(TypeScript 完整实现):Function Calling 循环、流式输出与错误恢复
人工智能·typescript·node.js·大模型·ai agent·functioncalling·llm 应用开发
深海鱼肝油ya3 天前
Agent工具系统——Tool Calling & Schema
人工智能·大模型·agent·schema·智能体·工具调用·toolcalling
深海鱼肝油ya3 天前
LangChain(一)概念&初识
langchain·大模型·llm·agent·思维链·智能体
艺杯羹3 天前
11月23日AI前沿复盘:安全双警报、模型降价潮与工程防线
人工智能·网络安全·系统架构·大模型·ai agent
VIP_CQCRE4 天前
¥99起多模型编程套餐怎么控预算:Coding Plan缓存、Credits用量与续费指南
大模型·ai编程·codex·claude code·coding plan
Thanks_ks4 天前
大模型应用的成本账:Token 计费原理、上下文窗口管理与省钱策略全解析
大模型·token·tiktoken·上下文窗口·max_tokens·上下文缓存·模型分层
Thanks_ks5 天前
AI Agent 开发入门:用 Function Calling 让大模型学会 “动手干活”,附订单查询机器人实战
python·ai·大模型·openai·agent·通义千问·mcp