use crate::{RedisWrite, ToRedisArgs};
#[derive(Clone, Default)]
pub struct VSimOptions {
with_scores: bool,
count: Option<usize>,
search_exploration_factor: Option<usize>,
filter: Option<String>,
filter_max_effort: Option<usize>,
truth: bool,
no_thread: bool,
}
impl VSimOptions {
pub fn set_with_scores(mut self, enabled: bool) -> Self {
self.with_scores = enabled;
self
}
pub fn set_count(mut self, count: usize) -> Self {
self.count = Some(count);
self
}
pub fn set_search_exploration_factor(mut self, factor: usize) -> Self {
self.search_exploration_factor = Some(factor);
self
}
pub fn set_filter_expression<S: Into<String>>(mut self, expression: S) -> Self {
self.filter = Some(expression.into());
self
}
pub fn set_max_filtering_effort(mut self, effort: usize) -> Self {
self.filter_max_effort = Some(effort);
self
}
pub fn set_truth(mut self, enabled: bool) -> Self {
self.truth = enabled;
self
}
pub fn set_no_thread(mut self, enabled: bool) -> Self {
self.no_thread = enabled;
self
}
}
impl ToRedisArgs for VSimOptions {
fn write_redis_args<W>(&self, out: &mut W)
where
W: ?Sized + RedisWrite,
{
if self.with_scores {
out.write_arg(b"WITHSCORES");
}
if let Some(count) = self.count {
out.write_arg(b"COUNT");
out.write_arg_fmt(count);
}
if let Some(ef) = self.search_exploration_factor {
out.write_arg(b"EF");
out.write_arg_fmt(ef);
}
if let Some(ref filter) = self.filter {
out.write_arg(b"FILTER");
out.write_arg(filter.as_bytes());
}
if let Some(filter_ef) = self.filter_max_effort {
out.write_arg(b"FILTER-EF");
out.write_arg_fmt(filter_ef);
}
if self.truth {
out.write_arg(b"TRUTH");
}
if self.no_thread {
out.write_arg(b"NOTHREAD");
}
}
}
#[derive(Clone)]
#[non_exhaustive]
pub enum EmbeddingInput<'a> {
Float32(&'a [f32]),
Float64(&'a [f64]),
String(&'a [&'a str]),
}
impl ToRedisArgs for EmbeddingInput<'_> {
fn write_redis_args<W>(&self, out: &mut W)
where
W: ?Sized + RedisWrite,
{
match self {
EmbeddingInput::Float32(vector) => {
out.write_arg_fmt(vector.len());
for &f in *vector {
out.write_arg_fmt(f);
}
}
EmbeddingInput::Float64(vector) => {
out.write_arg_fmt(vector.len());
for &f in *vector {
out.write_arg_fmt(f);
}
}
EmbeddingInput::String(vector) => {
out.write_arg_fmt(vector.len());
for v in *vector {
v.write_redis_args(out);
}
}
}
}
}
#[derive(Clone)]
#[non_exhaustive]
pub enum VectorAddInput<'a> {
Fp32(&'a [f32]),
Values(EmbeddingInput<'a>),
}
impl ToRedisArgs for VectorAddInput<'_> {
fn write_redis_args<W>(&self, out: &mut W)
where
W: ?Sized + RedisWrite,
{
match self {
VectorAddInput::Fp32(vector) => {
use std::io::Write;
out.write_arg(b"FP32");
let mut writer = out.writer_for_next_arg();
for &f in *vector {
writer.write_all(&f.to_le_bytes()).unwrap();
}
}
VectorAddInput::Values(embedding_input) => {
out.write_arg(b"VALUES");
embedding_input.write_redis_args(out);
}
}
}
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum VectorQuantization {
NoQuant,
Q8,
Bin,
}
#[derive(Clone, Default)]
pub struct VAddOptions {
pub(crate) reduction_dimension: Option<usize>,
enable_check_and_set_style: bool,
vector_quantization: Option<VectorQuantization>,
build_exploration_factor: Option<usize>,
attributes: Option<serde_json::Value>,
max_number_of_links: Option<usize>,
}
impl VAddOptions {
pub fn set_reduction_dimension(mut self, dimension: usize) -> Self {
self.reduction_dimension = Some(dimension);
self
}
pub fn set_check_and_set_style(mut self, cas_enabled: bool) -> Self {
self.enable_check_and_set_style = cas_enabled;
self
}
pub fn set_quantization(mut self, vector_quantization: VectorQuantization) -> Self {
self.vector_quantization = Some(vector_quantization);
self
}
pub fn set_build_exploration_factor(mut self, build_exploration_factor: usize) -> Self {
self.build_exploration_factor = Some(build_exploration_factor);
self
}
pub fn set_attributes(mut self, attributes: serde_json::Value) -> Self {
self.attributes = Some(attributes);
self
}
pub fn set_max_number_of_links(mut self, max_number_of_links: usize) -> Self {
self.max_number_of_links = Some(max_number_of_links);
self
}
}
impl ToRedisArgs for VAddOptions {
fn write_redis_args<W>(&self, out: &mut W)
where
W: ?Sized + RedisWrite,
{
if self.enable_check_and_set_style {
out.write_arg(b"CAS");
}
if let Some(ref quantization) = self.vector_quantization {
match quantization {
VectorQuantization::NoQuant => out.write_arg(b"NOQUANT"),
VectorQuantization::Q8 => out.write_arg(b"Q8"),
VectorQuantization::Bin => out.write_arg(b"BIN"),
}
}
if let Some(exploration_factor) = self.build_exploration_factor {
out.write_arg(b"EF");
out.write_arg_fmt(exploration_factor);
}
if let Some(ref attrs) = self.attributes {
out.write_arg(b"SETATTR");
out.write_arg(&serde_json::to_vec(&attrs).unwrap());
}
if let Some(max_links) = self.max_number_of_links {
out.write_arg(b"M");
out.write_arg_fmt(max_links);
}
}
}
#[derive(Clone, Default)]
pub struct VEmbOptions {
raw_representation: bool,
}
impl VEmbOptions {
pub fn set_raw_representation(mut self, enabled: bool) -> Self {
self.raw_representation = enabled;
self
}
}
impl ToRedisArgs for VEmbOptions {
fn write_redis_args<W>(&self, out: &mut W)
where
W: ?Sized + RedisWrite,
{
if self.raw_representation {
out.write_arg(b"RAW");
}
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "vector-sets")))]
#[derive(Clone)]
#[non_exhaustive]
pub enum VectorSimilaritySearchInput<'a> {
Fp32(&'a [f32]),
Values(EmbeddingInput<'a>),
Element(&'a str),
}
impl ToRedisArgs for VectorSimilaritySearchInput<'_> {
fn write_redis_args<W>(&self, out: &mut W)
where
W: ?Sized + RedisWrite,
{
match self {
VectorSimilaritySearchInput::Fp32(vector) => {
use std::io::Write;
out.write_arg(b"FP32");
let mut writer = out.writer_for_next_arg();
for &f in *vector {
writer.write_all(&f.to_le_bytes()).unwrap();
}
}
VectorSimilaritySearchInput::Values(embedding_input) => {
out.write_arg(b"VALUES");
embedding_input.write_redis_args(out);
}
VectorSimilaritySearchInput::Element(element) => {
out.write_arg(b"ELE");
element.write_redis_args(out);
}
}
}
}