Running Chroma as a Standalone Python Service for Spring Boot Integration
1. Install Chroma in a Python Environment
# Create and activate virtual environment
python -m venv chroma-env
source chroma-env/bin/activate # On Windows: chroma-env\Scripts\activate
# Install Chroma with server support
pip install chromadb uvicorn
2. Create Chroma Server Script
Create chroma_server.py:
from chromadb import Client
from chromadb.config import Settings
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
# Configure server settings
settings = Settings(
chroma_server_host="0.0.0.0", # Listen on all interfaces
chroma_server_http_port=8000, # Port for HTTP access
persist_directory="./chroma_data", # Local storage path
allow_origins=["*"] # Allow all origins for development
)
# Initialize FastAPI app
app = FastAPI(title="Chroma Vector Database")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Initialize Chroma client
client = Client(settings)
@app.get("/")
def read_root():
return {"status": "Chroma server running"}
@app.get("/api/v1/health")
def health_check():
return {"status": "healthy"}
if __name__ == "__main__":
# Start the server
uvicorn.run(
"chroma_server:app",
host="0.0.0.0",
port=8000,
reload=False
)
3. Run the Server
# Create data directory
mkdir -p chroma_data
# Start the server
python chroma_server.py
4. Create Systemd Service (Production)
Create /etc/systemd/system/chroma.service:
[Unit]
Description=Chroma Vector Database Service
After=network.target
[Service]
User=your_user
Group=your_group
WorkingDirectory=/path/to/chroma
ExecStart=/path/to/chroma-env/bin/python /path/to/chroma_server.py
Restart=always
Environment="PYTHONPATH=/path/to/chroma"
StandardOutput=file:/var/log/chroma.log
StandardError=file:/var/log/chroma_error.log
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable chroma
sudo systemctl start chroma
sudo systemctl status chroma
5. Spring Boot Integration
Add dependencies in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
Create configuration class:
@Configuration
public class ChromaClientConfig {
@Bean
public WebClient chromaWebClient() {
return WebClient.builder()
.baseUrl("http://localhost:8000")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build();
}
}
Example service class:
@Service
public class ChromaService {
private final WebClient webClient;
public ChromaService(WebClient chromaWebClient) {
this.webClient = chromaWebClient;
}
// Example: Create collection
public Mono<String> createCollection(String collectionName) {
return webClient.post()
.uri("/api/v1/collections")
.bodyValue(Map.of("name", collectionName))
.retrieve()
.bodyToMono(String.class);
}
// Example: Add embeddings
public Mono<String> addEmbeddings(String collectionName, List<List<Float>> embeddings) {
return webClient.post()
.uri("/api/v1/collections/{name}/add", collectionName)
.bodyValue(Map.of("embeddings", embeddings))
.retrieve()
.bodyToMono(String.class);
}
// Example: Query embeddings
public Mono<String> queryEmbeddings(String collectionName, List<List<Float>> queryVectors) {
return webClient.post()
.uri("/api/v1/collections/{name}/query", collectionName)
.bodyValue(Map.of("query_embeddings", queryVectors))
.retrieve()
.bodyToMono(String.class);
}
}
6. Test the Integration
From Spring Boot, you can test with:
@RestController
@RequestMapping("/api")
public class TestController {
private final ChromaService chromaService;
public TestController(ChromaService chromaService) {
this.chromaService = chromaService;
}
@GetMapping("/test")
public Mono<String> testChroma() {
String collectionName = "test_collection";
// Create collection
return chromaService.createCollection(collectionName)
.then(chromaService.addEmbeddings(collectionName, Arrays.asList(
Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f),
Arrays.asList(0.5f, 0.6f, 0.7f, 0.8f)
)))
.then(chromaService.queryEmbeddings(collectionName, Arrays.asList(
Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f)
)))
.map(result -> "Chroma test successful: " + result);
}
}
Key Notes:
- Chroma uses HTTP API by default at
http://localhost:8000
- Data is persisted in the
chroma_data directory
- The server can be accessed from any network interface (configured in settings)
- For production, consider adding:
- Authentication middleware
- Rate limiting
- HTTPS/TLS termination
- Persistent storage configuration
This setup allows you to run Chroma as a standalone service while maintaining integration with your Spring Boot application through its REST API.