Skip to content

Commit

Permalink
Refactor according to Vectorize V2 spec
Browse files Browse the repository at this point in the history
  • Loading branch information
garvit-gupta committed Oct 22, 2024
1 parent b598d71 commit ca41a86
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions worker/src/vectorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,18 @@ pub enum VectorizeIndexConfig {
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Metadata about an existing index.
///
/// This type is exclusively for the Vectorize **beta** and will be deprecated once Vectorize RC is released.
pub struct VectorizeIndexDetails {
pub id: String,
pub name: String,
pub description: Option<String>,
pub config: VectorizeIndexConfig,
pub vectors_count: u64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Results of an operation that performed a mutation on a set of vectors.
/// Here, `ids` is a list of vectors that were successfully processed.
///
/// This type is exclusively for the Vectorize **beta** and will be deprecated once Vectorize RC is released.
pub struct VectorizeVectorMutation {
/// List of ids of vectors that were successfully processed.
pub ids: Vec<String>,
/// Total count of the number of processed vectors.
pub count: u64,
pub struct VectorizeVectorAsyncMutation {
/// The unique identifier for the async mutation operation containing the changeset.
pub mutation_id: String,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -90,7 +81,7 @@ impl<'a> VectorizeVector<'a> {
}
}

#[derive(Debug, Serialize)]
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "kebab-case")]
/// Metadata return levels for a Vectorize query.
pub enum VectorizeMetadataRetrievalLevel {
Expand All @@ -99,6 +90,7 @@ pub enum VectorizeMetadataRetrievalLevel {
/// Return all metadata fields configured for indexing in the vector return set. This level of retrieval is "free" in that no additional overhead is incurred returning this data. However, note that indexed metadata is subject to truncation (especially for larger strings).
Indexed,
/// No indexed metadata will be returned.
#[default]
None,
}

Expand All @@ -120,13 +112,14 @@ type VectorizeVectorMetadataFilter =
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VectorizeQueryOptions {
// Default 3, max 20
// Default 5, max 100
top_k: u8,
/// Return vectors from the specified namespace. Default `none`.
namespace: Option<String>,
/// Return vector values. Default `false`.
return_values: bool,
/// Return vector metadata. Default `false`.
return_metadata: bool,
/// Return vector metadata. Default `None`.
return_metadata: VectorizeMetadataRetrievalLevel,
/// Default `none`.
filter: Option<VectorizeVectorMetadataFilter>,
}
Expand All @@ -151,7 +144,10 @@ impl VectorizeQueryOptions {
self
}

pub fn with_return_metadata(mut self, return_metadata: bool) -> Self {
pub fn with_return_metadata(
mut self,
return_metadata: VectorizeMetadataRetrievalLevel,
) -> Self {
self.return_metadata = return_metadata;
self
}
Expand All @@ -173,10 +169,10 @@ impl VectorizeQueryOptions {
impl Default for VectorizeQueryOptions {
fn default() -> Self {
Self {
top_k: 3,
top_k: 5,
namespace: None,
return_values: false,
return_metadata: false,
return_metadata: VectorizeMetadataRetrievalLevel::None,
filter: None,
}
}
Expand All @@ -191,6 +187,8 @@ pub struct VectorizeVectorResult {
pub values: Option<Vec<f32>>,
/// Metadata associated with the vector. Includes the values of other fields and potentially additional details.
pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
/** The namespace the vector belongs to. */
pub namespace: Option<String>,
}

#[derive(Debug, Deserialize)]
Expand All @@ -209,8 +207,6 @@ pub struct VectorizeMatches {
}

/// A Vectorize Vector Search Index for querying vectors/embeddings.
///
/// This type is exclusively for the Vectorize **beta** and will be deprecated once Vectorize RC is released.
pub struct VectorizeIndex(VectorizeIndexSys);

unsafe impl Send for VectorizeIndex {}
Expand All @@ -233,7 +229,7 @@ impl VectorizeIndex {
pub async fn insert<'a>(
&self,
vectors: &[VectorizeVector<'a>],
) -> Result<VectorizeVectorMutation> {
) -> Result<VectorizeVectorAsyncMutation> {
let promise = self
.0
.insert(serde_wasm_bindgen::to_value(&vectors)?.into())?;
Expand All @@ -246,7 +242,7 @@ impl VectorizeIndex {
pub async fn upsert<'a>(
&self,
vectors: &[VectorizeVector<'a>],
) -> Result<VectorizeVectorMutation> {
) -> Result<VectorizeVectorAsyncMutation> {
let promise = self
.0
.upsert(serde_wasm_bindgen::to_value(&vectors)?.into())?;
Expand All @@ -269,7 +265,7 @@ impl VectorizeIndex {
}

/// Delete a list of vectors with a matching id.
pub async fn delete_by_ids<'a, T>(&self, ids: T) -> Result<VectorizeVectorMutation>
pub async fn delete_by_ids<'a, T>(&self, ids: T) -> Result<VectorizeVectorAsyncMutation>
where
T: IntoIterator<Item = &'a str>,
{
Expand Down

0 comments on commit ca41a86

Please sign in to comment.