微信扫码
与创始人交个朋友
我要投稿
Chonkie是一个用于RAG(检索增强生成)任务的轻量级、快速的文本分块库。
安装命令 | 适用场景 | 依赖 |
---|---|---|
pip install chonkie | 基本的token和word分块 | autotiktokenizer |
pip install chonkie[semantic] | 语义分块 | + sentence-transformers, numpy |
pip install chonkie[all] | 所有功能 | all dependencies |
chunker种类 | default | semantic | all |
---|---|---|---|
TokenChunker | ✅ | ✅ | ✅ |
WordChunker | ✅ | ✅ | ✅ |
SentenceChunker | ✅ | ✅ | ✅ |
SemanticChunker | ❌ | ✅ | ✅ |
SDPMChunker | ❌ | ✅ | ✅ |
# Import the TokenChunker
from chonkie import TokenChunker
from autotiktokenizer import AutoTikTokenizer
# Initialize the tokenizer
tokenizer = AutoTikTokenizer.from_pretrained("gpt2")
# Initialize the chunker
chunker = TokenChunker(
tokenizer=tokenizer,
chunk_size=512,
chunk_overlap=128
)
# Chunk a single piece of text
chunks = chunker.chunk("Woah! Chonkie, the chunking library is so cool! I love the tiny hippo hehe.")
for chunk in chunks:
print(f"Chunk: {chunk.text}")
print(f"Tokens: {chunk.token_count}")
# Chunk a batch of texts
texts = ["First text to chunk.", "Second text to chunk."]
batch_chunks = chunker.chunk_batch(texts)
for text_chunks in batch_chunks:
for chunk in text_chunks:
print(f"Chunk: {chunk.text}")
print(f"Tokens: {chunk.token_count}")
# Use the chunker as a callable
chunks = chunker("Another text to chunk using __call__.")
for chunk in chunks:
print(f"Chunk: {chunk.text}")
print(f"Tokens: {chunk.token_count}")
from chonkie import WordChunker
from autotiktokenizer import AutoTikTokenizer
tokenizer = AutoTikTokenizer.from_pretrained("gpt2")
chunker = WordChunker(
tokenizer=tokenizer,
chunk_size=512,
chunk_overlap=128,
mode="advanced" # 'simple-基本的基于空间的分割' or 'advanced-处理标点符号和特殊大小写'
)
from chonkie import SentenceChunker
from autotiktokenizer import AutoTikTokenizer
tokenizer = AutoTikTokenizer.from_pretrained("gpt2")
chunker = SentenceChunker(
tokenizer=tokenizer, # (可选)传入您选择的分词器,可以接受 tiktoken、tokenizer 和 transformers 分词器,优先授予 tiktoken。
chunk_size=512, # (可选)传递块的大小。默认为 tokenizer 支持的最大大小(如果有)或 512。
chunk_overlap=128, # (可选)接受 int 或 float。文本的连续块之间的重叠。默认为 min(0.25 * chunk_size, 128)。
min_sentences_per_chunk=1 # 每个区块的最小句子数
)
⚠️:大多数情况下,chunk_size、token_count取决于向量化模型上下文大小,而不是生成模型上下文长度。
from chonkie import SemanticChunker
chunker = SemanticChunker(
embedding_model="all-minilm-l6-v2",
max_chunk_size=512, # 从 SemanticChunker 接收的 chunk 的最大大小
similarity_threshold=0.7 # 语义分组的阈值
)
通过语义双通道合并方法对内容进行分组,该方法通过使用跳过窗口对语义相似的段落进行分组,即使它们不是连续出现的。
from chonkie import SDPMChunker
chunker = SDPMChunker(
embedding_model="all-minilm-l6-v2",
max_chunk_size=512,
similarity_threshold=0.7,
skip_window=1 # 分块程序应注意的跳过窗口的大小。默认为 1。
)
from chonkie import TokenChunker
from tokenizers import Tokenizer
tokenizer = Tokenizer.from_pretrained("gpt2")
chunker = TokenChunker(tokenizer)
chunks = chunker("Woah! Chonkie, the chunking library is so cool!")
for chunk in chunks:
print(f"Chunk: {chunk.text}")
print(f"Tokens: {chunk.token_count}")
from chonkie import SemanticChunker
chunker = SemanticChunker(embedding_model="all-minilm-l6-v2", max_chunk_size=512, similarity_threshold=0.7)
chunks = chunker("Woah! Chonkie, the chunking library is so cool!")
for chunk in chunks:
print(f"Chunk: {chunk.text}")
print(f"Tokens: {chunk.token_count}")
53AI,企业落地应用大模型首选服务商
产品:大模型应用平台+智能体定制开发+落地咨询服务
承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2024-07-18
2024-05-05
2024-09-04
2024-06-20
2024-05-19
2024-07-09
2024-07-09
2024-07-07
2024-06-13
2024-07-07