来源:互联网 更新时间:2026-08-27 14:32
说到AI应用中的向量数据库,Milvus绝对是个绕不开的名字。它专为处理大规模向量数据而生,在机器学习、深度学习等场景里,提供了一套高效且强大的数据管理方案。

好消息是,现在这一切变得更简单了。PyMilvus (Milvus 的 Python SDK) 直接集成了模型模块,你不再需要东拼西凑工具链,就能方便地添加嵌入和重新排序模型。数据转换成可搜索的向量?对结果重新排序以提升精度?这些操作在检索增强生成(RAG)这类场景中,集成度变得空前之高。
接下来,我们从三个核心方向展开聊聊:
Rerank模型哪款最强?详解如何轻松集成到你的项目中!
如何高效选择RAG的中文Embedding模型?揭秘最佳实践与关键标准!
顺带一提,我们会通过实际案例,展示如何利用 Milvus Lite——这个可以在本地Python应用里直接运行的轻量版本来操作这些模型。它让向量搜索变得异常简便。
通常来说,向量嵌入可以分为两大类:
Milvus 两手都抓,既支持密集也支持稀疏嵌入,还提供了混合搜索功能。这个功能允许你在同一个集合内的不同向量字段上同时搜索。这些向量来源不同,代表数据的不同侧面,最终通过重新排序器将结果整合到一起,效果拔群。
下面,三个实际案例带你体验这套集成功能如何生成嵌入并进行向量搜索。
要使用Milvus的嵌入和重排序功能,首先得安装带模型包支持的pymilvus客户端。
pip install pymilvus[model]
# 如果你用的是 zsh,命令改为:pip install 'pymilvus[model]'
这一步会安装Milvus Lite,让你能在Python应用里本地运行Milvus。它包含的模型子包,装好了所有嵌入和重排序的实用工具。
这个模型子包能力可不少,支持OpenAI、Sentence Transformers、BGE-M3、BM25、SPLADE,还有Jina AI的预训练模型。
为了演示简单,这个例子用 DefaultEmbeddingFunction,它基于 all-MiniLM-L6-v2 这个 Sentence Transformer 模型。模型大约70MB,首次使用时自动下载:
from pymilvus import model
# 这会下载 "all-MiniLM-L6-v2" 模型,很轻量。
ef = model.DefaultEmbeddingFunction()
# 要生成嵌入的数据
docs = [
"Artificial intelligence was founded as an academic discipline in 1956.",
"Alan Turing was the first person to conduct substantial research in AI.",
"Born in Maida Vale, London, Turing was raised in southern England.",
]
embeddings = ef.encode_documents(docs)
print("Embeddings:", embeddings)
# 打印嵌入的维度和形状
print("Dim:", ef.dim, embeddings[0].shape)
BM25 是一种经典方法,通过计算单词出现频率来判断查询和文档之间的相关性。这里,我们用 BM25EmbeddingFunction 为查询和文档生成稀疏嵌入。
BM25 里有个关键步骤:计算文档的统计信息以获得 IDF(逆文档频率)。IDF 能衡量一个词信息量的大小,反映它在整个文档集中是常见还是稀有。
from pymilvus.model.sparse import BM25EmbeddingFunction
# 1. 准备一个小型语料库用于搜索
docs = [
"Artificial intelligence was founded as an academic discipline in 1956.",
"Alan Turing was the first person to conduct substantial research in AI.",
"Born in Maida Vale, London, Turing was raised in southern England.",
]
query = "Where was Turing born?"
bm25_ef = BM25EmbeddingFunction()
# 2. 拟合语料库,获取 BM25 模型参数
bm25_ef.fit(docs)
# 3. 存储拟合参数,加速后续处理
bm25_ef.sa ve("bm25_params.json")
# 4. 加载保存的参数
new_bm25_ef = BM25EmbeddingFunction()
new_bm25_ef.load("bm25_params.json")
docs_embeddings = new_bm25_ef.encode_documents(docs)
query_embeddings = new_bm25_ef.encode_queries([query])
print("Dim:", new_bm25_ef.dim, list(docs_embeddings)[0].shape)
搜索系统的终极目标,是又快又准地找到最相关的结果。传统方法像 BM25 或 TF-IDF,主要靠关键词匹配;近来的基于嵌入的余弦相似度方法虽然简单,但有时会忽略语言的微妙之处,尤其是查询与文档之间的深层交互。
这时候就需要重新排序器(ReRanker)登场了。它本质上是一个高级AI模型,拿到初始搜索结果后,会重新评估这些结果,确保它们更贴近用户的真实意图。它不止看表面术语的匹配,而是深入分析查询与文档内容之间的互动。
我们以 Jina AI Reranker 为例:
from pymilvus.model.reranker import JinaRerankFunction
jina_api_key = ""
rf = JinaRerankFunction("jina-reranker-v1-base-en", jina_api_key)
query = "What event in 1956 marked the official birth of artificial intelligence as a discipline?"
documents = [
"In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.",
"The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.",
"In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.",
"The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems."
]
results = rf(query, documents)
for result in results:
print(f"Index: {result.index}")
print(f"Score: {result.score:.6f}")
print(f"Text: ...")
Milvus:为AI应用而生的开源矢量数据库。
总的来说,Milvus 这个专为 AI 应用设计的开源向量数据库,凭借强大的处理能力和灵活的嵌入支持,特别适合需要高效处理和搜索大规模向量数据的场景。虽然学习曲线和资源要求摆在那里,但它广泛的应用前景,无疑让它成为值得关注的技术工具。
ELON币发展前景怎么样?未来潜力如何?ELON价格走势分析
黄金和比特币走势是相反吗?黄金和比特币哪个价值高?
豆包app正版免费版下载 豆包安卓正版安装包下载
55部泰腐剧全盘点,从《千星传说》到《以你的心诠释我的爱》
豆包app官方下载 豆包官方免费下载安装
LITH币可以长期持有吗?LITH币价格最新行情
vivo手机怎么设置来电闪光灯 vivo手机来电提醒设置教程
电视剧《温柔的谎言》剧情介绍
CCTV-1黄金档《藏锋》今晚首播!
2026暑期档票房破100亿:《功夫女足》《八仙!》TOP2
石头P30 Pro发布:8.98cm超薄热活水扫拖机器人
盘搜搜搜索入口地址 盘搜搜网盘资源在线查找入口
短剧《我本无念,奈何我有神之眼》剧情介绍
三款26年75寸Mini LED 电视横评|创维 A7H
短剧《云端负烟火》剧情介绍
2026磁轴键盘超短触发选购指南
iPhone 18支持多少瓦快充 苹果18选购适配器充电器推荐
电视剧《她的罪名》剧情介绍
闺蜜网名高冷女生(精选100个)
短剧《谁敢动我的家人》剧情介绍
手机号码测吉凶
本站所有软件,都由网友上传,如有侵犯你的版权,请发邮件haolingcc@hotmail.com 联系删除。 版权所有 Copyright@2012-2013 haoling.cc