-
-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* change sea-query to git * add column type * change to fork * vector * add feature flag * trygetable for vector * fix eq for vector * add size to vector * fix: pgvector version * Update Cargo.toml * Support PgVector * Use `pgvector/pgvector` docker image * Apply suggestions from code review * Fixup * Update tests/embedding_tests.rs --------- Co-authored-by: Leon Camus <[email protected]> Co-authored-by: Leon Camus <[email protected]> Co-authored-by: Chris Tsang <[email protected]>
- Loading branch information
1 parent
f5dab25
commit ce69458
Showing
13 changed files
with
252 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use super::sea_orm_active_enums::*; | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "embedding")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub id: i32, | ||
pub embedding: PgVector, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
#![allow(unused_imports, dead_code)] | ||
|
||
pub mod common; | ||
|
||
pub use common::{features::*, setup::*, TestContext}; | ||
use pretty_assertions::assert_eq; | ||
use sea_orm::{ | ||
entity::prelude::*, entity::*, DatabaseConnection, DerivePartialModel, FromQueryResult, | ||
}; | ||
use serde_json::json; | ||
|
||
#[sea_orm_macros::test] | ||
#[cfg(all(feature = "sqlx-postgres", feature = "postgres-vector"))] | ||
async fn main() -> Result<(), DbErr> { | ||
let ctx = TestContext::new("embedding_tests").await; | ||
create_tables(&ctx.db).await?; | ||
insert_embedding(&ctx.db).await?; | ||
update_embedding(&ctx.db).await?; | ||
select_embedding(&ctx.db).await?; | ||
ctx.delete().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn insert_embedding(db: &DatabaseConnection) -> Result<(), DbErr> { | ||
use embedding::*; | ||
|
||
assert_eq!( | ||
Model { | ||
id: 1, | ||
embedding: PgVector::from(vec![1.]), | ||
} | ||
.into_active_model() | ||
.insert(db) | ||
.await?, | ||
Model { | ||
id: 1, | ||
embedding: PgVector::from(vec![1.]), | ||
} | ||
); | ||
|
||
assert_eq!( | ||
Model { | ||
id: 2, | ||
embedding: PgVector::from(vec![1., 2.]), | ||
} | ||
.into_active_model() | ||
.insert(db) | ||
.await?, | ||
Model { | ||
id: 2, | ||
embedding: PgVector::from(vec![1., 2.]), | ||
} | ||
); | ||
|
||
assert_eq!( | ||
Model { | ||
id: 3, | ||
embedding: PgVector::from(vec![1., 2., 3.]), | ||
} | ||
.into_active_model() | ||
.insert(db) | ||
.await?, | ||
Model { | ||
id: 3, | ||
embedding: PgVector::from(vec![1., 2., 3.]), | ||
} | ||
); | ||
|
||
assert_eq!( | ||
Entity::find_by_id(3).into_json().one(db).await?, | ||
Some(json!({ | ||
"id": 3, | ||
"embedding": [1., 2., 3.], | ||
})) | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn update_embedding(db: &DatabaseConnection) -> Result<(), DbErr> { | ||
use embedding::*; | ||
|
||
let model = Entity::find_by_id(1).one(db).await?.unwrap(); | ||
|
||
ActiveModel { | ||
embedding: Set(PgVector::from(vec![10.])), | ||
..model.into_active_model() | ||
} | ||
.update(db) | ||
.await?; | ||
|
||
ActiveModel { | ||
id: Unchanged(3), | ||
embedding: Set(PgVector::from(vec![10., 20., 30.])), | ||
} | ||
.update(db) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn select_embedding(db: &DatabaseConnection) -> Result<(), DbErr> { | ||
use embedding::*; | ||
|
||
#[derive(DerivePartialModel, FromQueryResult, Debug, PartialEq)] | ||
#[sea_orm(entity = "Entity")] | ||
struct PartialSelectResult { | ||
embedding: PgVector, | ||
} | ||
|
||
let result = Entity::find_by_id(1) | ||
.into_partial_model::<PartialSelectResult>() | ||
.one(db) | ||
.await?; | ||
|
||
assert_eq!( | ||
result, | ||
Some(PartialSelectResult { | ||
embedding: PgVector::from(vec![10.]), | ||
}) | ||
); | ||
|
||
let result = Entity::find_by_id(2) | ||
.into_partial_model::<PartialSelectResult>() | ||
.one(db) | ||
.await?; | ||
|
||
assert_eq!( | ||
result, | ||
Some(PartialSelectResult { | ||
embedding: PgVector::from(vec![1., 2.]), | ||
}) | ||
); | ||
|
||
let result = Entity::find_by_id(3) | ||
.into_partial_model::<PartialSelectResult>() | ||
.one(db) | ||
.await?; | ||
|
||
assert_eq!( | ||
result, | ||
Some(PartialSelectResult { | ||
embedding: PgVector::from(vec![10., 20., 30.]), | ||
}) | ||
); | ||
|
||
Ok(()) | ||
} |