Retrieval-augmented generation
A documentation assistant needs to retrieve relevant passages before generating an answer.
AI Tools · Const
The RAG workflow uses pgvector with Postgres to store embeddings alongside chunked source content. Vector distance then supports similarity-based retrieval for AI applications.
The documented RAG workflow chunks source material, converts each chunk into an embedding, and stores the embedding with its text in PostgreSQL using the pgvector extension. Later, vector similarity can help retrieve relevant chunks for an AI application.
pnpm db:migrateimport { embed } from 'ai';
import { openai } from '@ai-sdk/openai';
const { embedding } = await embed({
model: openai.embedding('text-embedding-3-small'),
value: 'A guide to vector search',
});
console.log(embedding.length);Creates an embedding that can be stored alongside source content for later vector similarity search.
A documentation assistant needs to retrieve relevant passages before generating an answer.
An application needs semantic lookup over chunked text rather than exact keyword matching.
A pipeline needs persistent storage for embeddings and their source text.
The workflow first splits source material into chunks, then embeds each chunk and stores the embedding together with the chunk. PostgreSQL is used with the pgvector extension to provide vector storage.
Follow-up: Why chunk documents instead of embedding an entire document? Smaller chunks allow retrieval to select more focused passages for downstream generation.
An embedding represents text as a vector in a high-dimensional space. Similarity can be estimated from the distance between vectors, so nearby vectors represent semantically similar content.
Follow-up: What must the storage layer provide? It needs support for vector values and operations that compare their similarity or distance.
The migration command enables the pgvector extension and creates the resources table used by the tutorial. That table stores identifiers, content, and timestamps alongside the vector-related data defined by the schema.
Follow-up: What should be verified if vector queries fail immediately after setup? Confirm that the migration completed and that the extension is enabled in the target database.
EmbeddingsPostgresRAG Agentresources schema