Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate type definitions for Client Libraries #27

Merged
merged 20 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ahnlich/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ members = [
"db",
"types",
"client",
"tracer",
"tracer",
"typegen",
]
resolver = "2"

Expand Down
1 change: 0 additions & 1 deletion ahnlich/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ tracer = { path = "../tracer", version = "*" }


[dev-dependencies]
loom = "0.7.2"
reqwest = "0.12.4"
serde_json = "1.0.116"
tokio = { version = "1.37.0", features = ["io-util", "sync"] }
Expand Down
14 changes: 7 additions & 7 deletions ahnlich/db/src/algorithm/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<'a> MinHeap<'a> {
self.heap.pop().map(|popped_item| popped_item.0)
}

pub(crate) fn output(&mut self) -> Vec<(&'a StoreKey, f64)> {
pub(crate) fn output(&mut self) -> Vec<(&'a StoreKey, f32)> {
let mut result: Vec<_> = Vec::with_capacity(self.max_capacity.get());

loop {
Expand Down Expand Up @@ -65,7 +65,7 @@ impl<'a> MaxHeap<'a> {
self.heap.len()
}

fn output(&mut self) -> Vec<(&'a StoreKey, f64)> {
fn output(&mut self) -> Vec<(&'a StoreKey, f32)> {
let mut result: Vec<_> = Vec::with_capacity(self.max_capacity.get());

loop {
Expand Down Expand Up @@ -100,7 +100,7 @@ impl<'a> AlgorithmHeapType<'a> {
}
}

pub(crate) fn output(&mut self) -> Vec<(&'a StoreKey, f64)> {
pub(crate) fn output(&mut self) -> Vec<(&'a StoreKey, f32)> {
match self {
Self::Min(h) => h.output(),
Self::Max(h) => h.output(),
Expand Down Expand Up @@ -128,11 +128,11 @@ mod tests {
fn test_min_heap_ordering_works() {
let mut heap = MinHeap::new(NonZeroUsize::new(3).unwrap());
let mut count = 0.0;
let first_vector = StoreKey(ndarray::Array1::<f64>::zeros(2).map(|x| x + 2.0));
let first_vector = StoreKey(ndarray::Array1::<f32>::zeros(2).map(|x| x + 2.0));

// If we pop these scores now, they should come back in the reverse order.
while count < 5.0 {
let similarity: f64 = 1.0 + count;
let similarity: f32 = 1.0 + count;

let item: SimilarityVector = (&first_vector, similarity).into();

Expand All @@ -150,11 +150,11 @@ mod tests {
fn test_max_heap_ordering_works() {
let mut heap = MaxHeap::new(NonZeroUsize::new(3).unwrap());
let mut count = 0.0;
let first_vector = StoreKey(ndarray::Array1::<f64>::zeros(2).map(|x| x + 2.0));
let first_vector = StoreKey(ndarray::Array1::<f32>::zeros(2).map(|x| x + 2.0));

// If we pop these scores now, they should come back the right order(max first).
while count < 5.0 {
let similarity: f64 = 1.0 + count;
let similarity: f32 = 1.0 + count;
let item: SimilarityVector = (&first_vector, similarity).into();

heap.push(item);
Expand Down
14 changes: 7 additions & 7 deletions ahnlich/db/src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ use types::similarity::Algorithm;
use self::{heap::AlgorithmHeapType, similarity::SimilarityFunc};

#[derive(Debug)]
pub(crate) struct SimilarityVector<'a>((&'a StoreKey, f64));
pub(crate) struct SimilarityVector<'a>((&'a StoreKey, f32));

impl<'a> From<(&'a StoreKey, f64)> for SimilarityVector<'a> {
fn from(value: (&'a StoreKey, f64)) -> SimilarityVector<'a> {
impl<'a> From<(&'a StoreKey, f32)> for SimilarityVector<'a> {
fn from(value: (&'a StoreKey, f32)) -> SimilarityVector<'a> {
SimilarityVector((value.0, value.1))
}
}
impl<'a> From<SimilarityVector<'a>> for (&'a StoreKey, f64) {
fn from(value: SimilarityVector<'a>) -> (&'a StoreKey, f64) {
impl<'a> From<SimilarityVector<'a>> for (&'a StoreKey, f32) {
fn from(value: SimilarityVector<'a>) -> (&'a StoreKey, f32) {
((value.0).0, (value.0).1)
}
}
Expand Down Expand Up @@ -51,7 +51,7 @@ pub(crate) trait FindSimilarN {
search_vector: &StoreKey,
search_list: impl Iterator<Item = &'a StoreKey>,
n: NonZeroUsize,
) -> Vec<(&'a StoreKey, f64)>;
) -> Vec<(&'a StoreKey, f32)>;
}

impl FindSimilarN for Algorithm {
Expand All @@ -60,7 +60,7 @@ impl FindSimilarN for Algorithm {
search_vector: &StoreKey,
search_list: impl Iterator<Item = &'a StoreKey>,
n: NonZeroUsize,
) -> Vec<(&'a StoreKey, f64)> {
) -> Vec<(&'a StoreKey, f32)> {
let mut heap: AlgorithmHeapType = (self, n).into();

let similarity_function: SimilarityFunc = self.into();
Expand Down
20 changes: 10 additions & 10 deletions ahnlich/db/src/algorithm/similarity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::Deref;
use types::{keyval::StoreKey, similarity::Algorithm};

type SimFuncSig = fn(&StoreKey, &StoreKey) -> f64;
type SimFuncSig = fn(&StoreKey, &StoreKey) -> f32;

pub(crate) struct SimilarityFunc(SimFuncSig);

Expand Down Expand Up @@ -64,7 +64,7 @@ impl From<&Algorithm> for SimilarityFunc {
/// similarity
///

fn cosine_similarity(first: &StoreKey, second: &StoreKey) -> f64 {
fn cosine_similarity(first: &StoreKey, second: &StoreKey) -> f32 {
// formular = dot product of vectors / product of the magnitude of the vectors
// maginiture of a vector can be calcuated using pythagoras theorem.
// sqrt of sum of vector values
Expand All @@ -74,9 +74,9 @@ fn cosine_similarity(first: &StoreKey, second: &StoreKey) -> f64 {
let dot_product = dot_product(first, second);

// the magnitude can be calculated using the arr.norm method.
let mag_first = &first.0.iter().map(|x| x * x).sum::<f64>().sqrt();
let mag_first = &first.0.iter().map(|x| x * x).sum::<f32>().sqrt();

let mag_second = &second.0.iter().map(|x| x * x).sum::<f64>().sqrt();
let mag_second = &second.0.iter().map(|x| x * x).sum::<f32>().sqrt();

dot_product / (mag_first * mag_second)
}
Expand All @@ -89,7 +89,7 @@ fn cosine_similarity(first: &StoreKey, second: &StoreKey) -> f64 {
/// An Implementation for most similar items would be a MaxHeap.
/// The larger the dot product between two vectors, the more similar

fn dot_product(first: &StoreKey, second: &StoreKey) -> f64 {
fn dot_product(first: &StoreKey, second: &StoreKey) -> f32 {
let dot_product = second.0.dot(&first.0.t());
dot_product
}
Expand All @@ -113,7 +113,7 @@ fn dot_product(first: &StoreKey, second: &StoreKey) -> f64 {
/// two points, denotes higher similarity
///

fn euclidean_distance(first: &StoreKey, second: &StoreKey) -> f64 {
fn euclidean_distance(first: &StoreKey, second: &StoreKey) -> f32 {
// Calculate the sum of squared differences for each dimension
let mut sum_of_squared_differences = 0.0;
for (&coord1, &coord2) in first.0.iter().zip(second.0.iter()) {
Expand All @@ -122,7 +122,7 @@ fn euclidean_distance(first: &StoreKey, second: &StoreKey) -> f64 {
}

// Calculate the square root of the sum of squared differences
f64::sqrt(sum_of_squared_differences)
f32::sqrt(sum_of_squared_differences)
}

#[cfg(test)]
Expand All @@ -134,7 +134,7 @@ mod tests {
fn test_find_top_3_similar_words_using_cosine_similarity() {
let sentences_vectors = word_to_vector();

let mut most_similar_result: Vec<(&'static str, f64)> = vec![];
let mut most_similar_result: Vec<(&'static str, f32)> = vec![];

let first_vector = sentences_vectors.get(SEACH_TEXT).unwrap().to_owned();

Expand All @@ -157,7 +157,7 @@ mod tests {
fn test_find_top_3_similar_words_using_euclidean_distance() {
let sentences_vectors = word_to_vector();

let mut most_similar_result: Vec<(&'static str, f64)> = vec![];
let mut most_similar_result: Vec<(&'static str, f32)> = vec![];

let first_vector = sentences_vectors.get(SEACH_TEXT).unwrap().to_owned();

Expand All @@ -181,7 +181,7 @@ mod tests {
fn test_find_top_3_similar_words_using_dot_product() {
let sentences_vectors = word_to_vector();

let mut most_similar_result: Vec<(&'static str, f64)> = vec![];
let mut most_similar_result: Vec<(&'static str, f32)> = vec![];

let first_vector = sentences_vectors.get(SEACH_TEXT).unwrap().to_owned();

Expand Down
Loading