Skip to content

Commit a1f60d2

Browse files
authored
[fix] Replace nativeEnum in ChromaDB plugin; Improve metadata fetching (#1255)
1 parent 8b37692 commit a1f60d2

File tree

3 files changed

+87
-15
lines changed

3 files changed

+87
-15
lines changed

js/plugins/chroma/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"license": "Apache-2.0",
3232
"dependencies": {
3333
"ts-md5": "^1.3.1",
34-
"chromadb": "^1.7.3"
34+
"chromadb": "1.8.1"
3535
},
3636
"peerDependencies": {
3737
"genkit": "workspace:*"

js/plugins/chroma/src/index.ts

+51-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ChromaClient,
1919
Collection,
2020
CollectionMetadata,
21+
Embeddings,
2122
IEmbeddingFunction,
2223
IncludeEnum,
2324
Metadata,
@@ -42,8 +43,13 @@ export { IncludeEnum };
4243
const WhereSchema: z.ZodType<Where> = z.any();
4344
const WhereDocumentSchema: z.ZodType<WhereDocument> = z.any();
4445

46+
const IncludeOptionSchema = z
47+
.array(z.enum(['documents', 'embeddings', 'metadatas', 'distances']))
48+
.optional();
49+
type IncludeOption = z.infer<typeof IncludeOptionSchema>;
50+
4551
const ChromaRetrieverOptionsSchema = CommonRetrieverOptionsSchema.extend({
46-
include: z.array(z.nativeEnum(IncludeEnum)).optional(),
52+
include: IncludeOptionSchema,
4753
where: WhereSchema.optional(),
4854
whereDocument: WhereDocumentSchema.optional(),
4955
});
@@ -142,21 +148,23 @@ export function chromaRetriever<EmbedderCustomOptions extends z.ZodTypeAny>(
142148
});
143149
const results = await collection.query({
144150
nResults: options?.k,
145-
include: options?.include,
151+
include: getIncludes(options?.include),
146152
where: options?.where,
147153
whereDocument: options?.whereDocument,
148154
queryEmbeddings: embedding,
149155
});
150156

151157
const documents = results.documents[0];
152-
const metadatas = results.metadatas[0];
158+
const metadatas = results.metadatas;
159+
const embeddings = results.embeddings;
160+
const distances = results.distances;
153161

154162
const combined = documents
155163
.map((d, i) => {
156164
if (d !== null) {
157165
return {
158166
document: d,
159-
metadata: metadatas[i] ?? undefined,
167+
metadata: constructMetadata(i, metadatas, embeddings, distances),
160168
};
161169
}
162170
return undefined;
@@ -174,6 +182,45 @@ export function chromaRetriever<EmbedderCustomOptions extends z.ZodTypeAny>(
174182
);
175183
}
176184

185+
/**
186+
* Helper method to compute effective Include enum. It always
187+
* includes documents
188+
*/
189+
function getIncludes(includes: IncludeOption): IncludeEnum[] | undefined {
190+
if (!includes) {
191+
// Default behaviour
192+
return undefined;
193+
}
194+
195+
// Always include documents
196+
let effectiveIncludes = [IncludeEnum.Documents];
197+
effectiveIncludes = effectiveIncludes.concat(includes as IncludeEnum[]);
198+
const includesSet = new Set(effectiveIncludes);
199+
return Array.from(includesSet);
200+
}
201+
202+
/**
203+
* Helper method to construct metadata, including the optional {@link IncludeEnum} passed in config.
204+
*/
205+
function constructMetadata(
206+
i: number,
207+
metadatas: (Metadata | null)[][],
208+
embeddings: Embeddings[] | null,
209+
distances: number[][] | null
210+
): any {
211+
var fullMetadata: Record<string, any> = {};
212+
if (metadatas && metadatas[i]) {
213+
fullMetadata.metadata = metadatas[i];
214+
}
215+
if (embeddings) {
216+
fullMetadata.embedding = embeddings[i];
217+
}
218+
if (distances) {
219+
fullMetadata.distances = distances[i];
220+
}
221+
return fullMetadata;
222+
}
223+
177224
/**
178225
* Configures a Chroma indexer.
179226
*/

js/pnpm-lock.yaml

+35-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)