[ PROMPT_NODE_22964 ]
chroma
[ SKILL_DOCUMENTATION ]
# Chroma - 开源嵌入数据库
为构建具有记忆的 LLM 应用而生的 AI 原生数据库。
## 何时使用 Chroma
**在以下情况使用 Chroma:**
- 构建 RAG(检索增强生成)应用
- 需要本地/自托管的向量数据库
- 想要开源解决方案 (Apache 2.0)
- 在笔记本中进行原型设计
- 对文档进行语义搜索
- 存储带有元数据的嵌入
**指标**:
- **24,300+ GitHub stars**
- **1,900+ forks**
- **v1.3.3** (稳定,每周发布)
- **Apache 2.0 许可证**
**可替代方案**:
- **Pinecone**: 托管云服务,自动扩展
- **FAISS**: 纯相似度搜索,无元数据支持
- **Weaviate**: 生产级 ML 原生数据库
- **Qdrant**: 高性能,基于 Rust
## 快速开始
### 安装
bash
# Python
pip install chromadb
# JavaScript/TypeScript
npm install chromadb @chroma-core/default-embed
### 基本用法 (Python)
python
import chromadb
# 创建客户端
client = chromadb.Client()
# 创建集合
collection = client.create_collection(name="my_collection")
# 添加文档
collection.add(
documents=["This is document 1", "This is document 2"],
metadatas=[{"source": "doc1"}, {"source": "doc2"}],
ids=["id1", "id2"]
)
# 查询
results = collection.query(
query_texts=["document about topic"],
n_results=2
)
print(results)
## 核心操作
### 1. 创建集合
python
# 简单集合
collection = client.create_collection("my_docs")
# 使用自定义嵌入函数
from chromadb.utils import embedding_functions
openai_ef = embedding_functions.OpenAIEmbeddingFunction(
api_key="your-key",
model_name="text-embedding-3-small"
)
collection = client.create_collection(
name="my_docs",
embedding_function=openai_ef
)
# 获取现有集合
collection = client.get_collection("my_docs")
# 删除集合
client.delete_collection("my_docs")
### 2. 添加文档
python
# 使用自动生成的 ID 添加
collection.add(
documents=["Doc 1", "Doc 2", "Doc 3"],
metadatas=[
{"source": "web", "category": "tutorial"},
{"source": "pdf", "page": 5},
{"source": "api", "timestamp": "2025-01-01"}
],
ids=["id1", "id2", "id3"]
)
# 使用自定义嵌入添加
collection.add(
embeddings=[[0.1, 0.2, ...], [0.3, 0.4, ...]],
documents=["Doc 1", "Doc 2"],
ids=["id1", "id2"]
)
### 3. 查询 (相似度搜索)
python
# 基本查询
results = collection.query(
query_texts=["machine