use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MilvusConfig {
#[serde(default = "default_collection_name")]
pub collection_name: String,
#[serde(default = "default_host")]
pub host: String,
#[serde(default = "default_port")]
pub port: u16,
#[serde(default = "default_database")]
pub database: String,
pub api_key: Option<String>,
#[serde(default = "default_dimension")]
pub dimension: usize,
#[serde(default = "default_metric_type")]
pub metric_type: MetricType,
#[serde(default = "default_index_type")]
pub index_type: IndexType,
#[serde(default)]
pub secure: bool,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum MetricType {
Ip,
#[default]
L2,
Cosine,
}
impl MetricType {
pub fn as_str(&self) -> &'static str {
match self {
MetricType::Ip => "IP",
MetricType::L2 => "L2",
MetricType::Cosine => "COSINE",
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum IndexType {
Flat,
IvfFlat,
#[default]
Hnsw,
DiskAnn,
Autoindex,
}
fn default_collection_name() -> String {
"neomemx".to_string()
}
fn default_host() -> String {
"localhost".to_string()
}
fn default_port() -> u16 {
19530
}
fn default_database() -> String {
"default".to_string()
}
fn default_dimension() -> usize {
768
}
fn default_metric_type() -> MetricType {
MetricType::Cosine
}
fn default_index_type() -> IndexType {
IndexType::Autoindex
}
impl Default for MilvusConfig {
fn default() -> Self {
Self {
collection_name: default_collection_name(),
host: default_host(),
port: default_port(),
database: default_database(),
api_key: None,
dimension: default_dimension(),
metric_type: default_metric_type(),
index_type: default_index_type(),
secure: false,
}
}
}
impl MilvusConfig {
pub fn get_base_url(&self) -> String {
let protocol = if self.secure { "https" } else { "http" };
format!("{}://{}:{}", protocol, self.host, self.port)
}
pub fn get_api_key(&self) -> Option<String> {
self.api_key
.clone()
.or_else(|| std::env::var("MILVUS_API_KEY").ok())
}
}