Skip to content

Commit dfbb6c2

Browse files
authored
[BUG] TS vector config source key should accept Key (#5840)
## Description of changes _Summarize the changes made by this PR._ - Improvements & Bug fixes - #5830 - New functionality - N/A ## Test plan _How are these changes tested?_ - [ ] Tests pass locally with `pytest` for python, `yarn test` for js, `cargo test` for rust ## Migration plan _Are there any migrations, or any forwards/backwards compatibility changes needed in order to make sure this change deploys reliably?_ ## Observability plan _What is the plan to instrument and monitor this change?_ ## Documentation Changes _Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the [docs section](https://github.com/chroma-core/chroma/tree/main/docs/docs.trychroma.com)?_
1 parent 85622b1 commit dfbb6c2

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

clients/new-js/packages/chromadb/src/schema.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getEmbeddingFunction,
1414
getSparseEmbeddingFunction,
1515
} from "./embedding-function";
16+
import { Key } from "./execution";
1617

1718
export const DOCUMENT_KEY = "#document";
1819
export const EMBEDDING_KEY = "#embedding";
@@ -55,7 +56,7 @@ export class BoolInvertedIndexConfig {
5556
export interface VectorIndexConfigOptions {
5657
space?: Space | null;
5758
embeddingFunction?: EmbeddingFunction | null;
58-
sourceKey?: string | null;
59+
sourceKey?: string | Key | null;
5960
hnsw?: ApiHnswIndexConfig | null;
6061
spann?: ApiSpannIndexConfig | null;
6162
}
@@ -71,15 +72,15 @@ export class VectorIndexConfig {
7172
constructor(options: VectorIndexConfigOptions = {}) {
7273
this.space = options.space ?? null;
7374
this.embeddingFunction = options.embeddingFunction;
74-
this.sourceKey = options.sourceKey ?? null;
75+
this.sourceKey = options.sourceKey instanceof Key ? options.sourceKey.name : options.sourceKey ?? null;
7576
this.hnsw = options.hnsw ?? null;
7677
this.spann = options.spann ?? null;
7778
}
7879
}
7980

8081
export interface SparseVectorIndexConfigOptions {
8182
embeddingFunction?: SparseEmbeddingFunction | null;
82-
sourceKey?: string | null;
83+
sourceKey?: string | Key | null;
8384
bm25?: boolean | null;
8485
}
8586

@@ -91,7 +92,7 @@ export class SparseVectorIndexConfig {
9192

9293
constructor(options: SparseVectorIndexConfigOptions = {}) {
9394
this.embeddingFunction = options.embeddingFunction;
94-
this.sourceKey = options.sourceKey ?? null;
95+
this.sourceKey = options.sourceKey instanceof Key ? options.sourceKey.name : options.sourceKey ?? null;
9596
this.bm25 = options.bm25 ?? null;
9697
}
9798
}

clients/new-js/packages/chromadb/test/schema.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,4 +1709,60 @@ describe("Schema", () => {
17091709
expect(capturedRecords.metadatas[2]).not.toHaveProperty("field_sparse");
17101710
expect(capturedRecords.metadatas[3]).toBeNull();
17111711
});
1712+
1713+
it("accepts Key instance for VectorIndexConfig sourceKey", () => {
1714+
const { K } = require("../src/execution");
1715+
const schema = new Schema();
1716+
const vectorConfig = new VectorIndexConfig({
1717+
sourceKey: K.DOCUMENT,
1718+
});
1719+
1720+
expect(vectorConfig.sourceKey).toBe("#document");
1721+
1722+
// Also test with custom key
1723+
const customKey = K("myfield");
1724+
const vectorConfig2 = new VectorIndexConfig({
1725+
sourceKey: customKey,
1726+
});
1727+
1728+
expect(vectorConfig2.sourceKey).toBe("myfield");
1729+
});
1730+
1731+
it("accepts Key instance for SparseVectorIndexConfig sourceKey", () => {
1732+
const { K } = require("../src/execution");
1733+
const schema = new Schema();
1734+
const sparseConfig = new SparseVectorIndexConfig({
1735+
sourceKey: K.DOCUMENT,
1736+
});
1737+
1738+
expect(sparseConfig.sourceKey).toBe("#document");
1739+
1740+
// Also test with custom key
1741+
const customKey = K("myfield");
1742+
const sparseConfig2 = new SparseVectorIndexConfig({
1743+
sourceKey: customKey,
1744+
});
1745+
1746+
expect(sparseConfig2.sourceKey).toBe("myfield");
1747+
});
1748+
1749+
it("serializes Key sourceKey correctly", async () => {
1750+
const { K } = require("../src/execution");
1751+
const schema = new Schema();
1752+
const sparseEf = new MockSparseEmbedding("key_test");
1753+
1754+
schema.createIndex(
1755+
new SparseVectorIndexConfig({
1756+
embeddingFunction: sparseEf,
1757+
sourceKey: K.DOCUMENT,
1758+
}),
1759+
"sparse_field"
1760+
);
1761+
1762+
const json = schema.serializeToJSON();
1763+
expect(json.keys["sparse_field"]?.["sparse_vector"]?.["sparse_vector_index"]?.config?.source_key).toBe("#document");
1764+
1765+
const deserialized = await Schema.deserializeFromJSON(json);
1766+
expect(deserialized?.keys["sparse_field"].sparseVector?.sparseVectorIndex?.config.sourceKey).toBe("#document");
1767+
});
17121768
});

0 commit comments

Comments
 (0)