Get Sim N
Performs a similarity search in a vector store, retrieving the top-N most similar embeddings to a given query vector. This operation is the core of the AI clientβs retrieval capability, enabling semantic search, recommendation, and nearest-neighbor lookups.
Source Code Exampleβ
Click to expand
use ahnlich_client_rs::ai::AiClient;
use ahnlich_client_rs::error::AhnlichError;
use ahnlich_types::ai::preprocess::PreprocessAction;
use ahnlich_types::ai::query::GetSimN;
use ahnlich_types::algorithm::algorithms::Algorithm;
use ahnlich_types::keyval::StoreInput;
use ahnlich_types::keyval::store_input::Value;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), AhnlichError> {
// Connect to AI server
let addr = "127.0.0.1:1370";
let client = AiClient::new(addr.to_string()).await?;
// Prepare the search input
let search_input = StoreInput {
value: Some(Value::RawString("example query".into())),
};
// Construct GetSimN parameters
let params = GetSimN {
store: "Main0".to_string(),
search_input: Some(search_input),
closest_n: 3, // number of similar entries to retrieve
algorithm: Algorithm::CosineSimilarity as i32,
execution_provider: None,
preprocess_action: PreprocessAction::NoPreprocessing as i32,
condition: None,
model_params: HashMap::new(),
};
// Run the GetSimN command
let res = client.get_sim_n(params, None).await?;
println!("GetSimN result: {:?}", res);
Ok(())
}
Parametersβ
-
params: GetSimNβ The query input, including the target vector and configuration such as the number of neighbors (N) to return and optional filters.model_params: HashMap<String, String>β Optional runtime parameters for the AI model. For face detection models (Buffalo_L, SFace+YuNet), supports"confidence_threshold"to control minimum detection confidence. Pass an emptyHashMapto use model defaults. See Model Parameters for details.
-
tracing_id: Option<String>β Optional trace parent ID for distributed observability across services.
Returnsβ
-
Ok(GetSimNResult)β Contains the ranked list of the top-N closest embeddings along with their similarity scores and associated metadata. -
Err(AhnlichError)β Returned when the query fails, parameters are invalid, or the service encounters an error.
Behavior (explains the code, brief)β
-
Wraps the
GetSimNquery in atonic::Request. -
Adds trace propagation metadata if provided.
-
Sends the request to the AI serviceβs
get_sim_nRPC method. -
Waits for the response and unwraps the result.
-
Returns a
GetSimNResultwith the similarity matches.