Skip to content

postgresml/pgvector-rust

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector-rust

pgvector support for Rust

Supports Rust-Postgres, SQLx, and Diesel

Build Status

Getting Started

Follow the instructions for your database library:

Rust-Postgres

Add this line to your application’s Cargo.toml under [dependencies]:

pgvector = { version = "0.2", features = ["postgres"] }

Create a vector from a Vec<f32>

let embedding = pgvector::Vector::from(vec![1.0, 2.0, 3.0]);

Insert a vector

client.execute("INSERT INTO items (embedding) VALUES ($1)", &[&embedding])?;

Get the nearest neighbor

let row = client.query_one("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 1", &[&embedding])?;

Retrieve a vector

let row = client.query_one("SELECT embedding FROM items LIMIT 1", &[])?;
let embedding: pgvector::Vector = row.get(0);

Use Option if the value could be NULL

let embedding: Option<pgvector::Vector> = row.get(0);

SQLx

Add this line to your application’s Cargo.toml under [dependencies]:

pgvector = { version = "0.2", features = ["sqlx"] }

Create a vector from a Vec<f32>

let embedding = pgvector::Vector::from(vec![1.0, 2.0, 3.0]);

Insert a vector

sqlx::query("INSERT INTO items (embedding) VALUES ($1)").bind(embedding).execute(&pool).await?;

Get the nearest neighbors

let rows = sqlx::query("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 1")
    .bind(embedding).fetch_all(&pool).await?;

Retrieve a vector

let row = sqlx::query("SELECT embedding FROM items LIMIT 1").fetch_one(&pool).await?;
let embedding: pgvector::Vector = row.try_get("embedding")?;

Diesel

Add this line to your application’s Cargo.toml under [dependencies]:

pgvector = { version = "0.2", features = ["diesel"] }

And add this line to your application’s diesel.toml under [print_schema]:

import_types = ["diesel::sql_types::*", "pgvector::sql_types::*"]

Create a migration

diesel migration generate create_vector_extension

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

CREATE TABLE items (
  embedding VECTOR(3)
)

For models, use:

pub struct Item {
    pub embedding: Option<pgvector::Vector>
}

Create a vector from a Vec<f32>

let embedding = pgvector::Vector::from(vec![1.0, 2.0, 3.0]);

Insert a vector

let new_item = Item {
    embedding: Some(embedding)
};

diesel::insert_into(items::table)
    .values(&new_item)
    .get_result::<Item>(&mut conn)?;

Get the nearest neighbors

use pgvector::VectorExpressionMethods;

let neighbors = items::table
    .order(items::embedding.l2_distance(embedding))
    .limit(5)
    .load::<Item>(&mut conn)?;

Also supports max_inner_product and cosine_distance

Get the distances

let distances = items::table
    .select(items::embedding.l2_distance(embedding))
    .load::<Option<f64>>(&mut conn)?;

Add an approximate index in a migration

CREATE INDEX my_index ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)

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<f32> = vec.into();

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector-rust.git
cd pgvector-rust
createdb pgvector_rust_test
cargo test --all-features

About

pgvector support for Rust

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy