-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVectorTypes.cs
282 lines (250 loc) · 11.1 KB
/
VectorTypes.cs
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
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
using Pinecone.Rest;
namespace Pinecone;
/// <summary>
/// An object representing a vector.
/// </summary>
public record Vector
{
/// <summary>
/// Unique ID of the vector.
/// </summary>
public required string Id { get; init; }
/// <summary>
/// Vector data.
/// </summary>
public required ReadOnlyMemory<float> Values { get; init; }
/// <summary>
/// Sparse vector information.
/// </summary>
public SparseVector? SparseValues { get; init; }
/// <summary>
/// Metadata associated with this vector.
/// </summary>
public MetadataMap? Metadata { get; init; }
}
/// <summary>
/// Contains sparse vector information.
/// </summary>
public readonly record struct SparseVector
{
/// <summary>
/// The indices of the sparse data.
/// </summary>
public required ReadOnlyMemory<uint> Indices { get; init; }
/// <summary>
/// The corresponding values of the sparse data, which must be with the same length as the indices.
/// </summary>
public required ReadOnlyMemory<float> Values { get; init; }
}
/// <summary>
/// Vector returned as a result of a query operation. Contains regular vector information as well as similarity score.
/// </summary>
public record ScoredVector
{
/// <summary>
/// Unique ID of the vector.
/// </summary>
public required string Id { get; init; }
/// <summary>
/// This is a measure of similarity between this vector and the query vector. The higher the score, the more they are similar.
/// </summary>
public required double Score { get; init; }
/// <summary>
/// Vector data.
/// </summary>
public ReadOnlyMemory<float>? Values { get; init; }
/// <summary>
/// Sparse vector information.
/// </summary>
public SparseVector? SparseValues { get; init; }
/// <summary>
/// Metadata associated with this vector.
/// </summary>
public MetadataMap? Metadata { get; init; }
}
/// <summary>
/// Collection of metadata consisting of key-value-pairs of property names and their corresponding values.
/// </summary>
public sealed class MetadataMap : Dictionary<string, MetadataValue>
{
/// <summary>
/// Creates a new instance of the <see cref="MetadataMap" /> class.
/// </summary>
public MetadataMap() : base() { }
/// <summary>
/// Creates a new instance of the <see cref="MetadataMap" /> class from an existing collection.
/// </summary>
/// <param name="collection"></param>
public MetadataMap(IEnumerable<KeyValuePair<string, MetadataValue>> collection)
#if NETSTANDARD2_0
: base(collection.ToDictionary(e => e.Key, e => e.Value))
#else
: base(collection)
#endif
{ }
}
/// <summary>
/// Value corresponding to a metadata property.
/// </summary>
[JsonConverter(typeof(MetadataValueConverter))]
public readonly record struct MetadataValue
{
static readonly object True = true;
static readonly object False = false;
/// <summary>
/// Metadata value stored.
/// </summary>
public object? Inner { get; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal MetadataValue(object? value) => Inner = value;
/// <summary>
/// Tries to create a new instance of the <see cref="MetadataValue" /> from the specified value.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="value">The value to create a metadata value from.</param>
/// <param name="metadataValue">The metadata value created.</param>
/// <returns><c>true</c> if the metadata value supports provided type of value; otherwise, <c>false</c>.</returns>
public static bool TryCreate<T>(T? value, out MetadataValue metadataValue)
{
switch (value)
{
case null: metadataValue = default; return false;
case bool b: metadataValue = b; return true;
case string s: metadataValue = s; return true;
case int i: metadataValue = i; return true;
case long l: metadataValue = l; return true;
case float f: metadataValue = f; return true;
case double d: metadataValue = d; return true;
case decimal m: metadataValue = m; return true;
case MetadataMap map: metadataValue = map; return true;
case MetadataValue[] array: metadataValue = array; return true;
case List<MetadataValue> list: metadataValue = list; return true;
case string[] s: metadataValue = s; return true;
case int[] i: metadataValue = i; return true;
case long[] l: metadataValue = l; return true;
case float[] f: metadataValue = f; return true;
case double[] d: metadataValue = d; return true;
case decimal[] m: metadataValue = m; return true;
case List<string> s: metadataValue = s; return true;
case List<int> i: metadataValue = i; return true;
case List<long> l: metadataValue = l; return true;
case List<float> f: metadataValue = f; return true;
case List<double> d: metadataValue = d; return true;
case List<decimal> m: metadataValue = m; return true;
default: metadataValue = default; return false;
}
}
public override string ToString() => Inner?.ToString() ?? "null";
// Main supported types
public static implicit operator MetadataValue(bool value) => new(value ? True : False);
public static implicit operator MetadataValue(string? value) => new(value);
public static implicit operator MetadataValue(int value) => new((double)value);
public static implicit operator MetadataValue(long value) => new((double)value);
public static implicit operator MetadataValue(float value) => new((double)value);
public static implicit operator MetadataValue(double value) => new(value);
public static implicit operator MetadataValue(decimal value) => new((double)value);
public static implicit operator MetadataValue(MetadataMap? value) => new(value);
public static implicit operator MetadataValue(MetadataValue[]? value) => new(value);
public static implicit operator MetadataValue(List<MetadataValue>? value) => new(value);
// Compatible conversions: arrays
public static implicit operator MetadataValue(string[]? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(int[]? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(long[]? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(float[]? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(double[]? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(decimal[]? value) => new(value?.Select(v => (MetadataValue)v));
// Compatible conversions: lists
public static implicit operator MetadataValue(List<string>? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(List<int>? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(List<long>? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(List<float>? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(List<double>? value) => new(value?.Select(v => (MetadataValue)v));
public static implicit operator MetadataValue(List<decimal>? value) => new(value?.Select(v => (MetadataValue)v));
}
/// <summary>
/// An exception that occurs when <see cref="Index{T}.ListAll"/> operation fails.
/// <para/>
/// It contains the vector IDs that were successfully read before the operation failed,
/// and the pagination token that can be used to resume and/or retry the operation.
/// </summary>
public class ListOperationException(
Exception inner,
string[] vectorIds,
string? paginationToken,
uint readUnits
) : Exception(inner.Message, inner)
{
/// <summary>
/// The IDs of the vectors that were successfully read before the operation failed.
/// </summary>
public string[] VectorIds { get; } = vectorIds;
/// <summary>
/// The pagination token that can be passed to <see cref="Index{T}.ListAll"/> to resume/retry the operation
/// where it left off, or to <see cref="Index{T}.ListPaginated"/> to continue using the pagination token manually.
/// </summary>
public string? PaginationToken { get; } = paginationToken;
/// <summary>
/// The number of read units consumed by the operation.
/// </summary>
public uint ReadUnits { get; } = readUnits;
}
/// <summary>
/// An exception that occurs when one or more parallel batch upserts fail in scope of
/// <see cref="Index{T}.Upsert(IEnumerable{Vector}, string?, CancellationToken)"/> operation.
/// <para/>
/// It contains the vector count that was successfully upserted before the operation failed,
/// the IDs of the vectors from the batches that could not be upserted, and the exceptions that caused the failure.
/// </summary>
public class ParallelUpsertException(
uint upserted,
string message,
string[] failedBatchVectorIds,
Exception[] exceptions
) : AggregateException(message, exceptions)
{
/// <summary>
/// The number of vectors that were successfully upserted before the operation failed.
/// </summary>
public uint Upserted { get; } = upserted;
/// <summary>
/// The IDs of the vectors from the batches that failed to upsert.
/// </summary>
public string[] FailedBatchVectorIds { get; } = failedBatchVectorIds;
}
/// <summary>
/// An exception that occurs when one or more parallel batch fetches fail in scope of
/// <see cref="Index{T}.Fetch(IEnumerable{string}, string?, CancellationToken)"/> operation.
/// <para/>
/// It contains the fetched vectors that were successfully fetched before the operation failed,
/// and the exceptions that caused the failure.
/// </summary>
public class ParallelFetchException(
Dictionary<string, Vector> fetched,
string message,
Exception[] exceptions
) : AggregateException(message, exceptions)
{
/// <summary>
/// The vectors that were successfully fetched before the operation failed.
/// </summary>
public Dictionary<string, Vector> Fetched { get; } = fetched;
}
/// <summary>
/// An exception that occurs when one or more parallel batch delete operations fail in scope of
/// <see cref="Index{T}.Delete(IEnumerable{string}, string?, CancellationToken)"/> operation.
/// <para/>
/// It contains the IDs of the vectors from the batches that could not be deleted, and the exceptions that caused the failure.
/// </summary>
public class ParallelDeleteException(
string message,
string[] failedBatchVectorIds,
Exception[] exceptions
) : AggregateException(message, exceptions)
{
/// <summary>
/// The IDs of the vectors from the batches that could not be deleted.
/// </summary>
public string[] FailedBatchVectorIds { get; } = failedBatchVectorIds;
}