pgvector-rust
pgvector support for Rust
Supports Rust-Postgres, SQLx, and Diesel
Getting Started
Follow the instructions for your database library:
Rust-Postgres
Add this line to your application’s Cargo.toml
under [dependencies]
:
= { = "0.2", = ["postgres"] }
Create a vector from a Vec<f32>
let embedding = from;
Insert a vector
client.execute?;
Get the nearest neighbor
let row = client.query_one?;
Retrieve a vector
let row = client.query_one?;
let embedding: Vector = row.get;
Use Option
if the value could be NULL
let embedding: = row.get;
SQLx
Add this line to your application’s Cargo.toml
under [dependencies]
:
= { = "0.2", = ["sqlx"] }
Create a vector from a Vec<f32>
let embedding = from;
Insert a vector
.bind.execute.await?;
query
Get the nearest neighbors
let rows = query
.bind.fetch_all.await?;
Retrieve a vector
let row = query.fetch_one.await?;
let embedding: Vector = row.try_get?;
Diesel
Add this line to your application’s Cargo.toml
under [dependencies]
:
= { = "0.2", = ["diesel"] }
And add this line to your application’s diesel.toml
under [print_schema]
:
= ["diesel::sql_types::*", "pgvector::sql_types::*"]
Create a migration
with up.sql
:
CREATE EXTENSION vector
and down.sql
:
DROP EXTENSION vector
Run the migration
diesel migration run
You can now use the vector
type in future migrations
3)
)
(
embedding VECTOR(
For models, use:
Create a vector from a Vec<f32>
let embedding = from;
Insert a vector
let new_item = Item ;
insert_into
.values
.?;
Get the nearest neighbors
use VectorExpressionMethods;
let neighbors = table
.order
.limit
.?;
Also supports max_inner_product
and cosine_distance
Get the distances
let distances = table
.select
.?;
Add an approximate index in a migration
WITH (lists = 100)
(embedding vector_l2_ops)
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
Reference
Convert a vector to a Vec<f32>
let f32_vec: = vec.into;
History
View the changelog
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development: