This document describes the ChromaDB integration in the Agent.Api project, including setup, configuration, and usage examples.
- Repository Pattern:
IChromaDbServiceabstracts data access operations - Dependency Injection: Services are registered and injected through DI container
- Extension Method Pattern: Configuration is modularized using extension methods
- Builder Pattern: Application configuration follows the builder pattern
ChromaDbService: Main service implementingIChromaDbServiceChromaDbExtensions: Extension methods for service registrationChromaDbController: REST API endpoints for ChromaDB operations- Docker configuration for deployment
Add to appsettings.json:
{
"ConnectionStrings": {
"ChromaDb": "http://localhost:8000"
}
}Services are automatically registered in Program.cs:
builder.Services.AddChromaDb(builder.Configuration);GET /api/chromadb/collections- List all collectionsPOST /api/chromadb/collections- Create a new collectionGET /api/chromadb/collections/{name}- Get collection detailsDELETE /api/chromadb/collections/{name}- Delete a collection
POST /api/chromadb/collections/{name}/documents- Add documentsGET /api/chromadb/collections/{name}/documents- Get documentsPOST /api/chromadb/collections/{name}/query- Query documents
POST /api/chromadb/collections
Content-Type: application/json
{
"name": "my_collection",
"metadata": {
"description": "Sample collection for testing"
}
}POST /api/chromadb/collections/my_collection/documents
Content-Type: application/json
{
"documents": [
"This is the first document",
"This is the second document"
],
"ids": ["doc1", "doc2"],
"metadatas": [
{"source": "manual", "type": "text"},
{"source": "manual", "type": "text"}
]
}POST /api/chromadb/collections/my_collection/query
Content-Type: application/json
{
"queryTexts": ["search for documents"],
"nResults": 5
}# Start ChromaDB and Agent.Api
docker-compose -f docker-compose.chromadb.yml up -d
# View logs
docker-compose -f docker-compose.chromadb.yml logs -f
# Stop services
docker-compose -f docker-compose.chromadb.yml down# Build ChromaDB image
docker build -t chromadb-custom ./docker/chromadb
# Run ChromaDB container
docker run -d \
--name chromadb \
-p 8000:8000 \
-v chromadb_data:/chroma/chroma \
chromadb-customChromaDB is configured with basic authentication:
- Default admin credentials:
admin:chromadb123 - Default user credentials:
user:userpass
Important: Change default credentials in production!
CORS is configured to allow all origins for development. Restrict in production:
builder.Services.AddChromaDb(options =>
{
options.Url = "http://chromadb:8000";
options.TimeoutSeconds = 30;
});- ChromaDB:
http://localhost:8000/api/v1/heartbeat - Agent.Api:
http://localhost:5000/health - Nginx:
http://localhost:80/health
All operations are logged with structured logging:
- Information level for successful operations
- Error level for failures with exception details
- Connection refused: Ensure ChromaDB is running and accessible
- Authentication errors: Check credentials in
server.htpasswd - CORS errors: Verify CORS configuration in both ChromaDB and nginx
# Check ChromaDB status
curl http://localhost:8000/api/v1/heartbeat
# List collections
curl http://localhost:5000/api/chromadb/collections
# Check container logs
docker logs chromadb
docker logs agent-webapi- Use appropriate batch sizes for document operations
- Implement connection pooling for high-throughput scenarios
- Monitor memory usage with large document collections
- Use metadata filtering to improve query performance
- ChromaDB supports horizontal scaling through clustering
- Consider using persistent volumes for data durability
- Implement caching for frequently accessed collections