Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create serverless index functionality #3

Merged
merged 32 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e6d9537
added source tag cleaning
ericapywang Jun 4, 2024
7d4ea0e
added get_user_string function
ericapywang Jun 5, 2024
bbf12b9
added create_serverless_index function
ericapywang Jun 6, 2024
0bc7db9
added some notes for future
ericapywang Jun 6, 2024
11530db
Merge remote-tracking branch 'origin/main' into ericapywang/create-index
ericapywang Jun 7, 2024
35fbed4
fixed more merge conflicts and tests
ericapywang Jun 7, 2024
d0689b0
started error checking
ericapywang Jun 7, 2024
c48956b
Add error handling to create_index
emily-emily Jun 7, 2024
0b177db
combined create_index for serverless and pods into one function, crea…
ericapywang Jun 7, 2024
cd0dfcb
add builder pattern for CreateIndexParams
ericapywang Jun 10, 2024
901f0d5
added some error checking + more tests
ericapywang Jun 10, 2024
1bc27f1
added builder and more tests for Pinecone
ericapywang Jun 10, 2024
91c885c
added some comments
ericapywang Jun 10, 2024
099b0d9
Update params builder required arg validation
emily-emily Jun 10, 2024
c608955
merge with main
ericapywang Jun 11, 2024
a84154c
remove builder for CreateIndexParams
ericapywang Jun 11, 2024
8c289ea
change builder function names
ericapywang Jun 11, 2024
3c62d01
fixed test cases
ericapywang Jun 11, 2024
a9e2ebd
Merge branch 'main' into ericapywang/create-index
ericapywang Jun 12, 2024
7b64788
Implement Into trait to convert to openapi types
emily-emily Jun 12, 2024
9b620d4
Merge branch 'main' into ericapywang/create-index
emily-emily Jun 12, 2024
ef7870f
Add missing documentation
emily-emily Jun 12, 2024
a40712f
changed Pinecone to PineconeClient
ericapywang Jun 13, 2024
357ea5a
flattened PineconeClient fields, removed builder
ericapywang Jun 13, 2024
d5bf06e
removed parameter struct
ericapywang Jun 13, 2024
e3ecb04
Use Option for cloud and use defaults
emily-emily Jun 13, 2024
2035017
remove redundant test
ericapywang Jun 13, 2024
34871af
remove Option for Cloud and Metric
ericapywang Jun 13, 2024
cadf7b2
Condense control code
emily-emily Jun 13, 2024
24619e6
pass api key to config
ericapywang Jun 13, 2024
90e3254
changed api_key_opt to api_key_str
ericapywang Jun 13, 2024
e0accd4
fixed config construction in PineconeClient
ericapywang Jun 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 222 additions & 0 deletions pinecone_sdk/src/control/create_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
use crate::models::create_index_request_params::{CreateIndexParams, Spec, Metric, Cloud};
use crate::pinecone::Pinecone;
use crate::utils::errors::PineconeError;
use openapi::models::create_index_request;
use openapi::models::serverless_spec;
use openapi::models::{CreateIndexRequest, CreateIndexRequestSpec, IndexModel, ServerlessSpec};

