Skip to content

Commit

Permalink
WIP: Using Typebuilder to create simple struct params for rust client
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamdavidonuh committed Nov 29, 2024
1 parent a3aa36c commit 9cc3730
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 71 deletions.
21 changes: 21 additions & 0 deletions ahnlich/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ahnlich/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ ai = { path = "../ai", version = "*" }
pretty_assertions.workspace = true
ndarray.workspace = true
utils = { path = "../utils", version = "*" }
typed-builder = "0.20.0"
133 changes: 62 additions & 71 deletions ahnlich/client/src/ai.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::builders::ai as ai_params;
use crate::conn::{AIConn, Connection};
use crate::error::AhnlichError;
use crate::prelude::*;
Expand Down Expand Up @@ -211,148 +212,138 @@ impl AIClient {
))
}

#[allow(clippy::too_many_arguments)]
pub async fn create_store(
&self,
store: StoreName,
query_model: AIModel,
index_model: AIModel,
predicates: HashSet<MetadataKey>,
non_linear_indices: HashSet<NonLinearAlgorithm>,
error_if_exists: bool,
store_original: bool,
tracing_id: Option<String>,
store_params: ai_params::CreateStoreParams,
) -> Result<AIServerResponse, AhnlichError> {
self.exec(
AIQuery::CreateStore {
store,
query_model,
index_model,
predicates,
non_linear_indices,
error_if_exists,
store_original,
store: store_params.store,
query_model: store_params.query_model,
index_model: store_params.index_model,
predicates: store_params.predicates,
non_linear_indices: store_params.non_linear_indices,
error_if_exists: store_params.error_if_exists,
store_original: store_params.store_original,
},
tracing_id,
store_params.tracing_id,
)
.await
}

pub async fn get_pred(
&self,
store: StoreName,
condition: PredicateCondition,
tracing_id: Option<String>,
params: ai_params::GetPredParams,
) -> Result<AIServerResponse, AhnlichError> {
self.exec(AIQuery::GetPred { store, condition }, tracing_id)
.await
self.exec(
AIQuery::GetPred {
store: params.store,
condition: params.condition,
},
params.tracing_id,
)
.await
}

pub async fn get_sim_n(
&self,
store: StoreName,
search_input: StoreInput,
condition: Option<PredicateCondition>,
closest_n: NonZeroUsize,
algorithm: Algorithm,
tracing_id: Option<String>,
params: ai_params::GetSimNParams,
) -> Result<AIServerResponse, AhnlichError> {
self.exec(
AIQuery::GetSimN {
store,
search_input,
condition,
closest_n,
algorithm,
store: params.store,
search_input: params.search_input,
condition: params.condition,
closest_n: params.closest_n,
algorithm: params.algorithm,
},
tracing_id,
params.tracing_id,
)
.await
}

pub async fn create_pred_index(
&self,
store: StoreName,
predicates: HashSet<MetadataKey>,
tracing_id: Option<String>,
params: ai_params::CreatePredIndexParams,
) -> Result<AIServerResponse, AhnlichError> {
self.exec(AIQuery::CreatePredIndex { store, predicates }, tracing_id)
.await
self.exec(
AIQuery::CreatePredIndex {
store: params.store,
predicates: params.predicates,
},
params.tracing_id,
)
.await
}

pub async fn create_non_linear_algorithm_index(
&self,
store: StoreName,
non_linear_indices: HashSet<NonLinearAlgorithm>,
tracing_id: Option<String>,
params: ai_params::CreateNonLinearAlgorithmIndexParams,
) -> Result<AIServerResponse, AhnlichError> {
self.exec(
AIQuery::CreateNonLinearAlgorithmIndex {
store,
non_linear_indices,
store: params.store,
non_linear_indices: params.non_linear_indices,
},
tracing_id,
params.tracing_id,
)
.await
}

pub async fn drop_pred_index(
&self,
store: StoreName,
predicates: HashSet<MetadataKey>,
error_if_not_exists: bool,
tracing_id: Option<String>,
params: ai_params::DropPredIndexParams,
) -> Result<AIServerResponse, AhnlichError> {
self.exec(
AIQuery::DropPredIndex {
store,
predicates,
error_if_not_exists,
store: params.store,
predicates: params.predicates,
error_if_not_exists: params.error_if_not_exists,
},
tracing_id,
params.tracing_id,
)
.await
}

pub async fn set(
&self,
store: StoreName,
inputs: Vec<(StoreInput, StoreValue)>,
preprocess_action: PreprocessAction,
tracing_id: Option<String>,
params: ai_params::SetParams,
) -> Result<AIServerResponse, AhnlichError> {
self.exec(
AIQuery::Set {
store,
inputs,
preprocess_action,
store: params.store,
inputs: params.inputs,
preprocess_action: params.preprocess_action,
},
tracing_id,
params.tracing_id,
)
.await
}

