






















Chroma是一个开源的向量数据库,专门用于构建AI应用。它提供了:
# 安装Chroma
pip install chromadb
# 或者安装完整版本(包含所有依赖)
pip install chromadb[full]
# 安装特定嵌入函数依赖
pip install chromadb[openai] # OpenAI嵌入
pip install chromadb[sentence-transformers] # Sentence Transformers
pip install chromadb[huggingface] # Hugging Face模型
# 拉取Chroma镜像
docker pull chromadb/chroma
# 运行Chroma服务器
docker run -p 8000:8000 chromadb/chroma
import chromadb
print(chromadb.__version__)
# 创建客户端测试连接
client = chromadb.Client()
print("Chroma安装成功!")
# 文档结构示例
{
"id": "doc_001",
"document": "这是一个示例文档",
"embedding": [0.1, 0.2, 0.3, ...], # 向量表示
"metadata": {
"source": "example.txt",
"category": "技术文档",
"timestamp": "2024-01-01"
}
}
import chromadb
# 创建客户端
client = chromadb.Client()
# 创建Collection
collection = client.create_collection(
name="my_collection",
metadata={"description": "我的第一个Collection"}
)
print(f"Collection创建成功: {collection.name}")
# 添加文档
collection.add(
documents=[
"Python是一种高级编程语言",
"机器学习是人工智能的重要分支",
"Chroma是一个向量数据库"
],
ids=["doc1", "doc2", "doc3"],
metadatas=[
{"topic": "编程"},
{"topic": "AI"},
{"topic": "数据库"}
]
)
print("文档添加成功!文本已自动转换为向量")
# 验证向量已生成
stored_data = collection.get(include=["documents", "embeddings"])
if stored_data['embeddings']:
print(f"向量维度: {len(stored_data['embeddings'][0])}")
print("文本已成功转换为向量表示")
# 查询相似文档
results = collection.query(
query_texts=["什么是Python"],
n_results=2
)
print("查询结果:")
for i, doc in enumerate(results['documents'][0]):
print(f"{i+1}. {doc}")
print(f" 相似度分数: {results['distances'][0][i]}")
print(f" 元数据: {results['metadatas'][0][i]}")
import chromadb
def quick_start_demo():
# 1. 创建客户端
client = chromadb.Client()
# 2. 创建Collection
collection = client.create_collection("demo_collection")
# 3. 准备数据
documents = [
"苹果是一种水果,富含维生素",
"Python是一种编程语言",
"机器学习算法可以预测未来趋势",
"香蕉含有丰富的钾元素",
"深度学习是机器学习的子集"
]
ids = [f"doc_{i}" for i in range(len(documents))]
metadatas = [
{"category": "食物"},
{"category": "技术"},
{"category": "技术"},
{"category": "食物"},
{"category": "技术"}
]
# 4. 添加文档
collection.add(
documents=documents,
ids=ids,
metadatas=metadatas
)
# 5. 查询
results = collection.query(
query_texts=["编程相关的内容"],
n_results=3
)
# 6. 显示结果
print("最相似的文档:")
for doc, distance in zip(results['documents'][0], results['distances'][0]):
print(f"- {doc} (距离: {distance:.4f})")
if __name__ == "__main__":
quick_start_demo()
import chromadb
client = chromadb.Client()
# 基础创建
collection = client.create_collection(name="my_collection")
# 带参数创建
collection = client.create_collection(
name="advanced_collection",
metadata={
"description": "高级配置的Collection",
"version": "1.0"
},
embedding_function=None, # 使用默认嵌入函数
get_or_create=True # 如果存在则获取,否则创建
)
# 获取Collection
collection = client.get_collection(name="my_collection")
# 获取或创建
collection = client.get_or_create_collection(
name="my_collection",
metadata={"updated": "2024-01-01"}
)
# 获取所有Collection
collections = client.list_collections()
print("现有Collections:")
for collection in collections:
print(f"- {collection.name}: {collection.metadata}")
# 删除Collection
client.delete_collection(name="my_collection")
print("Collection已删除")
# 获取Collection统计信息
count = collection.count()
print(f"文档数量: {count}")
# 查看Collection详情
print(f"Collection名称: {collection.name}")
print(f"Collection ID: {collection.id}")
print(f"元数据: {collection.metadata}")
# 单个文档
collection.add(
documents="这是一个文档",
ids="doc_1",
metadatas={"source": "manual"}
)
# 批量添加
collection.add(
documents=[
"第一个文档内容",
"第二个文档内容",
"第三个文档内容"
],
ids=["doc_1", "doc_2", "doc_3"],
metadatas=[
{"category": "A", "priority": 1},
{"category": "B", "priority": 2},
{"category": "A", "priority": 3}
]
)
# 更新现有文档
collection.update(
ids=["doc_1"],
documents=["更新后的文档内容"],
metadatas=[{"category": "C", "updated": True}]
)
# 批量更新
collection.update(
ids=["doc_1", "doc_2"],
documents=[
"更新的第一个文档",
"更新的第二个文档"
],
metadatas=[
{"status": "updated"},
{"status": "updated"}
]
)
# 删除指定ID的文档
collection.delete(ids=["doc_1", "doc_2"])
# 条件删除
collection.delete(
where={"category": "temporary"}
)
# 删除所有文档
collection.delete()
# 获取所有文档
all_docs = collection.get()
# 获取指定ID的文档
specific_docs = collection.get(
ids=["doc_1", "doc_2"],
include=["documents", "metadatas", "embeddings"]
)
# 条件获取
filtered_docs = collection.get(
where={"category": "A"},
limit=10,
offset=0
)
print("获取的文档:")
for i, doc in enumerate(specific_docs['documents']):
print(f"{i+1}. ID: {specific_docs['ids'][i]}")
print(f" 内容: {doc}")
print(f" 元数据: {specific_docs['metadatas'][i]}")
# 推荐的元数据结构
metadata_example = {
"source_file": "tutorial.md",
"category": "technical",
"tags": ["database", "tutorial"],
"quality_score": 0.95,
"language": "zh-CN",
"created_at": "2024-01-01"
}
collection.add(
documents=["高质量的技术文档内容"],
ids=["high_quality_doc"],
metadatas=[metadata_example]
)
# 使用默认嵌入函数
client = chromadb.Client()
collection = client.create_collection("default_embedding")
# 文档会自动生成嵌入向量
collection.add(
documents=["这段文字会自动转换为向量"],
ids=["auto_embed_1"]
)
from chromadb.utils import embedding_functions
# OpenAI嵌入
openai_ef = embedding_functions.OpenAIEmbeddingFunction(
api_key="your-openai-api-key",
model_name="text-embedding-ada-002"
)
# Sentence Transformers嵌入
sentence_transformer_ef = embedding_functions.SentenceTransformerEmbeddingFunction(
model_name="distiluse-base-multilingual-cased"
)
collection = client.create_collection(
name="custom_collection",
embedding_function=sentence_transformer_ef
)
# 文本查询
results = collection.query(
query_texts=["Python编程"],
n_results=5
)
# 解析查询结果
for i, doc in enumerate(results['documents'][0]):
print(f"{i+1}. {doc}")
print(f" 距离: {results['distances'][0][i]:.4f}")
# 批量查询
results = collection.query(
query_texts=["Python编程", "机器学习", "数据库"],
n_results=3
)
for query_idx, query_text in enumerate(["Python", "ML", "DB"]):
print(f"查询: {query_text}")
for doc in results['documents'][query_idx]:
print(f" - {doc}")
# 元数据过滤
results = collection.query(
query_texts=["Python"],
where={"category": "programming"}
)
# 组合条件
results = collection.query(
query_texts=["机器学习"],
where={
"$and": [
{"category": "AI"},
{"difficulty": "beginner"}
]
}
)
# 比较和包含操作
results = collection.query(
query_texts=["查询文本"],
where={
"score": {"$gt": 0.8}, # 大于
"tags": {"$in": ["important", "urgent"]}, # 包含
"status": {"$ne": "deleted"} # 不等于
}
)
# 使用PersistentClient
client = chromadb.PersistentClient(path="./chroma_db")
# 创建持久化Collection
collection = client.create_collection("persistent_collection")
# 添加数据(自动持久化)
collection.add(
documents=["这些数据将被持久化存储"],
ids=["persist_1"]
)
import shutil
from datetime import datetime
def backup_chroma_db(source_path, backup_dir):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_path = f"{backup_dir}/chroma_backup_{timestamp}"
shutil.copytree(source_path, backup_path)
return backup_path
# 使用示例
backup_path = backup_chroma_db("./chroma_db", "./backups")
print(f"数据库已备份到: {backup_path}")
# Docker方式
docker run -p 8000:8000 chromadb/chroma
# Python方式
chroma run --host localhost --port 8000 --path ./chroma_data
# 连接到远程Chroma服务器
client = chromadb.HttpClient(
host="localhost",
port=8000,
ssl=False
)
# 验证连接
collections = client.list_collections()
print(f"连接成功,发现 {len(collections)} 个Collections")
def batch_add_documents(collection, documents, batch_size=1000):
"""批量添加文档以提高性能"""
for i in range(0, len(documents), batch_size):
batch = documents[i:i+batch_size]
batch_ids = [f"doc_{i+j}" for j in range(len(batch))]
collection.add(
documents=batch,
ids=batch_ids
)
print(f"已添加 {i+len(batch)}/{len(documents)} 个文档")
# 优化查询参数
optimized_results = collection.query(
query_texts=["查询文本"],
n_results=10, # 适中的结果数量
include=["documents", "metadatas"], # 只包含需要的字段
where={"category": "target_category"} # 使用过滤减少搜索空间
)
import numpy as np
from chromadb.api.types import EmbeddingFunction
class CustomEmbeddingFunction(EmbeddingFunction):
def __call__(self, input_texts):
# 自定义嵌入逻辑
embeddings = []
for text in input_texts:
embedding = np.random.rand(384).tolist() # 示例
embeddings.append(embedding)
return embeddings
# 使用自定义嵌入函数
custom_ef = CustomEmbeddingFunction()
collection = client.create_collection(
name="custom_embedding_collection",
embedding_function=custom_ef
)
class RAGQuestionAnswering:
def __init__(self, knowledge_base_path="./rag_kb"):
self.client = chromadb.PersistentClient(path=knowledge_base_path)
self.collection = self.client.get_or_create_collection(
name="knowledge_base"
)
def add_knowledge(self, documents, sources=None):
"""添加知识到知识库"""
ids = [f"kb_{i}" for i in range(len(documents))]
metadatas = [{"source": source} for source in (sources or ["unknown"] * len(documents))]
self.collection.add(
documents=documents,
ids=ids,
metadatas=metadatas
)
def query_knowledge(self, question, top_k=3):
"""查询相关知识"""
results = self.collection.query(
query_texts=[question],
n_results=top_k
)
relevant_docs = []
for doc, distance, metadata in zip(
results['documents'][0],
results['distances'][0],
results['metadatas'][0]
):
relevant_docs.append({
"content": doc,
"relevance_score": 1 - distance,
"source": metadata.get("source", "unknown")
})
return relevant_docs
# 使用示例
rag_system = RAGQuestionAnswering()
rag_system.add_knowledge([
"Python是一种高级编程语言",
"机器学习是人工智能的分支",
"Chroma是向量数据库"
], ["python_doc", "ml_doc", "chroma_doc"])
knowledge = rag_system.query_knowledge("什么是Python?")
for item in knowledge:
print(f"- {item['content']} (相关性: {item['relevance_score']:.3f})")
# 1. 使用有意义的ID和元数据
def create_document_id(source, timestamp, hash_value):
return f"{source}_{timestamp}_{hash_value[:8]}"
# 2. 标准化文档格式
def standardize_document(text, source_info):
cleaned_text = text.strip().replace('\n\n', '\n')
metadata = {
"source": source_info.get("source", "unknown"),
"word_count": len(cleaned_text.split()),
"language": "zh-CN"
}
return cleaned_text, metadata
# 3. 实现增量更新
class IncrementalUpdater:
def __init__(self, collection):
self.collection = collection
self.processed_hashes = set()
def add_if_new(self, document, doc_id, metadata=None):
doc_hash = hash(document)
if doc_hash not in self.processed_hashes:
self.collection.add(
documents=[document],
ids=[doc_id],
metadatas=[metadata or {}]
)
self.processed_hashes.add(doc_hash)
return True
return False
import time
from functools import wraps
def retry_on_failure(max_retries=3, delay=1):
"""重试装饰器"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
for attempt in range(max_retries):
try:
return func(*args, **kwargs)
except Exception as e:
if attempt == max_retries - 1:
raise e
print(f"操作失败,{delay}秒后重试: {e}")
time.sleep(delay)
return None
return wrapper
return decorator
@retry_on_failure(max_retries=3)
def robust_query(collection, query_text):
return collection.query(query_texts=[query_text], n_results=5)
问题1: 安装失败
# 解决方案:使用特定版本
pip install chromadb==0.4.15
# 或清理缓存重新安装
pip cache purge
pip install chromadb
问题2: 内存不足
# 解决方案:分批处理大数据集
def process_large_dataset(documents, batch_size=100):
for i in range(0, len(documents), batch_size):
batch = documents[i:i+batch_size]
collection.add(
documents=batch,
ids=[f"doc_{i+j}" for j in range(len(batch))]
)
# 强制垃圾回收
import gc
gc.collect()
问题3: 查询性能慢
# 解决方案:优化查询策略
# 1. 使用过滤条件
# 2. 减少返回结果数量
# 3. 只包含必要字段
optimized_results = collection.query(
query_texts=["查询文本"],
n_results=5, # 减少结果数量
where={"category": "specific"}, # 添加过滤
include=["documents"] # 只包含文档
)
# 启用详细日志
import logging
logging.basicConfig(level=logging.DEBUG)
# 检查Collection状态
def debug_collection(collection):
print(f"Collection名称: {collection.name}")
print(f"文档数量: {collection.count()}")
# 获取样本数据
sample = collection.get(limit=5)
print(f"样本文档: {len(sample['documents'])}")
# 检查嵌入维度
if sample['embeddings']:
print(f"嵌入维度: {len(sample['embeddings'][0])}")
# 性能分析
import time
def benchmark_query(collection, query_text, iterations=10):
times = []
for _ in range(iterations):
start = time.time()
collection.query(query_texts=[query_text], n_results=5)
times.append(time.time() - start)
print(f"平均查询时间: {sum(times)/len(times):.3f}秒")
print(f"最快查询: {min(times):.3f}秒")
print(f"最慢查询: {max(times):.3f}秒")
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。