refactor(lancedb): docs, make examples idempotent

refactor: clippy
This commit is contained in:
Joshua Mo 2025-04-09 19:17:41 +01:00
parent 92c91d23c3
commit 5ed3fcc401
4 changed files with 59 additions and 28 deletions

View File

@ -17,6 +17,9 @@
## Rig-Lancedb
This companion crate implements a Rig vector store based on Lancedb.
## Pre-requisites
If you are using `rig-lancedb` locally, you must ensure you have `protoc` (the [Protobuf Compiler](https://protobuf.dev/installation/)).
## Usage
Add the companion crate to your `Cargo.toml`, along with the rig-core crate:

View File

@ -38,8 +38,15 @@ async fn main() -> Result<(), anyhow::Error> {
.build()
.await?;
let table = db
.create_table(
let table = if db
.table_names()
.execute()
.await?
.contains(&"definitions".to_string())
{
db.open_table("definitions").execute().await?
} else {
db.create_table(
"definitions",
RecordBatchIterator::new(
vec![as_record_batch(embeddings, model.ndims())],
@ -47,9 +54,11 @@ async fn main() -> Result<(), anyhow::Error> {
),
)
.execute()
.await?;
.await?
};
// See [LanceDB indexing](https://lancedb.github.io/lancedb/concepts/index_ivfpq/#product-quantization) for more information
if table.index_stats("embedding").await?.is_none() {
table
.create_index(
&["embedding"],
@ -57,6 +66,7 @@ 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();

View File

@ -32,8 +32,15 @@ async fn main() -> Result<(), anyhow::Error> {
// Initialize LanceDB locally.
let db = lancedb::connect("data/lancedb-store").execute().await?;
let table = db
.create_table(
let table = if db
.table_names()
.execute()
.await?
.contains(&"definitions".to_string())
{
db.open_table("definitions").execute().await?
} else {
db.create_table(
"definitions",
RecordBatchIterator::new(
vec![as_record_batch(embeddings, model.ndims())],
@ -41,7 +48,8 @@ async fn main() -> Result<(), anyhow::Error> {
),
)
.execute()
.await?;
.await?
};
let vector_store = LanceDbVectorIndex::new(table, model, "id", search_params).await?;

View File

@ -44,8 +44,15 @@ async fn main() -> Result<(), anyhow::Error> {
.build()
.await?;
let table = db
.create_table(
let table = if db
.table_names()
.execute()
.await?
.contains(&"definitions".to_string())
{
db.open_table("definitions").execute().await?
} else {
db.create_table(
"definitions",
RecordBatchIterator::new(
vec![as_record_batch(embeddings, model.ndims())],
@ -53,9 +60,11 @@ async fn main() -> Result<(), anyhow::Error> {
),
)
.execute()
.await?;
.await?
};
// See [LanceDB indexing](https://lancedb.github.io/lancedb/concepts/index_ivfpq/#product-quantization) for more information
if table.index_stats("embedding").await?.is_none() {
table
.create_index(
&["embedding"],
@ -68,6 +77,7 @@ 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);