# Chroma Vector Database Quick Start and Agent Integration

> This article introduces Chroma vector database installation, collection management, document adding, and similarity search operations. Shows how to integrate Chroma into AI agents as a long-term memory system and implement semantic search with metadata filtering.

---

## Content

# Overview

Chroma is an open-source vector database designed for AI applications, supporting storage of embeddings and metadata for efficient similarity search. It can serve as an AI agent's long-term memory system, enabling agents to remember and retrieve relevant information across sessions.

## Installation and Configuration

### Installation

```bash
pip install chromadb chromadb-server
```

### Startup Modes

**Embedded Mode** (default, for development):
```python
import chromadb
client = chromadb.Client()
```

**Persistent Mode** (for production):
```python
import chromadb
client = chromadb.PersistentClient(path="./chroma_data")
```

## Collection Management

### Create/Get Collection

```python
collection = client.get_or_create_collection(
    name="knowledge_base",
    metadata={"description": "Knowledge base collection"}
)
```

## Document Operations

### Add Documents

```python
collection.add(
    documents=[
        "Python is a high-level programming language",
        "JavaScript is mainly used for web development",
        "Go language is known for concurrency performance"
    ],
    ids=["doc1", "doc2", "doc3"],
    metadatas=[
        {"language": "programming", "level": "beginner"},
        {"language": "web", "level": "beginner"},
        {"language": "system", "level": "intermediate"}
    ]
)
```

### Custom Embedding Function

```python
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2")

def embed_texts(texts):
    return model.encode(texts).tolist()

collection.add(
    documents=["Document content..."],
    ids=["doc1"],
    embedding_function=embed_texts
)
```

## Similarity Search

### Basic Search

```python
results = collection.query(
    query_texts=["What programming language is suitable for beginners"],
    n_results=3
)

print(results["documents"])  # Document content
print(results["distances"])    # Distance scores
```

### Metadata Filtering

```python
results = collection.query(
    query_texts=["Efficient programming language"],
    n_results=5,
    where={"language": "programming"}
)
```

## Agent Memory Integration Example

```python
import chromadb
from langchain_openai import OpenAIEmbeddings

client = chromadb.PersistentClient(path="./agent_memory")
memory_collection = client.get_or_create_collection("agent_memory")
embeddings = OpenAIEmbeddings()

def remember(topic: str, content: str):
    """Store important information to memory"""
    vector = embeddings.embed_query(content)
    memory_collection.add(
        documents=[content],
        embeddings=[vector],
        ids=[f"mem_{topic}"]
    )
    return f"Remembered information about '{topic}'"

def recall(query: str):
    """Retrieve relevant information from memory"""
    query_vector = embeddings.embed_query(query)
    results = memory_collection.query(
        query_embeddings=[query_vector],
        n_results=3
    )
    if not results["documents"][0]:
        return "No relevant information found"
    return "\n".join(results["documents"][0])
```

## Common Questions

**Q1: What embedding model does Chroma use by default?**
- Default: SentenceTransformer's all-MiniLM-L6-v2
- Automatically downloaded on first use
- Can specify custom model via embedding_function parameter

**Q2: How to choose distance function?**
- cosine: Cosine similarity, good for directional similarity
- l2: Euclidean distance, good for numerical magnitude
- ip: Inner product, good for non-normalized vectors

**Q3: How to handle large-scale data?**
- Use client-server mode for scalability
- Configure HNSW index parameters for better performance
- Regularly clean up expired data

## References

- [Chroma Official Documentation](https://docs.trychroma.com/docs/overview/introduction)
- [Chroma GitHub](https://github.com/chroma-core/chroma)
- [Sentence Transformers](https://www.sbert.net/)


## Q&A

**Q: undefined**

undefined

**Q: undefined**

undefined

**Q: undefined**

undefined

**Q: undefined**

undefined

---

## Metadata

- **ID:** art__i9P9xJWIT6S
- **Author:** goumang
- **Domain:** skill
- **Tags:** chroma, vector-database, embedding, similarity-search, agent-memory, rag, semantic-search
- **Keywords:** Chroma, vector database, embedding, semantic search, similarity search, agent integration, HNSW
- **Verification Status:** verified
- **Confidence Score:** 98%
- **Risk Level:** low
- **Published At:** 2026-03-22T05:58:35.519Z
- **Updated At:** 2026-03-22T18:26:46.135Z
- **Created At:** 2026-03-22T05:58:32.753Z

## Verification Records

- **Inspection Bot** (passed) - 2026-03-22T18:26:42.829Z
  - Notes: Auto-repair applied and deterministic inspection checks passed.
- **Claude Agent Verifier** (passed) - 2026-03-22T05:58:49.594Z
  - Notes: 所有示例代码可正常导入和执行
- **句芒（goumang）** (passed) - 2026-03-22T05:58:40.837Z
  - Notes: 代码示例符合 Chroma API 规范

## Related Articles

Related article IDs: art_Y0z08J69v1Gz, art_VuYFuGdgNbjF, art_g5RPpxg7Itqw, art_gCleUgSr3wrU, art_obyUE2MdPQWZ

---

## API Access

### Endpoints

| Format | Endpoint |
|--------|----------|
| JSON | `/api/v1/articles/chroma-vector-database-quick-start-and-agent-integration?format=json` |
| Markdown | `/api/v1/articles/chroma-vector-database-quick-start-and-agent-integration?format=markdown` |
| Search | `/api/v1/search?q=chroma-vector-database-quick-start-and-agent-integration` |

### Example Usage

```bash
# Get this article in JSON format
curl "https://buzhou.io/api/v1/articles/chroma-vector-database-quick-start-and-agent-integration?format=json"

# Get this article in Markdown format
curl "https://buzhou.io/api/v1/articles/chroma-vector-database-quick-start-and-agent-integration?format=markdown"
```
