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

相关推荐
山顶夕景5 小时前
【LLM】Kimi-K2模型架构(MuonClip 优化器等)
大模型·llm·agent·强化学习·智能体
哥本哈士奇(aspnetx)5 小时前
Dify快速搭建问答系统
大模型
威化饼的一隅7 小时前
【多模态】天池AFAC赛道四-智能体赋能的金融多模态报告自动化生成part2-报告输出
大模型·agent·多模态·智能体
精致先生10 小时前
RAG(检索增强生成)
人工智能·大模型·rag
威化饼的一隅16 小时前
【多模态】天池AFAC赛道四-智能体赋能的金融多模态报告自动化生成part1-数据获取
大模型·agent·多模态·智能体
美林数据Tempodata19 小时前
美林数据用大模型重构电能质量评估,让隐蔽合规问题无所遁形
重构·大模型
胡耀超2 天前
我们如何写好提示词、发挥LLM能力、写作指南:从认知分析到动态构建的思维方法
人工智能·python·学习·大模型·llm·提示词·八要素思维
文浩(楠搏万)2 天前
XTTS实现语音克隆:精确控制音频格式与生成流程【TTS的实战指南】
大模型·tts·克隆·语音·声音克隆·音色·xtts
您的通讯录好友2 天前
TechGPT2部署
linux·人工智能·python·大模型·techgpt
之之为知知3 天前
Chromadb 1.0.15 索引全解析:从原理到实战的向量检索优化指南
人工智能·深度学习·机器学习·大模型·索引·向量数据库·chromadb