Skip to content

Commit

Permalink
docs(swiftide): documented file swiftide/src/integrations/qdrant/inge…
Browse files Browse the repository at this point in the history
…stion_node.rs (#20)

Co-authored-by: bosun-ai[bot] <157630444+bosun-ai[bot]@users.noreply.github.com>
  • Loading branch information
bosun-ai[bot] authored Jun 13, 2024
1 parent 7688c99 commit d572c88
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion swiftide/src/integrations/qdrant/ingestion_node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//! This module provides functionality to convert an `IngestionNode` into a `qdrant::PointStruct`.
//! The conversion is essential for storing data in the Qdrant vector database, which is used
//! for efficient vector similarity search. The module handles metadata augmentation and ensures
//! data compatibility with Qdrant's required format.
use anyhow::{Context as _, Result};
use std::collections::HashMap;

Expand All @@ -7,12 +12,26 @@ use qdrant_client::{
qdrant::{self, Value},
};

/// Implements the `TryInto` trait to convert an `IngestionNode` into a `qdrant::PointStruct`.
/// This conversion is necessary for storing the node in the Qdrant vector database.
impl TryInto<qdrant::PointStruct> for IngestionNode {
type Error = anyhow::Error;

/// Converts the `IngestionNode` into a `qdrant::PointStruct`.
///
/// # Errors
///
/// Returns an error if the vector is not set in the `IngestionNode`.
///
/// # Returns
///
/// A `Result` which is `Ok` if the conversion is successful, containing the `qdrant::PointStruct`.
/// If the conversion fails, it returns an `anyhow::Error`.
fn try_into(mut self) -> Result<qdrant::PointStruct> {
// Calculate a unique identifier for the node.
let id = self.calculate_hash();

// Extend the metadata with additional information.
self.metadata.extend([
("path".to_string(), self.path.to_string_lossy().to_string()),
("content".to_string(), self.chunk),
Expand All @@ -22,14 +41,15 @@ impl TryInto<qdrant::PointStruct> for IngestionNode {
),
]);

// Damn who build this api
// Create a payload compatible with Qdrant's API.
let payload: Payload = self
.metadata
.iter()
.map(|(k, v)| (k.as_str(), Value::from(v.as_str())))
.collect::<HashMap<&str, Value>>()
.into();

// Construct the `qdrant::PointStruct` and return it.
Ok(qdrant::PointStruct::new(
id,
self.vector.context("Vector is not set")?,
Expand Down

0 comments on commit d572c88

Please sign in to comment.