Skip to content

ChromaDB

Install

1
npm install @testcontainers/chromadb --save-dev

Examples

These examples use the following libraries:

Choose an image from the container registry and substitute IMAGE.

Execute a query

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
await using container = await new ChromaDBContainer(IMAGE).start();

const client = new ChromaClient({ ssl: false, host: container.getHost(), port: container.getMappedPort(8000) });
const collection = await client.createCollection({ name: "test", metadata: { "hnsw:space": "cosine" } });
expect(collection.name).toBe("test");

await collection.add({ ids: ["1"], embeddings: [[1, 2, 3]], documents: ["my doc"], metadatas: [{ key: "value" }] });
const getResults = await collection.get({ ids: ["1"] });
expect(getResults.ids[0]).toBe("1");
expect(getResults.documents[0]).toStrictEqual("my doc");
expect(getResults.metadatas).toBeDefined();
expect(getResults.metadatas?.[0]?.key).toStrictEqual("value");

Embedding function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
await using container = await new ChromaDBContainer(IMAGE).start();

await using ollama = await new GenericContainer(OLLAMA_IMAGE).withExposedPorts(11434).start();
await ollama.exec(["ollama", "pull", "nomic-embed-text"]);
const client = new ChromaClient({ ssl: false, host: container.getHost(), port: container.getMappedPort(8000) });
const embedder = new OllamaEmbeddingFunction({
  url: `http://${ollama.getHost()}:${ollama.getMappedPort(11434)}`,
  model: "nomic-embed-text",
});

const collection = await client.createCollection({
  name: "test",
  metadata: { "hnsw:space": "cosine" },
  embeddingFunction: embedder,
});
expect(collection.name).toBe("test");

await collection.add({
  ids: ["1", "2"],
  documents: [
    "This is a document about dogs. Dogs are awesome.",
    "This is a document about cats. Cats are awesome.",
  ],
});
const results = await collection.query({ queryTexts: ["Tell me about dogs"], nResults: 1 });
expect(results).toBeDefined();
expect(results.ids[0]).toEqual(["1"]);
expect(results.ids[0][0]).toBe("1");

Persistent directory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const sourcePath = fs.mkdtempSync(path.join(os.tmpdir(), "chroma-temp"));
await using container = await new ChromaDBContainer(IMAGE)
  .withBindMounts([{ source: sourcePath, target: "/data" }])
  .start();

const client = new ChromaClient({ ssl: false, host: container.getHost(), port: container.getMappedPort(8000) });
const collection = await client.createCollection({ name: "test", metadata: { "hnsw:space": "cosine" } });
expect(collection.name).toBe("test");

await collection.add({ ids: ["1"], embeddings: [[1, 2, 3]], documents: ["my doc"] });
const getResults = await collection.get({ ids: ["1"] });
expect(getResults.ids[0]).toBe("1");
expect(getResults.documents[0]).toStrictEqual("my doc");
expect(fs.existsSync(`${sourcePath}/chroma.sqlite3`)).toBe(true);

Authentication

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const tenant = "test-tenant";
const key = "test-key";
const database = "test-db";

await using container = await new ChromaDBContainer(IMAGE)
  .withEnvironment({
    CHROMA_SERVER_AUTHN_CREDENTIALS: key,
    CHROMA_SERVER_AUTHN_PROVIDER: "chromadb.auth.token_authn.TokenAuthenticationServerProvider",
    CHROMA_AUTH_TOKEN_TRANSPORT_HEADER: "X-Chroma-Token",
  })
  .start();

const adminClient = new AdminClient({
  ssl: false,
  host: container.getHost(),
  port: container.getMappedPort(8000),
  headers: {
    "X-Chroma-Token": key,
  },
});

await adminClient.createTenant({ name: tenant });
await adminClient.createDatabase({ name: database, tenant: tenant });

const dbClient = new ChromaClient({
  tenant,
  ssl: false,
  host: container.getHost(),
  port: container.getMappedPort(8000),
  headers: {
    "X-Chroma-Token": key,
  },
  database,
});

const collection = await dbClient.createCollection({ name: "test-collection" });
expect(collection.name).toBe("test-collection");