forked from dotnet/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionModelBuilder.cs
More file actions
625 lines (526 loc) · 29.7 KB
/
Copy pathCollectionModelBuilder.cs
File metadata and controls
625 lines (526 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.AI;
using Microsoft.Shared.Diagnostics;
using Microsoft.Shared.DiagnosticIds;
namespace Microsoft.Extensions.VectorData.ProviderServices;
/// <summary>
/// Represents a builder for a <see cref="CollectionModel"/>.
/// This is an internal support type meant for use by providers only and not by applications.
/// </summary>
/// <remarks>This class is single-use only, and not thread-safe.</remarks>
[Experimental(DiagnosticIds.Experiments.VectorDataCollectionModel, UrlFormat = DiagnosticIds.UrlFormat)]
public abstract class CollectionModelBuilder
{
/// <summary>
/// Gets the options for building the model.
/// </summary>
protected CollectionModelBuildingOptions Options { get; }
#pragma warning disable CA1002 // Do not expose generic lists
/// <summary>
/// Gets the key properties of the record.
/// </summary>
protected List<KeyPropertyModel> KeyProperties { get; } = [];
/// <summary>
/// Gets the data properties of the record.
/// </summary>
protected List<DataPropertyModel> DataProperties { get; } = [];
/// <summary>
/// Gets the vector properties of the record.
/// </summary>
protected List<VectorPropertyModel> VectorProperties { get; } = [];
#pragma warning restore CA1002 // Do not expose generic lists
/// <summary>
/// Gets all properties of the record, of all types.
/// </summary>
protected IEnumerable<PropertyModel> Properties => PropertyMap.Values;
/// <summary>
/// Gets all properties of the record, of all types, indexed by their model name.
/// </summary>
protected Dictionary<string, PropertyModel> PropertyMap { get; } = [];
/// <summary>
/// Gets the default embedding generator to use for vector properties, when none is specified at the property or collection level.
/// </summary>
protected IEmbeddingGenerator? DefaultEmbeddingGenerator { get; private set; }
/// <summary>
/// Gets the collection's generic key type parameter (<c>TKey</c>), if provided.
/// Used by <see cref="ValidateKeyProperty"/> to validate that <c>TKey</c> corresponds to the key property type on the model.
/// </summary>
protected Type? KeyType { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="CollectionModelBuilder"/> class.
/// </summary>
protected CollectionModelBuilder(CollectionModelBuildingOptions options)
{
Options = options;
}
/// <summary>
/// Builds and returns an <see cref="CollectionModel"/> from the given <paramref name="recordType"/> and <paramref name="definition"/>.
/// </summary>
/// <param name="recordType">The CLR type of the record.</param>
/// <param name="keyType">The collection's generic key type parameter (<c>TKey</c>), used to validate correspondence with the key property type.</param>
/// <param name="definition">An optional record definition that overrides attribute-based configuration.</param>
/// <param name="defaultEmbeddingGenerator">An optional default embedding generator for vector properties.</param>
/// <returns>The built <see cref="CollectionModel"/>.</returns>
[RequiresDynamicCode("This model building variant is not compatible with NativeAOT. See BuildDynamic() for dynamic mapping, and a third variant accepting source-generated delegates will be introduced in the future.")]
[RequiresUnreferencedCode("This model building variant is not compatible with trimming. See BuildDynamic() for dynamic mapping, and a third variant accepting source-generated delegates will be introduced in the future.")]
public virtual CollectionModel Build(Type recordType, Type keyType, VectorStoreCollectionDefinition? definition, IEmbeddingGenerator? defaultEmbeddingGenerator)
{
_ = Throw.IfNull(recordType);
KeyType = keyType;
if (recordType == typeof(Dictionary<string, object?>))
{
Throw.ArgumentException(nameof(recordType), "Dynamic mapping with Dictionary<string, object?> requires calling BuildDynamic().");
}
DefaultEmbeddingGenerator = definition?.EmbeddingGenerator ?? defaultEmbeddingGenerator;
// Build a lookup of definition properties by name for matching with CLR properties.
Dictionary<string, VectorStoreProperty>? definitionByName = null;
if (definition is not null)
{
definitionByName = [];
foreach (var p in definition.Properties)
{
definitionByName[p.Name] = p;
}
}
// Process CLR properties, matching to definition properties where available.
// TODO: This traverses the CLR type's properties, making it incompatible with trimming (and NativeAOT).
// TODO: We could put [DynamicallyAccessedMembers] to preserve all properties, but that approach wouldn't
// TODO: work with hierarchical data models (https://github.com/microsoft/semantic-kernel/issues/10957).
foreach (var clrProperty in recordType.GetProperties())
{
VectorStoreProperty? definitionProperty = null;
_ = definitionByName?.TryGetValue(clrProperty.Name, out definitionProperty);
ProcessProperty(clrProperty, definitionProperty);
}
// Go over the properties, configure POCO accessors and validate type compatibility.
foreach (var property in Properties)
{
var clrProperty = recordType.GetProperty(property.ModelName)
?? throw new InvalidOperationException($"Property '{property.ModelName}' not found on CLR type '{recordType.FullName}'.");
var clrPropertyType = clrProperty.PropertyType;
if ((Nullable.GetUnderlyingType(clrPropertyType) ?? clrPropertyType) != (Nullable.GetUnderlyingType(property.Type) ?? property.Type))
{
throw new InvalidOperationException(
$"Property '{property.ModelName}' has a different CLR type in the record definition ('{property.Type.Name}') and on the .NET property ('{clrProperty.PropertyType}').");
}
property.ConfigurePocoAccessors(clrProperty);
}
Customize();
Validate(recordType, definition);
// Extra validation for non-dynamic mapping scenarios: ensure the type has a parameterless constructor.
if (!Options.UsesExternalSerializer && recordType.GetConstructor(Type.EmptyTypes) is null)
{
throw new NotSupportedException($"Type '{recordType.Name}' must have a parameterless constructor.");
}
return new(recordType, () => Activator.CreateInstance(recordType)!, KeyProperties, DataProperties, VectorProperties, PropertyMap);
}
/// <summary>
/// Builds and returns an <see cref="CollectionModel"/> for dynamic mapping scenarios from the given <paramref name="definition"/>.
/// </summary>
/// <param name="definition">The record definition describing the collection's schema.</param>
/// <param name="defaultEmbeddingGenerator">An optional default embedding generator for vector properties.</param>
/// <returns>The built <see cref="CollectionModel"/>.</returns>
public virtual CollectionModel BuildDynamic(VectorStoreCollectionDefinition definition, IEmbeddingGenerator? defaultEmbeddingGenerator)
{
_ = Throw.IfNull(definition);
DefaultEmbeddingGenerator = defaultEmbeddingGenerator;
foreach (var defProp in definition.Properties)
{
ProcessProperty(clrProperty: null, defProp);
}
Customize();
Validate(type: null, definition);
foreach (var property in Properties)
{
property.ConfigureDynamicAccessors();
}
return new(typeof(Dictionary<string, object?>), static () => new Dictionary<string, object?>(), KeyProperties, DataProperties, VectorProperties, PropertyMap);
}
/// <summary>
/// As part of building the model, this method processes a single property, accepting both a CLR <see cref="PropertyInfo"/>
/// (from which attributes are read) and a <see cref="VectorStoreProperty"/> from the user-provided record definition.
/// Either may be <see langword="null"/>, but not both.
/// When both are provided, the record definition values override attribute-configured values.
/// </summary>
protected virtual void ProcessProperty(PropertyInfo? clrProperty, VectorStoreProperty? definitionProperty)
{
Debug.Assert(clrProperty is not null || definitionProperty is not null, "Either a CLR property or a definition property must be provided.");
VectorStoreKeyAttribute? keyAttribute = null;
VectorStoreDataAttribute? dataAttribute = null;
VectorStoreVectorAttribute? vectorAttribute = null;
if (clrProperty is not null)
{
// Read attributes from CLR property.
keyAttribute = clrProperty.GetCustomAttribute<VectorStoreKeyAttribute>();
dataAttribute = clrProperty.GetCustomAttribute<VectorStoreDataAttribute>();
vectorAttribute = clrProperty.GetCustomAttribute<VectorStoreVectorAttribute>();
// Validate that at most one mapping attribute is present.
if ((keyAttribute is not null ? 1 : 0) + (dataAttribute is not null ? 1 : 0) + (vectorAttribute is not null ? 1 : 0) > 1)
{
throw new InvalidOperationException(
$"Property '{clrProperty.DeclaringType?.Name}.{clrProperty.Name}' has multiple of {nameof(VectorStoreKeyAttribute)}, {nameof(VectorStoreDataAttribute)} or {nameof(VectorStoreVectorAttribute)}. Only one of these attributes can be specified on a property.");
}
// If no mapping attribute and no definition, skip this property.
if (keyAttribute is null && dataAttribute is null && vectorAttribute is null && definitionProperty is null)
{
return;
}
// Validate kind compatibility between attribute and definition.
if (definitionProperty is not null
&& ((keyAttribute is not null && definitionProperty is not VectorStoreKeyProperty)
|| (dataAttribute is not null && definitionProperty is not VectorStoreDataProperty)
|| (vectorAttribute is not null && definitionProperty is not VectorStoreVectorProperty)))
{
string definitionKind = definitionProperty switch
{
VectorStoreKeyProperty => "key",
VectorStoreDataProperty => "data",
VectorStoreVectorProperty => "vector",
_ => throw new ArgumentException($"Unknown type '{definitionProperty.GetType().FullName}' in vector store record definition.")
};
throw new InvalidOperationException(
$"Property '{clrProperty.Name}' is present in the {nameof(VectorStoreCollectionDefinition)} as a {definitionKind} property, but the .NET property on type '{clrProperty.DeclaringType?.Name}' has an incompatible attribute.");
}
}
string propertyName = clrProperty?.Name ?? definitionProperty?.Name ?? throw new UnreachableException();
Type propertyType = clrProperty?.PropertyType
?? definitionProperty?.Type
?? throw new InvalidOperationException(VectorDataStrings.MissingTypeOnPropertyDefinition(definitionProperty!));
PropertyModel property;
string? attributeStorageName = null;
if (keyAttribute is not null || definitionProperty is VectorStoreKeyProperty)
{
var keyProperty = new KeyPropertyModel(propertyName, propertyType);
if (keyAttribute is not null)
{
keyProperty.IsAutoGenerated = keyAttribute.IsAutoGeneratedNullable ?? SupportsKeyAutoGeneration(keyProperty.Type);
attributeStorageName = keyAttribute.StorageName;
}
// Definition values override attribute values.
if (definitionProperty is VectorStoreKeyProperty defKey)
{
keyProperty.IsAutoGenerated = defKey.IsAutoGenerated ?? SupportsKeyAutoGeneration(keyProperty.Type);
}
KeyProperties.Add(keyProperty);
property = keyProperty;
}
else if (dataAttribute is not null || definitionProperty is VectorStoreDataProperty)
{
var dataProperty = new DataPropertyModel(propertyName, propertyType);
if (dataAttribute is not null)
{
dataProperty.IsIndexed = dataAttribute.IsIndexed;
dataProperty.IsFullTextIndexed = dataAttribute.IsFullTextIndexed;
attributeStorageName = dataAttribute.StorageName;
}
// Definition values override attribute values.
if (definitionProperty is VectorStoreDataProperty defData)
{
dataProperty.IsIndexed = defData.IsIndexed;
dataProperty.IsFullTextIndexed = defData.IsFullTextIndexed;
}
DataProperties.Add(dataProperty);
property = dataProperty;
}
else if (vectorAttribute is not null || definitionProperty is VectorStoreVectorProperty)
{
// If a definition exists, create via the definition to preserve generic type info (VectorStoreVectorProperty<TInput>).
var vectorProperty = definitionProperty is VectorStoreVectorProperty defVec
? defVec.CreatePropertyModel()
: new VectorPropertyModel(propertyName, propertyType);
if (vectorAttribute is not null)
{
vectorProperty.Dimensions = vectorAttribute.Dimensions;
vectorProperty.IndexKind = vectorAttribute.IndexKind;
vectorProperty.DistanceFunction = vectorAttribute.DistanceFunction;
attributeStorageName = vectorAttribute.StorageName;
}
// Definition values override attribute values.
if (definitionProperty is VectorStoreVectorProperty defVectorProp)
{
vectorProperty.Dimensions = defVectorProp.Dimensions;
if (defVectorProp.IndexKind is not null)
{
vectorProperty.IndexKind = defVectorProp.IndexKind;
}
if (defVectorProp.DistanceFunction is not null)
{
vectorProperty.DistanceFunction = defVectorProp.DistanceFunction;
}
}
ConfigureVectorPropertyEmbedding(
vectorProperty,
(definitionProperty as VectorStoreVectorProperty)?.EmbeddingGenerator ?? DefaultEmbeddingGenerator,
(definitionProperty as VectorStoreVectorProperty)?.EmbeddingType);
VectorProperties.Add(vectorProperty);
property = vectorProperty;
}
else
{
throw new UnreachableException();
}
// Apply storage name: attribute first, then definition (which takes precedence).
SetPropertyStorageName(property, attributeStorageName);
if (definitionProperty is not null)
{
SetPropertyStorageName(property, definitionProperty.StorageName);
}
if (definitionProperty?.ProviderAnnotations is not null)
{
property.ProviderAnnotations = new Dictionary<string, object?>(definitionProperty.ProviderAnnotations);
}
if (clrProperty is not null)
{
property.PropertyInfo = clrProperty;
}
PropertyMap.Add(propertyName, property);
}
private void SetPropertyStorageName(PropertyModel property, string? storageName)
{
if (property is KeyPropertyModel && Options.ReservedKeyStorageName is not null)
{
// If we have ReservedKeyStorageName, there can only be a single key property (validated in the constructor)
property.StorageName = Options.ReservedKeyStorageName;
return;
}
if (storageName is null)
{
return;
}
// If a custom serializer is used (e.g. JsonSerializer), it would ignore our own attributes/config, and
// our model needs to be in sync with the serializer's behavior (for e.g. storage names in filters).
// So we ignore the config here as well.
// TODO: Consider throwing here instead of ignoring
if (Options.UsesExternalSerializer && property.PropertyInfo is not null)
{
return;
}
property.StorageName = storageName;
}
/// <summary>
/// Gets the embedding types supported by this provider, in priority order.
/// The first type whose embedding generator is compatible with the input type will be used.
/// </summary>
/// <remarks>
/// Override this property in providers that support additional embedding types beyond <see cref="Embedding{T}"/> of <see langword="float"/>.
/// </remarks>
protected virtual IReadOnlyList<EmbeddingGenerationDispatcher> EmbeddingGenerationDispatchers { get; }
= [EmbeddingGenerationDispatcher.Create<Embedding<float>>()];
/// <summary>
/// Attempts to resolve the embedding type for the given vector property, iterating over <see cref="EmbeddingGenerationDispatchers"/> in priority order.
/// </summary>
private (Type? EmbeddingType, EmbeddingGenerationDispatcher? Handler) ResolveEmbeddingType(
VectorPropertyModel vectorProperty,
IEmbeddingGenerator embeddingGenerator,
Type? userRequestedEmbeddingType)
{
foreach (var supported in EmbeddingGenerationDispatchers)
{
if (supported.ResolveEmbeddingType(vectorProperty, embeddingGenerator, userRequestedEmbeddingType) is { } resolved)
{
return (resolved, supported);
}
}
return (null, null);
}
/// <summary>
/// Resolves the embedding handler for a native vector property type, where embedding generation is only needed for search.
/// Since the property type is already a valid native type, we only check if the generator can produce the
/// embedding output type (regardless of input type, which is only known at search time).
/// </summary>
private EmbeddingGenerationDispatcher? ResolveSearchOnlyEmbeddingHandler(VectorPropertyModel vectorProperty, IEmbeddingGenerator embeddingGenerator)
{
foreach (var supported in EmbeddingGenerationDispatchers)
{
if (supported.CanGenerateEmbedding(vectorProperty, embeddingGenerator))
{
return supported;
}
}
return null;
}
/// <summary>
/// Configures embedding generation for a vector property. Sets the embedding generator, resolves the embedding type,
/// and assigns the appropriate <see cref="EmbeddingGenerationDispatcher"/>.
/// </summary>
/// <remarks>
/// If the property's type is natively supported (e.g. <see cref="ReadOnlyMemory{T}"/> of <see langword="float"/>), the embedding type
/// is set to the property's type; if a generator is also configured, a search-only dispatcher is resolved so that search can convert
/// arbitrary inputs (e.g. string) to embeddings.
/// Otherwise, if a generator is configured, the embedding type is resolved from it. If resolution fails, the embedding type remains
/// <see langword="null"/> and an error is deferred to the validation phase.
/// </remarks>
private void ConfigureVectorPropertyEmbedding(
VectorPropertyModel vectorProperty,
IEmbeddingGenerator? embeddingGenerator,
Type? userRequestedEmbeddingType)
{
vectorProperty.EmbeddingGenerator = embeddingGenerator;
if (IsVectorPropertyTypeValid(vectorProperty.Type, out _))
{
if (userRequestedEmbeddingType is not null && userRequestedEmbeddingType != vectorProperty.Type)
{
throw new InvalidOperationException(VectorDataStrings.DifferentEmbeddingTypeSpecifiedForNativelySupportedType(vectorProperty, userRequestedEmbeddingType));
}
vectorProperty.EmbeddingType = vectorProperty.Type;
// Even for native types, if an embedding generator is configured, resolve the dispatcher
// so that search can convert arbitrary inputs (e.g. string) to embeddings.
if (embeddingGenerator is not null)
{
vectorProperty.EmbeddingGenerationDispatcher = ResolveSearchOnlyEmbeddingHandler(vectorProperty, embeddingGenerator);
}
}
else if (embeddingGenerator is not null)
{
// The property type isn't a valid embedding type, but an embedding generator is configured.
// Try to resolve the embedding type from it: if the configured generator supports translating the input type (e.g. string) to
// an output type supported by the provider, we set that as the embedding type.
// If this fails, EmbeddingType remains null and we defer the error to the validation phase.
var (embeddingType, handler) = ResolveEmbeddingType(vectorProperty, embeddingGenerator, userRequestedEmbeddingType);
vectorProperty.EmbeddingType = embeddingType;
vectorProperty.EmbeddingGenerationDispatcher = handler;
}
// If the property type isn't valid and there's no embedding generator, that's an error.
// But we throw later, in validation, to allow for provider customization to correct this invalid state after this step.
}
/// <summary>
/// Extension hook for providers to be able to customize the model.
/// </summary>
protected virtual void Customize()
{
}
/// <summary>
/// Validates the model after all properties have been processed.
/// </summary>
protected virtual void Validate(Type? type, VectorStoreCollectionDefinition? definition)
{
if (KeyProperties.Count > 1)
{
throw new NotSupportedException($"Multiple key properties found on {TypeMessage()}the provided {nameof(VectorStoreCollectionDefinition)} while only one is supported.");
}
if (KeyProperties.Count == 0)
{
throw new NotSupportedException($"No key property found on {TypeMessage()}the provided {nameof(VectorStoreCollectionDefinition)} while at least one is required.");
}
if (Options.RequiresAtLeastOneVector && VectorProperties.Count == 0)
{
throw new NotSupportedException($"No vector property found on {TypeMessage()}the provided {nameof(VectorStoreCollectionDefinition)} while at least one is required.");
}
if (!Options.SupportsMultipleVectors && VectorProperties.Count > 1)
{
throw new NotSupportedException($"Multiple vector properties found on {TypeMessage()}the provided {nameof(VectorStoreCollectionDefinition)} while only one is supported.");
}
var storageNameMap = new Dictionary<string, PropertyModel>();
foreach (var property in PropertyMap.Values)
{
ValidateProperty(property, definition);
if (storageNameMap.TryGetValue(property.StorageName, out var otherproperty))
{
throw new InvalidOperationException($"Property '{property.ModelName}' is being mapped to storage name '{property.StorageName}', but property '{otherproperty.ModelName}' is already mapped to the same storage name.");
}
storageNameMap[property.StorageName] = property;
}
string TypeMessage() => type is null ? string.Empty : $"type '{type.Name}' or ";
}
/// <summary>
/// Validates a single property, performing validation on it.
/// </summary>
protected virtual void ValidateProperty(PropertyModel propertyModel, VectorStoreCollectionDefinition? definition)
{
_ = Throw.IfNull(propertyModel);
var type = propertyModel.Type;
Debug.Assert(propertyModel.Type is not null, "propertyModel.Type must be non-null");
switch (propertyModel)
{
case KeyPropertyModel keyProperty:
if (keyProperty.IsAutoGenerated && !SupportsKeyAutoGeneration(keyProperty.Type))
{
throw new NotSupportedException(
$"Property '{keyProperty.ModelName}' is configured for auto-generation, but key properties of type '{keyProperty.Type.Name}' do not support auto-generation.");
}
ValidateKeyProperty(keyProperty);
break;
case DataPropertyModel dataProperty:
if (!IsDataPropertyTypeValid(dataProperty.Type, out var supportedTypes))
{
throw new NotSupportedException(
$"Property '{dataProperty.ModelName}' has unsupported type '{type.Name}'. Data properties must be one of the supported types: {supportedTypes}.");
}
break;
case VectorPropertyModel vectorProperty:
if (vectorProperty.EmbeddingType is null)
{
if (IsVectorPropertyTypeValid(vectorProperty.Type, out string? supportedVectorTypes))
{
throw new UnreachableException("EmbeddingType cannot be null when the property type is supported.");
}
if (vectorProperty.EmbeddingGenerator is null)
{
throw new InvalidOperationException(VectorDataStrings.UnsupportedVectorPropertyWithoutEmbeddingGenerator(vectorProperty));
}
// If the user has configured a desired embedding type (done to use en embedding type other than the provider's default one), throw errors tailored to that.
// Throw errors related to that.
var userRequestedEmbeddingType = definition?.Properties.OfType<VectorStoreVectorProperty>().SingleOrDefault(p => p.Name == vectorProperty.ModelName)?.EmbeddingType;
if (userRequestedEmbeddingType is not null)
{
throw new InvalidOperationException(IsVectorPropertyTypeValid(userRequestedEmbeddingType, out _)
? VectorDataStrings.ConfiguredEmbeddingTypeIsUnsupportedByTheGenerator(vectorProperty, userRequestedEmbeddingType)
: VectorDataStrings.ConfiguredEmbeddingTypeIsUnsupportedByTheProvider(vectorProperty, userRequestedEmbeddingType, supportedVectorTypes));
}
throw new InvalidOperationException(VectorDataStrings.IncompatibleEmbeddingGenerator(vectorProperty, vectorProperty.EmbeddingGenerator, supportedVectorTypes));
}
if (!IsVectorPropertyTypeValid(vectorProperty.EmbeddingType, out string? supportedVectorTypes2))
{
// Should in principle never happen, only with incorrect provider customization.
throw new InvalidOperationException($"Property '{vectorProperty.ModelName}' has unsupported embedding type '{vectorProperty.EmbeddingType.Name}'. Vector properties must be one of the supported types: {supportedVectorTypes2}.");
}
if (vectorProperty.Dimensions <= 0)
{
throw new InvalidOperationException($"Vector property '{propertyModel.ModelName}' must have a positive number of dimensions.");
}
break;
default:
throw new UnreachableException();
}
}
/// <summary>
/// Configures auto-generation for the given key property.
/// Defaults to configuring <see cref="Guid" /> key properties as auto-generated, and throwing if auto-generation is requested for
/// any other type.
/// </summary>
/// <returns><see langword="true"/> if auto-generation is supported for the given key property type; otherwise, <see langword="false"/>.</returns>
protected virtual bool SupportsKeyAutoGeneration(Type keyPropertyType)
=> keyPropertyType == typeof(Guid);
/// <summary>
/// Validates the key property. The default implementation validates that the collection's generic key type (<see cref="KeyType"/>)
/// corresponds to the key property type on the model, if <see cref="KeyType"/> was provided.
/// Provider overrides should call the base implementation.
/// </summary>
protected virtual void ValidateKeyProperty(KeyPropertyModel keyProperty)
{
_ = Throw.IfNull(keyProperty);
if (KeyType is not null && KeyType != typeof(object) && KeyType != keyProperty.Type)
{
throw new InvalidOperationException(
$"The collection's generic key type is '{KeyType.Name}', but the key property '{keyProperty.ModelName}' has type '{keyProperty.Type.Name}'. " +
"The generic key type must match the key property type.");
}
}
/// <summary>
/// Validates that the .NET type for a data property is supported by the provider.
/// </summary>
/// <returns><see langword="true"/> if the type is valid; otherwise, <see langword="false"/>.</returns>
protected abstract bool IsDataPropertyTypeValid(Type type, [NotNullWhen(false)] out string? supportedTypes);
/// <summary>
/// Validates that the .NET type for a vector property is supported by the provider.
/// </summary>
/// <returns><see langword="true"/> if the type is valid; otherwise, <see langword="false"/>.</returns>
protected abstract bool IsVectorPropertyTypeValid(Type type, [NotNullWhen(false)] out string? supportedTypes);
}