讨论广场 问答详情
Python整合实战:从零构建语义搜索系统
2501_94773192 2025-12-25 21:49:49
260 评论 分享
python

一、为什么选择Milvus?——向量数据库的入门钥匙
在AI大模型时代,向量嵌入(Embedding)已成为语义搜索、推荐系统、RAG(检索增强生成)等应用的核心。Milvus作为开源向量数据库,专为海量高维向量的高效存储与检索设计,是构建AI应用的基础设施。对于零基础学习者,通过‌Attu可视化界面‌管理集群、结合‌Python SDK‌进行实战开发,是最快上手的方式。

🛠️ 二、Attu可视化安装全流程(Windows/macOS/Linux通用)
Attu是Milvus官方推出的Web管理控制台,无需命令行即可完成集合管理、向量插入、相似性查询等操作。

步骤1:安装Docker
确保系统已安装Docker Desktop(官网下载),启动后验证:

bash
Copy Code
docker --version
步骤2:一键部署Milvus + Attu
创建 docker-compose.yml 文件,内容如下:
 

version: '3.8'

services:
  minio:
    image: minio/minio
    command: minio server /data --console-address ":9001"
    volumes:
      - minio_data:/data
    ports:
      - "9000:9000"
      - "9001:9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 20s
      retries: 3

  milvus-standalone:
    image: milvusdb/milvus:v2.4.5
    command: ["--config", "/etc/milvus/configs/milvus.yaml"]
    ports:
      - "19530:19530"
      - "9091:9091"
    depends_on:
      - minio
    volumes:
      - milvus_data:/var/lib/milvus
    environment:
      MINIO_ADDRESS: minio:9000
      MINIO_ACCESS_KEY: minioadmin
      MINIO_SECRET_KEY: minioadmin

  attu:
    image: milvusdb/attu:v2.4.5
    ports:
      - "3000:3000"
    depends_on:
      - milvus-standalone
    environment:
      MILVUS_URL: ws://milvus-standalone:19530

volumes:
  minio_data:
  milvus_data:
 

在终端执行:


 

bashCopy Code

docker-compose up -d

✅ 等待约2分钟,服务启动完成。访问 https://zhuanlan.zhihu.com/p/1987616010065446646/
即可打开Attu界面,连接地址默认为https://zhuanlan.zhihu.com/p/1987616002628919355

🔌 三、Python整合实战:从零构建语义搜索系统

我们将构建一个“电影简介语义搜索”系统,使用Sentence-BERT生成向量,存入Milvus,再通过Attu可视化查看。

步骤1:安装Python依赖

pymilvus==2.4.5
sentence-transformers==2.2.2
numpy==1.26.4
步骤2:Python完整代码(含数据插入、查询、可视化)
 

from pymilvus import connections, Collection, FieldSchema, CollectionSchema, DataType, utility
from sentence_transformers import SentenceTransformer
import numpy as np

# 1. 连接Milvus
connections.connect("default", host="localhost", port="19530")

# 2. 定义集合结构
fields = [
    FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=True),
    FieldSchema(name="title", dtype=DataType.VARCHAR, max_length=512),
    FieldSchema(name="description", dtype=DataType.VARCHAR, max_length=2048),
    FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=384)
]
schema = CollectionSchema(fields, "电影语义搜索集合")
collection_name = "movies"

if utility.has_collection(collection_name):
    collection = Collection(collection_name)
    collection.drop()
collection = Collection(collection_name, schema)

# 3. 创建索引(HNSW加速检索)
index_params = {
    "index_type": "HNSW",
    "metric_type": "L2",
    "params": {"M": 8, "efConstruction": 64}
}
collection.create_index("embedding", index_params)

# 4. 加载预训练模型
model = SentenceTransformer('all-MiniLM-L6-v2')

# 5. 准备数据
movies = [
    {"title": "The Matrix", "description": "A computer hacker learns about the true nature of reality and his role in the war against its controllers."},
    {"title": "Inception", "description": "A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O."},
    {"title": "Interstellar", "description": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival."},
    {"title": "The Godfather", "description": "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son."},
    {"title": "Parasite", "description": "A poor family schemes to become employed by a wealthy family and infiltrates their household."}
]

# 6. 生成向量
descriptions = [m["description"] for m in movies]
embeddings = model.encode(descriptions).tolist()

# 7. 插入数据

260 评论 分享
写回答
全部评论(0)