python使用milvus向量库

milvus向量库使用的时候,需要先本地安装,使用windows安装milvus需要使用docker

使用管理员身份打开powershell,执行

复制代码
Invoke-WebRequest https://raw.githubusercontent.com/milvus-io/milvus/refs/heads/master/scripts/standalone_embed.bat -OutFile standalone.bat

生成好文件standalone.bat
继续执行

复制代码
.\standalone.bat start

当看到类似 **Start successfully.**的提示,就表示启动成功了。

输入docker ps,就可以看见milvus-standalone进程

其中python代码里连接方式如下

复制代码
client = MilvusClient(uri="http://localhost:19530",
            token="root:Milvus" )

其中如果报错
(base) PS D:\> .\standalone.bat start

Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:2379 -> 127.0.0.1:0: listen tcp 0.0.0.0:2379: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

Error: failed to start containers: milvus-standalone

Start failed.

重启电脑即可

使用docker 安装miluvus之后,启动就比较快了

日志如下:
启动 2026-04-25 16:44:31

启动结束 2026-04-25 16:44:31

加载模型 2026-04-25 16:44:31

后面加载模型时候一直报错

复制代码
  embed_model = SentenceTransformer("all-MiniLM-L6-v2")

报错内容
'(MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /sentence-transformers/all-MiniLM-L6-v2/resolve/main/modules.json (Caused by ConnectTimeoutError(<HTTPSConnection(host='huggingface.co', port=443) at 0x2043accaab0>, 'Connection to huggingface.co timed out. (connect timeout=10)'))"), '(Request ID: c314c2ba-9ac0-4790-905a-96832e0122c9)')' thrown while requesting HEAD https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/./modules.json

Retrying in 1s Retry 1/5.

这个是因为网络原因,需要使用外网,或者加上

复制代码
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'

表示连接的是国内镜像源

在对向量库进行增删改查的时候,需要先创建集合,类似mysql里的表

复制代码
def create_collection_if_not_exists():
    print("创建collection")
    if client.has_collection(COLLECTION_NAME):
        return

    schema = client.create_schema(auto_id=True)
    schema.add_field(field_name="id",datatype=DataType.INT64, is_primary=True)
    schema.add_field(field_name="module", datatype=DataType.VARCHAR, max_length=1000)
    schema.add_field(field_name="section", datatype=DataType.VARCHAR, max_length=200)
    schema.add_field(field_name="content", datatype=DataType.VARCHAR, max_length=65535)
    schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=384)

    # 创建索引,必须创建
    index_params = client.prepare_index_params()
    index_params.add_index(
        field_name="vector",
        metric_type="COSINE",
        index_type="IVF_FLAT",
        params={"nlist": 128})

    # 这里是重点
    client.create_collection(
        collection_name=COLLECTION_NAME,
        schema=schema,
        index_params=index_params
    )

    print("集合创建成功")

另外浏览器端访问milvus的方式

docker run -d -p 8082:3000 \

-e MILVUS_URL=host.docker.internal:19530 \

zilliz/attu:latest

使用浏览器访问localhost:8082

相关推荐
特立独行的猫A3 小时前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
吃我一窝jio6 小时前
Python与Go线程模型深度对比
java·开发语言·python
zwd20057 小时前
Manim 怎么放大画面的某一部分?
python·动画·可视化·manim·数学动画
屿华AI7 小时前
AUTOSAR 实战-诊断工具 | UDS 0x27 安全访问的配置化实现:种子密钥算法插件化设计
python·测试工具·汽车
RobinDevNotes8 小时前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
如夏倾城9 小时前
VRRP(虚拟路由器冗余协议Virtual Router Redundancy Protocol)
网络·python·网络协议·安全·智能路由器·负载均衡·信息与通信
IvanCodes9 小时前
LangChain(04):Runnable 与 LCEL 的执行与组合机制
python·langchain·agent
特创数字科技10 小时前
纯本地离线运行的文档脱敏工具(技术分享)
前端·驱动开发·python
东莞市云毅网络有限公司11 小时前
用 Playwright 搭一套问答平台引用监测的最小骨架
python·数据清洗·rag·企业知识库·文档解析
Thanks_ks11 小时前
【第 010 讲】全面掌握 Python 变量与常量:变量的定义与赋值 | 标识符与命名规范 | 变量的底层机制 | 常量与魔术数字
python·内存·变量·标识符·常量·赋值·命名规范