-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexClient.cs
558 lines (537 loc) · 18.4 KB
/
IndexClient.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
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
using System.Net.Http;
using System.Text.Json;
using System.Threading;
using Grpc.Core;
using Pinecone.Core;
using Pinecone.Grpc;
#nullable enable
namespace Pinecone;
public partial class IndexClient
{
private RawClient _client;
private RawGrpcClient _grpc;
private VectorService.VectorServiceClient _vectorService;
internal IndexClient(RawClient client)
{
_client = client;
_grpc = _client.Grpc;
_vectorService = new VectorService.VectorServiceClient(_grpc.Channel);
}
/// <summary>
/// The `list_imports` operation lists all recent and ongoing import operations. For guidance and examples, see [Import data](https://docs.pinecone.io/guides/data/import-data).
/// </summary>
/// <example>
/// <code>
/// await client.Index.ListBulkImportsAsync(new ListBulkImportsRequest());
/// </code>
/// </example>
public async Task<ListImportsResponse> ListBulkImportsAsync(
ListBulkImportsRequest request,
RequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
var _query = new Dictionary<string, object>();
if (request.Limit != null)
{
_query["limit"] = request.Limit.ToString();
}
if (request.PaginationToken != null)
{
_query["paginationToken"] = request.PaginationToken;
}
var response = await _client.MakeRequestAsync(
new RawClient.JsonApiRequest
{
BaseUrl = _client.Options.BaseUrl,
Method = HttpMethod.Get,
Path = "bulk/imports",
Query = _query,
Options = options,
},
cancellationToken
);
var responseBody = await response.Raw.Content.ReadAsStringAsync();
if (response.StatusCode is >= 200 and < 400)
{
try
{
return JsonUtils.Deserialize<ListImportsResponse>(responseBody)!;
}
catch (JsonException e)
{
throw new PineconeException("Failed to deserialize response", e);
}
}
throw new PineconeApiException(
$"Error with status code {response.StatusCode}",
response.StatusCode,
responseBody
);
}
/// <summary>
/// The `start_import` operation starts an asynchronous import of vectors from object storage into an index. For guidance and examples, see [Import data](https://docs.pinecone.io/guides/data/import-data).
/// </summary>
/// <example>
/// <code>
/// await client.Index.StartBulkImportAsync(new StartImportRequest { Uri = "uri" });
/// </code>
/// </example>
public async Task<StartImportResponse> StartBulkImportAsync(
StartImportRequest request,
RequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
var response = await _client.MakeRequestAsync(
new RawClient.JsonApiRequest
{
BaseUrl = _client.Options.BaseUrl,
Method = HttpMethod.Post,
Path = "bulk/imports",
Body = request,
ContentType = "application/json",
Options = options,
},
cancellationToken
);
var responseBody = await response.Raw.Content.ReadAsStringAsync();
if (response.StatusCode is >= 200 and < 400)
{
try
{
return JsonUtils.Deserialize<StartImportResponse>(responseBody)!;
}
catch (JsonException e)
{
throw new PineconeException("Failed to deserialize response", e);
}
}
throw new PineconeApiException(
$"Error with status code {response.StatusCode}",
response.StatusCode,
responseBody
);
}
/// <summary>
/// The `describe_import` operation returns details of a specific import operation. For guidance and examples,
/// see [Import data](https://docs.pinecone.io/guides/data/import-data).
/// </summary>
/// <example>
/// <code>
/// await client.Index.DescribeBulkImportAsync("101");
/// </code>
/// </example>
public async Task<ImportModel> DescribeBulkImportAsync(
string id,
RequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
var response = await _client.MakeRequestAsync(
new RawClient.JsonApiRequest
{
BaseUrl = _client.Options.BaseUrl,
Method = HttpMethod.Get,
Path = $"bulk/imports/{id}",
Options = options,
},
cancellationToken
);
var responseBody = await response.Raw.Content.ReadAsStringAsync();
if (response.StatusCode is >= 200 and < 400)
{
try
{
return JsonUtils.Deserialize<ImportModel>(responseBody)!;
}
catch (JsonException e)
{
throw new PineconeException("Failed to deserialize response", e);
}
}
throw new PineconeApiException(
$"Error with status code {response.StatusCode}",
response.StatusCode,
responseBody
);
}
/// <summary>
/// The `cancel_import` operation cancels an import operation if it is not yet finished. It has no effect if the operation is already finished. For guidance and examples, see [Import data](https://docs.pinecone.io/guides/data/import-data).
/// </summary>
/// <example>
/// <code>
/// await client.Index.CancelBulkImportAsync("101");
/// </code>
/// </example>
public async Task<CancelImportResponse> CancelBulkImportAsync(
string id,
RequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
var response = await _client.MakeRequestAsync(
new RawClient.JsonApiRequest
{
BaseUrl = _client.Options.BaseUrl,
Method = HttpMethod.Delete,
Path = $"bulk/imports/{id}",
Options = options,
},
cancellationToken
);
var responseBody = await response.Raw.Content.ReadAsStringAsync();
if (response.StatusCode is >= 200 and < 400)
{
try
{
return JsonUtils.Deserialize<CancelImportResponse>(responseBody)!;
}
catch (JsonException e)
{
throw new PineconeException("Failed to deserialize response", e);
}
}
throw new PineconeApiException(
$"Error with status code {response.StatusCode}",
response.StatusCode,
responseBody
);
}
/// <summary>
/// Get index stats
///
/// The `describe_index_stats` operation returns statistics about the contents of an index, including the vector count per namespace, the number of dimensions, and the index fullness.
///
/// Serverless indexes scale automatically as needed, so index fullness is relevant only for pod-based indexes.
///
/// For pod-based indexes, the index fullness result may be inaccurate during pod resizing; to get the status of a pod resizing process, use [`describe_index`](https://docs.pinecone.io/reference/api/control-plane/describe_index).
/// </summary>
/// <example>
/// <code>
/// await client.Index.DescribeIndexStatsAsync(new DescribeIndexStatsRequest());
/// </code>
/// </example>
public async Task<DescribeIndexStatsResponse> DescribeIndexStatsAsync(
DescribeIndexStatsRequest request,
GrpcRequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
try
{
var callOptions = _grpc.CreateCallOptions(
options ?? new GrpcRequestOptions(),
cancellationToken
);
var call = _vectorService.DescribeIndexStatsAsync(request.ToProto(), callOptions);
var response = await call.ConfigureAwait(false);
return DescribeIndexStatsResponse.FromProto(response);
}
catch (RpcException rpc)
{
var statusCode = (int)rpc.StatusCode;
throw new PineconeApiException(
$"Error with gRPC status code {statusCode}",
statusCode,
rpc.Message
);
}
catch (Exception e)
{
throw new PineconeException("Error", e);
}
}
/// <summary>
/// Query vectors
///
/// The `query` operation searches a namespace, using a query vector. It retrieves the ids of the most similar items in a namespace, along with their similarity scores.
///
/// For guidance and examples, see [Query data](https://docs.pinecone.io/guides/data/query-data).
/// </summary>
/// <example>
/// <code>
/// await client.Index.QueryAsync(
/// new QueryRequest
/// {
/// TopK = 3,
/// Namespace = "example",
/// IncludeValues = true,
/// IncludeMetadata = true,
/// }
/// );
/// </code>
/// </example>
public async Task<QueryResponse> QueryAsync(
QueryRequest request,
GrpcRequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
try
{
var callOptions = _grpc.CreateCallOptions(
options ?? new GrpcRequestOptions(),
cancellationToken
);
var call = _vectorService.QueryAsync(request.ToProto(), callOptions);
var response = await call.ConfigureAwait(false);
return QueryResponse.FromProto(response);
}
catch (RpcException rpc)
{
var statusCode = (int)rpc.StatusCode;
throw new PineconeApiException(
$"Error with gRPC status code {statusCode}",
statusCode,
rpc.Message
);
}
catch (Exception e)
{
throw new PineconeException("Error", e);
}
}
/// <summary>
/// Delete vectors
///
/// The `delete` operation deletes vectors, by id, from a single namespace.
///
/// For guidance and examples, see [Delete data](https://docs.pinecone.io/guides/data/delete-data).
/// </summary>
/// <example>
/// <code>
/// await client.Index.DeleteAsync(
/// new DeleteRequest
/// {
/// Ids = new List<string>() { "v1", "v2", "v3" },
/// Namespace = "example",
/// }
/// );
/// </code>
/// </example>
public async Task<DeleteResponse> DeleteAsync(
DeleteRequest request,
GrpcRequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
try
{
var callOptions = _grpc.CreateCallOptions(
options ?? new GrpcRequestOptions(),
cancellationToken
);
var call = _vectorService.DeleteAsync(request.ToProto(), callOptions);
var response = await call.ConfigureAwait(false);
return DeleteResponse.FromProto(response);
}
catch (RpcException rpc)
{
var statusCode = (int)rpc.StatusCode;
throw new PineconeApiException(
$"Error with gRPC status code {statusCode}",
statusCode,
rpc.Message
);
}
catch (Exception e)
{
throw new PineconeException("Error", e);
}
}
/// <summary>
/// Fetch vectors
///
/// The `fetch` operation looks up and returns vectors, by ID, from a single namespace. The returned vectors include the vector data and/or metadata.
///
/// For guidance and examples, see [Fetch data](https://docs.pinecone.io/guides/data/fetch-data).
/// </summary>
/// <example>
/// <code>
/// await client.Index.FetchAsync(new FetchRequest { Ids = ["v1"], Namespace = "example" });
/// </code>
/// </example>
public async Task<FetchResponse> FetchAsync(
FetchRequest request,
GrpcRequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
try
{
var callOptions = _grpc.CreateCallOptions(
options ?? new GrpcRequestOptions(),
cancellationToken
);
var call = _vectorService.FetchAsync(request.ToProto(), callOptions);
var response = await call.ConfigureAwait(false);
return FetchResponse.FromProto(response);
}
catch (RpcException rpc)
{
var statusCode = (int)rpc.StatusCode;
throw new PineconeApiException(
$"Error with gRPC status code {statusCode}",
statusCode,
rpc.Message
);
}
catch (Exception e)
{
throw new PineconeException("Error", e);
}
}
/// <summary>
/// List vector IDs
///
/// The `list` operation lists the IDs of vectors in a single namespace of a serverless index. An optional prefix can be passed to limit the results to IDs with a common prefix.
///
/// `list` returns up to 100 IDs at a time by default in sorted order (bitwise/"C" collation). If the `limit` parameter is set, `list` returns up to that number of IDs instead. Whenever there are additional IDs to return, the response also includes a `pagination_token` that you can use to get the next batch of IDs. When the response does not include a `pagination_token`, there are no more IDs to return.
///
/// For guidance and examples, see [List record IDs](https://docs.pinecone.io/guides/data/list-record-ids).
///
/// **Note:** `list` is supported only for serverless indexes.
/// </summary>
/// <example>
/// <code>
/// await client.Index.ListAsync(
/// new ListRequest
/// {
/// Limit = 50,
/// Namespace = "example",
/// PaginationToken = "eyJza2lwX3Bhc3QiOiIxMDEwMy0=",
/// }
/// );
/// </code>
/// </example>
public async Task<ListResponse> ListAsync(
ListRequest request,
GrpcRequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
try
{
var callOptions = _grpc.CreateCallOptions(
options ?? new GrpcRequestOptions(),
cancellationToken
);
var call = _vectorService.ListAsync(request.ToProto(), callOptions);
var response = await call.ConfigureAwait(false);
return ListResponse.FromProto(response);
}
catch (RpcException rpc)
{
var statusCode = (int)rpc.StatusCode;
throw new PineconeApiException(
$"Error with gRPC status code {statusCode}",
statusCode,
rpc.Message
);
}
catch (Exception e)
{
throw new PineconeException("Error", e);
}
}
/// <summary>
/// Update a vector
///
/// The `update` operation updates a vector in a namespace. If a value is included, it will overwrite the previous value. If a `set_metadata` is included, the values of the fields specified in it will be added or overwrite the previous value.
///
/// For guidance and examples, see [Update data](https://docs.pinecone.io/guides/data/update-data).
/// </summary>
/// <example>
/// <code>
/// await client.Index.UpdateAsync(
/// new UpdateRequest
/// {
/// Id = "v1",
/// Namespace = "example",
/// Values = new[] { 42.2f, 50.5f, 60.8f },
/// }
/// );
/// </code>
/// </example>
public async Task<UpdateResponse> UpdateAsync(
UpdateRequest request,
GrpcRequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
try
{
var callOptions = _grpc.CreateCallOptions(
options ?? new GrpcRequestOptions(),
cancellationToken
);
var call = _vectorService.UpdateAsync(request.ToProto(), callOptions);
var response = await call.ConfigureAwait(false);
return UpdateResponse.FromProto(response);
}
catch (RpcException rpc)
{
var statusCode = (int)rpc.StatusCode;
throw new PineconeApiException(
$"Error with gRPC status code {statusCode}",
statusCode,
rpc.Message
);
}
catch (Exception e)
{
throw new PineconeException("Error", e);
}
}
/// <summary>
/// Upsert vectors
///
/// The `upsert` operation writes vectors into a namespace. If a new value is upserted for an existing vector ID, it will overwrite the previous value.
///
/// For guidance and examples, see [Upsert data](https://docs.pinecone.io/guides/data/upsert-data).
/// </summary>
/// <example>
/// <code>
/// await client.Index.UpsertAsync(
/// new UpsertRequest
/// {
/// Vectors = new List<Vector>()
/// {
/// new Vector { Id = "v1", Values = new[] { 0.1f, 0.2f, 0.3f } },
/// },
/// }
/// );
/// </code>
/// </example>
public async Task<UpsertResponse> UpsertAsync(
UpsertRequest request,
GrpcRequestOptions? options = null,
CancellationToken cancellationToken = default
)
{
try
{
var callOptions = _grpc.CreateCallOptions(
options ?? new GrpcRequestOptions(),
cancellationToken
);
var call = _vectorService.UpsertAsync(request.ToProto(), callOptions);
var response = await call.ConfigureAwait(false);
return UpsertResponse.FromProto(response);
}
catch (RpcException rpc)
{
var statusCode = (int)rpc.StatusCode;
throw new PineconeApiException(
$"Error with gRPC status code {statusCode}",
statusCode,
rpc.Message
);
}
catch (Exception e)
{
throw new PineconeException("Error", e);
}
}
}