‹ 首页

chunking-strategy

@giuseppe-trisciuoglio · 收录于 1 周前

Provides chunking strategies for RAG systems. Generates chunk size recommendations (256-1024 tokens), overlap percentages (10-20%), and semantic boundary detection methods. Validates semantic coherence and evaluates retrieval precision/recall metrics. Use when building retrieval-augmented generation systems, vector databases, or processing large documents.

适合你,如果在构建检索增强生成系统时需要优化文档分块参数

/ 下载安装
chunking-strategy.skill双击,或拖进 Claude 桌面版 / Cowork,即完成安装↓ .skill↓ .zip
用别的 agent?下载 .zip 解压,把文件夹放进它的技能目录
Claude Code~/.claude/skills/(项目级 .claude/skills/)
Codex CLI~/.codex/skills/
Cursor自动读取上面两处目录
其他工具见其文档的「skills」目录;两个下载是同一份文件,只是名字不同
/ 通过 npx 安装 校验哈希
npx oh-my-skill add giuseppe-trisciuoglio/developer-kit/chunking-strategy
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- giuseppe-trisciuoglio/developer-kit/chunking-strategy
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify giuseppe-trisciuoglio/developer-kit/chunking-strategy
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
303GitHub stars
~1.2K最小装载
~39.3K含声明引用
~39.3K文本包总量
镜像托管

怎么用

技能原文 SKILL.md作者撰写 · MIT · 306f428

Chunking Strategy for RAG Systems

Overview

Provides chunking strategies for RAG systems, vector databases, and document processing. Recommends chunk sizes, overlap percentages, and boundary detection methods; validates semantic coherence; evaluates retrieval metrics.

When to Use

Use when building or optimizing RAG systems, vector search pipelines, document chunking workflows, or performance-tuning existing systems with poor retrieval quality.

Instructions
Choose Chunking Strategy

Select based on document type and use case:

  1. Fixed-Size Chunking (Level 1)
  2. Use for simple documents without clear structure
  3. Start with 512 tokens and 10-20% overlap
  4. Adjust: 256 for factoid queries, 1024 for analytical
  1. Recursive Character Chunking (Level 2)
  2. Use for documents with structural boundaries
  3. Hierarchical separators: paragraphs → sentences → words
  4. Customize for document types (HTML, Markdown, JSON)
  1. Structure-Aware Chunking (Level 3)
  2. Use for structured content (Markdown, code, tables, PDFs)
  3. Preserve semantic units: functions, sections, table blocks
  4. Validate structure preservation post-split
  1. Semantic Chunking (Level 4)
  2. Use for complex documents with thematic shifts
  3. Embedding-based boundary detection with 0.8 similarity threshold
  4. Buffer size: 3-5 sentences
  1. Advanced Methods (Level 5)
  2. Late Chunking for long-context models
  3. Contextual Retrieval for high-precision requirements
  4. Monitor computational cost vs. retrieval gain

Reference: [references/strategies.md](references/strategies.md).

Implement Chunking Pipeline
  1. Pre-process documents
  2. Analyze structure, content types, information density
  3. Identify multi-modal content (tables, images, code)
  1. Select parameters
  2. Chunk size: embedding model context window / 4
  3. Overlap: 10-20% for most cases
  4. Strategy-specific settings
  1. Process and validate
  2. Apply chunking strategy
  3. Validate coherence: run evaluate_chunks.py --coherence (see below)
  4. Test with representative documents
  1. Evaluate and iterate
  2. Measure precision and recall
  3. If precision < 0.7: reduce chunk_size by 25% and re-evaluate
  4. If recall < 0.6: increase overlap by 10% and re-evaluate
  5. Monitor latency and memory usage

Reference: [references/implementation.md](references/implementation.md).

Validate Chunk Quality

Run validation commands to assess chunk quality:

# Check semantic coherence (requires sentence-transformers)
python -c "
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
chunks = [...]  # your chunks
embeddings = model.encode(chunks)
similarity = (embeddings @ embeddings.T).mean()
print(f'Cohesion: {similarity:.3f}')  # target: 0.3-0.7
"

# Measure retrieval precision
python -c "
relevant = sum(1 for c in retrieved if c in relevant_chunks)
precision = relevant / len(retrieved)
print(f'Precision: {precision:.2f}')  # target: >= 0.7
"

# Check chunk size distribution
python -c "
import numpy as np
sizes = [len(c.split()) for c in chunks]
print(f'Mean: {np.mean(sizes):.0f}, Std: {np.std(sizes):.0f}')
print(f'Min: {min(sizes)}, Max: {max(sizes)}')
"

Reference: [references/evaluation.md](references/evaluation.md).

Examples
Fixed-Size Chunking
from langchain.text_splitter import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(
    chunk_size=256,
    chunk_overlap=25,
    length_function=len
)
chunks = splitter.split_documents(documents)
Structure-Aware Code Chunking
import ast

def chunk_python_code(code):
    tree = ast.parse(code)
    chunks = []
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef, ast.ClassDef)):
            chunks.append(ast.get_source_segment(code, node))
    return chunks
Semantic Chunking
def semantic_chunk(text, similarity_threshold=0.8):
    sentences = split_into_sentences(text)
    embeddings = generate_embeddings(sentences)
    chunks, current = [], [sentences[0]]
    for i in range(1, len(sentences)):
        sim = cosine_similarity(embeddings[i-1], embeddings[i])
        if sim < similarity_threshold:
            chunks.append(" ".join(current))
            current = [sentences[i]]
        else:
            current.append(sentences[i])
    chunks.append(" ".join(current))
    return chunks
Best Practices
Core Principles
  • Balance context preservation with retrieval precision
  • Maintain semantic coherence within chunks
  • Optimize for embedding model context window constraints
Implementation
  • Start with fixed-size (512 tokens, 15% overlap)
  • Iterate based on document characteristics
  • Test with domain-specific documents before deployment
Pitfalls to Avoid
  • Over-chunking: context-poor small chunks
  • Under-chunking: missing information in oversized chunks
  • Ignoring semantic boundaries and document structure
  • One-size-fits-all for diverse content types
Constraints and Warnings
Resource Considerations
  • Semantic methods require significant compute resources
  • Late chunking needs long-context embedding models
  • Complex strategies increase processing latency
  • Monitor memory for large document batches
Quality Requirements
  • Validate semantic coherence post-processing
  • Test with representative documents before deployment
  • Ensure chunks maintain standalone meaning
  • Implement error handling for malformed content
References
  • [strategies.md](references/strategies.md) - Detailed strategies
  • [implementation.md](references/implementation.md) - Implementation guidelines
  • [evaluation.md](references/evaluation.md) - Performance metrics
  • [tools.md](references/tools.md) - Libraries and frameworks
  • [research.md](references/research.md) - Research papers
  • [advanced-strategies.md](references/advanced-strategies.md) - 11 advanced methods
  • [semantic-methods.md](references/semantic-methods.md) - Semantic approaches
  • [visualization-tools.md](references/visualization-tools.md) - Visualization tools
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。