Rig-postgres
This companion crate implements a Rig vector store based on PostgreSQL.
Usage
Add the companion crate to your Cargo.toml
, along with the rig-core crate:
[]
= "0.4.0"
= "0.1.0"
You can also run cargo add rig-core rig-postgres
to add the most recent versions of the dependencies to your project.
PostgreSQL setup
The crate utilizes pgvector extension, which is available for PostgreSQL version 13 and later. Use any of the official or alternative methods to install psql.
You can install Postgres using Docker:
Now you can configure Postgres, the recommended way is using sqlx and migrations (you can find an example inside integration tests folder).
Example sql:
-- ensure PgVector extension is installed
CREATE EXTENSION IF NOT EXISTS vector;
-- create table with embeddings using 1536 dimensions (based on OpenAI model text-embedding-3-small)
(
id uuid DEFAULT gen_random_uuid , -- we can have repeated entries
document jsonb NOT NULL,
embedded_text text NOT NULL,
embedding vector(1536)
);
-- create index on embeddings
USING hnsw(embedding vector_cosine_ops); -- recommended for text embeddings
You can change the table name and the number of dimensions but keep the same fields schema.
You can use different indexes depending the type of distance method you want to use, check PgVector documentation.
Usage
Declare the database URL:
Define the document you want to index, it has to implement Embed
, Serialize
and Deserialize
.
Note: you can index different type of documents in the same table.
Example:
Example usage
// Create OpenAI client
let openai_client = from_env;
let model = openai_client.embedding_model;
// connect to Postgres
let database_url = var.expect;
let pool = new .connect .await?;
// run migrations (optional but recommended)
migrate!.run.await?;
// init documents
let products: = ...;
let documents = new
.documents
.unwrap
.build
.await?;
// Create your index
let vector_store = default;
// store documents
vector_store.insert_documents.await?;
// retrieve embeddings
let results = vector_store..await?
...