neomemx 0.1.2

A high-performance memory library for AI agents with semantic search
Documentation
//! Milvus vector store configuration

use serde::{Deserialize, Serialize};

/// Configuration for Milvus vector store
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MilvusConfig {
    /// Name of the collection
    #[serde(default = "default_collection_name")]
    pub collection_name: String,

    /// Host address for Milvus server
    #[serde(default = "default_host")]
    pub host: String,

    /// Port for Milvus REST API
    #[serde(default = "default_port")]
    pub port: u16,

    /// Database name
    #[serde(default = "default_database")]
    pub database: String,

    /// API key for Milvus Cloud (Zilliz)
    pub api_key: Option<String>,

    /// Dimension of vectors (must match your embedding model)
    #[serde(default = "default_dimension")]
    pub dimension: usize,

    /// Metric type for similarity search
    #[serde(default = "default_metric_type")]
    pub metric_type: MetricType,

    /// Index type for the collection
    #[serde(default = "default_index_type")]
    pub index_type: IndexType,

    /// Use secure connection (HTTPS)
    #[serde(default)]
    pub secure: bool,
}

/// Metric types for similarity search
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum MetricType {
    /// Inner product (for normalized vectors)
    Ip,
    /// L2/Euclidean distance
    #[default]
    L2,
    /// Cosine similarity
    Cosine,
}

impl MetricType {
    /// Get the string representation for Milvus API
    pub fn as_str(&self) -> &'static str {
        match self {
            MetricType::Ip => "IP",
            MetricType::L2 => "L2",
            MetricType::Cosine => "COSINE",
        }
    }
}

/// Index types for vector search
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum IndexType {
    /// Flat index (brute force, most accurate)
    Flat,
    /// IVF Flat index
    IvfFlat,
    /// HNSW index (default, good balance of speed/accuracy)
    #[default]
    Hnsw,
    /// Disk-based index
    DiskAnn,
    /// Auto index (let Milvus decide)
    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 {
    /// Get the base URL for REST API
    pub fn get_base_url(&self) -> String {
        let protocol = if self.secure { "https" } else { "http" };
        format!("{}://{}:{}", protocol, self.host, self.port)
    }

    /// Get API key from config or environment variable
    pub fn get_api_key(&self) -> Option<String> {
        self.api_key
            .clone()
            .or_else(|| std::env::var("MILVUS_API_KEY").ok())
    }
}