Skip to content

Instantly share code, notes, and snippets.

@densumesh
Created December 15, 2023 16:38
Show Gist options
  • Save densumesh/df405d26730d2f389f6aff0004888edd to your computer and use it in GitHub Desktop.
Save densumesh/df405d26730d2f389f6aff0004888edd to your computer and use it in GitHub Desktop.
qdrant_upsert.rs
pub async fn create_new_qdrant_point_query(
point_id: uuid::Uuid,
embedding_vector: Vec<f32>,
private: bool,
card_metadata: CardMetadata,
splade_models: web::Data<SpladeModels>,
author_id: Option<uuid::Uuid>,
dataset_id: uuid::Uuid,
) -> Result<(), actix_web::Error> {
let qdrant_collection = get_env!(
"QDRANT_COLLECTION",
"QDRANT_COLLECTION should be set if this is called"
)
.to_string();
let qdrant = get_qdrant_connection()
.await
.map_err(|err| ServiceError::BadRequest(err.message.into()))?;
let splade_vector = splade_models
.get_doc_embedding(card_metadata.card_html.clone().unwrap_or_default())
.map_err(|err| {
log::info!("Failed to get splade vector {:?}", err);
ServiceError::BadRequest("Failed to get splade vector".into())
})?;
let payload = json!({"private": private, "authors": vec![author_id.unwrap_or_default().to_string()], "tag_set": card_metadata.tag_set.unwrap_or("".to_string()).split(',').collect_vec(), "link": card_metadata.link.unwrap_or("".to_string()).split(',').collect_vec(), "card_html": card_metadata.card_html.unwrap_or("".to_string()), "metadata": card_metadata.metadata.unwrap_or_default(), "time_stamp": card_metadata.time_stamp.unwrap_or_default().timestamp(), "dataset_id": dataset_id.to_string()})
.try_into()
.expect("A json! Value must always be a valid Payload");
let point = PointStruct::new(
point_id.clone().to_string(),
HashMap::from([
("dense_vectors".to_string(), embedding_vector),
("sparse_vectors".to_string(), splade_vector),
]),
payload,
);
qdrant
.upsert_points_blocking(qdrant_collection, None, vec![point], None)
.await
.map_err(|err| {
log::info!("Failed inserting card to qdrant {:?}", err);
ServiceError::BadRequest("Failed inserting card to qdrant".into())
})?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment