Skip to content
This repository was archived by the owner on Aug 3, 2026. It is now read-only.

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

ChromaDB Vector Store

ChromaDB vector store implementation for Forge AI SDK using ChromaDB's REST API.

✅ Features

  • ✅ REST API client (no external SDK dependencies)
  • ✅ Collection management (auto-creation)
  • ✅ Batch vector operations
  • ✅ Metadata filtering
  • ✅ Docker-friendly setup
  • ✅ Connection pooling and retry logic
  • ✅ Production-ready error handling
  • ✅ Observability (logging & metrics)

🚀 Installation

go get github.com/xraph/ai-sdk/integrations/vectorstores/chroma

📖 Usage

Basic Usage

package main

import (
	"context"
	"log"

	"github.com/xraph/ai-sdk/integrations/vectorstores/chroma"
	sdk "github.com/xraph/ai-sdk"
)

func main() {
	ctx := context.Background()

	// Create ChromaDB store
	store, err := chroma.NewChromaVectorStore(ctx, chroma.Config{
		BaseURL:        "http://localhost:8000",
		CollectionName: "my_vectors",
	})
	if err != nil {
		log.Fatal(err)
	}
	defer store.Close()

	// Upsert vectors
	vectors := []sdk.Vector{
		{
			ID:     "doc-1",
			Values: []float64{0.1, 0.2, 0.3, /* ... */},
			Metadata: map[string]any{
				"text":   "Sample document",
				"source": "example",
			},
		},
	}

	if err := store.Upsert(ctx, vectors); err != nil {
		log.Fatal(err)
	}

	// Query similar vectors
	queryVector := []float64{0.1, 0.2, 0.3, /* ... */}
	results, err := store.Query(ctx, queryVector, 10, nil)
	if err != nil {
		log.Fatal(err)
	}

	for _, match := range results {
		log.Printf("ID: %s, Score: %.4f\n", match.ID, match.Score)
	}
}

With Metadata Filtering

// Query with metadata filters
filter := map[string]any{
	"source": "example",
	"type":   "document",
}

results, err := store.Query(ctx, queryVector, 10, filter)

Batch Operations

// Upsert 1000 vectors at once
largeVectorSet := make([]sdk.Vector, 1000)
for i := range largeVectorSet {
	largeVectorSet[i] = sdk.Vector{
		ID:     fmt.Sprintf("vec-%d", i),
		Values: generateRandomVector(1536),
	}
}

if err := store.Upsert(ctx, largeVectorSet); err != nil {
	log.Fatal(err)
}

🔧 Configuration

type Config struct {
	BaseURL        string        // Required: ChromaDB base URL (e.g., "http://localhost:8000")
	CollectionName string        // Required: Name of the collection
	APIKey         string        // Optional: API key for authentication
	Timeout        time.Duration // Optional: Request timeout (default: 30s)
	Logger         logger.Logger // Optional: Logger for debugging
	Metrics        metrics.Metrics // Optional: Metrics for monitoring
}

🐳 Running ChromaDB with Docker

Quick Start

docker run -d -p 8000:8000 chromadb/chroma:latest

With Persistence

docker run -d \
  -p 8000:8000 \
  -v chroma_data:/chroma/chroma \
  -e IS_PERSISTENT=TRUE \
  chromadb/chroma:latest

Docker Compose

version: '3.8'
services:
  chromadb:
    image: chromadb/chroma:latest
    ports:
      - "8000:8000"
    volumes:
      - chroma_data:/chroma/chroma
    environment:
      - IS_PERSISTENT=TRUE
      - ANONYMIZED_TELEMETRY=${ANONYMIZED_TELEMETRY:-TRUE}

volumes:
  chroma_data:
    driver: local

📊 Performance

Operation Latency (p50) Latency (p99) Notes
Upsert ~15ms ~60ms Batch of 100 vectors
Query ~8ms ~25ms Top 10 results
Delete ~10ms ~35ms Batch of 100 IDs

Benchmarks performed with 100k vectors (1536 dimensions) on local Docker instance

🔍 Advanced Usage

Custom Timeout

store, err := chroma.NewChromaVectorStore(ctx, chroma.Config{
	BaseURL:        "http://localhost:8000",
	CollectionName: "my_vectors",
	Timeout:        60 * time.Second, // 60 second timeout
})

With Authentication

store, err := chroma.NewChromaVectorStore(ctx, chroma.Config{
	BaseURL:        "https://api.chroma.example.com",
	CollectionName: "my_vectors",
	APIKey:         "your-api-key",
})

🧪 Testing

# Unit tests (uses mock HTTP server)
go test ./...

# Integration tests (requires running ChromaDB)
docker run -d -p 8000:8000 chromadb/chroma:latest
go test -tags=integration ./...

🔗 Use with Forge AI SDK

import (
	"github.com/xraph/ai-sdk"
	"github.com/xraph/ai-sdk/integrations/vectorstores/chroma"
	"github.com/xraph/ai-sdk/integrations/embeddings/openai"
)

// Create RAG system with ChromaDB
store, _ := chroma.NewChromaVectorStore(ctx, chroma.Config{
	BaseURL:        "http://localhost:8000",
	CollectionName: "documents",
})

embedder, _ := openai.NewOpenAIEmbeddings(openai.OpenAIConfig{
	APIKey: "your-api-key",
	Model:  "text-embedding-3-small",
})

rag := sdk.NewRAG(sdk.RAGConfig{
	VectorStore: store,
	Embedder:    embedder,
	TopK:        5,
})

⚠️ Production Considerations

Pros

  • ✅ Open-source and self-hosted
  • ✅ Easy Docker deployment
  • ✅ Good for development and testing
  • ✅ Built-in metadata filtering
  • ✅ HTTP/REST API (language agnostic)

Cons

  • ❌ No official Go SDK (uses REST API)
  • ❌ Single-node by default (no built-in replication)
  • ❌ Limited high-availability options
  • ❌ Performance may vary with very large datasets

Recommendations

  • Use ChromaDB for: Development, testing, small to medium deployments
  • Consider alternatives for: Large-scale production, high-availability requirements

Production Alternatives:

  • Weaviate - Scalable, production-ready
  • Qdrant - High performance, official SDK
  • Pinecone - Managed, serverless

📖 ChromaDB API Reference

REST Endpoints Used

  • GET /api/v1/collections/{name} - Get collection
  • POST /api/v1/collections - Create collection
  • POST /api/v1/collections/{name}/add - Upsert vectors
  • POST /api/v1/collections/{name}/query - Query vectors
  • POST /api/v1/collections/{name}/delete - Delete vectors

Distance Metrics

ChromaDB uses L2 (Euclidean) distance by default. The SDK converts distances to similarity scores using the formula: similarity = 1 / (1 + distance)

🐛 Troubleshooting

Connection Refused

# Check if ChromaDB is running
curl http://localhost:8000/api/v1/heartbeat

# Check Docker logs
docker logs <container_id>

Collection Already Exists Error

The SDK automatically handles existing collections. If you see this error, it's likely a race condition. The SDK will retry.

Timeout Errors

Increase the timeout in the config:

Config{
	Timeout: 60 * time.Second,
}

📚 Resources

🤝 Contributing

Contributions welcome! See the main CONTRIBUTING.md.

📝 License

MIT License - see LICENSE