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'}}
相关推荐
流浪0012 小时前
LLM 大模型三种主流接入方式详解(API / 本地部署 / SDK)
langchain·llm
二进制_博客14 小时前
LangSmith 调试
langchain·langsmith
DRXB25072020 小时前
开源自由还是生态红利?LangChain 的灵活性与小艺开放平台的鸿蒙流量池,开发者该如何抉择?
langchain·开源·harmonyos
鱼饼Y1 天前
AI时代,使用大模型学习LangChain (3)——LCEL
人工智能·langchain
用户667675093791 天前
LangChain 向量检索为什么要用 as_retriever?从 LCEL 到 RunnableParallel 一次讲清
langchain
二进制_博客1 天前
Milvus-lite的使用
milvus
lzjava20241 天前
LangGraph Subgraphs 子图
langchain
元Y亨H1 天前
大模型技术-RAG 常用的向量数据库
langchain·llm
元Y亨H1 天前
大模型技术 Agents 概述
langchain·llm