PricingDocsTutorialsBlogAbout

Develop

Get Started
Store Embeddings
Generate Embeddings
Automatic Embedding Generation
Calculate Distance
Query Embeddings
Create Index
Quantization
Asynchronous Tasks
Weighted Vector Search
Troubleshooting
Single Operator
Postgres Notes
Security

Languages

Javascript
Python
Ruby
Rust

Migrate

Migrate from Postgres to Lantern Cloud
Migrate from pgvector to Lantern Cloud
Migrate from pgvector to self-hosted Lantern
Migrate from Pinecone to Lantern Cloud

Lantern HNSW

Installation

Lantern Extras

Installation
Generate Embeddings
Lantern Daemon

Lantern CLI

Installation
Generate Embeddings
Indexing Server
Daemon
Autotune Index
Product Quantization

Contributing

Dockerfile
Visual Studio Code

Languages

Rust

In the subsequent examples, assume the following variables are defined

rust
Copy
// 384 is the dimensions of bge-small-en model
let embedding: vec<f32> = vec![1.0; 384];
let query = "my text input";

rust-postgres

shell
Copy
cargo add postgres

Connect

rust
Copy
use postgres::{types::ToSql, Client, NoTls};

let mut client = Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut tx = client.transaction().unwrap();

tx.execute("CREATE EXTENSION IF NOT EXISTS lantern)", &[])
    .unwrap();
tx.execute("CREATE EXTENSION IF NOT EXISTS lantern_extras)", &[])
    .unwrap();

Insert a vector

rust
Copy
tx.execute("CREATE TABLE books (book_embedding REAL[])", &[])
    .unwrap();

tx.execute(
    "INSERT INTO books (book_embedding) VALUES ($1)",
    &[&embedding as &(dyn ToSql + Sync)],
)
.unwrap();

Find nearest rows

rust
Copy
tx.execute(
    "SELECT * FROM books ORDER BY book_embedding <-> $1 LIMIT 5",
    &[&embedding as &(dyn ToSql + Sync)],
)
.unwrap();

let rows = tx.query(
    "SELECT * FROM books ORDER BY book_embedding <-> text_embedding('BAAI/bge-small-en', $1) LIMIT 5",
    &[&query],
).unwrap();

tokio-postgres

shell
Copy
cargo add tokio-postgres

Insert a vector

rust
Copy
tx.execute("CREATE TABLE books (book_embedding REAL[])", &[])
    .await.unwrap();

tx.execute(
    "INSERT INTO books (book_embedding) VALUES ($1)",
    &[&embedding as &(dyn ToSql + Sync)],
)
.await.unwrap();

Find nearest rows

rust
Copy
tx.execute(
    "SELECT * FROM books ORDER BY book_embedding <-> $1 LIMIT 5",
    &[&embedding as &(dyn ToSql + Sync)],
)
.await.unwrap();

let rows = tx.query(
    "SELECT * FROM books ORDER BY book_embedding <-> text_embedding('BAAI/bge-small-en', $1) LIMIT 5",
    &[&query],
).await.unwrap();

Edit this page

On this page

  • rust-postgres
  • tokio-postgres
PricingDocsTutorialsBlogAbout
LoginSign Up