1import{j as e,L as m}from"./index-DrBPhjy5.js";import{L as b,D as j,g as v,c as s}from"./Layout-LwDNXt6D.js";import{C as i,a as r,b as c,d as o}from"./card-BCaF5CiV.js";import{B as h}from"./badge-CMh-GqOk.js";import{T as y,a as f,b as l,c as d}from"./tabs-COJfkQl5.js";import{C as a}from"./copy-Y-6CfcZE.js";import{E as u}from"./external-link-CLJaV4l5.js";import{P as N}from"./play-N8xnDCNq.js";const E=()=>{const t=n=>{navigator.clipboard.writeText(n)};return e.jsx(b,{title:"Vector Database Workshop - VibeCoding",description:"Hands-on workshop covering vector databases, embeddings, similarity search, and building RAG applications for AI-powered development.",children:e.jsx("div",{className:"min-h-screen bg-gradient-to-br from-indigo-50 to-purple-50",children:e.jsxs("div",{className:"container mx-auto px-4 py-12",children:[e.jsxs("div",{className:"text-center mb-16",children:[e.jsx("div",{className:"flex justify-center mb-6",children:e.jsx("div",{className:"p-4 bg-primary/10 rounded-full",children:e.jsx(j,{className:"h-12 w-12 text-primary"})})}),e.jsx("h1",{className:"text-5xl font-bold text-gray-900 mb-6",children:"Vector Database Workshop"}),e.jsx("p",{className:"text-xl text-gray-600 max-w-3xl mx-auto leading-relaxed",children:"Master vector databases and semantic search to build powerful AI applications with retrieval-augmented generation (RAG)."}),e.jsxs("div",{className:"flex gap-4 justify-center mt-8",children:[e.jsx(h,{variant:"secondary",className:"px-4 py-2",children:"Hands-on Workshop"}),e.jsx(h,{variant:"secondary",className:"px-4 py-2",children:"RAG Implementation"}),e.jsx(h,{variant:"secondary",className:"px-4 py-2",children:"Production Ready"})]})]}),e.jsxs(y,{defaultValue:"concepts",className:"mb-12",children:[e.jsx("div",{className:"flex justify-center mb-6",children:e.jsxs(f,{className:"grid grid-cols-2 md:grid-cols-5 gap-2",children:[e.jsx(l,{value:"concepts",children:"Concepts"}),e.jsx(l,{value:"embeddings",children:"Embeddings"}),e.jsx(l,{value:"databases",children:"Databases"}),e.jsx(l,{value:"rag",children:"RAG Implementation"}),e.jsx(l,{value:"production",children:"Production"})]})}),e.jsx(d,{value:"concepts",children:e.jsxs(i,{children:[e.jsx(r,{children:e.jsxs(c,{className:"text-2xl flex items-center gap-2",children:[e.jsx(v,{className:"h-6 w-6"}),"Vector Database Fundamentals"]})}),e.jsx(o,{className:"space-y-6",children:e.jsxs("div",{className:"grid md:grid-cols-2 gap-8",children:[e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"What are Vector Databases?"}),e.jsx("p",{className:"text-gray-700 mb-4",children:"Vector databases are specialized databases designed to store, index, and query high-dimensional vectors (embeddings). They enable semantic search by finding similar vectors based on mathematical distance rather than exact matches."}),e.jsxs("div",{className:"bg-blue-50 p-4 rounded-lg",children:[e.jsx("h4",{className:"font-medium mb-2",children:"Key Capabilities:"}),e.jsxs("ul",{className:"space-y-1 text-sm",children:[e.jsx("li",{children:"⢠Similarity search and ranking"}),e.jsx("li",{children:"⢠Semantic understanding of content"}),e.jsx("li",{children:"⢠Real-time query performance"}),e.jsx("li",{children:"⢠Scalable vector indexing"})]})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Vector Embeddings"}),e.jsx("p",{className:"text-gray-700 mb-4",children:"Embeddings are numerical representations of data (text, images, audio) in high-dimensional space. Similar items are positioned close to each other in this space."}),e.jsxs("div",{className:"bg-green-50 p-4 rounded-lg",children:[e.jsx("h4",{className:"font-medium mb-2",children:"Example:"}),e.jsxs("div",{className:"text-sm space-y-1",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Text:"}),' "The cat sits on the mat"']}),e.jsxs("p",{children:[e.jsx("strong",{children:"Vector:"})," [0.1, -0.3, 0.7, ..., 0.2] (1536 dimensions)"]})]})]})]})]}),e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Similarity Metrics"}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"border-l-4 border-blue-500 pl-4",children:[e.jsx("h4",{className:"font-medium",children:"Cosine Similarity"}),e.jsx("p",{className:"text-sm text-gray-600",children:"Measures angle between vectors (0-1 scale)"})]}),e.jsxs("div",{className:"border-l-4 border-green-500 pl-4",children:[e.jsx("h4",{className:"font-medium",children:"Euclidean Distance"}),e.jsx("p",{className:"text-sm text-gray-600",children:"Straight-line distance between points"})]}),e.jsxs("div",{className:"border-l-4 border-purple-500 pl-4",children:[e.jsx("h4",{className:"font-medium",children:"Dot Product"}),e.jsx("p",{className:"text-sm text-gray-600",children:"Product of magnitudes and cosine"})]})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Use Cases"}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"bg-yellow-50 p-3 rounded-lg",children:[e.jsx("h4",{className:"font-medium text-yellow-800",children:"ð Semantic Search"}),e.jsx("p",{className:"text-sm text-yellow-700",children:"Find documents by meaning, not just keywords"})]}),e.jsxs("div",{className:"bg-purple-50 p-3 rounded-lg",children:[e.jsx("h4",{className:"font-medium text-purple-800",children:"ð¤ RAG Applications"}),e.jsx("p",{className:"text-sm text-purple-700",children:"Enhance LLMs with relevant context"})]}),e.jsxs("div",{className:"bg-green-50 p-3 rounded-lg",children:[e.jsx("h4",{className:"font-medium text-green-800",children:"ð¡ Recommendation Systems"}),e.jsx("p",{className:"text-sm text-green-700",children:"Suggest similar content or products"})]})]})]})]})]})})]})}),e.jsx(d,{value:"embeddings",children:e.jsx("div",{className:"space-y-6",children:e.jsxs(i,{children:[e.jsx(r,{children:e.jsx(c,{className:"text-2xl",children:"Creating and Using Embeddings"})}),e.jsxs(o,{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"OpenAI Embeddings"}),e.jsxs("div",{className:"bg-gray-900 text-gray-100 p-4 rounded-lg relative",children:[e.jsx("pre",{className:"text-sm",children:`import OpenAI from 'openai'; 2 3const openai = new OpenAI({ 4 apiKey: process.env.OPENAI_API_KEY, 5}); 6 7async function createEmbedding(text: string): Promise<number[]> { 8 const response = await openai.embeddings.create({ 9 model: 'text-embedding-3-small', 10 input: text, 11 }); 12 13 return response.data[0].embedding; 14} 15 16// Usage 17const text = "Vector databases enable semantic search"; 18const embedding = await createEmbedding(text); 19console.log(\`Embedding dimensions: \${embedding.length}\`); 20console.log(\`First 5 values: \${embedding.slice(0, 5)}\`);`}),e.jsx(s,{size:"sm",variant:"ghost",className:"absolute top-2 right-2 text-gray-300 hover:text-white",onClick:()=>t(`import OpenAI from 'openai'; 21 22const openai = new OpenAI({ 23 apiKey: process.env.OPENAI_API_KEY, 24}); 25 26async function createEmbedding(text: string): Promise<number[]> { 27 const response = await openai.embeddings.create({ 28 model: 'text-embedding-3-small', 29 input: text, 30 }); 31 32 return response.data[0].embedding; 33}`),children:e.jsx(a,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Batch Processing"}),e.jsxs("div",{className:"bg-gray-900 text-gray-100 p-4 rounded-lg relative",children:[e.jsx("pre",{className:"text-sm",children:`async function createEmbeddings(texts: string[]): Promise<number[][]> { 34 // Process in batches to avoid rate limits 35 const batchSize = 100; 36 const embeddings: number[][] = []; 37 38 for (let i = 0; i < texts.length; i += batchSize) { 39 const batch = texts.slice(i, i + batchSize); 40 41 const response = await openai.embeddings.create({ 42 model: 'text-embedding-3-small', 43 input: batch, 44 }); 45 46 const batchEmbeddings = response.data.map(item => item.embedding); 47 embeddings.push(...batchEmbeddings); 48 49 // Add delay to respect rate limits 50 await new Promise(resolve => setTimeout(resolve, 1000)); 51 } 52 53 return embeddings; 54} 55 56// Usage 57const documents = [ 58 "React is a JavaScript library for building user interfaces", 59 "Vue.js is a progressive framework for building user interfaces", 60 "Angular is a platform for building mobile and desktop web applications" 61]; 62 63const embeddings = await createEmbeddings(documents); 64console.log(\`Created \${embeddings.length} embeddings\`);`}),e.jsx(s,{size:"sm",variant:"ghost",className:"absolute top-2 right-2 text-gray-300 hover:text-white",onClick:()=>t(`async function createEmbeddings(texts: string[]): Promise<number[][]> { 65 const batchSize = 100; 66 const embeddings: number[][] = []; 67 68 for (let i = 0; i < texts.length; i += batchSize) { 69 const batch = texts.slice(i, i + batchSize); 70 71 const response = await openai.embeddings.create({ 72 model: 'text-embedding-3-small', 73 input: batch, 74 }); 75 76 const batchEmbeddings = response.data.map(item => item.embedding); 77 embeddings.push(...batchEmbeddings); 78 79 await new Promise(resolve => setTimeout(resolve, 1000)); 80 } 81 82 return embeddings; 83}`),children:e.jsx(a,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Similarity Calculation"}),e.jsxs("div",{className:"bg-gray-900 text-gray-100 p-4 rounded-lg relative",children:[e.jsx("pre",{className:"text-sm",children:`function cosineSimilarity(vecA: number[], vecB: number[]): number {
84 const dotProduct = vecA.reduce((sum, a, i) => sum + a * vecB[i], 0); 85 const magnitudeA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0)); 86 const magnitudeB = Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0)); 87 88 return dotProduct / (magnitudeA * magnitudeB); 89} 90 91function euclideanDistance(vecA: number[], vecB: number[]): number { 92 const squaredDiffs = vecA.map((a, i) => Math.pow(a - vecB[i], 2)); 93 return Math.sqrt(squaredDiffs.reduce((sum, diff) => sum + diff, 0)); 94} 95 96// Usage 97const query = "JavaScript framework for UI"; 98const doc1 = "React library for user interfaces"; 99const doc2 = "Python web development framework"; 100 101const queryEmbedding = await createEmbedding(query); 102const doc1Embedding = await createEmbedding(doc1); 103const doc2Embedding = await createEmbedding(doc2); 104 105const similarity1 = cosineSimilarity(queryEmbedding, doc1Embedding); 106const similarity2 = cosineSimilarity(queryEmbedding, doc2Embedding); 107 108console.log(\`Query-Doc1 similarity: \${similarity1.toFixed(3)}\`); 109console.log(\`Query-Doc2 similarity: \${similarity2.toFixed(3)}\`);`}),e.jsx(s,{size:"sm",variant:"ghost",className:"absolute top-2 right-2 text-gray-300 hover:text-white",onClick:()=>t(`function cosineSimilarity(vecA: number[], vecB: number[]): number { 110 const dotProduct = vecA.reduce((sum, a, i) => sum + a * vecB[i], 0); 111 const magnitudeA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0)); 112 const magnitudeB = Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0)); 113 114 return dotProduct / (magnitudeA * magnitudeB); 115}`),children:e.jsx(a,{className:"h-4 w-4"})})]})]})]})]})})}),e.jsx(d,{value:"databases",children:e.jsx("div",{className:"space-y-6",children:[{name:"Pinecone",description:"Managed vector database with excellent performance",installation:"npm install @pinecone-database/pinecone",code:`import { Pinecone } from '@pinecone-database/pinecone'; 116 117const pinecone = new Pinecone({ 118 apiKey: process.env.PINECONE_API_KEY, 119}); 120 121const index = pinecone.Index('your-index-name'); 122 123// Upsert vectors 124await index.upsert([ 125 { 126 id: 'doc1', 127 values: embedding1, 128 metadata: { text: 'Document content here' } 129 }, 130 { 131 id: 'doc2', 132 values: embedding2, 133 metadata: { text: 'Another document' } 134 } 135]); 136 137// Query similar vectors 138const queryResponse = await index.query({ 139 vector: queryEmbedding, 140 topK: 5, 141 includeMetadata: true 142}); 143 144console.log('Similar documents:', queryResponse.matches);`,features:["Fully managed","Auto-scaling","Real-time updates","Multi-region support"]},{name:"Weaviate",description:"Open-source vector database with GraphQL API",installation:"npm install weaviate-ts-client",code:`import weaviate from 'weaviate-ts-client'; 145 146const client = weaviate.client({ 147 scheme: 'https', 148 host: 'your-cluster.weaviate.network', 149 apiKey: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY), 150}); 151 152// Create a class (schema) 153await client.schema 154 .classCreator() 155 .withClass({ 156 class: 'Document', 157 properties: [ 158 { 159 name: 'content', 160 dataType: ['text'], 161 }, 162 ], 163 }) 164 .do(); 165 166// Add data with vector 167await client.data 168 .creator() 169 .withClassName('Document') 170 .withProperties({ content: 'Document text here' }) 171 .withVector(embedding) 172 .do(); 173 174// Search 175const result = await client.graphql 176 .get() 177 .withClassName('Document') 178 .withFields('content') 179 .withNearVector({ vector: queryEmbedding }) 180 .withLimit(5) 181 .do();`,features:["Open source","GraphQL API","Multi-modal","Self-hosted or cloud"]},{name:"Chroma",description:"Simple, lightweight vector database for development",installation:"pip install chromadb",code:`// Using Chroma with HTTP API 182const CHROMA_URL = 'http://localhost:8000'; 183 184async function addToChroma(documents: string[], embeddings: number[][], ids: string[]) { 185 const response = await fetch(\`\${CHROMA_URL}/api/v1/collections/my_collection/add\`, { 186 method: 'POST', 187 headers: { 'Content-Type': 'application/json' }, 188 body: JSON.stringify({ 189 documents, 190 embeddings, 191 ids, 192 }), 193 }); 194 return response.json(); 195} 196 197async function queryChroma(queryEmbedding: number[], nResults: number = 5) { 198 const response = await fetch(\`\${CHROMA_URL}/api/v1/collections/my_collection/query\`, { 199 method: 'POST', 200 headers: { 'Content-Type': 'application/json' }, 201 body: JSON.stringify({ 202 query_embeddings: [queryEmbedding], 203 n_results: nResults, 204 }), 205 }); 206 return response.json(); 207} 208 209// Usage 210await addToChroma( 211 ['Document 1 content', 'Document 2 content'], 212 [embedding1, embedding2], 213 ['doc1', 'doc2'] 214); 215 216const results = await queryChroma(queryEmbedding, 3);`,features:["Easy setup","Python-first","Local development","Fast prototyping"]}].map((n,x)=>e.jsxs(i,{children:[e.jsxs(r,{children:[e.jsx(c,{className:"text-xl",children:n.name}),e.jsx("p",{className:"text-gray-600",children:n.description})]}),e.jsxs(o,{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"font-semibold mb-2",children:"Installation:"}),e.jsx("div",{className:"bg-gray-100 p-2 rounded text-sm font-mono",children:n.installation})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"font-semibold mb-2",children:"Example Usage:"}),e.jsxs("div",{className:"bg-gray-900 text-gray-100 p-4 rounded-lg relative",children:[e.jsx("pre",{className:"text-sm overflow-x-auto",children:n.code}),e.jsx(s,{size:"sm",variant:"ghost",className:"absolute top-2 right-2 text-gray-300 hover:text-white",onClick:()=>t(n.code),children:e.jsx(a,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"font-semibold mb-2",children:"Key Features:"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:n.features.map((p,g)=>e.jsx(h,{variant:"outline",children:p},g))})]})]})]},x))})}),e.jsx(d,{value:"rag",children:e.jsx("div",{className:"space-y-6",children:e.jsxs(i,{children:[e.jsx(r,{children:e.jsx(c,{className:"text-2xl",children:"Building a RAG Application"})}),e.jsxs(o,{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"RAG Architecture"}),e.jsx("div",{className:"bg-blue-50 p-4 rounded-lg mb-4",children:e.jsxs("div",{className:"space-y-2 text-sm",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"w-6 h-6 bg-blue-500 text-white rounded-full flex items-center justify-center text-xs",children:"1"}),e.jsx("span",{children:"User asks a question"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"w-6 h-6 bg-blue-500 text-white rounded-full flex items-center justify-center text-xs",children:"2"}),e.jsx("span",{children:"Convert question to embedding"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"w-6 h-6 bg-blue-500 text-white rounded-full flex items-center justify-center text-xs",children:"3"}),e.jsx("span",{children:"Search vector database for similar content"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"w-6 h-6 bg-blue-500 text-white rounded-full flex items-center justify-center text-xs",children:"4"}),e.jsx("span",{children:"Combine retrieved context with question"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"w-6 h-6 bg-blue-500 text-white rounded-full flex items-center justify-center text-xs",children:"5"}),e.jsx("span",{children:"Send to LLM for answer generation"})]})]})})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Complete RAG Implementation"}),e.jsxs("div",{className:"bg-gray-900 text-gray-100 p-4 rounded-lg relative",children:[e.jsx("pre",{className:"text-sm",children:`import OpenAI from 'openai'; 217import { Pinecone } from '@pinecone-database/pinecone'; 218 219class RAGSystem { 220 private openai: OpenAI; 221 private pinecone: Pinecone; 222 private index: any; 223 224 constructor() { 225 this.openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); 226 this.pinecone = new Pinecone({ apiKey: process.env.PINECONE_API_KEY }); 227 this.index = this.pinecone.Index('knowledge-base'); 228 } 229 230 async addDocument(id: string, text: string, metadata?: any): Promise<void> { 231 // Create embedding for the document 232 const embedding = await this.createEmbedding(text); 233 234 // Store in vector database 235 await this.index.upsert([{ 236 id, 237 values: embedding, 238 metadata: { text, ...metadata } 239 }]); 240 } 241 242 async query(question: string, topK: number = 5): Promise<string> { 243 // 1. Convert question to embedding 244 const questionEmbedding = await this.createEmbedding(question); 245 246 // 2. Search for similar documents 247 const searchResults = await this.index.query({ 248 vector: questionEmbedding, 249 topK, 250 includeMetadata: true 251 }); 252 253 // 3. Extract relevant context 254 const context = searchResults.matches 255 .map(match => match.metadata?.text) 256 .filter(Boolean) 257 .join('\\n\\n'); 258 259 // 4. Generate answer using LLM 260 const prompt = \`Context information: 261\${context} 262 263Question: \${question} 264 265Please answer the question based on the context information provided. If the context doesn't contain enough information to answer the question, say so.\`; 266
267 const completion = await this.openai.chat.completions.create({ 268 model: 'gpt-6-astra', 269 messages: [{ role: 'user', content: prompt }], 270 temperature: 0.1, 271 }); 272 273 return completion.choices[0]?.message?.content || 'No answer generated'; 274 } 275 276 private async createEmbedding(text: string): Promise<number[]> { 277 const response = await this.openai.embeddings.create({ 278 model: 'text-embedding-3-small', 279 input: text, 280 }); 281 return response.data[0].embedding; 282 } 283} 284 285// Usage 286const rag = new RAGSystem(); 287 288// Add documents to the knowledge base 289await rag.addDocument('doc1', 'React is a JavaScript library for building user interfaces'); 290await rag.addDocument('doc2', 'Vector databases store high-dimensional vectors for similarity search'); 291 292// Query the system 293const answer = await rag.query('What is React?'); 294console.log(answer);`}),e.jsx(s,{size:"sm",variant:"ghost",className:"absolute top-2 right-2 text-gray-300 hover:text-white",onClick:()=>t(`import OpenAI from 'openai'; 295import { Pinecone } from '@pinecone-database/pinecone'; 296 297class RAGSystem { 298 private openai: OpenAI; 299 private pinecone: Pinecone; 300 private index: any; 301 302 constructor() { 303 this.openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); 304 this.pinecone = new Pinecone({ apiKey: process.env.PINECONE_API_KEY }); 305 this.index = this.pinecone.Index('knowledge-base'); 306 } 307}`),children:e.jsx(a,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Document Processing Pipeline"}),e.jsxs("div",{className:"bg-gray-900 text-gray-100 p-4 rounded-lg relative",children:[e.jsx("pre",{className:"text-sm",children:`class DocumentProcessor { 308 private chunkSize = 1000; 309 private chunkOverlap = 200; 310 311 // Split large documents into chunks 312 splitDocument(text: string): string[] { 313 const sentences = text.split(/[.!?]+/).filter(s => s.trim().length > 0); 314 const chunks: string[] = []; 315 let currentChunk = ''; 316 317 for (const sentence of sentences) { 318 if (currentChunk.length + sentence.length > this.chunkSize) { 319 if (currentChunk) { 320 chunks.push(currentChunk.trim()); 321 // Start new chunk with overlap 322 const words = currentChunk.split(' '); 323 const overlapWords = words.slice(-this.chunkOverlap / 10); 324 currentChunk = overlapWords.join(' ') + ' ' + sentence; 325 } else { 326 currentChunk = sentence; 327 } 328 } else { 329 currentChunk += ' ' + sentence; 330 } 331 } 332 333 if (currentChunk) { 334 chunks.push(currentChunk.trim()); 335 } 336 337 return chunks; 338 } 339 340 // Process and add document to RAG system 341 async processDocument(rag: RAGSystem, docId: string, content: string, metadata?: any): Promise<void> { 342 const chunks = this.splitDocument(content); 343 344 for (let i = 0; i < chunks.length; i++) { 345 await rag.addDocument( 346 \`\${docId}_chunk_\${i}\`, 347 chunks[i], 348 { 349 ...metadata, 350 chunkIndex: i, 351 totalChunks: chunks.length, 352 originalDocId: docId 353 } 354 ); 355 } 356 } 357} 358 359// Usage 360const processor = new DocumentProcessor(); 361const longDocument = "This is a very long document that needs to be split..."; 362 363await processor.processDocument(rag, 'long-doc-1', longDocument, { 364 title: 'Vector Database Guide', 365 author: 'AI Expert', 366 category: 'technical' 367});`}),e.jsx(s,{size:"sm",variant:"ghost",className:"absolute top-2 right-2 text-gray-300 hover:text-white",onClick:()=>t(`class DocumentProcessor { 368 private chunkSize = 1000; 369 private chunkOverlap = 200; 370 371 splitDocument(text: string): string[] { 372 const sentences = text.split(/[.!?]+/).filter(s => s.trim().length > 0); 373 const chunks: string[] = []; 374 let currentChunk = ''; 375 376 for (const sentence of sentences) { 377 if (currentChunk.length + sentence.length > this.chunkSize) { 378 if (currentChunk) { 379 chunks.push(currentChunk.trim()); 380 const words = currentChunk.split(' '); 381 const overlapWords = words.slice(-this.chunkOverlap / 10); 382 currentChunk = overlapWords.join(' ') + ' ' + sentence; 383 } else { 384 currentChunk = sentence; 385 } 386 } else { 387 currentChunk += ' ' + sentence; 388 } 389 } 390 391 if (currentChunk) { 392 chunks.push(currentChunk.trim()); 393 } 394 395 return chunks; 396 } 397}`),children:e.jsx(a,{className:"h-4 w-4"})})]})]})]})]})})}),e.jsx(d,{value:"production",children:e.jsx("div",{className:"space-y-6",children:e.jsxs(i,{children:[e.jsx(r,{children:e.jsx(c,{className:"text-2xl",children:"Production Optimization"})}),e.jsxs(o,{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Performance Optimization"}),e.jsxs("div",{className:"grid md:grid-cols-2 gap-6",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"font-medium mb-3",children:"Indexing Strategies"}),e.jsxs("ul",{className:"space-y-2 text-sm",children:[e.jsx("li",{children:"⢠Use HNSW for fast approximate search"}),e.jsx("li",{children:"⢠Implement metadata filtering"}),e.jsx("li",{children:"⢠Optimize vector dimensions"}),e.jsx("li",{children:"⢠Regular index maintenance"})]})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"font-medium mb-3",children:"Caching"}),e.jsxs("ul",{className:"space-y-2 text-sm",children:[e.jsx("li",{children:"⢠Cache frequent embeddings"}),e.jsx("li",{children:"⢠Implement query result caching"}),e.jsx("li",{children:"⢠Use CDN for static embeddings"}),e.jsx("li",{children:"⢠Batch similar queries"})]})]})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Advanced RAG Techniques"}),e.jsxs("div",{className:"bg-gray-900 text-gray-100 p-4 rounded-lg relative",children:[e.jsx("pre",{className:"text-sm",children:`class AdvancedRAGSystem extends RAGSystem {
398 // Hybrid search combining vector and keyword search 399 async hybridSearch(question: string, topK: number = 10): Promise<any[]> { 400 // Vector search 401 const vectorResults = await this.vectorSearch(question, topK); 402 403 // Keyword search (implement with your preferred search engine) 404 const keywordResults = await this.keywordSearch(question, topK); 405 406 // Combine and rerank results 407 return this.rerankResults(vectorResults, keywordResults, question); 408 } 409 410 // Re-ranking using a cross-encoder model 411 async rerankResults(results: any[], question: string): Promise<any[]> { 412 // Score each result based on relevance to the question 413 const scoredResults = await Promise.all( 414 results.map(async (result) => { 415 const relevanceScore = await this.calculateRelevance(question, result.metadata.text); 416 return { ...result, relevanceScore }; 417 }) 418 ); 419 420 // Sort by relevance score 421 return scoredResults.sort((a, b) => b.relevanceScore - a.relevanceScore); 422 } 423 424 // Query expansion to handle different phrasings 425 async queryExpansion(question: string): Promise<string[]> { 426 const prompt = \`Generate three alternative ways to phrase this question: "\${question}"\`; 427 428 const result = await this.openai.chat.completions.create({ 429 model: 'gpt-3.5-turbo', 430 messages: [{ role: 'user', content: prompt }], 431 temperature: 0.7, 432 }); 433 434 const alternatives = result.choices[0]?.message?.content?.split('\\n') || []; 435 return [question, ...alternatives.filter(a => a.trim().length > 0)]; 436 } 437}`}),e.jsx(s,{size:"sm",variant:"ghost",className:"absolute top-2 right-2 text-gray-300 hover:text-white",onClick:()=>t(`class AdvancedRAGSystem extends RAGSystem { 438 async hybridSearch(question: string, topK: number = 10): Promise<any[]> { 439 // Vector search 440 const vectorResults = await this.vectorSearch(question, topK); 441 442 // Keyword search (implement with your preferred search engine) 443 const keywordResults = await this.keywordSearch(question, topK); 444 445 // Combine and rerank results 446 return this.rerankResults(vectorResults, keywordResults, question); 447 } 448}`),children:e.jsx(a,{className:"h-4 w-4"})})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Scaling Considerations"}),e.jsxs("div",{className:"grid md:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"bg-yellow-50 p-4 rounded-lg",children:[e.jsx("h4",{className:"font-medium mb-2 text-yellow-800",children:"Storage Growth"}),e.jsxs("ul",{className:"space-y-2 text-sm text-yellow-700",children:[e.jsx("li",{children:"⢠Monitor vector collection size"}),e.jsx("li",{children:"⢠Implement data retention policies"}),e.jsx("li",{children:"⢠Consider distributed storage"}),e.jsx("li",{children:"⢠Archive unused vectors"})]})]}),e.jsxs("div",{className:"bg-blue-50 p-4 rounded-lg",children:[e.jsx("h4",{className:"font-medium mb-2 text-blue-800",children:"Query Performance"}),e.jsxs("ul",{className:"space-y-2 text-sm text-blue-700",children:[e.jsx("li",{children:"⢠Use vector dimension reduction"}),e.jsx("li",{children:"⢠Implement sharding for large indices"}),e.jsx("li",{children:"⢠Consider approximate vs. exact search tradeoffs"}),e.jsx("li",{children:"⢠Load balance across replicas"})]})]})]})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-lg font-semibold mb-3",children:"Monitoring & Analytics"}),e.jsxs("div",{className:"bg-gray-900 text-gray-100 p-4 rounded-lg relative",children:[e.jsx("pre",{className:"text-sm",children:`import { createLogger } from 'winston'; 449 450class RAGMonitoring { 451 private logger = createLogger({/* your configuration */}); 452 453 trackQueryPerformance(query: string, results: any[], responseTime: number): void { 454 this.logger.info('RAG Query', { 455 query, 456 resultCount: results.length, 457 topResult: results[0]?.id, 458 responseTimeMs: responseTime, 459 timestamp: new Date().toISOString(), 460 }); 461 } 462 463 trackUserFeedback(query: string, answer: string, userRating: number): void { 464 this.logger.info('User Feedback', { 465 query, 466 answerPreview: answer.substring(0, 100), 467 userRating, 468 timestamp: new Date().toISOString(), 469 }); 470 } 471 472 generateDailyReport(): Promise<any> { 473 // Analyze logs and generate performance report
474 // Implementation depends on your logging infrastructure 475 } 476} 477 478// Usage 479const monitoring = new RAGMonitoring(); 480 481// Track a query 482const startTime = Date.now(); 483const results = await rag.query('What is a vector database?'); 484const responseTime = Date.now() - startTime; 485 486monitoring.trackQueryPerformance('What is a vector database?', results, responseTime); 487 488// Track user feedback 489monitoring.trackUserFeedback( 490 'What is a vector database?', 491 results, 492 4.5 // Rating out of 5 493);`}),e.jsx(s,{size:"sm",variant:"ghost",className:"absolute top-2 right-2 text-gray-300 hover:text-white",onClick:()=>t(`import { createLogger } from 'winston'; 494 495class RAGMonitoring { 496 private logger = createLogger({/* your configuration */}); 497 498 trackQueryPerformance(query: string, results: any[], responseTime: number): void { 499 this.logger.info('RAG Query', { 500 query, 501 resultCount: results.length, 502 topResult: results[0]?.id, 503 responseTimeMs: responseTime, 504 timestamp: new Date().toISOString(), 505 }); 506 } 507}`),children:e.jsx(a,{className:"h-4 w-4"})})]})]})]})]})})})]}),e.jsxs(i,{className:"mb-12",children:[e.jsx(r,{children:e.jsx(c,{className:"text-2xl",children:"Additional Resources"})}),e.jsx(o,{children:e.jsxs("div",{className:"grid md:grid-cols-3 gap-6",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"font-semibold mb-3",children:"Documentation"}),e.jsxs("ul",{className:"space-y-2 text-sm",children:[e.jsx("li",{children:e.jsxs("a",{href:"https://docs.pinecone.io",className:"text-primary hover:underline flex items-center gap-1",children:["Pinecone Documentation ",e.jsx(u,{className:"h-3 w-3"})]})}),e.jsx("li",{children:e.jsxs("a",{href:"https://weaviate.io/developers/weaviate",className:"text-primary hover:underline flex items-center gap-1",children:["Weaviate Documentation ",e.jsx(u,{className:"h-3 w-3"})]})}),e.jsx("li",{children:e.jsxs("a",{href:"https://docs.trychroma.com",className:"text-primary hover:underline flex items-center gap-1",children:["Chroma Documentation ",e.jsx(u,{className:"h-3 w-3"})]})})]})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"font-semibold mb-3",children:"Tutorials"}),e.jsxs("ul",{className:"space-y-2 text-sm",children:[e.jsx("li",{children:e.jsx("a",{href:"#",className:"text-primary hover:underline",children:"RAG Pattern Explained"})}),e.jsx("li",{children:e.jsx("a",{href:"#",className:"text-primary hover:underline",children:"Vector Database Benchmarks"})}),e.jsx("li",{children:e.jsx("a",{href:"#",className:"text-primary hover:underline",children:"Advanced RAG Techniques"})})]})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"font-semibold mb-3",children:"Related Topics"}),e.jsxs("ul",{className:"space-y-2 text-sm",children:[e.jsx("li",{children:e.jsx(m,{to:"/llm-integration-tutorial",className:"text-primary hover:underline",children:"LLM Integration Tutorial"})}),e.jsx("li",{children:e.jsx(m,{to:"/prompt-engineering-guide",className:"text-primary hover:underline",children:"Prompt Engineering Guide"})}),e.jsx("li",{children:e.jsx(m,{to:"/api-integration",className:"text-primary hover:underline",children:"API Integration"})})]})]})]})})]}),e.jsxs("div",{className:"text-center bg-primary/5 rounded-lg p-8",children:[e.jsx("h2",{className:"text-2xl font-bold mb-4",children:"Build Powerful Vector Applications"}),e.jsx("p",{className:"text-gray-600 mb-6 max-w-2xl mx-auto",children:"Start implementing vector search and RAG in your applications to enhance their capabilities with semantic understanding and context-aware responses."}),e.jsxs("div",{className:"flex gap-4 justify-center",children:[e.jsxs(s,{size:"lg",children:[e.jsx(N,{className:"mr-2 h-5 w-5"}),"Try the Demo"]}),e.jsx(s,{variant:"outline",size:"lg",asChild:!0,children:e.jsx(m,{to:"/glossary",children:"Back to Glossary"})})]})]})]})})})};export{E as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.