impl Pinecone {
// Creates a new index based on the provided parameters
pub async fn create_index(
&self,
params: CreateIndexParams
) -> Result<IndexModel, PineconeError> {

// Check if creating serverless or pod-based index and call respective builder function
match params.spec {
Spec::Serverless { cloud, region } => {
self.create_serverless_index(
params.name,
params.dimension,
params.metric,
cloud,
region
).await
}
Spec::Pod {
environment: _,
replicas: _,
shards: _,
pod_type: _,
pods: _,
metadata_config: _,
source_collection: _,
} => {
// eventually change this to be pod index
self.create_serverless_index(params.name, params.dimension, params.metric, Cloud::Aws, "".to_string()).await
}
}
}

// Creates serverless index
async fn create_serverless_index(
&self,
name: String,
dimension: u32,
metric: Metric,
cloud: Cloud,
region: String
) -> Result<IndexModel, PineconeError> {

// clean metric enum
let metric_enum = match metric {
Metric::Cosine => Some(create_index_request::Metric::Cosine),
Metric::Dotproduct => Some(create_index_request::Metric::Dotproduct),
Metric::Euclidean => Some(create_index_request::Metric::Euclidean),
};

// clean cloud enum
let cloud_enum = match cloud {
Cloud::Aws => serverless_spec::Cloud::Aws,
Cloud::Gcp => serverless_spec::Cloud::Gcp,
Cloud::Azure => serverless_spec::Cloud::Azure,
};

// create request specs
let create_index_request_spec = CreateIndexRequestSpec {
serverless: Some(Box::new(ServerlessSpec {
cloud: cloud_enum,
region,
})),
pod: None,
};

let create_index_request = CreateIndexRequest {
name,
dimension: dimension.try_into().unwrap(),
metric: metric_enum,
spec: Some(Box::new(create_index_request_spec)),
};

match openapi::apis::manage_indexes_api::create_index(
&self.openapi_config(),
create_index_request,
)
.await
{
Ok(index) => Ok(index),
Err(e) => Err(PineconeError::CreateIndexError { openapi_error: e }),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use core::panic;
use mockito::mock;
use serial_test::serial;
use tokio;

#[tokio::test]
#[serial]
async fn test_create_serverless_index() {
let _m = mock("POST", "/indexes")
.with_status(201)
.with_header("content-type", "application/json")
.with_body(
r#"
{
"name": "index_name",
"dimension": 10,
"metric": "cosine",
"host": "host1",
"spec": {
"serverless": {
"cloud": "aws",
"region": "us-east-1"
}
},
"status": {
"ready": true,
"state": "Initializing"
}
}
"#,
)
.create();

let pinecone = Pinecone::new(
Some("api_key".to_string()),
Some(mockito::server_url()),
None,
None,
);
let name = "index_name".to_string();
let dimension = 10;

let create_index_request = pinecone.unwrap().create_serverless_index(
name,
dimension,
Metric::Cosine,
Cloud::Aws,
"us-east-1".to_string()
).await;
assert!(create_index_request.is_ok());

let create_index_req = create_index_request.unwrap();
assert_eq!(create_index_req.name, "index_name");
assert_eq!(create_index_req.dimension, 10);
assert_eq!(create_index_req.metric, openapi::models::index_model::Metric::Cosine);

let spec = create_index_req.spec.serverless.unwrap();
assert_eq!(spec.cloud, openapi::models::serverless_spec::Cloud::Aws);
assert_eq!(spec.region, "us-east-1");
}

#[tokio::test]
async fn test_create_index_serverless() {
// Create a mock server
let _m = mock("POST", "/indexes")
.with_status(201)
.with_header("content-type", "application/json")
.with_body(
r#"
{
"name": "index_name",
"dimension": 10,
"metric": "euclidean",
"host": "host1",
"spec": {
"serverless": {
"cloud": "aws",
"region": "us-east-1"
}
},
"status": {
"ready": true,
"state": "Initializing"
}
}
"#,
)
.create();

let pinecone = Pinecone::new(
Some("api_key".to_string()),
Some(mockito::server_url()),
None,
None,
);
let params = CreateIndexParams {
name: "index_name".to_string(),
dimension: 10,
metric: Metric::Euclidean,
spec: Spec::Serverless {
cloud: Cloud::Aws,
region: "us-east-1".to_string(),
},
};

let result = pinecone.unwrap().create_index(params).await;

match result {
Ok(index) => {
assert_eq!(index.name, "index_name");
assert_eq!(index.dimension, 10);
assert_eq!(
index.metric,
openapi::models::index_model::Metric::Euclidean
);
let spec = *index.spec;
let serverless_spec = spec.serverless.unwrap();
assert_eq!(
serverless_spec.cloud,
openapi::models::serverless_spec::Cloud::Aws
);
assert_eq!(serverless_spec.region, "us-east-1");
}
Err(e) => panic!("{}", e),
}
}
}
3 changes: 0 additions & 3 deletions pinecone_sdk/src/control/list_indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ impl Pinecone {
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use crate::control::list_indexes::models::index_model::Metric;
use mockito::mock;
use openapi::apis::configuration::ApiKey;
use openapi::apis::configuration::Configuration;
use openapi::models::IndexList;
use openapi::models::IndexModel;
use tokio;
Expand Down
5 changes: 2 additions & 3 deletions pinecone_sdk/src/control/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod list_indexes;

pub use list_indexes::*;
pub mod create_index;
pub mod list_indexes;
1 change: 1 addition & 0 deletions pinecone_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod config;
pub mod control;
pub mod pinecone;
pub mod utils;
pub mod models;
Loading