Text Chunking Strategies for RAG Applications

Chunking strategies greatly affect RAG quality: import { RecursiveCharacterTextSplitter } from ‘langchain/text_splitter’; // Basic chunking const splitter = new RecursiveCharacterTextSplitter({ chunkSize: 1000, chunkOverlap: 200, separators: [‘ ‘, ‘ ‘, ‘ ‘, ”] }); const chunks = await splitter.splitText(document); Semantic Chunking async function semanticChunk(text, maxTokens = 500) { const sentences = text.match(/[^.!?]+[.!?]+/g) || [text]; const chunks…

Building Conversational AI with Context Memory in Node.js

Building a conversational AI with memory requires careful context management: class ConversationManager { constructor(options = {}) { this.maxTokens = options.maxTokens || 4000; this.systemPrompt = options.systemPrompt || ‘You are a helpful assistant.’; this.conversations = new Map(); } getHistory(sessionId) { if (!this.conversations.has(sessionId)) { this.conversations.set(sessionId, []); } return this.conversations.get(sessionId); } async chat(sessionId, userMessage) { const history = this.getHistory(sessionId);…