mcpField
C
chroma

chroma

github.com/orchestra-researchデータ・データベース14.1k962 回インストール19 週間前に更新
リポジトリ

chroma について

Chromaは、RAGやセマンティック検索などのLLMアプリ向けオープンソース埋め込みデータベースです。ベクトル検索、メタデータフィルタリング、永続化ストレージを備えています。

主な機能

  1. 01ドキュメント、埋め込み、メタデータの保存
  2. 02類似検索とメタデータフィルター
  3. 03コレクションデータの作成・取得・更新・削除
  4. 04ローカル永続化とサーバーモード
  5. 05複数の埋め込み関数に対応

活用シーン

  1. 01RAGアプリの構築
  2. 02ドキュメントのセマンティック検索
  3. 03ノートブックでのプロトタイプ作成
  4. 04メタデータ付き埋め込みの保存

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/orchestra-research/ai-research-skills --skill chroma

インストール後、Claude Code で/chromaを入力して読み込まれたか確認します。

同梱ファイルreferences/integration.md

詳細

Chroma - オープンソースのエンベディングデータベース

メモリを備えたLLMアプリケーションを構築するためのAIネイティブなデータベースです。

Chromaを使うべき場面

Chromaを使うとよい場合:

  • RAG(検索拡張生成)アプリケーションを構築している
  • ローカルやセルフホスト可能なベクトルデータベースが必要である
  • オープンソースのソリューション(Apache 2.0)を希望する
  • ノートブックでプロトタイプを作成している
  • ドキュメントに対するセマンティック検索を行いたい
  • メタデータ付きのエンベディングを保存したい

指標:

  • 24,300+ GitHub スター
  • 1,900+ フォーク
  • v1.3.3(安定版、毎週リリース)
  • Apache 2.0 ライセンス

代わりに検討すべき代替手段:

  • Pinecone: マネージドなクラウドサービスで、自動スケーリングに対応
  • FAISS: 純粋な類似度検索に特化しており、メタデータは扱えない
  • Weaviate: 本番環境向けのML ネイティブなデータベース
  • Qdrant: 高性能で、Rust で実装されている

クイックスタート

インストール
# Python
pip install chromadb

# JavaScript/TypeScript
npm install chromadb @chroma-core/default-embed
基本的な使い方(Python)
import chromadb

# Create client
client = chromadb.Client()

# Create collection
collection = client.create_collection(name="my_collection")

# Add documents
collection.add(
    documents=["This is document 1", "This is document 2"],
    metadatas=[{"source": "doc1"}, {"source": "doc2"}],
    ids=["id1", "id2"]
)

# Query
results = collection.query(
    query_texts=["document about topic"],
    n_results=2
)

print(results)

主要な操作

1. コレクションの作成
# Simple collection
collection = client.create_collection("my_docs")

# With custom embedding function
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
)

# Get existing collection
collection = client.get_collection("my_docs")

# Delete collection
client.delete_collection("my_docs")
2. ドキュメントの追加
# Add with auto-generated IDs
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"]
)

# Add with custom embeddings
collection.add(
    embeddings=[[0.1, 0.2, ...], [0.3, 0.4, ...]],
    documents=["Doc 1", "Doc 2"],
    ids=["id1", "id2"]
)
3. クエリ(類似度検索)
# Basic query
results = collection.query(
    query_texts=["machine learning tutorial"],
    n_results=5
)

# Query with filters
results = collection.query(
    query_texts=["Python programming"],
    n_results=3,
    where={"source": "web"}
)

# Query with metadata filters
results = collection.query(
    query_texts=["advanced topics"],
    where={
        "$and": [
            {"category": "tutorial"},
            {"difficulty": {"$gte": 3}}
        ]
    }
)

# Access results
print(results["documents"])      # List of matching documents
print(results["metadatas"])      # Metadata for each doc
print(results["distances"])      # Similarity scores
print(results["ids"])            # Document IDs
4. ドキュメントの取得
# Get by IDs
docs = collection.get(
    ids=["id1", "id2"]
)

# Get with filters
docs = collection.get(
    where={"category": "tutorial"},
    limit=10
)

# Get all documents
docs = collection.get()
5. ドキュメントの更新
# Update document content
collection.update(
    ids=["id1"],
    documents=["Updated content"],
    metadatas=[{"source": "updated"}]
)
6. ドキュメントの削除
# Delete by IDs
collection.delete(ids=["id1", "id2"])

