ChromaDB 速查表
ChromaDB 是一个开源的嵌入式向量数据库,专为 LLM 应用设计。支持语义搜索、元数据过滤、文档管理和持久化存储。可作为独立服务运行,也可在 Python 进程中直接使用,是 RAG 应用的首选向量库。
更新: 2026-07-20·17 条命令
快速开始
``bash
# 安装
pip install chromadb
# 启动服务 chroma run --path ./chroma_data --port 8000
# Python 客户端
python -c "
import chromadb
client = chromadb.HttpClient(host='localhost', port=8000)
print(client.heartbeat())
"
`
集合管理
集合类似传统数据库的表。每个集合有名称和可选的嵌入函数。默认使用 all-MiniLM-L6-v2。支持创建、列出、获取和删除集合。
增删改查
collection.add(documents, metadatas, ids) 添加。collection.query(query_texts, n_results, where) 查询。collection.update(ids, documents) 更新。collection.delete(ids) 删除。collection.get(ids) 按 ID 获取。
元数据过滤
查询时用 where 参数过滤。支持比较运算符($gt、$lt、$ne、$eq)和逻辑运算符($and、$or)。元数据可以是任意 JSON 可序列化的字典。
REST API
ChromaDB 提供 REST API:/api/v1/collections`。可直接用 curl 或 HTTP 客户端访问,无需 Python 库。
启动模式(12)
| 命令 | 难度 | ||
|---|---|---|---|
chroma --help显示 Chroma CLI 帮助 | 基础 | chroma --help | |
pip install chromadb安装 ChromaDB | 基础 | pip install chromadb | |
python import chromadb用 Python 创建 Chroma 客户端 | 中级 | python -c 'import chromadb; client = chromadb.HttpClient(host="localhost", port=8000); print(client.heartbeat())'
| |
create collection创建集合 | 中级 | python -c 'import chromadb; c = chromadb.HttpClient().create_collection(name="my_docs"); print(c.count())'
| |
collection add向集合添加文档 | 中级 | python -c 'import chromadb; c = chromadb.HttpClient().get_collection("my_docs"); c.add(documents=["Hello world"], ids=["doc1"])'
| |
collection query查询集合(语义搜索) | 中级 | python -c 'import chromadb; c = chromadb.HttpClient().get_collection("my_docs"); print(c.query(query_texts=["Hello"], n_results=5))'
| |
collection add with metadata带元数据添加文档 | 中级 | python -c 'import chromadb; c = chromadb.HttpClient().get_collection("my_docs"); c.add(documents=["Doc1"], metadatas=[{"source":"web"}], ids=["1"])'
| |
collection delete从集合中删除文档 | 中级 | python -c 'import chromadb; c = chromadb.HttpClient().get_collection("my_docs"); c.delete(ids=["doc1"])'
| |
collection get按 ID 获取文档 | 中级 | python -c 'import chromadb; c = chromadb.HttpClient().get_collection("my_docs"); print(c.get(ids=["doc1"]))'
| |
collection update更新集合中的文档 | 中级 | python -c 'import chromadb; c = chromadb.HttpClient().get_collection("my_docs"); c.update(ids=["doc1"], documents=["Updated text"])'
| |
list collections列出所有集合 | 基础 | python -c 'import chromadb; print(chromadb.HttpClient().list_collections())' | |
delete collection删除集合 | 中级 | python -c 'import chromadb; chromadb.HttpClient().delete_collection("my_docs")' |
ai-server(5)
| 命令 | 难度 | ||
|---|---|---|---|
chroma run启动 Chroma 服务 | 基础 | chroma run --path ./my_chroma_data | |
chroma run --path指定数据持久化路径 | 基础 | chroma run --path /data/chroma | |
chroma run --host指定绑定的主机地址 | 基础 | chroma run --host 0.0.0.0 --port 8000 | |
chroma run --port指定服务端口(默认 8000) | 基础 | chroma run --path ./data --port 8080 | |
curl collections API通过 API 获取集合列表 | 基础 | curl -s http://localhost:8000/api/v1/collections |
常见问题
本速查表数据整理自各工具官方文档。最后更新: 2026-07-20。