@@ -18,6 +18,7 @@ import {
18
18
ChromaClient ,
19
19
Collection ,
20
20
CollectionMetadata ,
21
+ Embeddings ,
21
22
IEmbeddingFunction ,
22
23
IncludeEnum ,
23
24
Metadata ,
@@ -42,8 +43,13 @@ export { IncludeEnum };
42
43
const WhereSchema : z . ZodType < Where > = z . any ( ) ;
43
44
const WhereDocumentSchema : z . ZodType < WhereDocument > = z . any ( ) ;
44
45
46
+ const IncludeOptionSchema = z
47
+ . array ( z . enum ( [ 'documents' , 'embeddings' , 'metadatas' , 'distances' ] ) )
48
+ . optional ( ) ;
49
+ type IncludeOption = z . infer < typeof IncludeOptionSchema > ;
50
+
45
51
const ChromaRetrieverOptionsSchema = CommonRetrieverOptionsSchema . extend ( {
46
- include : z . array ( z . nativeEnum ( IncludeEnum ) ) . optional ( ) ,
52
+ include : IncludeOptionSchema ,
47
53
where : WhereSchema . optional ( ) ,
48
54
whereDocument : WhereDocumentSchema . optional ( ) ,
49
55
} ) ;
@@ -142,21 +148,23 @@ export function chromaRetriever<EmbedderCustomOptions extends z.ZodTypeAny>(
142
148
} ) ;
143
149
const results = await collection . query ( {
144
150
nResults : options ?. k ,
145
- include : options ?. include ,
151
+ include : getIncludes ( options ?. include ) ,
146
152
where : options ?. where ,
147
153
whereDocument : options ?. whereDocument ,
148
154
queryEmbeddings : embedding ,
149
155
} ) ;
150
156
151
157
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 ;
153
161
154
162
const combined = documents
155
163
. map ( ( d , i ) => {
156
164
if ( d !== null ) {
157
165
return {
158
166
document : d ,
159
- metadata : metadatas [ i ] ?? undefined ,
167
+ metadata : constructMetadata ( i , metadatas , embeddings , distances ) ,
160
168
} ;
161
169
}
162
170
return undefined ;
@@ -174,6 +182,45 @@ export function chromaRetriever<EmbedderCustomOptions extends z.ZodTypeAny>(
174
182
) ;
175
183
}
176
184
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
+
177
224
/**
178
225
* Configures a Chroma indexer.
179
226
*/
0 commit comments