pinecone-sdk 0.1.0

Pinecone Rust SDK
Documentation
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
# Pinecone Rust SDK

This SDK is still in an alpha state. While it is mostly built out and functional, it may undergo changes as we continue to improve the UX. Please try it out and give us your feedback, but be aware that updates may introduce breaking changes.

## Documentation

[reference the documentation here]

## Prerequisites

Rust version: tested with Rust version 1.78.0

Before you can use the Pinecone SDK, you must sign up for an account and find your API key in the Pinecone console dashboard at [https://app.pinecone.io](https://app.pinecone.io).

## Installation

How to install - instruction for getting the package from crates.io

## Usage

The `PineconeClient` class is the main point of entry into the Rust SDK. Parameters may either be directly passed in as `Options`, or read through environment variables as follows. More detail:
- The API key must be passed in either as an argument or as an environment variable called `PINECONE_API_KEY`. If passed in as `None`, the client will attempt to read in an environment variable value.
- The control plane host, if passed in as `None`, will attempt to read in an environment variable called `PINECONE_CONTROLLER_HOST`. If it is not an environment variable, it will default to `https://api.pinecone.io`.

There are a couple of ways to instantiate the client, detailed below:
### PineconeClientConfig
Initialize a `PineconeClientConfig` struct with parameters, and call `config` using the struct.
```rust
use pinecone_sdk::pinecone::{PineconeClient, PineconeClientConfig};

let config = PineconeClientConfig {
    api_key: Some("INSERT_API_KEY".to_string()),
    control_plane_host: Some("INSERT_CONTROLLER_HOST".to_string()),
    ..Default::default()
};

let pinecone: PineconeClient = config.client().expect("Failed to create Pinecone instance");
```

### Default client
Use the `default_client()` function, which is the equivalent of constructing a `PineconeClientConfig` struct with all fields set to `None`. The API key and control plane host (optional) will be read from environment variables.
```rust
let pinecone: PineconeClient = pinecone_sdk::pinecone::default_client().expect("Failed to create Pinecone instance");
```

# Indexes

## Create Index

### Create serverless index
The following example creates a serverless index in the `us-east-1` region of AWS. For more information on serverless and regional availability, see [Understanding indexes](https://docs.pinecone.io/guides/indexes/understanding-indexes#serverless-indexes)
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::control::{Metric, Cloud, WaitPolicy, IndexModel};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;
 
let index_description: IndexModel = pinecone.create_serverless_index(
    "index-name",       // Name of the index
    10,                 // Dimension of the vectors
    Metric::Cosine,     // Distance metric
    Cloud::Aws,         // Cloud provider
    "us-east-1",        // Region
    WaitPolicy::NoWait  // Timeout
).await?;
```

### Create pod index
The following example creates a pod index in the `us-east-1` region of AWS.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::control::{Metric, Cloud, WaitPolicy, IndexModel};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let index_description: IndexModel = pinecone.create_pod_index(
    "index-name",       // Index name
    10,                 // Dimension
    Metric::Cosine,     // Distance metric
    "us-east-1",        // Region
    "p1.x1",            // Pod type
    1,                  // Number of pods
    None,               // Number of replicas
    None,               // Number of shards
    None,               // Metadata to index
    None,               // Source collection
    WaitPolicy::NoWait  // Wait policy
).await?;
```

Pod indexes support several optional configuration fields. The following example constructs a pod index with some specification for these fields.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::control::{Metric, Cloud, WaitPolicy, IndexModel};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let index_description: IndexModel = pinecone.create_pod_index(
    "index-name",       // Index name
    10,                 // Dimension
    Metric::Cosine,     // Distance metric
    "us-east-1",        // Region
    "p1.x1",            // Pod type
    1,                  // Number of pods
    Some(1),            // Number of replicas
    Some(1),            // Number of shards
    Some(               // Metadata fields to index
        &vec!["genre",
            "title",
            "imdb_rating"]),
    Some("collection"), // Source collection
    WaitPolicy::NoWait  // Wait policy
).await?;
```

## List indexes
The following example lists all indexes in your project.
```rust
use pinecone_sdk::pinecone::{PineconeClientConfig, control::IndexList};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let index_list: IndexList = pinecone.list_indexes().await?;
```

## Describe index
The following example returns information about the index `index-name`.
```rust
use pinecone_sdk::pinecone::{PineconeClientConfig, control::IndexModel};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let index_description: IndexModel = pinecone.describe_index("index-name").await?;
```

## Configure index
Configuring an index takes in three optional parameters -- a DeletionProtection enum, the number of replicas, and the pod type. The deletion protection can be updated for any index type, while the number of replicas and the pod type can only be updated for pod indexes.

The following example disables deletion protection for the index `index-name`.
```rust
use pinecone_sdk::pinecone::{PineconeClientConfig, control::IndexModel};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let index_description: IndexModel = pinecone.configure_index("index-name", Some(DeletionProtection::Disabled), None, None).await?;
```

The following example changes the index `index-name` to have 6 replicas and pod type `s1`. The deletion protection type will not be changed in this case.
```rust
use pinecone_sdk::pinecone::{PineconeClientConfig, control::IndexModel};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;
    
let index_description: IndexModel = pinecone.configure_index("index-name", None, Some(6), Some("s1")).await?;
```

## Delete index
The following example deletes the index `index-name`.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;
    
pinecone.delete_index("index-name").await?;
```

## Describe index statistics
The following example returns statistics about the index with host `index-host`.
Without filter
```rust
use std::collections::BTreeMap;
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::data::DescribeIndexStatsResponse;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut index = pinecone.index("index-host").await?;

let response: DescribeIndexStatsResponse = index.describe_index_stats(None).await?;
```

With filter
```rust
use std::collections::BTreeMap;
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::data::{Value, Kind, Metadata, DescribeIndexStatsResponse};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut index = pinecone.index("index-host").await?;

let mut fields = BTreeMap::new();
let kind = Some(Kind::StringValue("value".to_string()));
fields.insert("field".to_string(), Value { kind });

let response: DescribeIndexStatsResponse = index.describe_index_stats(Some(Metadata { fields })).await?;
```

## Upsert vectors
The following example upserts two vectors into the index with host `index-host`.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::data::{Vector, UpsertResponse};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut index = pinecone.index("index-host").await?;

let vectors = [Vector {
    id: "id1".to_string(),
    values: vec![1.0, 2.0, 3.0, 4.0],
    sparse_values: None,
    metadata: None,
}, Vector {
    id: "id2".to_string(),
    values: vec1![2.0, 3.0, 4.0, 5.0],
    sparse_values: None,
    metadata: None,
}];

let response: UpsertResponse = index.upsert(&vectors, &"namespace".into()).await?;
```

## Query vectors
There are two supported ways of querying an index.
### Query by index
The following example queries the index with host `index-host` for the vector with ID `vector-id`, and returns the top 10 matches.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::data::{Namespace, QueryResponse};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

// Connect to index at host "index-host"
let mut index = pinecone.index("index-host").await?;

// Query the vector with id "vector-id" in the namespace "namespace"
let response: QueryResponse = index.query_by_id(
    "vector-id".to_string(),
    10,
    &Namespace::default(),
    None,
    None,
    None
).await?;
```

### Query by value
The following example queries the index with host `index-host` for the vector with values `[1.0, 2.0, 3.0, 4.0]`, and returns the top 10 matches.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::data::{Namespace, QueryResponse};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut index = pinecone.index("index-host").await?;

let vector = vec![1.0, 2.0, 3.0, 4.0];

let response: QueryResponse = index.query_by_value(
    vector,
    None,
    10,
    &Namespace::default(),
    None,
    None,
    None
).await?;
```

## Delete vectors
There are three supported ways of deleting vectors.
### Delete by ID
The following example deletes the vector with ID `vector-id` in the namespace `namespace`.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut index = pinecone.index("index-host").await?;

let ids = ["vector-id"]

index.delete_by_id(&ids, &"namespace".into()).await?;
```

### Delete by filter:
The following example deletes vectors that satisfy the filter in the namespace `namespace`.
```rust
use std::collections::BTreeMap;
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::data::{Metadata, Value, Kind, Namespace};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut fields = BTreeMap::new();
let kind = Some(Kind::StringValue("value".to_string()));
fields.insert("field".to_string(), Value { kind });

index.delete_by_filter(Metadata { fields }, &"namespace".into()).await?;
```

### Delete all:
The following example deletes all vectors in the namespace `namespace`.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut index = pinecone.index("index-host").await?;

index.delete_all(&"namespace".into()).await?;
```

## Fetch vectors
The following example fetches the vectors with IDs `1` and `2` from the default namespace.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::data::FetchResponse;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut index = pinecone.index("index-host").await?;

let vectors = &["1", "2"];

let response: FetchResponse = index.fetch(vectors, &Default::default()).await?;
```

## Update vectors
The following example updates the vector with ID `vector-id` in the namespace `namespace` to have values `[1.0, 2.0, 3.0, 4.0]`.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::data::UpdateResponse;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut index = pinecone.index("index-host").await?;

let response: UpdateResponse = index.update("vector-id", vec![1.0, 2.0, 3.0, 4.0], None, None, &"namespace".into()).await?;
```

## List vectors
The following example lists vectors in the namespace `namespace`.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::data::{Namespace, ListResponse};

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let mut index = pinecone.index("index-host").await?;

let response: ListResponse = index.list(&"namespace".into(), None, None, None).await?;
```

# Collections
## Create collection
The following example creates a collection `collection-name` in the index `index-name`.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::control::CollectionModel;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let collection: CollectionModel = pinecone.create_collection("collection-name", "index-name").await?;
```

## List collections
The following example lists all collections in a project.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::control::CollectionList;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let collection_list: CollectionList = pinecone.list_collections().await?;
```

## Describe collection
The following example describes the collection `collection-name`.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;
use pinecone_sdk::pinecone::control::CollectionModel;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

let collection: CollectionModel = pinecone.describe_collection("collection-name").await?;
```

## Delete collection
The following example deletes the collection `collection-name`.
```rust
use pinecone_sdk::pinecone::PineconeClientConfig;

let config = PineconeClientConfig {
    api_key: Some('<<PINECONE_API_KEY>>'),
    ..Default::default()
};
let pinecone = config.client()?;

pinecone.delete_collection("collection-name").await?;
```

# Contributing
If you'd like to make a contribution, or get setup locally to develop the Pinecone Rust client, please see our [contributing guide](https://github.com/pinecone-io/pinecone-rust-client/blob/emily/update-readme/CONTRIBUTING.md)