mirror of https://github.com/0xplaygrounds/rig
refactor(lancedb): docs, make examples idempotent (#387)
* refactor(lancedb): docs, make examples idempotent refactor: clippy * fix: README typo
This commit is contained in:
parent
e9c50241da
commit
b7ba9bbd93
|
@ -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/)) installed.
|
||||
|
||||
## Usage
|
||||
|
||||
Add the companion crate to your `Cargo.toml`, along with the rig-core crate:
|
||||
|
|
|
@ -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,16 +54,19 @@ 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
|
||||
table
|
||||
.create_index(
|
||||
&["embedding"],
|
||||
lancedb::index::Index::IvfPq(IvfPqIndexBuilder::default()),
|
||||
)
|
||||
.execute()
|
||||
.await?;
|
||||
if table.index_stats("embedding").await?.is_none() {
|
||||
table
|
||||
.create_index(
|
||||
&["embedding"],
|
||||
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();
|
||||
|
|
|
@ -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?;
|
||||
|
||||
|
|
|
@ -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,21 +60,24 @@ 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
|
||||
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),
|
||||
),
|
||||
)
|
||||
.execute()
|
||||
.await?;
|
||||
if table.index_stats("embedding").await?.is_none() {
|
||||
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),
|
||||
),
|
||||
)
|
||||
.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);
|
||||
|
|
Loading…
Reference in New Issue