1- import { WeaviateDeserializationError } from '../../errors.js' ;
21import {
2+ WeaviateDeserializationError ,
3+ WeaviateInvalidInputError ,
4+ WeaviateUnsupportedFeatureError ,
5+ } from '../../errors.js' ;
6+ import {
7+ Properties ,
38 WeaviateBM25Config ,
49 WeaviateClass ,
510 WeaviateInvertedIndexConfig ,
@@ -13,11 +18,19 @@ import {
1318 WeaviateVectorIndexConfig ,
1419 WeaviateVectorsConfig ,
1520} from '../../openapi/types.js' ;
21+ import { DbVersionSupport } from '../../utils/dbVersion.js' ;
22+ import { QuantizerGuards } from '../configure/parsing.js' ;
1623import {
1724 PropertyConfigCreate ,
1825 ReferenceConfigCreate ,
1926 ReferenceMultiTargetConfigCreate ,
2027 ReferenceSingleTargetConfigCreate ,
28+ VectorIndexConfigCreate ,
29+ VectorIndexConfigDynamicCreate ,
30+ VectorIndexConfigFlatCreate ,
31+ VectorIndexConfigHNSWCreate ,
32+ VectorizersConfigAdd ,
33+ VectorizersConfigCreate ,
2134} from '../configure/types/index.js' ;
2235import {
2336 BQConfig ,
@@ -46,6 +59,7 @@ import {
4659 VectorIndexConfigHNSW ,
4760 VectorIndexConfigType ,
4861 VectorIndexFilterStrategy ,
62+ VectorIndexType ,
4963 VectorizerConfig ,
5064} from './types/index.js' ;
5165
@@ -123,6 +137,91 @@ export const classToCollection = <T>(cls: WeaviateClass): CollectionConfig => {
123137 } ;
124138} ;
125139
140+ export const parseVectorIndex = ( module : ModuleConfig < VectorIndexType , VectorIndexConfigCreate > ) : any => {
141+ if ( module . config === undefined ) return undefined ;
142+ if ( module . name === 'dynamic' ) {
143+ const { hnsw, flat, ...conf } = module . config as VectorIndexConfigDynamicCreate ;
144+ return {
145+ ...conf ,
146+ hnsw : parseVectorIndex ( { name : 'hnsw' , config : hnsw } ) ,
147+ flat : parseVectorIndex ( { name : 'flat' , config : flat } ) ,
148+ } ;
149+ }
150+ const { quantizer, ...conf } = module . config as
151+ | VectorIndexConfigFlatCreate
152+ | VectorIndexConfigHNSWCreate
153+ | Record < string , any > ;
154+ if ( quantizer === undefined ) return conf ;
155+ if ( QuantizerGuards . isBQCreate ( quantizer ) ) {
156+ const { type, ...quant } = quantizer ;
157+ return {
158+ ...conf ,
159+ bq : {
160+ ...quant ,
161+ enabled : true ,
162+ } ,
163+ } ;
164+ }
165+ if ( QuantizerGuards . isPQCreate ( quantizer ) ) {
166+ const { type, ...quant } = quantizer ;
167+ return {
168+ ...conf ,
169+ pq : {
170+ ...quant ,
171+ enabled : true ,
172+ } ,
173+ } ;
174+ }
175+ } ;
176+
177+ export const parseVectorizerConfig = ( config ?: VectorizerConfig ) : any => {
178+ if ( config === undefined ) return { } ;
179+ const { vectorizeCollectionName, ...rest } = config as any ;
180+ return {
181+ ...rest ,
182+ vectorizeClassName : vectorizeCollectionName ,
183+ } ;
184+ } ;
185+
186+ export const makeVectorsConfig = < TProperties extends Properties | undefined = undefined > (
187+ configVectorizers : VectorizersConfigCreate < TProperties > | VectorizersConfigAdd < TProperties > ,
188+ supportsDynamicVectorIndex : Awaited < ReturnType < DbVersionSupport [ 'supportsDynamicVectorIndex' ] > >
189+ ) => {
190+ let vectorizers : string [ ] = [ ] ;
191+ const vectorsConfig : Record < string , any > = { } ;
192+ const vectorizersConfig = Array . isArray ( configVectorizers )
193+ ? configVectorizers
194+ : [
195+ {
196+ ...configVectorizers ,
197+ name : configVectorizers . name || 'default' ,
198+ } ,
199+ ] ;
200+ vectorizersConfig . forEach ( ( v ) => {
201+ if ( v . vectorIndex . name === 'dynamic' && ! supportsDynamicVectorIndex . supports ) {
202+ throw new WeaviateUnsupportedFeatureError ( supportsDynamicVectorIndex . message ) ;
203+ }
204+ const vectorConfig : any = {
205+ vectorIndexConfig : parseVectorIndex ( v . vectorIndex ) ,
206+ vectorIndexType : v . vectorIndex . name ,
207+ vectorizer : { } ,
208+ } ;
209+ const vectorizer = v . vectorizer . name === 'text2vec-azure-openai' ? 'text2vec-openai' : v . vectorizer . name ;
210+ vectorizers = [ ...vectorizers , vectorizer ] ;
211+ vectorConfig . vectorizer [ vectorizer ] = {
212+ properties : v . properties ,
213+ ...parseVectorizerConfig ( v . vectorizer . config ) ,
214+ } ;
215+ if ( v . name === undefined ) {
216+ throw new WeaviateInvalidInputError (
217+ 'vectorName is required for each vectorizer when specifying more than one vectorizer'
218+ ) ;
219+ }
220+ vectorsConfig [ v . name ] = vectorConfig ;
221+ } ) ;
222+ return { vectorsConfig, vectorizers } ;
223+ } ;
224+
126225function populated < T > ( v : T | null | undefined ) : v is T {
127226 return v !== undefined && v !== null ;
128227}
0 commit comments