langchain_text_splitters.base
.TextSplitter¶
- class langchain_text_splitters.base.TextSplitter(chunk_size: int = 4000, chunk_overlap: int = 200, length_function: ~typing.Callable[[str], int] = <built-in function len>, keep_separator: bool = False, add_start_index: bool = False, strip_whitespace: bool = True)[source]¶
文本分割接口。
创建一个新的TextSplitter。
- 参数:
chunk_size:返回的块的最大大小 chunk_overlap:块之间的字符重叠 length_function:测量给定块长度的函数 keep_separator:是否在块中保留分隔符 add_start_index:如果为`True`,在元数据中包含块的起始索引 strip_whitespace:如果为`True`,从每个文档的开头和结尾去除空白字符
Methods
__init__
([chunk_size, chunk_overlap, ...])创建一个新的TextSplitter。
atransform_documents
(documents, **kwargs)异步转换文档列表。
create_documents
(texts[, metadatas])从文本列表中创建文档。
from_huggingface_tokenizer
(tokenizer, **kwargs)使用HuggingFace分词器进行文本拆分以计算长度。
from_tiktoken_encoder
([encoding_name, ...])使用tiktoken编码器来计算长度的文本分割器。
split_documents
(documents)分割文档。
split_text
(text)将文本分割成多个部分。
transform_documents
(documents, **kwargs)将文档序列通过拆分进行转换。
- Parameters
chunk_size (int) –
chunk_overlap (int) –
length_function (Callable[[str], int]) –
keep_separator (bool) –
add_start_index (bool) –
strip_whitespace (bool) –
- Return type
None
- __init__(chunk_size: int = 4000, chunk_overlap: int = 200, length_function: ~typing.Callable[[str], int] = <built-in function len>, keep_separator: bool = False, add_start_index: bool = False, strip_whitespace: bool = True) None [source]¶
创建一个新的TextSplitter。
- 参数:
chunk_size:返回的块的最大大小 chunk_overlap:块之间的字符重叠 length_function:测量给定块长度的函数 keep_separator:是否在块中保留分隔符 add_start_index:如果为`True`,在元数据中包含块的起始索引 strip_whitespace:如果为`True`,从每个文档的开头和结尾去除空白字符
- Parameters
chunk_size (int) –
chunk_overlap (int) –
length_function (Callable[[str], int]) –
keep_separator (bool) –
add_start_index (bool) –
strip_whitespace (bool) –
- Return type
None
- async atransform_documents(documents: Sequence[Document], **kwargs: Any) Sequence[Document] ¶
异步转换文档列表。
- 参数:
documents:要转换的文档序列。
- 返回:
转换后的文档列表。
- create_documents(texts: List[str], metadatas: Optional[List[dict]] = None) List[Document] [source]¶
从文本列表中创建文档。
- Parameters
texts (List[str]) –
metadatas (Optional[List[dict]]) –
- Return type
List[Document]
- classmethod from_huggingface_tokenizer(tokenizer: Any, **kwargs: Any) TextSplitter [source]¶
使用HuggingFace分词器进行文本拆分以计算长度。
- Parameters
tokenizer (Any) –
kwargs (Any) –
- Return type
- classmethod from_tiktoken_encoder(encoding_name: str = 'gpt2', model_name: Optional[str] = None, allowed_special: Union[Literal['all'], AbstractSet[str]] = {}, disallowed_special: Union[Literal['all'], Collection[str]] = 'all', **kwargs: Any) TS [source]¶
使用tiktoken编码器来计算长度的文本分割器。
- Parameters
encoding_name (str) –
model_name (Optional[str]) –
allowed_special (Union[Literal['all'], AbstractSet[str]]) –
disallowed_special (Union[Literal['all'], Collection[str]]) –
kwargs (Any) –
- Return type
TS