{"id":13880551,"url":"https://github.com/pgvector/pgvector-rust","last_synced_at":"2025-06-20T10:40:27.079Z","repository":{"id":57654695,"uuid":"375548507","full_name":"pgvector/pgvector-rust","owner":"pgvector","description":"pgvector support for Rust","archived":false,"fork":false,"pushed_at":"2025-05-20T22:34:07.000Z","size":167,"stargazers_count":156,"open_issues_count":2,"forks_count":19,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-20T22:45:47.419Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pgvector.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-10T02:41:07.000Z","updated_at":"2025-05-20T22:34:08.000Z","dependencies_parsed_at":"2023-09-28T00:59:38.603Z","dependency_job_id":"22acd64b-719e-4160-9432-46fd5f0a7955","html_url":"https://github.com/pgvector/pgvector-rust","commit_stats":{"total_commits":69,"total_committers":1,"mean_commits":69.0,"dds":0.0,"last_synced_commit":"382223df6dcd44cafe6e9f0bf138ea756993bcd2"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/pgvector/pgvector-rust","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pgvector","download_url":"https://codeload.github.com/pgvector/pgvector-rust/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pgvector%2Fpgvector-rust/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260927917,"owners_count":23084118,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-06T08:03:10.120Z","updated_at":"2025-06-20T10:40:22.066Z","avatar_url":"https://github.com/pgvector.png","language":"Rust","funding_links":[],"categories":["Rust","Sdks \u0026 Libraries"],"sub_categories":[],"readme":"# pgvector-rust\n\n[pgvector](https://github.com/pgvector/pgvector) support for Rust\n\nSupports [Rust-Postgres](https://github.com/sfackler/rust-postgres), [SQLx](https://github.com/launchbadge/sqlx), and [Diesel](https://github.com/diesel-rs/diesel)\n\n[![Build Status](https://github.com/pgvector/pgvector-rust/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-rust/actions)\n\n## Getting Started\n\nFollow the instructions for your database library:\n\n- [Rust-Postgres](#rust-postgres)\n- [SQLx](#sqlx)\n- [Diesel](#diesel)\n\nOr check out some examples:\n\n- [Embeddings](https://github.com/pgvector/pgvector-rust/blob/master/examples/openai/src/main.rs) with OpenAI\n- [Binary embeddings](https://github.com/pgvector/pgvector-rust/blob/master/examples/cohere/src/main.rs) with Cohere\n- [Sentence embeddings](https://github.com/pgvector/pgvector-rust/blob/master/examples/candle/src/main.rs) with Candle\n- [Hybrid search](https://github.com/pgvector/pgvector-rust/blob/master/examples/hybrid_search/src/main.rs) with Candle (Reciprocal Rank Fusion)\n- [Recommendations](https://github.com/pgvector/pgvector-rust/blob/master/examples/disco/src/main.rs) with Disco\n- [Bulk loading](https://github.com/pgvector/pgvector-rust/blob/master/examples/loading/src/main.rs) with `COPY`\n\n## Rust-Postgres\n\nAdd this line to your application’s `Cargo.toml` under `[dependencies]`:\n\n```toml\npgvector = { version = \"0.4\", features = [\"postgres\"] }\n```\n\nEnable the extension\n\n```rust\nclient.execute(\"CREATE EXTENSION IF NOT EXISTS vector\", \u0026[])?;\n```\n\nCreate a table\n\n```rust\nclient.execute(\"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))\", \u0026[])?;\n```\n\nCreate a vector from a `Vec\u003cf32\u003e`\n\n```rust\nuse pgvector::Vector;\n\nlet embedding = Vector::from(vec![1.0, 2.0, 3.0]);\n```\n\nInsert a vector\n\n```rust\nclient.execute(\"INSERT INTO items (embedding) VALUES ($1)\", \u0026[\u0026embedding])?;\n```\n\nGet the nearest neighbor\n\n```rust\nlet row = client.query_one(\n    \"SELECT * FROM items ORDER BY embedding \u003c-\u003e $1 LIMIT 1\",\n    \u0026[\u0026embedding],\n)?;\n```\n\nRetrieve a vector\n\n```rust\nlet row = client.query_one(\"SELECT embedding FROM items LIMIT 1\", \u0026[])?;\nlet embedding: Vector = row.get(0);\n```\n\nUse `Option` if the value could be `NULL`\n\n```rust\nlet embedding: Option\u003cVector\u003e = row.get(0);\n```\n\n## SQLx\n\nAdd this line to your application’s `Cargo.toml` under `[dependencies]`:\n\n```toml\npgvector = { version = \"0.4\", features = [\"sqlx\"] }\n```\n\nFor SQLx \u003c 0.8, use `version = \"0.3\"` and [this readme](https://github.com/pgvector/pgvector-rust/blob/v0.3.4/README.md).\n\nEnable the extension\n\n```rust\nsqlx::query(\"CREATE EXTENSION IF NOT EXISTS vector\")\n    .execute(\u0026pool)\n    .await?;\n```\n\nCreate a table\n\n```rust\nsqlx::query(\"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))\")\n    .execute(\u0026pool)\n    .await?;\n```\n\nCreate a vector from a `Vec\u003cf32\u003e`\n\n```rust\nuse pgvector::Vector;\n\nlet embedding = Vector::from(vec![1.0, 2.0, 3.0]);\n```\n\nInsert a vector\n\n```rust\nsqlx::query(\"INSERT INTO items (embedding) VALUES ($1)\")\n    .bind(embedding)\n    .execute(\u0026pool)\n    .await?;\n```\n\nGet the nearest neighbors\n\n```rust\nlet rows = sqlx::query(\"SELECT * FROM items ORDER BY embedding \u003c-\u003e $1 LIMIT 1\")\n    .bind(embedding)\n    .fetch_all(\u0026pool)\n    .await?;\n```\n\nRetrieve a vector\n\n```rust\nlet row = sqlx::query(\"SELECT embedding FROM items LIMIT 1\").fetch_one(\u0026pool).await?;\nlet embedding: Vector = row.try_get(\"embedding\")?;\n```\n\n## Diesel\n\nAdd this line to your application’s `Cargo.toml` under `[dependencies]`:\n\n```toml\npgvector = { version = \"0.4\", features = [\"diesel\"] }\n```\n\nAnd update your application’s `diesel.toml` under `[print_schema]`:\n\n```toml\nimport_types = [\"diesel::sql_types::*\", \"pgvector::sql_types::*\"]\ngenerate_missing_sql_type_definitions = false\n```\n\nCreate a migration\n\n```sh\ndiesel migration generate create_vector_extension\n```\n\nwith `up.sql`:\n\n```sql\nCREATE EXTENSION vector\n```\n\nand `down.sql`:\n\n```sql\nDROP EXTENSION vector\n```\n\nRun the migration\n\n```sql\ndiesel migration run\n```\n\nYou can now use the `vector` type in future migrations\n\n```sql\nCREATE TABLE items (\n  id SERIAL PRIMARY KEY,\n  embedding VECTOR(3)\n)\n```\n\nFor models, use:\n\n```rust\nuse pgvector::Vector;\n\n#[derive(Queryable)]\n#[diesel(table_name = items)]\npub struct Item {\n    pub id: i32,\n    pub embedding: Option\u003cVector\u003e,\n}\n\n#[derive(Insertable)]\n#[diesel(table_name = items)]\npub struct NewItem {\n    pub embedding: Option\u003cVector\u003e,\n}\n```\n\nCreate a vector from a `Vec\u003cf32\u003e`\n\n```rust\nlet embedding = Vector::from(vec![1.0, 2.0, 3.0]);\n```\n\nInsert a vector\n\n```rust\nlet new_item = NewItem {\n    embedding: Some(embedding)\n};\n\ndiesel::insert_into(items::table)\n    .values(\u0026new_item)\n    .get_result::\u003cItem\u003e(\u0026mut conn)?;\n```\n\nGet the nearest neighbors\n\n```rust\nuse pgvector::VectorExpressionMethods;\n\nlet neighbors = items::table\n    .order(items::embedding.l2_distance(embedding))\n    .limit(5)\n    .load::\u003cItem\u003e(\u0026mut conn)?;\n```\n\nAlso supports `max_inner_product`, `cosine_distance`, `l1_distance`, `hamming_distance`, and `jaccard_distance`\n\nGet the distances\n\n```rust\nlet distances = items::table\n    .select(items::embedding.l2_distance(embedding))\n    .load::\u003cOption\u003cf64\u003e\u003e(\u0026mut conn)?;\n```\n\nAdd an approximate index in a migration\n\n```sql\nCREATE INDEX my_index ON items USING hnsw (embedding vector_l2_ops)\n-- or\nCREATE INDEX my_index ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)\n```\n\nUse `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance\n\n## Serialization\n\nUse the `serde` feature to enable serialization\n\n## Half Vectors\n\nUse the `halfvec` feature to enable half vectors\n\n## Reference\n\nConvert a vector to a `Vec\u003cf32\u003e`\n\n```rust\nlet f32_vec: Vec\u003cf32\u003e = vec.into();\n```\n\nGet a slice\n\n```rust\nlet slice = vec.as_slice();\n```\n\n## History\n\nView the [changelog](https://github.com/pgvector/pgvector-rust/blob/master/CHANGELOG.md)\n\n## Contributing\n\nEveryone is encouraged to help improve this project. Here are a few ways you can help:\n\n- [Report bugs](https://github.com/pgvector/pgvector-rust/issues)\n- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-rust/pulls)\n- Write, clarify, or fix documentation\n- Suggest or add new features\n\nTo get started with development:\n\n```sh\ngit clone https://github.com/pgvector/pgvector-rust.git\ncd pgvector-rust\ncreatedb pgvector_rust_test\ncargo test --all-features\n```\n\nTo run an example:\n\n```sh\ncd examples/loading\ncreatedb pgvector_example\ncargo run\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgvector%2Fpgvector-rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpgvector%2Fpgvector-rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgvector%2Fpgvector-rust/lists"}