Can I model a vector type using pgvector? #1041
-
SeaORM doesn't yet have this functionality - it seems to be just about complete but they're waiting on this to be merged. It'd be doable in sqlx but I'm just wondering how I'd go about it without screwing things up with loco. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I made it custom for now. pg_vector.rs custom type file use sea_orm_migration::sea_query::types::Iden;
use std::fmt;
// default vector size
pub const DEFAULT_VECTOR_SIZE: usize = 1536;
// define a vector type
pub type VectorEmbedding = Vec<f32>;
// SeaORM vector type definition
pub struct PGVectorType<const SIZE: usize>;
// implement Iden for SeaORM
impl<const SIZE: usize> Iden for PGVectorType<SIZE> {
fn unquoted(&self, s: &mut dyn fmt::Write) {
write!(s, "VECTOR({})", SIZE).unwrap(); // Dynamically set the dimension, default is 1536
}
} in the migrations file .col(
ColumnDef::new(Chunks::Embedding)
.custom(PGVectorType::<DEFAULT_VECTOR_SIZE>) // entity type VectorEmbedding
.not_null(),
) this creates expected tables properly. as for entities, the type needs to be added to generated entities. for now manually. #[sea_orm(column_type = "custom(\"vector\")")]
pub embedding: VectorEmbedding, I am yet to test and use the generated entities. |
Beta Was this translation helpful? Give feedback.
-
@fivehanz Hi, thanks for your code. I used it and do migration success, but test failed. When call: let created = chunk.insert(&boot.app_context.db).await.unwrap(); the record wrote, but not returned, seems decode failed:
Do you meet this issue? how can I fix it? The full log:
|
Beta Was this translation helpful? Give feedback.
-
Just fixed by add #[sea_orm(column_type = "custom(\"vector\")", select_as = "float4[]")]
pub embedding: VectorEmbedding, But most cases we doesn't need to query this field, and I try to find how to exclude it. I'm a newbie in Rust and loco. |
Beta Was this translation helpful? Give feedback.
I made it custom for now.
pg_vector.rs custom type file
in the migrations file