LangChain--11--Milvus

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • Milvus
  • [Milvus 基本用法](#Milvus 基本用法)
    • 1、DDL操作
      • [1.1 Database](#1.1 Database)
      • [1.2 Collection](#1.2 Collection)
    • 2、DML操作
      • [2.1 准备嵌入模型](#2.1 准备嵌入模型)
      • [2.2 准备collection](#2.2 准备collection)
      • [2.3 准备数据](#2.3 准备数据)
      • [2.4 写入数据](#2.4 写入数据)
    • 3、DQL操作
      • [3.1 扫描数据](#3.1 扫描数据)
      • [3.2 通过主键查询数据](#3.2 通过主键查询数据)
      • [3.3 相似度检索](#3.3 相似度检索)

Milvus

1、安装Docker DeskTop

https://www.docker.com/

2、安装Milvus

bash 复制代码
(base) PS D:\developTools\Milvus> Invoke-WebRequest
https://raw.githubusercontent.com/milvusio/
milvus/refs/heads/master/scripts/standalone_embed.bat -OutFile
standalone.bat
bash 复制代码
(base) PS D:\developTools\Milvus> .\1 standalone.bat start

3、测试前准备工作

Milvus 基本用法

1、DDL操作

1.1 Database

1.2 Collection

2、DML操作

确保已删除名为docs 的collection,下文会重新创建

2.1 准备嵌入模型

2.2 准备collection

python 复制代码
{
    'collection_name': 'docs',
    'auto_id': False,
    'num_shards': 1,
    'description': '',
    'fields': [
        {
            'field_id': 100,
            'name': 'id',
            'description': '',
            'type': <DataType.INT64: 5>,
            'params': {},
            'is_primary': True
        },
        {
            'field_id': 101,
            'name': 'vector',
            'description': '',
            'type': <DataType.FLOAT_VECTOR: 101>,
            'params': {'dim': 1024}
        }
    ],
    'functions': [],
    'aliases': [],
    'collection_id': 467040847074183555,
    'consistency_level': 2,
    'properties': {'timezone': 'UTC'},
    'num_partitions': 1,
    'enable_dynamic_field': True,
    'enable_namespace': False,
    'created_timestamp': 467041742317420562,
    'update_timestamp': 467041742317420562
}

2.3 准备数据

2.4 写入数据

3、DQL操作

3.1 扫描数据

通过query_iterator 扫描collection下的所有数据

python 复制代码
iterator = client.query_iterator(
    collection_name=collection_name,
    filter="",
    output_fields=["*"]
)

i = 0
while True:

    rows = iterator.next()

    if not rows:
        break

    for row in rows:
        print(f"第{i + 1}条数据:")
        # print(row)

        print(f"id : {row["id"]},vector = {row["vector"][:5]},text = {row["text"]},source = {row["source"]}")

        i += 1

iterator.close()
python 复制代码
第1条数据:
id : 0,vector = [-0.011260323226451874, 0.04925568029284477, 0.04267069697380066, -0.0021236573811620474, 0.005432611797004938],text = LangChain 是一个用于构建 LLM 应用的开发框架。,source = demo
第2条数据:
id : 1,vector = [-0.02432798594236374, 0.0016291062347590923, 0.01846320368349552, 0.00872476864606142, -0.009267804212868214],text = Milvus 是一个适合 AI 应用的向量数据库。,source = demo
第3条数据:
id : 2,vector = [-0.004508086945861578, 0.030777014791965485, 0.0059316931292414665, -0.03660701960325241, -0.0033217482268810272],text = RAG 的核心是先检索相关知识,再让大模型生成答案。,source = demo
第4条数据:
id : 3,vector = [0.005521129816770554, 0.013496095314621925, -0.013929124921560287, -0.02554875798523426, -0.010753572918474674],text = Docker Desktop 可以方便地在本地运行 Milvus Standalone。,source = demo

3.2 通过主键查询数据

python 复制代码
res = client.get(
    collection_name=collection_name,
    ids=[0,1,2]
)

print(len(res))

for i in range(len(res)):
    print(f"第{i + 1}条数据:")
    print(f"id : {res[i]["id"]},vector = {res[i]["vector"][:5]},text = {res[i]["text"]},source = {res[i]["source"]}")
    # print(res[i])
python 复制代码
3
第1条数据:
id : 0,vector = [-0.011260323226451874, 0.04925568029284477, 0.04267069697380066, -0.0021236573811620474, 0.005432611797004938],text = LangChain 是一个用于构建 LLM 应用的开发框架。,source = demo
第2条数据:
id : 1,vector = [-0.02432798594236374, 0.0016291062347590923, 0.01846320368349552, 0.00872476864606142, -0.009267804212868214],text = Milvus 是一个适合 AI 应用的向量数据库。,source = demo
第3条数据:
id : 2,vector = [-0.004508086945861578, 0.030777014791965485, 0.0059316931292414665, -0.03660701960325241, -0.0033217482268810272],text = RAG 的核心是先检索相关知识,再让大模型生成答案。,source = demo

3.3 相似度检索

① 准备查询嵌入

python 复制代码
#%%
# 相似度检索
query = "什么是向量数据库?"
query_vector = embed_model.embed_query(query)

② 检索

python 复制代码
results = client.search(
    collection_name=collection_name,
    data=[query_vector],
    limit=3,
    output_fields=["text","source","id"]
)

for res in results[0]:
    print(res)
python 复制代码
{'id': 0, 'distance': 0.6388449668884277, 'entity': {'id': 0, 'text': 'LangChain 是一个用于构建 LLM 应用的开发框架。', 'source': 'demo'}}
{'id': 3, 'distance': 0.31398770213127136, 'entity': {'id': 3, 'text': 'Docker Desktop 可以方便地在本地运行 Milvus Standalone。', 'source': 'demo'}}
{'id': 2, 'distance': 0.2985413074493408, 'entity': {'id': 2, 'text': 'RAG 的核心是先检索相关知识,再让大模型生成答案。', 'source': 'demo'}}
相关推荐
K79 小时前
别再搜「LangChain 教程」了——你学的 Chain 已经被判了死刑
langchain·ai编程
xyphf_和派孔明10 小时前
第六章: AI Agent长期记忆的存储引擎介绍
langchain·llm·agent
xyphf_和派孔明10 小时前
第四章: AI Agent短期记忆的信息管理机制设计
langchain·llm·agent
叼烟扛炮14 小时前
[LangChain] 学习笔记(13):并发不是越大越好——batch / gather / 信号量 / 配额治理
笔记·学习·langchain
叼烟扛炮1 天前
[LangChain] 学习笔记(09):工具(Tool)定义与调用闭环 · 理论+实战完整版
笔记·学习·langchain
仍然.2 天前
LangChain核心组件---消息
langchain
xyphf_和派孔明2 天前
第十章: Agent Skill的调用机制解读
langchain·llm·agent
掘黄金的打工人3 天前
LangChain 少样本示例 和 动态示例选择
langchain·aigc·ai编程
kevin_org3 天前
LangChain入门学习(四)
python·学习·langchain·fastapi