@anvia/pgvector
v1.1.5
Published
Postgres pgvector store adapter for Anvia.
Readme
@anvia/pgvector
Store and search Anvia documents in Postgres with pgvector. Pair the store with your choice of embedding model to add semantic retrieval to agents and applications.
Install
pnpm add @anvia/pgvector @anvia/core @anvia/openai@anvia/openai supplies the embedding model in this example; other embedding adapters work too.
Quickstart
Set DATABASE_URL and OPENAI_API_KEY. Your Postgres server must provide the pgvector extension.
The database role needs permission to create the extension and table when using ensure().
import { embedDocuments } from "@anvia/core/embeddings";
import { retrieveDocuments } from "@anvia/core/vector-store";
import { OpenAIClient } from "@anvia/openai";
import { PgVectorClient } from "@anvia/pgvector";
const openai = new OpenAIClient({ apiKey: process.env.OPENAI_API_KEY! });
const model = openai.embeddingModel({ modelId: "text-embedding-3-small" });
const client = new PgVectorClient({ connectionString: process.env.DATABASE_URL! });
const store = client.vectorStore<{ id: string; text: string }>({
tableName: "support_docs",
dimensions: 1536,
metric: "cosine",
});
try {
await store.ensure();
const { documents } = await embedDocuments({
model,
documents: [{ id: "password-reset", text: "Reset links expire after 30 minutes." }],
id: (document) => document.id,
content: (document) => document.text,
});
await store.upsert({ documents });
const results = await retrieveDocuments({
store,
model,
query: "How long does a reset link last?",
topK: 3,
});
console.log(results);
} finally {
await client.close();
}Store capabilities
- Explicit provisioning with
ensure()and readiness checks withvalidate(). - Document replacement, vector search, and metadata filtering.
- Bring your own embedding model;
store.search()accepts vectors directly.
Keep semantic retrieval alongside application data in Postgres.
Pass client to use a preconfigured native SDK client. Injected clients remain caller-owned.
