-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathPinecone.java
More file actions
1812 lines (1673 loc) · 85.8 KB
/
Pinecone.java
File metadata and controls
1812 lines (1673 loc) · 85.8 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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.pinecone.clients;
import io.pinecone.configs.PineconeConfig;
import io.pinecone.configs.PineconeConnection;
import io.pinecone.configs.ProxyConfig;
import io.pinecone.configs.ResponseMetadataListener;
import io.pinecone.exceptions.*;
import okhttp3.OkHttpClient;
import org.openapitools.db_control.client.ApiClient;
import org.openapitools.db_control.client.ApiException;
import org.openapitools.db_control.client.Configuration;
import org.openapitools.db_control.client.api.ManageIndexesApi;
import org.openapitools.db_control.client.model.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* The Pinecone class is the main entry point for interacting with Pinecone via the Java SDK.
* It is used to create, delete, and manage your indexes and collections, along with the inference api.
* Note that the Pinecone class instantiates a single shared {@link OkHttpClient} instance,
* which is used for both control plane and inference operations.The OkHttpClient performs best when you create a single
* `OkHttpClient` instance and reuse it for all of your HTTP calls. This is because each client holds its own connection
* pool and thread pools. Reusing connections and threads reduces latency and saves memory. Conversely, creating a
* client for each request wastes resources on idle pools.
* <p>
* To instantiate the Pinecone class, use the {@link Pinecone.Builder} class to pass
* an API key and any other optional configuration.
*
* <pre>{@code
* import io.pinecone.clients.Pinecone;
*
* Pinecone client = new Pinecone.Builder("PINECONE_API_KEY").build();
*
* // Use the client to interact with Pinecone
* client.describeIndex("YOUR-INDEX");
*
* // Use the client to obtain an Index or AsyncIndex instance to upsert or query
* Index index = client.getIndexConnection("YOUR-INDEX");
* index.upsert(...);
* }</pre>
*/
public class Pinecone {
private static final ConcurrentHashMap<String, PineconeConnection> connectionsMap = new ConcurrentHashMap<>();
private final ManageIndexesApi manageIndexesApi;
private final PineconeConfig config;
Pinecone(PineconeConfig config, ManageIndexesApi manageIndexesApi) {
this.config = config;
this.manageIndexesApi = manageIndexesApi;
}
PineconeConfig getConfig() {
return config;
}
/**
* Creates a new serverless index with the specified parameters.
* <p>
* Example:
* <pre>{@code
* client.createServerlessIndex("YOUR-INDEX", "cosine", 1536, "aws", "us-west-2", DeletionProtection.ENABLED);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param metric The metric type for the index. Must be one of "cosine", "euclidean", or "dotproduct".
* @param dimension The number of dimensions for the index.
* @param cloud The cloud provider for the index.
* @param region The cloud region for the index.
* @param deletionProtection Enable or disable deletion protection for the index.
* @param tags A map of tags to associate with the Index.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createServerlessIndex(String indexName,
String metric,
int dimension,
String cloud,
String region,
String deletionProtection,
Map<String, String> tags) throws PineconeException {
return createServerlessIndex(indexName, metric, dimension, cloud, region, deletionProtection, tags, null, null);
}
/**
* Creates a new serverless index with the specified parameters, including optional read capacity and metadata schema configuration.
* <p>
* This method allows you to configure dedicated read capacity nodes for better performance and cost predictability,
* and to limit metadata indexing to specific fields for improved performance.
* <p>
* Example - Create index with OnDemand read capacity (default):
* <pre>{@code
* import org.openapitools.db_control.client.model.ReadCapacity;
* import org.openapitools.db_control.client.model.ReadCapacityOnDemandSpec;
* ...
*
* ReadCapacity readCapacity = new ReadCapacity(new ReadCapacityOnDemandSpec().mode("OnDemand"));
* client.createServerlessIndex("YOUR-INDEX", "cosine", 1536, "aws", "us-west-2",
* DeletionProtection.ENABLED, null, readCapacity, null);
* }</pre>
* <p>
* Example - Create index with Dedicated read capacity:
* <pre>{@code
* import org.openapitools.db_control.client.model.ReadCapacity;
* import org.openapitools.db_control.client.model.ReadCapacityDedicatedSpec;
* import org.openapitools.db_control.client.model.ReadCapacityDedicatedConfig;
* import org.openapitools.db_control.client.model.ScalingConfigManual;
* ...
*
* ScalingConfigManual manual = new ScalingConfigManual().shards(2).replicas(2);
* ReadCapacityDedicatedConfig dedicated = new ReadCapacityDedicatedConfig()
* .nodeType("t1")
* .scaling("Manual")
* .manual(manual);
* ReadCapacity readCapacity = new ReadCapacity(
* new ReadCapacityDedicatedSpec().mode("Dedicated").dedicated(dedicated));
* client.createServerlessIndex("YOUR-INDEX", "cosine", 1536, "aws", "us-west-2",
* DeletionProtection.ENABLED, null, readCapacity, null);
* }</pre>
* <p>
* Example - Create index with metadata schema:
* <pre>{@code
* import org.openapitools.db_control.client.model.BackupModelSchema;
* import org.openapitools.db_control.client.model.BackupModelSchemaFieldsValue;
* ...
*
* Map<String, BackupModelSchemaFieldsValue> fields = new HashMap<>();
* fields.put("genre", new BackupModelSchemaFieldsValue().filterable(true));
* fields.put("year", new BackupModelSchemaFieldsValue().filterable(true));
* BackupModelSchema schema = new BackupModelSchema().fields(fields);
* client.createServerlessIndex("YOUR-INDEX", "cosine", 1536, "aws", "us-west-2",
* DeletionProtection.ENABLED, null, null, schema);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param metric The metric type for the index. Must be one of "cosine", "euclidean", or "dotproduct".
* @param dimension The number of dimensions for the index.
* @param cloud The cloud provider for the index.
* @param region The cloud region for the index.
* @param deletionProtection Enable or disable deletion protection for the index.
* @param tags A map of tags to associate with the Index.
* @param readCapacity The read capacity configuration. If null, defaults to OnDemand mode.
* Use {@link ReadCapacityOnDemandSpec} for OnDemand or {@link ReadCapacityDedicatedSpec} for Dedicated mode.
* @param schema The metadata schema configuration. If null, all metadata fields are indexed.
* Use this to limit metadata indexing to specific fields for improved performance.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createServerlessIndex(String indexName,
String metric,
int dimension,
String cloud,
String region,
String deletionProtection,
Map<String, String> tags,
ReadCapacity readCapacity,
BackupModelSchema schema) throws PineconeException {
if (indexName == null || indexName.isEmpty()) {
throw new PineconeValidationException("Index name cannot be null or empty");
}
if (metric == null || metric.isEmpty()) {
metric = "cosine";
}
if (dimension < 1) {
throw new PineconeValidationException("Dimension must be greater than 0. See limits for more info: https://docs.pinecone.io/reference/limits");
}
if (cloud == null || cloud.isEmpty()) {
throw new PineconeValidationException("Cloud cannot be null or empty");
}
if (region == null || region.isEmpty()) {
throw new PineconeValidationException("Region cannot be null or empty");
}
ServerlessSpec serverlessSpec = new ServerlessSpec().cloud(cloud).region(region);
if (readCapacity != null) {
serverlessSpec.readCapacity(readCapacity);
}
if (schema != null) {
serverlessSpec.schema(schema);
}
IndexSpec createServerlessIndexRequestSpec = new IndexSpec(new IndexSpecServerless().serverless(serverlessSpec));
IndexModel indexModel = null;
try {
CreateIndexRequest createIndexRequest = new CreateIndexRequest()
.name(indexName)
.metric(metric)
.dimension(dimension)
.spec(createServerlessIndexRequestSpec)
.deletionProtection(deletionProtection);
if(tags != null && !tags.isEmpty()) {
createIndexRequest.tags(tags);
}
indexModel = manageIndexesApi.createIndex(Configuration.VERSION, createIndexRequest);
} catch (ApiException apiException) {
handleApiException(apiException);
}
return indexModel;
}
/**
* Creates a new sparse serverless index.
* <p>
* Example:
* <pre>{@code
* client.createServerlessIndex("YOUR-INDEX", "cosine", 1536, "aws", "us-west-2", DeletionProtection.ENABLED);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param cloud The cloud provider for the index.
* @param region The cloud region for the index.
* @param deletionProtection Enable or disable deletion protection for the index.
* @param tags A map of tags to associate with the Index.
* @param vectorType The metric type for the index. Must be one of "cosine", "euclidean", or "dotproduct".
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createSparseServelessIndex(String indexName,
String cloud,
String region,
String deletionProtection,
Map<String, String> tags,
String vectorType) throws PineconeException {
if (indexName == null || indexName.isEmpty()) {
throw new PineconeValidationException("Index name cannot be null or empty");
}
if (cloud == null || cloud.isEmpty()) {
throw new PineconeValidationException("Cloud cannot be null or empty");
}
if (region == null || region.isEmpty()) {
throw new PineconeValidationException("Region cannot be null or empty");
}
if(!vectorType.equalsIgnoreCase("sparse") && !vectorType.equalsIgnoreCase("dense")) {
throw new PineconeValidationException("vectorType must be sparse or dense");
}
ServerlessSpec serverlessSpec = new ServerlessSpec().cloud(cloud).region(region);
IndexSpec createServerlessIndexRequestSpec = new IndexSpec(new IndexSpecServerless().serverless(serverlessSpec));
IndexModel indexModel = null;
try {
CreateIndexRequest createIndexRequest = new CreateIndexRequest()
.name(indexName)
.metric("dotproduct")
.spec(createServerlessIndexRequestSpec)
.deletionProtection(deletionProtection)
.vectorType(vectorType);
if(tags != null && !tags.isEmpty()) {
createIndexRequest.tags(tags);
}
indexModel = manageIndexesApi.createIndex(Configuration.VERSION, createIndexRequest);
} catch (ApiException apiException) {
handleApiException(apiException);
}
return indexModel;
}
/**
* Creates a new serverless index with an associated embedding model.
* <p>
* Example:
* <pre>{@code
* client.createIndexForModel("my-index", CreateIndexForModelRequest.CloudEnum.AWS,
* "us-west-2", embedConfig, DeletionProtection.DISABLED, tags);
* }</pre>
*
* @param name The name of the index to be created. The name must be between 1 and 45 characters,
* start and end with an alphanumeric character, and consist only of lowercase alphanumeric
* characters or hyphens ('-').
* @param cloud The cloud provider where the index will be hosted. Must be one of the supported cloud providers.
* @param region The cloud region where the index will be created.
* @param embed The embedding model configuration. Once set, the model cannot be changed, but configurations
* such as field map and parameters can be updated.
* @param deletionProtection Whether deletion protection is enabled for the index. If enabled, the index
* cannot be deleted. Defaults to disabled if not provided.
* @param tags A map of custom user tags to associate with the index. Keys must be alphanumeric or contain
* underscores ('_') or hyphens ('-'). Values must be alphanumeric, or contain characters such
* as ';', '@', '_', '-', '.', '+', or spaces.
* @return {@link IndexModel} representing the created serverless index with the associated embedding model.
* @throws PineconeException if the API encounters an error during index creation, or if any of the arguments
* are invalid.
* @throws ApiException if an error occurs while communicating with the API.
*/
public IndexModel createIndexForModel(String name,
String cloud,
String region,
CreateIndexForModelRequestEmbed embed,
String deletionProtection,
Map<String, String> tags) throws PineconeException, ApiException {
return createIndexForModel(name, cloud, region, embed, deletionProtection, tags, null, null);
}
/**
* Creates a new serverless index with an associated embedding model, including optional read capacity and metadata schema configuration.
* <p>
* This method allows you to configure dedicated read capacity nodes for better performance and cost predictability,
* and to limit metadata indexing to specific fields for improved performance.
* <p>
* Example - Create index for model with Dedicated read capacity:
* <pre>{@code
* import org.openapitools.db_control.client.model.ReadCapacity;
* import org.openapitools.db_control.client.model.ReadCapacityDedicatedSpec;
* import org.openapitools.db_control.client.model.ReadCapacityDedicatedConfig;
* import org.openapitools.db_control.client.model.ScalingConfigManual;
* ...
*
* ScalingConfigManual manual = new ScalingConfigManual().shards(1).replicas(1);
* ReadCapacityDedicatedConfig dedicated = new ReadCapacityDedicatedConfig()
* .nodeType("t1")
* .scaling("Manual")
* .manual(manual);
* ReadCapacity readCapacity = new ReadCapacity(
* new ReadCapacityDedicatedSpec().mode("Dedicated").dedicated(dedicated));
*
* CreateIndexForModelRequestEmbed embed = new CreateIndexForModelRequestEmbed();
* embed.model("multilingual-e5-large");
* Map<String, String> fieldMap = new HashMap<>();
* fieldMap.put("text", "my-sample-text");
* embed.fieldMap(fieldMap);
*
* client.createIndexForModel("my-index", "aws", "us-east-1", embed,
* DeletionProtection.DISABLED, null, readCapacity, null);
* }</pre>
* <p>
* Example - Create index for model with metadata schema:
* <pre>{@code
* import org.openapitools.db_control.client.model.BackupModelSchema;
* import org.openapitools.db_control.client.model.BackupModelSchemaFieldsValue;
* ...
*
* Map<String, BackupModelSchemaFieldsValue> fields = new HashMap<>();
* fields.put("category", new BackupModelSchemaFieldsValue().filterable(true));
* fields.put("tags", new BackupModelSchemaFieldsValue().filterable(true));
* BackupModelSchema schema = new BackupModelSchema().fields(fields);
*
* CreateIndexForModelRequestEmbed embed = new CreateIndexForModelRequestEmbed();
* embed.model("multilingual-e5-large");
* Map<String, String> fieldMap = new HashMap<>();
* fieldMap.put("text", "my-sample-text");
* embed.fieldMap(fieldMap);
*
* client.createIndexForModel("my-index", "aws", "us-east-1", embed,
* DeletionProtection.DISABLED, null, null, schema);
* }</pre>
*
* @param name The name of the index to be created. The name must be between 1 and 45 characters,
* start and end with an alphanumeric character, and consist only of lowercase alphanumeric
* characters or hyphens ('-').
* @param cloud The cloud provider where the index will be hosted. Must be one of the supported cloud providers.
* @param region The cloud region where the index will be created.
* @param embed The embedding model configuration. Once set, the model cannot be changed, but configurations
* such as field map and parameters can be updated.
* @param deletionProtection Whether deletion protection is enabled for the index. If enabled, the index
* cannot be deleted. Defaults to disabled if not provided.
* @param tags A map of custom user tags to associate with the index. Keys must be alphanumeric or contain
* underscores ('_') or hyphens ('-'). Values must be alphanumeric, or contain characters such
* as ';', '@', '_', '-', '.', '+', or spaces.
* @param readCapacity The read capacity configuration. If null, defaults to OnDemand mode.
* Use {@link ReadCapacityOnDemandSpec} for OnDemand or {@link ReadCapacityDedicatedSpec} for Dedicated mode.
* @param schema The metadata schema configuration. If null, all metadata fields are indexed.
* Use this to limit metadata indexing to specific fields for improved performance.
* @return {@link IndexModel} representing the created serverless index with the associated embedding model.
* @throws PineconeException if the API encounters an error during index creation, or if any of the arguments
* are invalid.
* @throws ApiException if an error occurs while communicating with the API.
*/
public IndexModel createIndexForModel(String name,
String cloud,
String region,
CreateIndexForModelRequestEmbed embed,
String deletionProtection,
Map<String, String> tags,
ReadCapacity readCapacity,
BackupModelSchema schema) throws PineconeException, ApiException {
CreateIndexForModelRequest createIndexForModelRequest = new CreateIndexForModelRequest()
.name(name)
.cloud(cloud)
.region(region)
.embed(embed)
.deletionProtection(deletionProtection)
.tags(tags);
if (readCapacity != null) {
createIndexForModelRequest.readCapacity(readCapacity);
}
if (schema != null) {
createIndexForModelRequest.schema(schema);
}
return manageIndexesApi.createIndexForModel(Configuration.VERSION, createIndexForModelRequest);
}
/**
* Creates a new BYOC (Bring Your Own Cloud) index with minimal required parameters.
* <p>
* BYOC indexes allow you to deploy Pinecone indexes in your own cloud infrastructure.
* You must have a BYOC environment set up with Pinecone before creating a BYOC index.
* <p>
* Example:
* <pre>{@code
* client.createByocIndex("YOUR-INDEX", "cosine", 1536, "your-byoc-environment");
* }</pre>
*
* @param indexName The name of the index to be created.
* @param metric The metric type for the index. Must be one of "cosine", "euclidean", or "dotproduct".
* @param dimension The number of dimensions for the index.
* @param environment The BYOC environment where the index will be hosted. This is provided during BYOC onboarding.
* @return {@link IndexModel} representing the created BYOC index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createByocIndex(String indexName,
String metric,
int dimension,
String environment) throws PineconeException {
return createByocIndex(indexName, metric, dimension, environment, "disabled", null, null);
}
/**
* Creates a new BYOC (Bring Your Own Cloud) index with the specified parameters, including optional deletion protection, tags, and metadata schema configuration.
* <p>
* BYOC indexes allow you to deploy Pinecone indexes in your own cloud infrastructure.
* You must have a BYOC environment set up with Pinecone before creating a BYOC index.
* <p>
* Example with metadata schema:
* <pre>{@code
* import org.openapitools.db_control.client.model.BackupModelSchema;
* import org.openapitools.db_control.client.model.BackupModelSchemaFieldsValue;
* ...
*
* Map<String, BackupModelSchemaFieldsValue> fields = new HashMap<>();
* fields.put("genre", new BackupModelSchemaFieldsValue().filterable(true));
* fields.put("year", new BackupModelSchemaFieldsValue().filterable(true));
* BackupModelSchema schema = new BackupModelSchema().fields(fields);
* client.createByocIndex("YOUR-INDEX", "cosine", 1536, "aws-us-east-1-b921",
* DeletionProtection.ENABLED, null, schema);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param metric The metric type for the index. Must be one of "cosine", "euclidean", or "dotproduct".
* @param dimension The number of dimensions for the index.
* @param environment The BYOC environment where the index will be hosted. This is provided during BYOC onboarding.
* @param deletionProtection Enable or disable deletion protection for the index.
* @param tags A map of tags to associate with the Index.
* @param schema The metadata schema configuration. If null, all metadata fields are indexed.
* Use this to limit metadata indexing to specific fields for improved performance.
* @return {@link IndexModel} representing the created BYOC index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createByocIndex(String indexName,
String metric,
int dimension,
String environment,
String deletionProtection,
Map<String, String> tags,
BackupModelSchema schema) throws PineconeException {
if (indexName == null || indexName.isEmpty()) {
throw new PineconeValidationException("Index name cannot be null or empty");
}
if (metric == null || metric.isEmpty()) {
metric = "cosine";
}
if (dimension < 1) {
throw new PineconeValidationException("Dimension must be greater than 0. See limits for more info: https://docs.pinecone.io/reference/limits");
}
if (environment == null || environment.isEmpty()) {
throw new PineconeValidationException("Environment cannot be null or empty");
}
ByocSpec byocSpec = new ByocSpec().environment(environment);
if (schema != null) {
byocSpec.schema(schema);
}
IndexSpec createByocIndexRequestSpec = new IndexSpec(new IndexSpecBYOC().byoc(byocSpec));
IndexModel indexModel = null;
try {
CreateIndexRequest createIndexRequest = new CreateIndexRequest()
.name(indexName)
.metric(metric)
.dimension(dimension)
.spec(createByocIndexRequestSpec)
.deletionProtection(deletionProtection);
if(tags != null && !tags.isEmpty()) {
createIndexRequest.tags(tags);
}
indexModel = manageIndexesApi.createIndex(Configuration.VERSION, createIndexRequest);
} catch (ApiException apiException) {
handleApiException(apiException);
}
return indexModel;
}
/**
* Overload for creating a new pods index with environment and podType, the minimum required parameters.
* <p>
* Example:
* <pre>{@code
* client.createPodsIndex("YOUR-INDEX", 1536, "us-east4-gcp", "p1.x2");
* }</pre>
*
* @param indexName The name of the index to be created.
* @param dimension The number of dimensions for the index.
* @param environment The cloud environment where you want the index to be hosted.
* @param podType The type of pod to use. A string with one of s1, p1, or p2 appended with a "." and one of x1, x2, x4, or x8.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createPodsIndex(String indexName, Integer dimension, String environment, String podType) {
return createPodsIndex(indexName, dimension, environment, podType, null, null, null,
null, null, null, "disabled", null);
}
/**
* Overload for creating a new pods index with environment, podType, and deletion protection.
* <p>
* Example:
* <pre>{@code
* client.createPodsIndex("YOUR-INDEX", 1536, "us-east4-gcp", "p1.x2", DeletionProtection.ENABLED);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param dimension The number of dimensions for the index.
* @param environment The cloud environment where you want the index to be hosted.
* @param podType The type of pod to use. A string with one of s1, p1, or p2 appended with a "." and one of x1, x2, x4, or x8.
* @param deletionProtection Enable or disable deletion protection for the index.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createPodsIndex(String indexName,
Integer dimension,
String environment,
String podType,
String deletionProtection) {
return createPodsIndex(indexName, dimension, environment, podType, null, null, null,
null, null, null, deletionProtection, null);
}
/**
* Overload for creating a new pods index with environment, podType, metric, and metadataConfig.
* <p>
* Example:
* <pre>{@code
* import org.openapitools.control.client.model.PodSpecMetadataConfig;
* ...
*
* PodSpecMetadataConfig metadataConfig =
* new PodSpecMetadataConfig()
* .fields(Arrays.asList("genre", "year"));
*
* client.createPodsIndex("YOUR-INDEX", 1536, "us-east4-gcp", "p1.x2", "cosine", metadataConfig);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param dimension The number of dimensions for the index.
* @param environment The cloud environment where you want the index to be hosted.
* @param podType The type of pod to use. A string with one of s1, p1, or p2 appended with a "." and one of x1, x2, x4, or x8.
* @param metric The metric type for the index. Must be one of "cosine", "euclidean", or "dotproduct".
* @param metadataConfig The configuration for the behavior of Pinecone's internal metadata index. By default, all metadata is indexed;
* when metadataConfig is present, only specified metadata fields are indexed.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createPodsIndex(String indexName, Integer dimension, String environment,
String podType, String metric, PodSpecMetadataConfig metadataConfig) {
return createPodsIndex(indexName, dimension, environment, podType, metric, null, null, null,
metadataConfig,null, "disabled", null);
}
/**
* Overload for creating a new pods index with environment, podType, metric, and sourceCollection.
* <p>
* Example:
* <pre>{@code
* client.createPodsIndex("YOUR-INDEX", 1536, "us-east4-gcp", "p1.x2", "cosine", "my-collection");
* }</pre>
*
* @param indexName The name of the index to be created.
* @param dimension The number of dimensions for the index.
* @param environment The cloud environment where you want the index to be hosted.
* @param podType The type of pod to use. A string with one of s1, p1, or p2 appended with a "." and one of x1, x2, x4, or x8.
* @param metric The metric type for the index. Must be one of "cosine", "euclidean", or "dotproduct".
* @param sourceCollection The name of the collection to be used as the source for the index. Collections are snapshots of an index at a point in time.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createPodsIndex(String indexName, Integer dimension, String environment,
String podType, String metric, String sourceCollection) {
return createPodsIndex(indexName, dimension, environment, podType, metric, null, null, null, null,
sourceCollection, "disabled", null);
}
/**
* Overload for creating a new pods index with environment, podType, and pods.
* <p>
* Example:
* <pre>{@code
* client.createPodsIndex("YOUR-INDEX", 1536, "us-east4-gcp", "p1.x2", "cosine", 6);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param dimension The number of dimensions for the index.
* @param environment The cloud environment where you want the index to be hosted.
* @param podType The type of pod to use. A string with one of s1, p1, or p2 appended with a "." and one of x1, x2, x4, or x8.
* @param pods The number of pods to be used in the index.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createPodsIndex(String indexName, Integer dimension, String environment,
String podType, Integer pods) {
return createPodsIndex(indexName, dimension, environment, podType, null, null, null, pods,
null, null, "disabled", null);
}
/**
* Overload for creating a new pods index with environment, podType, pods, and metadataConfig.
* <p>
* Example:
* <pre>{@code
* import org.openapitools.control.client.model.PodSpecMetadataConfig;
* ...
*
* PodSpecMetadataConfig metadataConfig =
* new PodSpecMetadataConfig()
* .fields(Arrays.asList("genre", "year"));
* client.createPodsIndex("YOUR-INDEX", 1536, "us-east4-gcp", "p1.x2", "cosine", 6);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param dimension The number of dimensions for the index.
* @param environment The cloud environment where you want the index to be hosted.
* @param podType The type of pod to use. A string with one of s1, p1, or p2 appended with a "." and one of x1, x2, x4, or x8.
* @param pods The number of pods to be used in the index.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createPodsIndex(String indexName, Integer dimension, String environment,
String podType, Integer pods,
PodSpecMetadataConfig metadataConfig) {
return createPodsIndex(indexName, dimension, environment, podType, null, null, null, pods, metadataConfig,
null, "disabled", null);
}
/**
* Overload for creating a new pods index with environment, podType, replicas, and shards.
* <p>
* Example:
* <pre>{@code
* client.createPodsIndex("YOUR-INDEX", 1536, "us-east4-gcp", "p1.x2", "cosine", 2, 2);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param dimension The number of dimensions for the index.
* @param environment The cloud environment where you want the index to be hosted.
* @param podType The type of pod to use. A string with one of s1, p1, or p2 appended with a "." and one of x1, x2, x4, or x8.
* @param replicas The number of replicas. Replicas duplicate your index. They provide higher availability and throughput and can be scaled.
* @param shards The number of shards. Shards split your data across multiple pods so you can fit more data into an index.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createPodsIndex(String indexName, Integer dimension, String environment,
String podType, Integer replicas,
Integer shards) {
return createPodsIndex(indexName, dimension, environment, podType, null, replicas, shards, null,
null, null, "disabled", null);
}
/**
* Overload for creating a new pods index with environment, podType, replicas, shards, and metadataConfig.
* <p>
* Example:
* <pre>{@code
* import org.openapitools.control.client.model.PodSpecMetadataConfig;
* ...
*
* PodSpecMetadataConfig metadataConfig =
* new PodSpecMetadataConfig()
* .fields(Arrays.asList("genre", "year"));
* client.createPodsIndex("YOUR-INDEX", 1536, "us-east4-gcp", "p1.x2", "cosine", 2, 2, metadataConfig);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param dimension The number of dimensions for the index.
* @param environment The cloud environment where you want the index to be hosted.
* @param podType The type of pod to use. A string with one of s1, p1, or p2 appended with a "." and one of x1, x2, x4, or x8.
* @param replicas The number of replicas. Replicas duplicate your index. They provide higher availability and throughput and can be scaled.
* @param shards The number of shards. Shards split your data across multiple pods so you can fit more data into an index.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createPodsIndex(String indexName, Integer dimension, String environment,
String podType, Integer replicas,
Integer shards, PodSpecMetadataConfig metadataConfig) {
return createPodsIndex(indexName, dimension, environment,
podType, null, replicas,
shards, null, metadataConfig,
null, "disabled", null);
}
/**
* Creates a new pods index with the specified parameters.
* <p>
* Example:
* <pre>{@code
* import org.openapitools.control.client.model.PodSpecMetadataConfig;
* ...
*
* PodSpecMetadataConfig metadataConfig =
* new PodSpecMetadataConfig()
* .fields(Arrays.asList("genre", "year"));
* client.createPodsIndex("YOUR-INDEX", 1536, "us-east4-gcp", "p1.x2", "cosine", 2, 2, 4, null, null, DeletionProtection.DISABLED);
* }</pre>
*
* @param indexName The name of the index to be created.
* @param dimension The number of dimensions for the index.
* @param environment The cloud environment where you want the index to be hosted.
* @param podType The type of pod to use. A string with one of s1, p1, or p2 appended with a "." and one of x1, x2, x4, or x8.
* @param metric The metric type for the index. Must be one of "cosine", "euclidean", or "dotproduct".
* @param replicas The number of replicas. Replicas duplicate your index. They provide higher availability and throughput and can be scaled.
* @param shards The number of shards. Shards split your data across multiple pods so you can fit more data into an index.
* @param pods The number of pods to be used in the index. This should be equal to shards x replicas.
* @param metadataConfig The configuration for the behavior of Pinecone's internal metadata index. By default, all metadata is indexed;
* when metadataConfig is present, only specified metadata fields are indexed.
* @param sourceCollection The name of the collection to be used as the source for the index. Collections are snapshots of an index at a point in time.
* @param deletionProtection Enable or disable deletion protection for the index.
* @param tags A map of tags to associate with the Index.
* @return {@link IndexModel} representing the created serverless index.
* @throws PineconeException if the API encounters an error during index creation or if any of the arguments are invalid.
*/
public IndexModel createPodsIndex(String indexName,
Integer dimension,
String environment,
String podType,
String metric,
Integer replicas,
Integer shards,
Integer pods,
PodSpecMetadataConfig metadataConfig,
String sourceCollection,
String deletionProtection,
Map<String, String> tags) throws PineconeException {
validatePodIndexParams(indexName, dimension, environment, podType, metric, replicas, shards, pods);
PodSpec podSpec = new PodSpec().environment(environment)
.podType(podType)
.replicas(replicas)
.shards(shards)
.pods(pods)
.metadataConfig(metadataConfig)
.sourceCollection(sourceCollection);
IndexSpec createIndexRequestSpec = new IndexSpec(new IndexSpecPodBased().pod(podSpec));
CreateIndexRequest createIndexRequest = new CreateIndexRequest()
.name(indexName)
.dimension(dimension)
.metric(metric)
.spec(createIndexRequestSpec)
.deletionProtection(deletionProtection);
if (tags != null && !tags.isEmpty()) {
createIndexRequest.tags(tags);
}
IndexModel indexModel = null;
try {
indexModel = manageIndexesApi.createIndex(Configuration.VERSION, createIndexRequest);
} catch (ApiException apiException) {
handleApiException(apiException);
}
return indexModel;
}
public static void validatePodIndexParams(String indexName, Integer dimension, String environment,
String podType, String metric,
Integer replicas, Integer shards, Integer pods) {
if (indexName == null || indexName.isEmpty()) {
throw new PineconeValidationException("indexName cannot be null or empty");
}
if (dimension == null) {
throw new PineconeValidationException("Dimension cannot be null");
}
if (dimension < 1) {
throw new PineconeValidationException("Dimension must be greater than 0. See limits for more info: https://docs.pinecone.io/reference/limits");
}
if (environment == null || environment.isEmpty()) {
throw new PineconeValidationException("Environment cannot be null or empty");
}
if (podType == null || podType.isEmpty()) {
throw new PineconeValidationException("podType cannot be null or empty");
}
if (metric != null && metric.isEmpty()) {
throw new PineconeValidationException("Metric cannot be null or empty. Must be cosine, euclidean, or dotproduct.");
}
if (replicas != null && replicas < 1) {
throw new PineconeValidationException("Number of replicas must be >= 1");
}
if (shards != null && shards < 1) {
throw new PineconeValidationException("Number of shards must be >= 1");
}
if (pods != null && pods < 1) {
throw new PineconeValidationException("Number of pods must be >= 1");
}
if (replicas != null && shards != null && pods != null && (replicas*shards != pods)) {
throw new PineconeValidationException("Number of pods does not equal number of shards times number of " +
"replicas"); }
}
/**
* Retrieves information about an existing index.
* <p>
* Example:
* <pre>{@code
* import org.openapitools.control.client.model.IndexModel;
* ...
*
* IndexModel indexModel = client.describeIndex("YOUR-INDEX");
* }</pre>
*
* @param indexName The name of the index to describe.
* @return {@link IndexModel} with the details of the index.
* @throws PineconeException if an error occurs during the operation or the index does not exist.
*/
public IndexModel describeIndex(String indexName) throws PineconeException {
IndexModel indexModel = null;
try {
indexModel = manageIndexesApi.describeIndex(Configuration.VERSION, indexName);
} catch (ApiException apiException) {
handleApiException(apiException);
}
return indexModel;
}
/**
* Configures an existing pod-based index with new settings.
* <p>
* Example:
* <pre>{@code
* import org.openapitools.control.client.model.IndexModel;
* ...
*
* // Make a configuration change
* IndexModel indexModel = client.configurePodsIndex("YOUR-INDEX", "p1.x2", 4, DeletionProtection.ENABLED);
*
* // Call describeIndex to see the index status as the change is applied.
* indexModel = client.describeIndex("YOUR-INDEX");
* }</pre>
*
* @param indexName The name of the index to configure.
* @param podType The new podType for the index. Can be null if not changing the pod type.
* @param replicas The desired number of replicas for the index, lowest value is 0. Can be null if not changing the number of replicas.
* @param deletionProtection Enable or disable deletion protection for the index.
* @param tags A map of tags to associate with the Index.
* @return {@link IndexModel} representing the configured index.
* @throws PineconeException if an error occurs during the operation, the index does not exist, or if any of the arguments are invalid.
*/
public IndexModel configurePodsIndex(String indexName,
String podType,
Integer replicas,
String deletionProtection,
Map<String, String> tags) throws PineconeException {
if (indexName == null || indexName.isEmpty()) {
throw new PineconeValidationException("indexName cannot be null or empty");
}
// If you pass a # replicas, but they're < 1, throw an exception
if (replicas != null) {
if (replicas < 1) {
throw new PineconeValidationException("Number of replicas must be >= 1");
}
}
// Build ConfigureIndexRequest object
ConfigureIndexRequest configureIndexRequest = new ConfigureIndexRequest()
.spec(new ConfigureIndexRequestSpec(
new ConfigureIndexRequestPodBased()
.pod(new ConfigureIndexRequestPodBasedConfig()
.replicas(replicas)
.podType(podType)
)
)).deletionProtection(deletionProtection);
if(tags != null && !tags.isEmpty()) {
configureIndexRequest.tags(tags);
}
IndexModel indexModel = null;
try {
indexModel = manageIndexesApi.configureIndex(Configuration.VERSION, indexName, configureIndexRequest);
} catch (ApiException apiException) {
handleApiException(apiException);
}
return indexModel;
}
/**
* Overload for configurePodsIndex to change the number of replicas and deletion protection for an index.
* <p>
* Example:
* <pre>{@code
* import org.openapitools.control.client.model.IndexModel;
* ...
*
* IndexModel indexModel = client.configurePodsIndex("YOUR-INDEX", 4, DeletionProtection.ENABLED);
* }</pre>
*
* @param indexName The name of the index.
* @param replicas The desired number of replicas for the index, lowest value is 0.
* @param deletionProtection Enable or disable deletion protection for the index.
* @return {@link IndexModel} of the configured index.
* @throws PineconeException if an error occurs during the operation, the index does not exist, or if the number of replicas is invalid.
*/
public IndexModel configurePodsIndex(String indexName, Integer replicas, String deletionProtection) throws PineconeException {
return configurePodsIndex(indexName, null, replicas, deletionProtection, null);
}
/**
* Overload for configurePodsIndex to only change the deletion protection of an index.
* <p>
* Example:
* <pre>{@code
* import org.openapitools.control.client.model.IndexModel;
* ...
*
* IndexModel indexModel = client.configurePodsIndex("YOUR-INDEX", DeletionProtection.ENABLED);
* }</pre>
*
* @param indexName The name of the index.
* @param deletionProtection Enable or disable deletion protection for the index.
* @return {@link IndexModel} of the configured index.
* @throws PineconeException if an error occurs during the operation, the index does not exist, or if the podType is invalid.
*/
public IndexModel configurePodsIndex(String indexName, String deletionProtection) throws PineconeException {
return configurePodsIndex(indexName, null, null, deletionProtection, null);
}
/**
* Configures an existing serverless index with deletion protection.
* <p>
* Example:
* <pre>{@code
* import org.openapitools.control.client.model.IndexModel;
* ...
*
* HashMap<String, String> tags = new HashMap<>();
* tags.put("env", "test);
*
* ConfigureIndexRequestEmbed embed = new ConfigureIndexRequestEmbed();
* embed.model("multilingual-e5-large");
* HashMap<String, String> fieldMap = new HashMap<>();
* fieldMap.put("text", "your-text-field");
* embed.fieldMap(fieldMap);
*
* // Make a configuration change
* IndexModel indexModel = client.configureServerlessIndex("YOUR-INDEX", DeletionProtection.ENABLED, tags, embed);
*
* // Call describeIndex to see the index status as the change is applied.
* indexModel = client.describeIndex("YOUR-INDEX");
* }</pre>
*
* @param indexName The name of the index to configure.
* @param deletionProtection Enable or disable deletion protection for the index.
* @param tags A map of tags to associate with the Index.