1- import { WeaviateDeserializationError } from '../../errors.js' ;
1+ import {
2+ WeaviateDeserializationError ,
3+ WeaviateInvalidInputError ,
4+ WeaviateUnsupportedFeatureError ,
5+ } from '../../errors.js' ;
26import {
37 WeaviateBM25Config ,
48 WeaviateClass ,
@@ -13,11 +17,18 @@ import {
1317 WeaviateVectorIndexConfig ,
1418 WeaviateVectorsConfig ,
1519} from '../../openapi/types.js' ;
20+ import { DbVersionSupport } from '../../utils/dbVersion.js' ;
21+ import { QuantizerGuards , VectorIndexGuards } from '../configure/parsing.js' ;
1622import {
1723 PropertyConfigCreate ,
1824 ReferenceConfigCreate ,
1925 ReferenceMultiTargetConfigCreate ,
2026 ReferenceSingleTargetConfigCreate ,
27+ VectorIndexConfigCreate ,
28+ VectorIndexConfigFlatCreate ,
29+ VectorIndexConfigHNSWCreate ,
30+ VectorizersConfigAdd ,
31+ VectorizersConfigCreate ,
2132} from '../configure/types/index.js' ;
2233import {
2334 BQConfig ,
@@ -47,6 +58,7 @@ import {
4758 VectorIndexConfigHNSW ,
4859 VectorIndexConfigType ,
4960 VectorIndexFilterStrategy ,
61+ VectorIndexType ,
5062 VectorizerConfig ,
5163} from './types/index.js' ;
5264
@@ -124,6 +136,100 @@ export const classToCollection = <T>(cls: WeaviateClass): CollectionConfig => {
124136 } ;
125137} ;
126138
139+ export const parseVectorIndex = ( module : ModuleConfig < VectorIndexType , VectorIndexConfigCreate > ) : any => {
140+ if ( module . config === undefined ) return undefined ;
141+ if ( VectorIndexGuards . isDynamic ( module . config ) ) {
142+ const { hnsw, flat, ...conf } = module . config ;
143+ return {
144+ ...conf ,
145+ hnsw : parseVectorIndex ( { name : 'hnsw' , config : hnsw } ) ,
146+ flat : parseVectorIndex ( { name : 'flat' , config : flat } ) ,
147+ } ;
148+ }
149+
150+ let multiVector ;
151+ if ( VectorIndexGuards . isHNSW ( module . config ) && module . config . multiVector !== undefined ) {
152+ multiVector = {
153+ ...module . config . multiVector ,
154+ enabled : true ,
155+ } ;
156+ }
157+
158+ const { quantizer, ...conf } = module . config as
159+ | VectorIndexConfigFlatCreate
160+ | VectorIndexConfigHNSWCreate
161+ | Record < string , any > ;
162+ if ( quantizer === undefined ) return conf ;
163+ if ( QuantizerGuards . isBQCreate ( quantizer ) ) {
164+ const { type, ...quant } = quantizer ;
165+ return {
166+ ...conf ,
167+ bq : {
168+ ...quant ,
169+ enabled : true ,
170+ } ,
171+ } ;
172+ }
173+ if ( QuantizerGuards . isPQCreate ( quantizer ) ) {
174+ const { type, ...quant } = quantizer ;
175+ return {
176+ ...conf ,
177+ pq : {
178+ ...quant ,
179+ enabled : true ,
180+ } ,
181+ } ;
182+ }
183+ } ;
184+
185+ export const parseVectorizerConfig = ( config ?: VectorizerConfig ) : any => {
186+ if ( config === undefined ) return { } ;
187+ const { vectorizeCollectionName, ...rest } = config as any ;
188+ return {
189+ ...rest ,
190+ vectorizeClassName : vectorizeCollectionName ,
191+ } ;
192+ } ;
193+
194+ export const makeVectorsConfig = (
195+ configVectorizers : VectorizersConfigCreate < any , any > | VectorizersConfigAdd < any > ,
196+ supportsDynamicVectorIndex : Awaited < ReturnType < DbVersionSupport [ 'supportsDynamicVectorIndex' ] > >
197+ ) => {
198+ let vectorizers : string [ ] = [ ] ;
199+ const vectorsConfig : Record < string , any > = { } ;
200+ const vectorizersConfig = Array . isArray ( configVectorizers )
201+ ? configVectorizers
202+ : [
203+ {
204+ ...configVectorizers ,
205+ name : configVectorizers . name || 'default' ,
206+ } ,
207+ ] ;
208+ vectorizersConfig . forEach ( ( v ) => {
209+ if ( v . vectorIndex . name === 'dynamic' && ! supportsDynamicVectorIndex . supports ) {
210+ throw new WeaviateUnsupportedFeatureError ( supportsDynamicVectorIndex . message ) ;
211+ }
212+ const vectorConfig : any = {
213+ vectorIndexConfig : parseVectorIndex ( v . vectorIndex ) ,
214+ vectorIndexType : v . vectorIndex . name ,
215+ vectorizer : { } ,
216+ } ;
217+ const vectorizer = v . vectorizer . name === 'text2vec-azure-openai' ? 'text2vec-openai' : v . vectorizer . name ;
218+ vectorizers = [ ...vectorizers , vectorizer ] ;
219+ vectorConfig . vectorizer [ vectorizer ] = {
220+ properties : v . properties ,
221+ ...parseVectorizerConfig ( v . vectorizer . config ) ,
222+ } ;
223+ if ( v . name === undefined ) {
224+ throw new WeaviateInvalidInputError (
225+ 'vectorName is required for each vectorizer when specifying more than one vectorizer'
226+ ) ;
227+ }
228+ vectorsConfig [ v . name ] = vectorConfig ;
229+ } ) ;
230+ return { vectorsConfig, vectorizers } ;
231+ } ;
232+
127233function populated < T > ( v : T | null | undefined ) : v is T {
128234 return v !== undefined && v !== null ;
129235}
0 commit comments