Current collected state Current
Observed since 21 Oct, 10:08 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
Enterprise-grade context-aware semantic chunking library powered by LLMs.
Context-Aware Semantic Chunking for RAG
Smart AI Chunking 是一个专为大模型(RAG)设计的高级文本切分库。与传统的固定长度切分(Fixed-Size Chunking)不同,它采用**“先理解,后切分”**的两阶段策略,确保每个切片都包含完整的语义和全局上下文。
可以通过 PyPI 直接安装:
pip install ai-chunking-ctk
或者从源码安装:
git clone https://github.com/chutiankuo0121/ai-chunking.git
cd ai-chunking
pip install .
使用我们提供的高级 API,只需几行代码即可完成智能切分。
import asyncio
import json
from ai_chunking import smart_chunk_file, get_openai_wrapper
my_llm = get_openai_wrapper(
api_key="sk-...",
model="deepseek-ai/deepseek-v3.2",
concurrency=20, # 并发数量
base_url="https://..."
)
async def main():
file_path = "document.md"
print(f"开始处理: {file_path}")
async for chunk in smart_chunk_file(
file_path=file_path,
llm_func=my_llm,
target_tokens=3500, # 每个块的目标长度
max_llm_context=256000 # 模型的最大上下文窗口
):
# 打印完整的 JSON 结果
print(json.dumps(chunk, indent=2, ensure_ascii=False))
if __name__ == "__main__":
asyncio.run(main())
smart_chunk_file这是本库的核心入口函数。
async def smart_chunk_file(
file_path: str | Path,
llm_func: Callable[[str], Awaitable[str]],
target_tokens: int,
max_llm_context: int
) -> AsyncGenerator[Dict, None]
file_path: 目标 Markdown 或文本文件路径。llm_func: 用户提供的异步 LLM 调用函数。接收 prompt (str),返回 response (str)。target_tokens: (必填) 每个切片的期望 Token 数(例如 2000-4000)。建议设为模型上下文的 1/3 到 1/10。max_llm_context: (必填) 当前 LLM 的最大上下文窗口(例如 128000)。系统会根据此值自动优化扫描步长。异步生成器,产出如下字典结构:
{
"chunk_id": "ck_001",
"title": "Introduction to AI Chunking",
"summary": "This section introduces the concept of semantic chunking...",
"content": "Full text content of this chunk...",
"tokens": 850
}
Phase 1: Scanner (全知扫描)
Phase 2: Splitter (坐标切分)
[ID: 10] 等坐标标记。MIT License
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 0.2.8 | 20 Feb 2026 | 4 |
This describes our collected data; it is not a security assessment.
Observed since 21 Oct, 10:08 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:ffc16e0e072f9c9a875e36be4334757ea48edd735de1ba18b0e1b4e2925e8793