# Delete with filter
collection.delete(
    where={"source": "outdated"}
)

永続ストレージ

# Persist to disk
client = chromadb.PersistentClient(path="./chroma_db")

collection = client.create_collection("my_docs")
collection.add(documents=["Doc 1"], ids=["id1"])

# Data persisted automatically
# Reload later with same path
client = chromadb.PersistentClient(path="./chroma_db")
collection = client.get_collection("my_docs")

エンベディング関数

デフォルト(Sentence Transformers)
# Uses sentence-transformers by default
collection = client.create_collection("my_docs")
# Default model: all-MiniLM-L6-v2
OpenAI
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="openai_docs",
    embedding_function=openai_ef
)
HuggingFace
huggingface_ef = embedding_functions.HuggingFaceEmbeddingFunction(
    api_key="your-key",
    model_name="sentence-transformers/all-mpnet-base-v2"
)

collection = client.create_collection(
    name="hf_docs",
    embedding_function=huggingface_ef
)
カスタムエンベディング関数
from chromadb import Documents, EmbeddingFunction, Embeddings

class MyEmbeddingFunction(EmbeddingFunction):
    def __call__(self, input: Documents) -> Embeddings:
        # Your embedding logic
        return embeddings

my_ef = MyEmbeddingFunction()
collection = client.create_collection(
    name="custom_docs",
    embedding_function=my_ef
)

メタデータフィルタリング

# Exact match
results = collection.query(
    query_texts=["query"],
    where={"category": "tutorial"}
)

# Comparison operators
results = collection.query(
    query_texts=["query"],
    where={"page": {"$gt": 10}}  # $gt, $gte, $lt, $lte, $ne
)

# Logical operators
results = collection.query(
    query_texts=["query"],
    where={
        "$and": [
            {"category": "tutorial"},
            {"difficulty": {"$lte": 3}}
        ]
    }  # Also: $or
)

# Contains
results = collection.query(
    query_texts=["query"],
    where={"tags": {"$in": ["python", "ml"]}}
)

LangChain 連携

from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter

# Split documents
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000)
docs = text_splitter.split_documents(documents)

# Create Chroma vector store
vectorstore = Chroma.from_documents(
    documents=docs,
    embedding=OpenAIEmbeddings(),
    persist_directory="./chroma_db"
)

# Query
results = vectorstore.similarity_search("machine learning", k=3)

# As retriever
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

# Initialize Chroma
db = chromadb.PersistentClient(path="./chroma_db")
collection = db.get_or_create_collection("my_collection")

# Create vector store
vector_store = ChromaVectorStore(chroma_collection=collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)

# Create index
index = VectorStoreIndex.from_documents(
    documents,
    storage_context=storage_context
)

# Query
query_engine = index.as_query_engine()
response = query_engine.query("What is machine learning?")

サーバーモード

# Run Chroma server
# Terminal: chroma run --path ./chroma_db --port 8000

# Connect to server
import chromadb
from chromadb.config import Settings

client = chromadb.HttpClient(
    host="localhost",
    port=8000,
    settings=Settings(anonymized_telemetry=False)
)

# Use as normal
collection = client.get_or_create_collection("my_docs")

ベストプラクティス

  1. 永続クライアントを使う - 再起動時にデータを失わないようにする
  2. メタデータを追加する - フィルタリングと追跡が可能になる
  3. 操作をバッチ化する - 複数のドキュメントを一度に追加する
  4. 適切なエンベディングモデルを選ぶ - 速度と品質のバランスを取る
  5. フィルターを使う - 検索範囲を絞り込む
  6. 一意のIDを使う - 衝突を避ける
  7. 定期的にバックアップを取る - chroma_db ディレクトリをコピーする
  8. コレクションのサイズを監視する - 必要に応じてスケールアップする
  9. エンベディング関数をテストする - 品質を確認する
  10. 本番環境ではサーバーモードを使う - 複数ユーザーでの利用に適している

パフォーマンス

操作レイテンシ備考
100 件のドキュメントを追加約 1〜3 秒エンベディングを含む
クエリ(上位 10 件)約 50〜200 ミリ秒コレクションのサイズによって変動する
メタデータフィルター約 10〜50 ミリ秒適切なインデックスがあれば高速

リソース

コメント