返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

RAG构建知识库还在忍受慢和重?试试Rust原生ChromaDB,轻量、高速、易用!

[复制链接]
链载Ai 显示全部楼层 发表于 昨天 22:14 |阅读模式 打印 上一主题 下一主题


最近在做大模型RAG相关项目,了解一个向量数据库叫Chroma DB,使用Rust来开发的。

它的优点,开源的,简单轻量,容易上手,对CPU的依赖较低,更多是依赖内存来完成大规模数据,适合做一些矢量搜索、全文搜索等操作。

比较适合中小型项目,不需要过多复杂配置,功能丰富,支持嵌入式文档、查询、搜索嵌入、多模态检测,元数据过滤等。

基本使用很简单。

1、安装chroma DB库

pip install chromadb

2、创建 Client

importchromadb

// 有三种方式创建
#1非持久化客户端
client = chromadb.Client()

#2持久化客户端
client = chromadb.PersistentClient(path="/chroma/myCollection")

#3http模式
client = chromadb.HttpClient(host='localhost', port=8000)

3、创建Collect

fromdatetimeimportdatetime

collection = client.create_collection(
name="my_collection",
embedding_function=emb_fn,
metadata={
"description":"my first Chroma collection",
"created": str(datetime.now())
}
)

可以配置相关参数,name 标识 collect 名称,必填项;embedding_function是指定嵌入函数,可默认嵌入模型;metadata 元数据,如索引方式等(可选)。

4、写入

collection.add(
ids=["id1","id2","id3", ...],
embeddings=[[1.1,2.3,3.2], [4.5,6.9,4.4], [1.1,2.3,3.2], ...],
documents=["doc1","doc2","doc3", ...],
metadatas=[{"chapter":3,"verse":16}, {"chapter":3,"verse":5}, {"chapter":29,"verse":11}, ...],

)

参数解释:

  • ids 是指文本块唯一标识
  • embeddings是对于已经向量化的文本块,可以直接写入结果。注意,如不填,写入时默认的选嵌入函数进行向量化
  • document 是指原始的文本块
  • metadatas 是用来描述文本块的元数据,k-v键值对

5、查询

通过一个小案例;

importchromadb

# 初始化持久化客户端(数据保存到本地)
chroma_client = chromadb.PersistentClient(path="./chroma_data")

# 获取或创建集合(类似于数据库表)
collection = chroma_client.get_or_create_collection(name="my_documents")
# 准备要添加的数据
documents = [
"一只柯基在蹦蹦跳跳",
"一只小狗在奔跑",
"一只鸟在飞",
"汽车在高速公路上行驶",
"苹果是一种水果"
]
ids = ["id1","id2","id3","id4","id5"]
metadatas = [{"type":"动物"}, {"type":"动物"}, {"type":"动物"}, {"type":"交通工具"}, {"type":"植物"}]

# 将数据添加到集合中
collection.add(
documents=documents,
metadatas=metadatas,
ids=ids
)
# 执行相似性查询:查找与"蹦蹦跳跳的小狗"相似的文档
results = collection.query(
query_texts=["蹦蹦跳跳的小狗"],
n_results=3
)

# 打印查询结果
print("最相似的文档:", results['documents'])
print("对应的ID:", results['ids'])
print("相似度距离:", results['distances'])# 距离越小相似度越高

执行结果:

image.png
  1. "一只小狗在奔跑" (id2) - 距离:0.312 - 最相似

  2. "一只鸟在飞" (id3) - 距离:0.764 - 中等相似

  3. "一只柯基在蹦蹦跳跳" (id1) - 距离:0.782 - 相似度最低

其中的常用向量数据库如ingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif;font-size: 16px;font-style: normal;font-variant-ligatures: no-common-ligatures;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">Qdrant、ingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif;font-size: 16px;font-style: normal;font-variant-ligatures: no-common-ligatures;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(255, 255, 255);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">Milvus、及FAISS等也非常出色,具体根据需求选择,比如要做超大规模多模态检索,AI搜索引擎,良好集成LangChain/LlamaIndex,可以选择Milvusz这种十几亿级别量级。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