种棵许愿树 發表於 2025-7-13 11:22:00

向量数据库 Chroma 和 Milvus的使用

<h2 id="一什么是向量数据库">一、什么是向量数据库?</h2>
<p>向量数据库(Vector Database)是专门用来存储和检索向量数据的数据库。它广泛应用于图像搜索、推荐系统、自然语言处理等领域。</p>
<p>简单理解:</p>
<ul>
<li>你给数据库一堆「特征向量」(比如图片、文本的数字表达)</li>
<li>你问数据库「最像这个向量的有哪些?」</li>
<li>数据库快速返回「最相似」的结果</li>
</ul>
<h2 id="二chroma-和-milvus-简介">二、Chroma 和 Milvus 简介</h2>
<table>
<thead>
<tr>
<th>名称</th>
<th>特点</th>
<th>语言支持</th>
<th>适用场景</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Chroma</strong></td>
<td>轻量级、Python友好、易上手</td>
<td>Python</td>
<td>小项目、原型、快速开发</td>
</tr>
<tr>
<td><strong>Milvus</strong></td>
<td>企业级、高性能、支持多种部署方案</td>
<td>多语言(Python、Go等)</td>
<td>大规模、高并发、复杂场景</td>
</tr>
</tbody>
</table>
<h2 id="三环境准备">三、环境准备</h2>
<ul>
<li>操作系统:Windows / Mac / Linux 都可以</li>
<li>Python 版本:3.7 及以上</li>
<li>安装包管理器:pip</li>
</ul>
<h2 id="四安装与配置">四、安装与配置</h2>
<h3 id="1-安装-chroma">1 、安装 Chroma</h3>
<p><strong>直接安装Python库</strong></p>
<pre><code class="language-bash">pip install chromadb
</code></pre>
<h3 id="2-安装-milvus">2 、安装 Milvus</h3>
<p>Milvus 分为两个部分:</p>
<ul>
<li><strong>Milvus Server</strong>(核心数据库服务,需单独安装或用Docker运行)</li>
<li><strong>Milvus Python SDK</strong>(客户端,方便Python调用)</li>
</ul>
<h4 id="21使用官方推荐脚本最省心">2.1、使用官方推荐脚本(最省心)</h4>
<p>Milvus 官方提供的脚本会自动启用嵌入式 etcd 并正确配置启动:.</p>
<pre><code class="language-bash">curl -sfL https://raw.githubusercontent.com/milvus-io/milvus/master/scripts/standalone_embed.sh -o standalone_embed.sh
bash standalone_embed.sh start
</code></pre>
<h4 id="22验证安装">2.2、验证安装</h4>
<p><strong>启动后查看容器状态:</strong></p>
<pre><code class="language-plain">docker ps
</code></pre>
<p>应显示 <code>milvus_standalone</code> 正常运行</p>
<p><img src="https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/816d589dccea4e898681614cdc171435~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6L2v5Lu25rWL6K-V5ZCb:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzU0MDkwMTExMjMxNzIyNCJ9&amp;rk3s=f64ab15b&amp;x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&amp;x-orig-expires=1752981701&amp;x-orig-sign=5CM2MWqB1pnzlJPNTPQvA9LxGb4%3D" alt="" loading="lazy"></p>
<p><strong>查看日志确认 embedded etcd 启动成功,无连接错误:</strong></p>
<pre><code class="language-plain">docker logs milvus_standalone
</code></pre>
<p>启动日志无报错</p>
<p><img src="https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/5e6f70a4685e44d19cfcad350079f2ff~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6L2v5Lu25rWL6K-V5ZCb:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzU0MDkwMTExMjMxNzIyNCJ9&amp;rk3s=f64ab15b&amp;x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&amp;x-orig-expires=1752981701&amp;x-orig-sign=ZUNhm3UNs80LzExURhKTGiBKlco%3D" alt="" loading="lazy"></p>
<p><strong>测试连接端口:</strong></p>
<pre><code class="language-plain">nc -zv localhost 19530
</code></pre>
<p>成功连接表示 Milvus 已正常监听端口。</p>
<p><img src="https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/451f3ed1b40540db9aae92316f558e8e~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6L2v5Lu25rWL6K-V5ZCb:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzU0MDkwMTExMjMxNzIyNCJ9&amp;rk3s=f64ab15b&amp;x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&amp;x-orig-expires=1752981701&amp;x-orig-sign=GAIZDHQ6UilBmYyTiXVAgJHAD0M%3D" alt="" loading="lazy"></p>
<h4 id="23安装-milvus-python-sdk">2.3、安装 Milvus Python SDK</h4>
<pre><code class="language-bash">pip install pymilvus
</code></pre>
<h2 id="五使用示例">五、使用示例</h2>
<h3 id="1chroma-简单示例">1、Chroma 简单示例</h3>
<pre><code class="language-python">import chromadb