pub async fn del_key(
&self,
store: StoreName,
key: StoreInput,
tracing_id: Option<String>,
params: ai_params::DelKeyParams,
) -> Result<AIServerResponse, AhnlichError> {
self.exec(AIQuery::DelKey { store, key }, tracing_id).await
self.exec(
AIQuery::DelKey {
store: params.store,
key: params.key,
},
params.tracing_id,
)
.await
}

pub async fn drop_store(
&self,
store: StoreName,
error_if_not_exists: bool,
tracing_id: Option<String>,
params: ai_params::DropStoreParams,
) -> Result<AIServerResponse, AhnlichError> {
self.exec(
AIQuery::DropStore {
store,
error_if_not_exists,
store: params.store,
error_if_not_exists: params.error_if_not_exists,
},
tracing_id,
params.tracing_id,
)
.await
}
Expand Down
129 changes: 129 additions & 0 deletions ahnlich/client/src/builders/ai.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use std::{collections::HashSet, num::NonZeroUsize};
use typed_builder::TypedBuilder;

use ahnlich_types::{
ai::{AIModel, PreprocessAction},
keyval::{StoreInput, StoreName, StoreValue},
metadata::MetadataKey,
predicate::PredicateCondition,
similarity::{Algorithm, NonLinearAlgorithm},
};

#[derive(TypedBuilder)]
pub struct CreateStoreParams {
#[builder(setter(into, transform = |s: String| StoreName(s)))]
pub store: StoreName,

#[builder(default = AIModel::AllMiniLML6V2)]
pub query_model: AIModel,

#[builder(default = AIModel::AllMiniLML6V2)]
pub index_model: AIModel,

#[builder(default = HashSet::new())]
pub predicates: HashSet<MetadataKey>,

#[builder(default = HashSet::new())]
pub non_linear_indices: HashSet<NonLinearAlgorithm>,

#[builder(default = true)]
pub error_if_exists: bool,

#[builder(default = true)]
pub store_original: bool,

pub tracing_id: Option<String>,
}

#[derive(TypedBuilder)]
pub struct GetPredParams {
#[builder(setter(into, transform = |s: String| StoreName(s)))]
pub store: StoreName,
pub condition: PredicateCondition,
pub tracing_id: Option<String>,
}

#[derive(TypedBuilder)]
pub struct GetSimNParams {
#[builder(setter(into, transform = |s: String| StoreName(s)))]
pub store: StoreName,
pub search_input: StoreInput,
pub condition: Option<PredicateCondition>,

#[builder(default=NonZeroUsize::new(1).unwrap())]
pub closest_n: NonZeroUsize,

#[builder(default=Algorithm::CosineSimilarity)]
pub algorithm: Algorithm,

pub tracing_id: Option<String>,
}

#[derive(TypedBuilder)]
pub struct CreatePredIndexParams {
#[builder(setter(into, transform = |s: String| StoreName(s)))]
pub store: StoreName,

//#[builder(default = HashSet::new())]
pub predicates: HashSet<MetadataKey>,

pub tracing_id: Option<String>,
}

#[derive(TypedBuilder)]
pub struct CreateNonLinearAlgorithmIndexParams {
#[builder(setter(into, transform = |s: String| StoreName(s)))]
pub store: StoreName,

#[builder(default = HashSet::from_iter(&[NonLinearAlgorithm::KDTree]))]
pub non_linear_indices: HashSet<NonLinearAlgorithm>,

pub tracing_id: Option<String>,
}

#[derive(TypedBuilder)]
pub struct DropPredIndexParams {
#[builder(setter(into, transform = |s: String| StoreName(s)))]
pub store: StoreName,

//#[builder(default = HashSet::new())]
pub predicates: HashSet<MetadataKey>,

#[builder(default = true)]
pub error_if_not_exists: bool,

pub tracing_id: Option<String>,
}

#[derive(TypedBuilder)]
pub struct SetParams {
#[builder(setter(into, transform = |s: String| StoreName(s)))]
pub store: StoreName,

pub inputs: Vec<(StoreInput, StoreValue)>,

#[builder(default = PreprocessAction::NoPreprocessing)]
pub preprocess_action: PreprocessAction,

pub tracing_id: Option<String>,
}

#[derive(TypedBuilder)]
pub struct DelKeyParams {
#[builder(setter(into, transform = |s: String| StoreName(s)))]
pub store: StoreName,

pub key: StoreInput,
pub tracing_id: Option<String>,
}

#[derive(TypedBuilder)]
pub struct DropStoreParams {
#[builder(setter(into, transform = |s: String| StoreName(s)))]
pub store: StoreName,

#[builder(default = true)]
pub error_if_not_exists: bool,

pub tracing_id: Option<String>,
}
Empty file.
2 changes: 2 additions & 0 deletions ahnlich/client/src/builders/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod ai;
pub mod db;
1 change: 1 addition & 0 deletions ahnlich/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
//! let results = pipeline.exec().await.unwrap();
//! ```
pub mod ai;
pub mod builders;
pub mod conn;
pub mod db;
pub mod error;
Expand Down

0 comments on commit 9cc3730

Please sign in to comment.