Skip to content

Commit

Permalink
refactor: lance db examples
Browse files Browse the repository at this point in the history
  • Loading branch information
marieaurore123 committed Oct 7, 2024
1 parent 9a310cb commit 9b09639
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
18 changes: 6 additions & 12 deletions rig-lancedb/examples/vector_search_local_ann.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ async fn main() -> Result<(), anyhow::Error> {
// Select an embedding model.
let model = openai_client.embedding_model(TEXT_EMBEDDING_ADA_002);

// Initialize LanceDB locally.
let db = lancedb::connect("data/lancedb-store").execute().await?;

// Set up test data for RAG demo
let definition = "Definition of *flumbuzzle (verb)*: to bewilder or confuse someone completely, often by using nonsensical or overly complex explanations or instructions.".to_string();

Expand All @@ -44,12 +47,6 @@ async fn main() -> Result<(), anyhow::Error> {
.build()
.await?;

// Define search_params params that will be used by the vector store to perform the vector search.
let search_params = SearchParams::default().distance_type(DistanceType::Cosine);

// Initialize LanceDB locally.
let db = lancedb::connect("data/lancedb-store").execute().await?;

// Create table with embeddings.
let record_batch = as_record_batch(embeddings, model.ndims());
let table = db
Expand All @@ -64,16 +61,13 @@ async fn main() -> Result<(), anyhow::Error> {
table
.create_index(
&["embedding"],
lancedb::index::Index::IvfPq(
IvfPqIndexBuilder::default()
// This overrides the default distance type of L2.
// Needs to be the same distance type as the one used in search params.
.distance_type(DistanceType::Cosine),
),
lancedb::index::Index::IvfPq(IvfPqIndexBuilder::default()),
)
.execute()
.await?;

// Define search_params params that will be used by the vector store to perform the vector search.
let search_params = SearchParams::default();
let vector_store = LanceDbVectorStore::new(table, model, "id", search_params).await?;

// Query the index
Expand Down
19 changes: 10 additions & 9 deletions rig-lancedb/examples/vector_search_s3_ann.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ async fn main() -> Result<(), anyhow::Error> {
// Select the embedding model and generate our embeddings
let model = openai_client.embedding_model(TEXT_EMBEDDING_ADA_002);

// Initialize LanceDB on S3.
// Note: see below docs for more options and IAM permission required to read/write to S3.
// https://lancedb.github.io/lancedb/guides/storage/#aws-s3
let db = lancedb::connect("s3://lancedb-test-829666124233")
.execute()
.await?;

// Set up test data for RAG demo
let definition = "Definition of *flumbuzzle (verb)*: to bewilder or confuse someone completely, often by using nonsensical or overly complex explanations or instructions.".to_string();

Expand All @@ -46,15 +53,6 @@ async fn main() -> Result<(), anyhow::Error> {
.build()
.await?;

// Define search_params params that will be used by the vector store to perform the vector search.
let search_params = SearchParams::default().distance_type(DistanceType::Cosine);

// Initialize LanceDB on S3.
// Note: see below docs for more options and IAM permission required to read/write to S3.
// https://lancedb.github.io/lancedb/guides/storage/#aws-s3
let db = lancedb::connect("s3://lancedb-test-829666124233")
.execute()
.await?;
// Create table with embeddings.
let record_batch = as_record_batch(embeddings, model.ndims());
let table = db
Expand All @@ -79,6 +77,9 @@ async fn main() -> Result<(), anyhow::Error> {
.execute()
.await?;

// Define search_params params that will be used by the vector store to perform the vector search.
let search_params = SearchParams::default().distance_type(DistanceType::Cosine);

let vector_store = LanceDbVectorStore::new(table, model, "id", search_params).await?;

// Query the index
Expand Down

0 comments on commit 9b09639

Please sign in to comment.