# 创建客户端 - 使用新的配置方式
client = chromadb.PersistentClient(path=".chromadb/")

# 创建/获取集合 - 使用 get_or_create_collection 避免重复创建错误
collection = client.get_or_create_collection("test_collection")

# 插入向量数据
collection.add(
    documents=["苹果", "香蕉", "橘子"],# 文本描述
    embeddings=[, , ],# 对应向量(示例)
    ids=["1", "2", "3"]
)

# 查询最相似向量
results = collection.query(
    query_embeddings=[],
    n_results=1
)

print(results)

</code></pre>
<p><strong>返回结果</strong></p>
<p><img src="https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/0f3cb555a01b4dd689311fbf87bbceee~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6L2v5Lu25rWL6K-V5ZCb:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzU0MDkwMTExMjMxNzIyNCJ9&amp;rk3s=f64ab15b&amp;x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&amp;x-orig-expires=1752981701&amp;x-orig-sign=0X9bc%2FmF3Vt2ke%2B2APQzAw2iBF0%3D" alt="" loading="lazy"></p>
<p><strong>说明</strong>:</p>
<ul>
<li><code>documents</code> 是你给数据库的文本</li>
<li><code>embeddings</code> 是文本的向量表示(通常由模型生成)</li>
<li>查询时传入一个向量,返回最接近的n个结果</li>
</ul>
<h3 id="2-milvus-简单示例">2 、Milvus 简单示例</h3>
<pre><code class="language-python">from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection

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

# 定义集合结构
fields = [
    FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=False),
    FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=3)
]

schema = CollectionSchema(fields, "test collection")

# 创建集合
collection = Collection("test_collection", schema)

# 插入数据
ids =
embeddings = [
    ,
    ,
   
]

collection.insert()

# 创建索引
index_params = {
    "index_type": "IVF_FLAT",
    "params": {"nlist": 10},
    "metric_type": "L2"
}
collection.create_index("embedding", index_params)

# 加载集合
collection.load()

# 查询向量
search_params = {"metric_type": "L2", "params": {"nprobe": 10}}
results = collection.search([], "embedding", search_params, limit=2)

for result in results:
    print(f"id: {result.id}, distance: {result.distance}")
</code></pre>
<p><strong>运行结果</strong></p>
<p><img src="https://p0-xtjj-private.juejin.cn/tos-cn-i-73owjymdk6/2f99537108aa4e20b3b596451a27cdaf~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg6L2v5Lu25rWL6K-V5ZCb:q75.awebp?policy=eyJ2bSI6MywidWlkIjoiMzU0MDkwMTExMjMxNzIyNCJ9&amp;rk3s=f64ab15b&amp;x-orig-authkey=f32326d3454f2ac7e96d3d06cdbb035152127018&amp;x-orig-expires=1752981701&amp;x-orig-sign=We6XIRuOQZgYLZeCE8sqaJQpM%2Bo%3D" alt="" loading="lazy"></p>
<h1 id="六总结">六、总结</h1>
<table>
<thead>
<tr>
<th>功能</th>
<th>Chroma</th>
<th>Milvus</th>
</tr>
</thead>
<tbody>
<tr>
<td>安装</td>
<td>纯Python库,简单快速</td>
<td>需要运行服务,推荐Docker部署</td>
</tr>
<tr>
<td>适合项目规模</td>
<td>小型、开发测试</td>
<td>大规模、生产环境</td>
</tr>
<tr>
<td>语言支持</td>
<td>Python优先</td>
<td>多语言支持</td>
</tr>
<tr>
<td>性能</td>
<td>适中</td>
<td>高性能,支持分布式</td>
</tr>
</tbody>
</table>


</div>
<div id="MySignature" role="contentinfo">
    <p><span style="font-family: 微软雅黑; font-size: 22px; font-weight: normal; font-style: italic; text-decoration: none"><strong>优秀不够,你是否无可替代</strong></span></p>
<p><span style="font-family: 微软雅黑; font-size: 18px; font-weight: normal; font-style: italic; text-decoration: none"><strong>
软件测试交流QQ群:721256703,期待你的加入!!</strong></span></p>
<p><span style="font-family: 微软雅黑; font-size: 18px; font-weight: normal; font-style: italic; text-decoration: none"><strong>欢迎关注我的微信公众号:软件测试君 </strong></span></p>
<img src="https://www.cnblogs.com/images/cnblogs_com/longronglang/1061549/o_QQ%E6%88%AA%E5%9B%BE20190728134401.jpg" height="200" width="450"><br><br><br>
来源:https://www.cnblogs.com/longronglang/p/18982082
頁: [1]
查看完整版本: 向量数据库 Chroma 和 Milvus的使用