SuperAPI AI 工具集

chroma

数据与分析

查看项目

更新于 10-15 10:24

技能介绍

Chroma 是一个开源的嵌入数据库(embedding database),定位为构建带记忆的 LLM 应用的 AI 原生数据库。项目采用 Apache 2.0 许可,GitHub 星标 24,300+,fork 1,900+,版本 1.3.3(稳定版,每周发布)。

用途

Chroma 适用于以下场景:构建 RAG(检索增强生成)应用;需要本地或自托管的向量数据库;希望使用开源方案(Apache 2.0);在 notebook 中做原型开发;对文档进行语义搜索;存储带元数据的嵌入向量。

官方也列出了替代方案供对比:Pinecone(托管云、自动扩缩容)、FAISS(纯相似度搜索、无元数据)、Weaviate(生产级 ML 原生数据库)、Qdrant(高性能、基于 Rust)。

主要能力

  • 集合管理:创建、获取、删除集合,可指定自定义嵌入函数。
  • 文档操作:添加文档(可自动生成 ID 或使用自定义嵌入)、按 ID 或过滤条件获取文档、更新文档、按 ID 或过滤条件删除文档。
  • 查询:基于文本的相似度搜索,返回文档、元数据、距离分数和 ID;支持元数据过滤。
  • 元数据过滤:支持精确匹配、比较运算符($gt、$gte、$lt、$lte、$ne)、逻辑运算符($and、$or)以及 $in 包含判断。
  • 持久化存储:使用 PersistentClient 将数据保存到磁盘,重启后按相同路径重新加载。
  • 嵌入函数:默认使用 Sentence Transformers(默认模型 all-MiniLM-L6-v2),可接入 OpenAI、HuggingFace,或通过继承 EmbeddingFunction 实现自定义嵌入。
  • 服务端模式:可运行 Chroma 服务并通过 HttpClient 连接,适合多用户生产环境。
  • 框架集成:提供 LangChain 与 LlamaIndex 集成方式,可作为向量存储或检索器使用。

使用方式

安装:

Python:pip install chromadb

JavaScript/TypeScript:npm install chromadb @chroma-core/default-embed

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)

持久化:

client = chromadb.PersistentClient(path="./chroma_db") collection = client.create_collection("my_docs") collection.add(documents=["Doc 1"], ids=["id1"])

服务端模式:

chroma run --path ./chroma_db --port 8000

import chromadb from chromadb.config import Settings client = chromadb.HttpClient( host="localhost", port=8000, settings=Settings(anonymized_telemetry=False) ) collection = client.get_or_create_collection("my_docs")

LangChain 集成示例:

from langchain_chroma import Chroma from langchain_openai import OpenAIEmbeddings from langchain.text_splitter import RecursiveCharacterTextSplitter text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000) docs = text_splitter.split_documents(documents) vectorstore = Chroma.from_documents( documents=docs, embedding=OpenAIEmbeddings(), persist_directory="./chroma_db" ) results = vectorstore.similarity_search("machine learning", k=3) retriever = vectorstore.as_retriever(search_kwargs={"k": 5})

LlamaIndex 集成示例:

from llama_index.vector_stores.chroma import ChromaVectorStore from llama_index.core import VectorStoreIndex, StorageContext import chromadb db = chromadb.PersistentClient(path="./chroma_db") collection = db.get_or_create_collection("my_collection") vector_store = ChromaVectorStore(chroma_collection=collection) storage_context = StorageContext.from_defaults(vector_store=vector_store) index = VectorStoreIndex.from_documents( documents, storage_context=storage_context ) query_engine = index.as_query_engine() response = query_engine.query("What is machine learning?")

适用场景

  • RAG 应用:为 LLM 提供可检索的外部知识。
  • 语义搜索:对文档集合按语义相似度检索。
  • 本地/自托管向量库:数据保存在本地或自有服务器。
  • 原型开发:在 notebook 中快速搭建向量检索。
  • 带元数据的嵌入存储:结合过滤条件缩小检索范围。

最佳实践(来自资料)

使用持久化客户端避免重启丢数据;添加元数据以支持过滤与追踪;批量添加文档;选择合适的嵌入模型平衡速度与质量;使用过滤器缩小搜索空间;使用唯一 ID 避免冲突;定期备份(复制 chroma_db 目录);监控集合规模;测试嵌入函数质量;生产环境使用服务端模式。

性能参考(资料给出)

添加 100 个文档约 1-3 秒(含嵌入);查询 top 10 约 50-200 毫秒(取决于集合大小);元数据过滤约 10-50 毫秒(索引良好时)。

限制与说明

资料未提供收费、价格、平台支持范围、并发上限、最大数据规模等具体信息,暂未提供。使用前建议参考官方文档与 GitHub 仓库获取最新信息。

资源

GitHub:https://github.com/chroma-core/chroma 文档:https://docs.trychroma.com Discord:https://discord.gg/MMeYNTmh3x 版本:1.3.3+ 许可:Apache 2.0