rag-implementation
RAG (Retrieval Augmented Generation) implementation patterns including document chunking, embedding generation, vector database integration, semantic search, and RAG pipelines. Use when building RAG systems, implementing semantic search, creating knowledge bases, or when user mentions RAG, embeddings, vector database, retrieval, document chunking, or knowledge retrieval.
What this skill does
# RAG Implementation Patterns
**Purpose:** Provide complete RAG pipeline templates, chunking strategies, vector database schemas, and retrieval patterns for building production-ready RAG systems with Vercel AI SDK.
**Activation Triggers:**
- Building RAG (Retrieval Augmented Generation) systems
- Implementing semantic search functionality
- Creating AI-powered knowledge bases
- Document ingestion and embedding generation
- Vector database integration
- Hybrid search (vector + keyword) implementation
**Key Resources:**
- `templates/rag-pipeline.ts` - Complete RAG pipeline template
- `templates/vector-db-schemas/` - Database schemas for Pinecone, Chroma, pgvector, Weaviate
- `templates/chunking-strategies.ts` - Document chunking implementations
- `templates/retrieval-patterns.ts` - Semantic search and hybrid search patterns
- `scripts/chunk-documents.sh` - Document chunking utility
- `scripts/generate-embeddings.sh` - Batch embedding generation
- `scripts/validate-rag-setup.sh` - Validate RAG configuration
- `examples/` - Complete RAG implementations (chatbot, Q&A, search)
## Core RAG Pipeline
### 1. Document Ingestion → Chunking → Embedding → Storage → Retrieval → Generation
**Template:** `templates/rag-pipeline.ts`
**Workflow:**
```typescript
// 1. Ingest documents
const documents = await loadDocuments()
// 2. Chunk documents
const chunks = await chunkDocuments(documents, {
chunkSize: 1000
overlap: 200
strategy: 'semantic'
})
// 3. Generate embeddings
const embeddings = await embedMany({
model: openai.embedding('text-embedding-3-small')
values: chunks.map(c => c.text)
})
// 4. Store in vector DB
await vectorDB.upsert(chunks.map((chunk, i) => ({
id: chunk.id
embedding: embeddings.embeddings[i]
metadata: chunk.metadata
})))
// 5. Retrieve relevant chunks
const query = await embed({
model: openai.embedding('text-embedding-3-small')
value: userQuestion
})
const results = await vectorDB.query({
vector: query.embedding
topK: 5
})
// 6. Generate response with context
const response = await generateText({
model: openai('gpt-4o')
messages: [
{
role: 'system'
content: `Answer based on this context:\n\n${results.map(r => r.text).join('\n\n')}`
}
{ role: 'user', content: userQuestion }
]
})
```
## Chunking Strategies
### 1. Fixed-Size Chunking
**When to use:** Simple documents, consistent structure
**Template:** `templates/chunking-strategies.ts#fixedSize`
```typescript
function chunkByFixedSize(text: string, chunkSize: number, overlap: number) {
const chunks = []
for (let i = 0; i < text.length; i += chunkSize - overlap) {
chunks.push(text.slice(i, i + chunkSize))
}
return chunks
}
```
**Best for:** Articles, blog posts, documentation
### 2. Semantic Chunking
**When to use:** Preserve meaning and context
**Template:** `templates/chunking-strategies.ts#semantic`
```typescript
function chunkBySemantic(text: string) {
// Split on paragraphs, headings, or natural breaks
const sections = text.split(/\n\n+/)
const chunks = []
let currentChunk = ''
for (const section of sections) {
if ((currentChunk + section).length > 1000) {
if (currentChunk) chunks.push(currentChunk.trim())
currentChunk = section
} else {
currentChunk += '\n\n' + section
}
}
if (currentChunk) chunks.push(currentChunk.trim())
return chunks
}
```
**Best for:** Books, research papers, structured content
### 3. Recursive Chunking
**When to use:** Hierarchical documents with sections/subsections
**Template:** `templates/chunking-strategies.ts#recursive`
**Best for:** Technical docs, manuals, legal documents
## Vector Database Integration
### Supported Databases
**1. Pinecone (Fully Managed)**
**Template:** `templates/vector-db-schemas/pinecone-schema.ts`
```typescript
import { Pinecone } from '@pinecone-database/pinecone'
const pinecone = new Pinecone({
apiKey: process.env.PINECONE_API_KEY!
})
const index = pinecone.index('knowledge-base')
// Upsert embeddings
await index.upsert([
{
id: 'doc-1-chunk-1'
values: embedding
metadata: {
text: chunk.text
source: chunk.source
timestamp: Date.now()
}
}
])
// Query
const results = await index.query({
vector: queryEmbedding
topK: 5
includeMetadata: true
})
```
**2. Chroma (Open Source)**
**Template:** `templates/vector-db-schemas/chroma-schema.ts`
**Best for:** Local development, prototyping
**3. pgvector (Postgres Extension)**
**Template:** `templates/vector-db-schemas/pgvector-schema.sql`
**Best for:** Existing Postgres infrastructure, cost-effective
**4. Weaviate (Open Source/Cloud)**
**Template:** `templates/vector-db-schemas/weaviate-schema.ts`
**Best for:** Advanced filtering, hybrid search
## Retrieval Patterns
### 1. Simple Semantic Search
**Template:** `templates/retrieval-patterns.ts#simpleSearch`
```typescript
async function semanticSearch(query: string, topK: number = 5) {
// Embed query
const { embedding } = await embed({
model: openai.embedding('text-embedding-3-small')
value: query
})
// Search vector DB
const results = await vectorDB.query({
vector: embedding
topK
})
return results
}
```
### 2. Hybrid Search (Vector + Keyword)
**Template:** `templates/retrieval-patterns.ts#hybridSearch`
```typescript
async function hybridSearch(query: string, topK: number = 10) {
// Vector search
const vectorResults = await semanticSearch(query, topK)
// Keyword search (BM25 or full-text)
const keywordResults = await fullTextSearch(query, topK)
// Combine and re-rank
const combined = rerank(vectorResults, keywordResults)
return combined.slice(0, topK)
}
```
**Best practice:** Use hybrid search for better recall
### 3. Re-Ranking
**Template:** `templates/retrieval-patterns.ts#reranking`
```typescript
async function rerankResults(query: string, results: any[]) {
// Use cross-encoder or LLM for re-ranking
const reranked = await generateObject({
model: openai('gpt-4o')
schema: z.object({
rankedIds: z.array(z.string())
})
messages: [
{
role: 'system'
content: 'Rank these documents by relevance to the query.'
}
{
role: 'user'
content: `Query: ${query}\n\nDocuments: ${JSON.stringify(results)}`
}
]
})
return reranked.object.rankedIds.map(id =>
results.find(r => r.id === id)
)
}
```
## Implementation Workflow
### Step 1: Validate RAG Setup
```bash
# Check dependencies and configuration
./scripts/validate-rag-setup.sh
```
**Checks:**
- AI SDK installation
- Vector database client installed
- Environment variables configured
- Embedding model accessible
### Step 2: Choose Chunking Strategy
**Decision tree:**
- Uniform documents → Fixed-size chunking
- Natural sections → Semantic chunking
- Hierarchical structure → Recursive chunking
- Mixed content → Hybrid approach
### Step 3: Select Vector Database
**Considerations:**
- **Pinecone**: Best for production, fully managed, higher cost
- **Chroma**: Best for prototypes, local development, free
- **pgvector**: Best if using Postgres, cost-effective
- **Weaviate**: Best for complex filtering, hybrid search
### Step 4: Implement Embedding Generation
```bash
# Batch generate embeddings
./scripts/generate-embeddings.sh ./documents/ openai
```
**Optimization:**
- Use `embedMany` for batch processing
- Implement rate limiting for API quotas
- Cache embeddings to avoid re-generation
- Use cheaper models for prototyping
### Step 5: Build Retrieval Pipeline
**Use template:** `templates/retrieval-patterns.ts`
**Customize:**
1. Set topK (typically 3-10 chunks)
2. Add metadata filtering if needed
3. Implement re-ranking for better results
4. Add hybrid search for improved recall
### Step 6: Integrate with Generation
**Pattern:**
```typescript
const context = retrievedChunks.map(chunk => chunk.text).join('\n\n')
const response = await generateText({
model: openai(Related in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.