This guide walks through building a production-ready RAG (Retrieval-Augmented Generation) application using SolVec. You’ll learn how to:
Load and chunk documents
Generate embeddings
Store vectors in SolVec
Retrieve relevant context
Generate responses with an LLM
Verify data integrity on-chain
RAG combines the precision of semantic search with the reasoning of large language models. SolVec adds a third dimension: cryptographic verifiability of your knowledge base.
import { SolVec } from 'solvec';import OpenAI from 'openai';import * as fs from 'fs';const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });const sv = new SolVec({ network: 'devnet' });const collection = sv.collection('docs', { dimensions: 1536 });async function buildRAG() { // 1. Load & chunk document const text = fs.readFileSync('./veclabs-README.md', 'utf-8'); const chunks = chunkDocument(text, 1000, 200); console.log(`✓ Split into ${chunks.length} chunks`); // 2. Embed chunks const embeddings = await embedChunks(chunks); console.log(`✓ Generated ${embeddings.length} embeddings`); // 3. Store in SolVec await collection.upsert( chunks.map((chunk, i) => ({ id: chunk.id, values: embeddings[i], metadata: { text: chunk.text }, })) ); console.log(`✓ Stored in SolVec`); // 4. Verify on-chain const proof = await collection.verify(); console.log(`✓ Verified: ${proof.solanaExplorerUrl}`);}async function queryRAG() { const questions = [ 'What is VecLabs?', 'How fast is query latency?', 'How does verification work?', ]; for (const q of questions) { console.log(`\nQ: ${q}`); const answer = await answerQuestion(q); console.log(`A: ${answer}`); }}// Runawait buildRAG();await queryRAG();
Expected Output:
✓ Split into 47 chunks✓ Generated 47 embeddings[SolVec] Upserted 47 vectors to collection 'docs'✓ Stored in SolVec✓ Verified: https://explorer.solana.com/address/8iLpy...?cluster=devnetQ: What is VecLabs?A: VecLabs is a decentralized vector database built on Solana. It providessub-5ms query latency using a Rust HNSW implementation, with encrypted storageand on-chain verification of data integrity.Q: How fast is query latency?A: VecLabs achieves sub-5ms p99 latency at 100K vectors, with p50 at 1.9msand p95 at 2.8ms. This is 5-6x faster than Pinecone and Weaviate.Q: How does verification work?A: After every write, SolVec computes a SHA-256 Merkle root of all vector IDsand posts it to Solana. Anyone can verify that the local collection matchesthe on-chain root, proving the data hasn't been tampered with.