跳转到内容

langchain-text-splitters

PyPI - Version PyPI - License PyPI - Downloads

langchain-text-splitters 包的参考文档。

langchain_text_splitters

文本分割器是用于分割文本的类。

注意

MarkdownHeaderTextSplitterHTMLHeaderTextSplitter 不派生自 TextSplitter

函数 描述
split_text_on_tokens

使用分词器分割输入文本并返回块。

Language

基类: str, Enum

编程语言的枚举。

TextSplitter

基类:BaseDocumentTransformer, ABC

将文本分割成块的接口。

方法 描述
atransform_documents

异步转换文档列表。

__init__

创建一个新的 TextSplitter。

split_text

将文本分割成多个组件。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

transform_documents

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

__init__

__init__(
    chunk_size: int = 4000,
    chunk_overlap: int = 200,
    length_function: Callable[[str], int] = len,
    keep_separator: bool | Literal["start", "end"] = False,
    add_start_index: bool = False,
    strip_whitespace: bool = True,
) -> None

创建一个新的 TextSplitter。

参数 描述
chunk_size

返回的块的最大大小

类型: int 默认值: 4000

chunk_overlap

块之间的字符重叠

类型: int 默认值: 200

length_function

测量给定块长度的函数

类型: Callable[[str], int] 默认值: len

keep_separator

是否保留分隔符以及将其放置在每个对应块中的位置 (True='start')

类型: bool | Literal['start', 'end'] 默认值: False

add_start_index

如果为 True,则在元数据中包含块的起始索引

类型: bool 默认值: False

strip_whitespace

如果为 True,则从每个文档的开头和结尾剥离空白

类型: bool 默认值: True

split_text abstractmethod

split_text(text: str) -> list[str]

将文本分割成多个组件。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

Tokenizer dataclass

分词器数据类。

chunk_overlap instance-attribute

chunk_overlap: int

块之间令牌的重叠

tokens_per_chunk instance-attribute

tokens_per_chunk: int

每个块的最大令牌数

decode instance-attribute

decode: Callable[[list[int]], str]

将令牌 ID 列表解码为字符串的函数

encode instance-attribute

encode: Callable[[str], list[int]]

将字符串编码为令牌 ID 列表的函数

TokenTextSplitter

基类:TextSplitter

使用模型分词器将文本分割成令牌。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

__init__

创建一个新的 TextSplitter。

split_text

根据分词将输入文本分割成更小的块。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

__init__

__init__(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> None

创建一个新的 TextSplitter。

split_text

split_text(text: str) -> list[str]

根据分词将输入文本分割成更小的块。

此方法使用自定义分词器配置将输入文本编码为令牌,以指定大小和重叠的块处理令牌,然后将它们解码回文本块。分割使用 split_text_on_tokens 函数执行。

参数 描述
text

要分割成更小块的输入文本。

类型: str

返回 描述
list[str]

文本块列表,其中每个块都基于分词和分块规则从输入文本的一部分派生。

CharacterTextSplitter

基类:TextSplitter

按字符分割文本。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

__init__

创建一个新的 TextSplitter。

split_text

分割成块而不重新插入环视分隔符。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

__init__

__init__(
    separator: str = "\n\n", is_separator_regex: bool = False, **kwargs: Any
) -> None

创建一个新的 TextSplitter。

split_text

split_text(text: str) -> list[str]

分割成块而不重新插入环视分隔符。

RecursiveCharacterTextSplitter

基类:TextSplitter

通过递归查找字符来分割文本。

递归地尝试按不同的字符分割以找到有效的分隔符。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

__init__

创建一个新的 TextSplitter。

split_text

根据预定义的分隔符将输入文本分割成更小的块。

from_language

根据特定语言返回此类的实例。

get_separators_for_language

检索给定语言的特定分隔符列表。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

__init__

__init__(
    separators: list[str] | None = None,
    keep_separator: bool | Literal["start", "end"] = True,
    is_separator_regex: bool = False,
    **kwargs: Any,
) -> None

创建一个新的 TextSplitter。

split_text

split_text(text: str) -> list[str]

根据预定义的分隔符将输入文本分割成更小的块。

参数 描述
text

要分割的输入文本。

类型: str

返回 描述
list[str]

分割后获得的文本块列表。

from_language classmethod

from_language(language: Language, **kwargs: Any) -> RecursiveCharacterTextSplitter

根据特定语言返回此类的实例。

此方法使用特定于语言的分隔符初始化文本分割器。

参数 描述
language

配置文本分割器所用的语言。

类型: Language

**kwargs

用于自定义分割器的其他关键字参数。

类型: Any 默认值: {}

返回 描述
RecursiveCharacterTextSplitter

为指定语言配置的文本分割器实例。

get_separators_for_language staticmethod

get_separators_for_language(language: Language) -> list[str]

检索给定语言的特定分隔符列表。

参数 描述
language

要获取分隔符的语言。

类型: Language

返回 描述
list[str]

适用于指定语言的分隔符列表。

ElementType

基类:TypedDict

类型化字典中的元素类型。

HTMLHeaderTextSplitter

根据指定的标题将 HTML 内容分割成结构化文档。

通过检测指定的标题标签并将 HTML 内容分割,并创建反映原始内容语义层次结构的 Document 对象。对于每个识别出的部分,分割器将提取的文本与与遇到的标题对应的元数据关联起来。

如果未找到指定的标题,则整个内容将作为单个 Document 返回。这允许灵活处理 HTML 输入,确保信息根据其语义标题进行组织。

分割器提供选项,可以将每个 HTML 元素作为单独的 Document 返回,或者将它们聚合到具有语义意义的块中。它还优雅地处理多级嵌套标题,创建丰富、分层的内容表示。

示例
from langchain_text_splitters.html_header_text_splitter import (
    HTMLHeaderTextSplitter,
)

# Define headers for splitting on h1 and h2 tags.
headers_to_split_on = [("h1", "Main Topic"), ("h2", "Sub Topic")]

splitter = HTMLHeaderTextSplitter(
    headers_to_split_on=headers_to_split_on,
    return_each_element=False
)

html_content = """
<html>
    <body>
        <h1>Introduction</h1>
        <p>Welcome to the introduction section.</p>
        <h2>Background</h2>
        <p>Some background details here.</p>
        <h1>Conclusion</h1>
        <p>Final thoughts.</p>
    </body>
</html>
"""

documents = splitter.split_text(html_content)

# 'documents' now contains Document objects reflecting the hierarchy:
# - Document with metadata={"Main Topic": "Introduction"} and
#   content="Introduction"
# - Document with metadata={"Main Topic": "Introduction"} and
#   content="Welcome to the introduction section."
# - Document with metadata={"Main Topic": "Introduction",
#   "Sub Topic": "Background"} and content="Background"
# - Document with metadata={"Main Topic": "Introduction",
#   "Sub Topic": "Background"} and content="Some background details here."
# - Document with metadata={"Main Topic": "Conclusion"} and
#   content="Conclusion"
# - Document with metadata={"Main Topic": "Conclusion"} and
#   content="Final thoughts."
方法 描述
__init__

使用要分割的标题进行初始化。

split_text

将给定文本分割成 Document 对象列表。

split_text_from_url

从 URL 获取文本内容并将其分割成文档。

split_text_from_file

将文件中的 HTML 内容分割成 Document 对象列表。

__init__

__init__(
    headers_to_split_on: list[tuple[str, str]], return_each_element: bool = False
) -> None

使用要分割的标题进行初始化。

参数 描述
headers_to_split_on

表示分割边界的 (header_tag, header_name) 对列表。例如,[("h1", "Header 1"), ("h2", "Header 2")] 将按 h1h2 标签分割内容,将其文本内容分配给 Document 元数据。

类型: list[tuple[str, str]]

return_each_element

如果为 True,则遇到的每个 HTML 元素(包括标题、段落等)都将作为单独的 Document 返回。如果为 False,则相同标题层次结构下的内容将聚合为更少的 Document 对象。

类型: bool 默认值: False

split_text

split_text(text: str) -> list[Document]

将给定文本分割成 Document 对象列表。

参数 描述
text

要分割的 HTML 文本。

类型: str

返回 描述
list[Document]

分割后的 Document 对象列表。每个 Document 包含提取的文本 page_content 和将标题层次结构映射到其相应标题的 metadata

split_text_from_url

split_text_from_url(url: str, timeout: int = 10, **kwargs: Any) -> list[Document]

从 URL 获取文本内容并将其分割成文档。

参数 描述
url

要获取内容的 URL。

类型: str

timeout

请求超时时间。

类型: int 默认值: 10

**kwargs

请求的其他关键字参数。

类型: Any 默认值: {}

返回 描述
list[Document]

分割后的 Document 对象列表。每个 Document 包含提取的文本 page_content 和将标题层次结构映射到其相应标题的 metadata

引发 描述
RequestException

如果 HTTP 请求失败。

split_text_from_file

split_text_from_file(file: str | IO[str]) -> list[Document]

将文件中的 HTML 内容分割成 Document 对象列表。

参数 描述
file

包含 HTML 内容的文件路径或类似文件的对象。

类型: str | IO[str]

返回 描述
list[Document]

分割后的 Document 对象列表。每个 Document 包含提取的文本 page_content 和将标题层次结构映射到其相应标题的 metadata

HTMLSectionSplitter

根据指定的标签和字体大小分割 HTML 文件。

需要 lxml 包。

方法 描述
__init__

创建一个新的 HTMLSectionSplitter

split_documents

分割文档。

split_text

分割 HTML 文本字符串。

create_documents

从文本列表创建 Document 对象列表。

split_html_by_headers

根据指定的标题标签将 HTML 文档分割成节。

convert_possible_tags_to_header

使用 XSLT 转换将特定的 HTML 标签转换为标题。

split_text_from_file

将文件中的 HTML 内容分割成 Document 对象列表。

__init__

__init__(headers_to_split_on: list[tuple[str, str]], **kwargs: Any) -> None

创建一个新的 HTMLSectionSplitter

参数 描述
headers_to_split_on

要跟踪的标题列表,映射到元数据的(任意)键。允许的标题值:h1h2h3h4h5h6,例如 [("h1", "Header 1"), ("h2", "Header 2"]

类型: list[tuple[str, str]]

**kwargs

其他可选的自定义参数。

类型: Any 默认值: {}

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

split_text

split_text(text: str) -> list[Document]

分割 HTML 文本字符串。

参数 描述
text

HTML 文本

类型: str

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_html_by_headers

split_html_by_headers(html_doc: str) -> list[dict[str, str | None]]

根据指定的标题标签将 HTML 文档分割成节。

此方法使用 BeautifulSoup 解析 HTML 内容,并根据 headers_to_split_on 中定义的标题将其分割成节。每个节包含标题文本、标题下的内容以及标签名称。

参数 描述
html_doc

要分割成节的 HTML 文档。

类型: str

返回 描述
list[dict[str, str | None]]

表示节的字典列表。每个字典包含

  • 'header':标题文本或第一部分的默认标题。
  • 'content':标题下的内容。
  • 'tag_name':标题标签的名称(例如,h1h2)。

convert_possible_tags_to_header

convert_possible_tags_to_header(html_content: str) -> str

使用 XSLT 转换将特定的 HTML 标签转换为标题。

此方法使用 XSLT 文件转换 HTML 内容,将某些标签转换为标题以便于解析。如果未提供 XSLT 路径,则 HTML 内容将原样返回。

参数 描述
html_content

要转换的 HTML 内容。

类型: str

返回 描述
str

转换后的 HTML 内容作为字符串。

split_text_from_file

split_text_from_file(file: StringIO) -> list[Document]

将文件中的 HTML 内容分割成 Document 对象列表。

参数 描述
file

包含 HTML 内容的文件路径或类似文件的对象。

类型: StringIO

返回 描述
list[Document]

分割后的 Document 对象列表。

HTMLSemanticPreservingSplitter

基类:BaseDocumentTransformer

分割 HTML 内容,同时保留语义结构。

按标题将 HTML 内容分割成通用块,同时保留语义结构。如果块超出最大块大小,它将使用 RecursiveCharacterTextSplitter 进行进一步分割。

分割器保留完整的 HTML 元素,并将链接转换为类似 Markdown 的链接。它还可以通过将图像、视频和音频元素转换为 Markdown 格式来保留它们。请注意,为了保持语义完整性,某些块可能会超出最大大小。

0.3.5 版新增

示例
from langchain_text_splitters.html import HTMLSemanticPreservingSplitter

def custom_iframe_extractor(iframe_tag):
    ```
    Custom handler function to extract the 'src' attribute from an <iframe> tag.
    Converts the iframe to a Markdown-like link: [iframe:<src>](src).

    Args:
        iframe_tag (bs4.element.Tag): The <iframe> tag to be processed.

    Returns:
        str: A formatted string representing the iframe in Markdown-like format.
    ```
    iframe_src = iframe_tag.get('src', '')
    return f"[iframe:{iframe_src}]({iframe_src})"

text_splitter = HTMLSemanticPreservingSplitter(
    headers_to_split_on=[("h1", "Header 1"), ("h2", "Header 2")],
    max_chunk_size=500,
    preserve_links=True,
    preserve_images=True,
    custom_handlers={"iframe": custom_iframe_extractor}
)
方法 描述
atransform_documents

异步转换文档列表。

__init__

初始化分割器。

split_text

根据配置将提供的 HTML 文本分割成较小的块。

transform_documents

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

__init__

__init__(
    headers_to_split_on: list[tuple[str, str]],
    *,
    max_chunk_size: int = 1000,
    chunk_overlap: int = 0,
    separators: list[str] | None = None,
    elements_to_preserve: list[str] | None = None,
    preserve_links: bool = False,
    preserve_images: bool = False,
    preserve_videos: bool = False,
    preserve_audio: bool = False,
    custom_handlers: dict[str, Callable[[Tag], str]] | None = None,
    stopword_removal: bool = False,
    stopword_lang: str = "english",
    normalize_text: bool = False,
    external_metadata: dict[str, str] | None = None,
    allowlist_tags: list[str] | None = None,
    denylist_tags: list[str] | None = None,
    preserve_parent_metadata: bool = False,
    keep_separator: bool | Literal["start", "end"] = True,
) -> None

初始化分割器。

参数 描述
headers_to_split_on

定义内容部分的 HTML 标题(例如 h1h2)。

类型: list[tuple[str, str]]

max_chunk_size

每个块的最大大小,允许超出此限制以保留语义。

类型: int 默认值: 1000

chunk_overlap

为确保上下文连续性,块之间重叠的字符数。

类型: int 默认值: 0

separators

RecursiveCharacterTextSplitter 用于进一步分割的分隔符。

类型: list[str] | None 默认值: None

elements_to_preserve

在分割过程中保持完整的 HTML 标签(例如 tableul)。

类型: list[str] | None 默认值: None

preserve_links

a 标签转换为 Markdown 链接 ([text](url))。

类型: bool 默认值: False

preserve_images

img 标签转换为 Markdown 图像 (![alt](src))。

类型: bool 默认值: False

preserve_videos

video 标签转换为 Markdown 视频链接 (![video](src))。

类型: bool 默认值: False

preserve_audio

audio 标签转换为 Markdown 音频链接 (![audio](src))。

类型: bool 默认值: False

custom_handlers

可选的特定 HTML 标签自定义处理程序,允许量身定制的提取或处理。

类型: dict[str, Callable[[Tag], str]] | None 默认值: None

stopword_removal

(可选)从文本中删除停用词。

类型: bool 默认值: False

stopword_lang

要删除的停用词语言。

类型: str 默认值: 'english'

normalize_text

(可选)规范化文本(例如,小写、删除标点符号)。

类型: bool 默认值: False

external_metadata

要附加到 Document 对象的额外元数据。

类型: dict[str, str] | None 默认值: None

allowlist_tags

只有这些标签将保留在 HTML 中。

类型: list[str] | None 默认值: None

denylist_tags

这些标签将从 HTML 中删除。

类型: list[str] | None 默认值: None

preserve_parent_metadata

在调用 transform_documents/atransform_documents() 时,是否将父文档元数据传递给分割文档。

类型: bool 默认值: False

keep_separator

分隔符应该位于块的开头、结尾还是根本不应该位于块中。

类型: bool | Literal['start', 'end'] 默认值: True

split_text

split_text(text: str) -> list[Document]

根据配置将提供的 HTML 文本分割成较小的块。

参数 描述
text

要分割的 HTML 内容。

类型: str

返回 描述
list[Document]

包含分割内容的 Document 对象列表。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> list[Document]

通过分割来转换文档序列。

RecursiveJsonSplitter

将 JSON 数据分割成更小、结构化的块,同时保留层次结构。

此类提供将 JSON 数据分割成较小的字典或 JSON 格式字符串的方法,基于可配置的最大和最小块大小。它支持嵌套 JSON 结构,可以选择将列表转换为字典以便更好地分块,并允许创建文档对象以供进一步使用。

方法 描述
__init__

为文本处理初始化块大小配置。

split_json

将 JSON 分割成 JSON 块列表。

split_text

将 JSON 分割成 JSON 格式字符串列表。

create_documents

从 json 对象列表(dict)创建 Document 对象列表。

max_chunk_size class-attribute instance-attribute

max_chunk_size: int = max_chunk_size

每个块的最大大小。

min_chunk_size class-attribute instance-attribute

min_chunk_size: int = (
    min_chunk_size if min_chunk_size is not None else max(max_chunk_size - 200, 50)
)

每个块的最小大小,如果未明确提供,则从 max_chunk_size 派生。

__init__

__init__(max_chunk_size: int = 2000, min_chunk_size: int | None = None) -> None

为文本处理初始化块大小配置。

此构造函数设置最大和最小块大小,确保如果未明确提供,min_chunk_size 默认为略小于 max_chunk_size 的值。

参数 描述
max_chunk_size

块的最大大小。

类型: int 默认值: 2000

min_chunk_size

块的最小大小。如果为 None,则默认为最大块大小减去 200,下限为 50。

TYPE: int | None DEFAULT: None

split_json

split_json(
    json_data: dict[str, Any], convert_lists: bool = False
) -> list[dict[str, Any]]

将 JSON 分割成 JSON 块列表。

split_text

split_text(
    json_data: dict[str, Any], convert_lists: bool = False, ensure_ascii: bool = True
) -> list[str]

将 JSON 分割成 JSON 格式字符串列表。

create_documents

create_documents(
    texts: list[dict[str, Any]],
    convert_lists: bool = False,
    ensure_ascii: bool = True,
    metadatas: list[dict[Any, Any]] | None = None,
) -> list[Document]

从 json 对象列表(dict)创建 Document 对象列表。

JSFrameworkTextSplitter

基类:RecursiveCharacterTextSplitter

处理 React (JSX)、Vue 和 Svelte 代码的文本分割器。

此分割器通过以下方式扩展 RecursiveCharacterTextSplitter 以处理 React (JSX)、Vue 和 Svelte 代码:

  1. 检测并从文本中提取自定义组件标签
  2. 将这些标签作为附加分隔符以及标准 JS 语法

分割器结合了

  • 自定义组件标签作为分隔符(例如
  • JavaScript 语法元素(function、const、if 等)
  • 按换行符进行标准文本分割

这允许块在 React、Vue 和 Svelte 组件代码的自然边界处中断。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

from_language

根据特定语言返回此类的实例。

get_separators_for_language

检索给定语言的特定分隔符列表。

__init__

初始化 JS Framework 文本分割器。

split_text

将文本分割成块。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

from_language classmethod

from_language(language: Language, **kwargs: Any) -> RecursiveCharacterTextSplitter

根据特定语言返回此类的实例。

此方法使用特定于语言的分隔符初始化文本分割器。

参数 描述
language

配置文本分割器所用的语言。

类型: Language

**kwargs

用于自定义分割器的其他关键字参数。

类型: Any 默认值: {}

返回 描述
RecursiveCharacterTextSplitter

为指定语言配置的文本分割器实例。

get_separators_for_language staticmethod

get_separators_for_language(language: Language) -> list[str]

检索给定语言的特定分隔符列表。

参数 描述
language

要获取分隔符的语言。

类型: Language

返回 描述
list[str]

适用于指定语言的分隔符列表。

__init__

__init__(
    separators: list[str] | None = None,
    chunk_size: int = 2000,
    chunk_overlap: int = 0,
    **kwargs: Any,
) -> None

初始化 JS Framework 文本分割器。

参数 描述
separators

要使用的自定义分隔符字符串的可选列表

类型: list[str] | None 默认值: None

chunk_size

返回的块的最大大小

类型: int 默认值: 2000

chunk_overlap

块之间的字符重叠

类型: int 默认值: 0

**kwargs

要传递给父类的附加参数

类型: Any 默认值: {}

split_text

split_text(text: str) -> list[str]

将文本分割成块。

此方法通过以下方式将文本分割成块:

  • 使用正则表达式提取唯一的起始组件标签
  • 创建包含提取的标签和 JS 分隔符的分隔符列表
  • 通过调用父类方法使用分隔符分割文本
参数 描述
text

要分割的代码字符串

类型: str

返回 描述
list[str]

按组件和 JS 边界分割的文本块列表

KonlpyTextSplitter

基类:TextSplitter

使用 Konlpy 包分割文本。

它很适合分割韩语文本。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

__init__

初始化 Konlpy 文本分割器。

split_text

分割传入文本并返回块。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

__init__

__init__(separator: str = '\n\n', **kwargs: Any) -> None

初始化 Konlpy 文本分割器。

split_text

split_text(text: str) -> list[str]

分割传入文本并返回块。

LatexTextSplitter

基类:RecursiveCharacterTextSplitter

尝试沿 Latex 格式的布局元素分割文本。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

split_text

根据预定义的分隔符将输入文本分割成更小的块。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

from_language

根据特定语言返回此类的实例。

get_separators_for_language

检索给定语言的特定分隔符列表。

__init__

初始化 LatexTextSplitter。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

split_text

split_text(text: str) -> list[str]

根据预定义的分隔符将输入文本分割成更小的块。

参数 描述
text

要分割的输入文本。

类型: str

返回 描述
list[str]

分割后获得的文本块列表。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

from_language classmethod

from_language(language: Language, **kwargs: Any) -> RecursiveCharacterTextSplitter

根据特定语言返回此类的实例。

此方法使用特定于语言的分隔符初始化文本分割器。

参数 描述
language

配置文本分割器所用的语言。

类型: Language

**kwargs

用于自定义分割器的其他关键字参数。

类型: Any 默认值: {}

返回 描述
RecursiveCharacterTextSplitter

为指定语言配置的文本分割器实例。

get_separators_for_language staticmethod

get_separators_for_language(language: Language) -> list[str]

检索给定语言的特定分隔符列表。

参数 描述
language

要获取分隔符的语言。

类型: Language

返回 描述
list[str]

适用于指定语言的分隔符列表。

__init__

__init__(**kwargs: Any) -> None

初始化 LatexTextSplitter。

ExperimentalMarkdownSyntaxTextSplitter

用于处理 Markdown 语法的实验性文本分割器。

此分割器旨在保留原始文本的确切空白,同时提取结构化元数据(例如标题)。它是 MarkdownHeaderTextSplitter 的重新实现,在方法和附加功能方面有显著变化。

主要特点

  • 保留 Markdown 文本的原始空白和格式。
  • 提取标题、代码块和水平线作为元数据。
  • 分割代码块并将语言包含在“Code”元数据键中。
  • 也按水平线(---)分割文本。
  • 默认为合理的分割行为,可以使用 headers_to_split_on 参数覆盖。

示例

headers_to_split_on = [
    ("#", "Header 1"),
    ("##", "Header 2"),
]
splitter = ExperimentalMarkdownSyntaxTextSplitter(
    headers_to_split_on=headers_to_split_on
)
chunks = splitter.split(text)
for chunk in chunks:
    print(chunk)

此类目前处于实验阶段,可能会根据反馈和进一步开发进行更改。

方法 描述
__init__

使用标题分割和格式选项初始化文本分割器。

split_text

将输入文本分割成结构化块。

__init__

__init__(
    headers_to_split_on: list[tuple[str, str]] | None = None,
    return_each_line: bool = False,
    strip_headers: bool = True,
) -> None

使用标题分割和格式选项初始化文本分割器。

此构造函数设置了基于指定标题和格式偏好将文本分割成块所需的配置。

参数 描述
headers_to_split_on

元组列表,其中每个元组包含一个标题标签(例如“h1”)及其相应的元数据键。如果为 None,则使用默认标题。

类型: Union[list[tuple[str, str]], None] 默认值: None

return_each_line

是否将每行作为单独的块返回。默认为 False,这会将行聚合到更大的块中。

类型: bool 默认值: False

strip_headers

是否从结果块的内容中排除标题。

类型: bool 默认值: True

split_text

split_text(text: str) -> list[Document]

将输入文本分割成结构化块。

此方法逐行处理输入文本,识别并处理特定模式(例如标题、代码块和水平线),以将其分割成基于标题、代码块和水平线的结构化块。

参数 描述
text

要分割成块的输入文本。

类型: str

返回 描述
list[Document]

表示结构化块的 Document 对象列表

list[Document]

输入文本的块。如果启用 return_each_line,则每行

list[Document]

作为单独的 Document 返回。

HeaderType

基类:TypedDict

类型化字典中的标题类型。

LineType

基类:TypedDict

类型化字典中的行类型。

MarkdownHeaderTextSplitter

根据指定的标题分割 Markdown 文件。

方法 描述
__init__

创建一个新的 MarkdownHeaderTextSplitter。

aggregate_lines_to_chunks

将具有相同元数据的行组合成块。

split_text

分割 Markdown 文件。

__init__

__init__(
    headers_to_split_on: list[tuple[str, str]],
    return_each_line: bool = False,
    strip_headers: bool = True,
    custom_header_patterns: dict[str, int] | None = None,
) -> None

创建一个新的 MarkdownHeaderTextSplitter。

参数 描述
headers_to_split_on

我们想要跟踪的标题

类型: list[tuple[str, str]]

return_each_line

返回每行及相关的标题

类型: bool 默认值: False

strip_headers

从块内容中剥离分割的标题

类型: bool 默认值: True

custom_header_patterns

将标题模式映射到其级别的可选字典。例如:{"**": 1, "***": 2} 将 **Header** 视为级别 1,**_Header_** 视为级别 2 标题。

类型: dict[str, int] | None 默认值: None

aggregate_lines_to_chunks

aggregate_lines_to_chunks(lines: list[LineType]) -> list[Document]

将具有相同元数据的行组合成块。

参数 描述
lines

文本行/关联的标题元数据

类型: list[LineType]

split_text

split_text(text: str) -> list[Document]

分割 Markdown 文件。

参数 描述
text

Markdown 文件

类型: str

MarkdownTextSplitter

基类:RecursiveCharacterTextSplitter

尝试沿 Markdown 格式的标题分割文本。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

split_text

根据预定义的分隔符将输入文本分割成更小的块。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

from_language

根据特定语言返回此类的实例。

get_separators_for_language

检索给定语言的特定分隔符列表。

__init__

初始化 MarkdownTextSplitter。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

split_text

split_text(text: str) -> list[str]

根据预定义的分隔符将输入文本分割成更小的块。

参数 描述
text

要分割的输入文本。

类型: str

返回 描述
list[str]

分割后获得的文本块列表。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

from_language classmethod

from_language(language: Language, **kwargs: Any) -> RecursiveCharacterTextSplitter

根据特定语言返回此类的实例。

此方法使用特定于语言的分隔符初始化文本分割器。

参数 描述
language

配置文本分割器所用的语言。

类型: Language

**kwargs

用于自定义分割器的其他关键字参数。

类型: Any 默认值: {}

返回 描述
RecursiveCharacterTextSplitter

为指定语言配置的文本分割器实例。

get_separators_for_language staticmethod

get_separators_for_language(language: Language) -> list[str]

检索给定语言的特定分隔符列表。

参数 描述
language

要获取分隔符的语言。

类型: Language

返回 描述
list[str]

适用于指定语言的分隔符列表。

__init__

__init__(**kwargs: Any) -> None

初始化 MarkdownTextSplitter。

NLTKTextSplitter

基类:TextSplitter

使用 NLTK 包分割文本。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

__init__

初始化 NLTK 分割器。

split_text

分割传入文本并返回块。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

__init__

__init__(
    separator: str = "\n\n",
    language: str = "english",
    *,
    use_span_tokenize: bool = False,
    **kwargs: Any,
) -> None

初始化 NLTK 分割器。

split_text

split_text(text: str) -> list[str]

分割传入文本并返回块。

PythonCodeTextSplitter

基类:RecursiveCharacterTextSplitter

尝试沿 Python 语法分割文本。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

split_text

根据预定义的分隔符将输入文本分割成更小的块。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

from_language

根据特定语言返回此类的实例。

get_separators_for_language

检索给定语言的特定分隔符列表。

__init__

初始化 PythonCodeTextSplitter。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

split_text

split_text(text: str) -> list[str]

根据预定义的分隔符将输入文本分割成更小的块。

参数 描述
text

要分割的输入文本。

类型: str

返回 描述
list[str]

分割后获得的文本块列表。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

from_language classmethod

from_language(language: Language, **kwargs: Any) -> RecursiveCharacterTextSplitter

根据特定语言返回此类的实例。

此方法使用特定于语言的分隔符初始化文本分割器。

参数 描述
language

配置文本分割器所用的语言。

类型: Language

**kwargs

用于自定义分割器的其他关键字参数。

类型: Any 默认值: {}

返回 描述
RecursiveCharacterTextSplitter

为指定语言配置的文本分割器实例。

get_separators_for_language staticmethod

get_separators_for_language(language: Language) -> list[str]

检索给定语言的特定分隔符列表。

参数 描述
language

要获取分隔符的语言。

类型: Language

返回 描述
list[str]

适用于指定语言的分隔符列表。

__init__

__init__(**kwargs: Any) -> None

初始化 PythonCodeTextSplitter。

SentenceTransformersTokenTextSplitter

基类:TextSplitter

使用句子模型分词器将文本分割成令牌。

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

__init__

创建一个新的 TextSplitter。

split_text

通过将文本分割成令牌,将输入文本分割成更小的组件。

count_tokens

计算给定文本中的令牌数。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

__init__

__init__(
    chunk_overlap: int = 50,
    model_name: str = "sentence-transformers/all-mpnet-base-v2",
    tokens_per_chunk: int | None = None,
    **kwargs: Any,
) -> None

创建一个新的 TextSplitter。

split_text

split_text(text: str) -> list[str]

通过将文本分割成令牌,将输入文本分割成更小的组件。

此方法使用私有 _encode 方法编码输入文本,然后从编码结果中剥离起始和停止令牌 ID。它将处理后的段作为字符串列表返回。

参数 描述
text

要分割的输入文本。

类型: str

返回 描述
list[str]

经过编码和处理后从输入文本派生出的字符串组件列表。

list[str]

处理。

count_tokens

count_tokens(*, text: str) -> int

计算给定文本中的令牌数。

此方法使用私有 _encode 方法编码输入文本,并计算编码结果中的令牌总数。

参数 描述
text

计算令牌数的输入文本。

类型: str

返回 描述
int

编码文本中的令牌数。

类型: int

SpacyTextSplitter

基类:TextSplitter

使用 Spacy 包分割文本。

默认情况下,Spacy 的 en_core_web_sm 模型被使用,其默认的 max_length 为 1000000(这是该模型接受的最大字符长度,对于大型文件可以增加)。为了更快但可能不太准确的拆分,您可以使用 pipeline='sentencizer'

方法 描述
transform_documents

通过分割来转换文档序列。

atransform_documents

异步转换文档列表。

create_documents

从文本列表创建 Document 对象列表。

split_documents

分割文档。

from_huggingface_tokenizer

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder

使用 tiktoken 编码器计算长度的文本分割器。

__init__

初始化 Spacy 文本分割器。

split_text

分割传入文本并返回块。

transform_documents

transform_documents(documents: Sequence[Document], **kwargs: Any) -> Sequence[Document]

通过分割来转换文档序列。

atransform_documents async

atransform_documents(
    documents: Sequence[Document], **kwargs: Any
) -> Sequence[Document]

异步转换文档列表。

参数 描述
documents

要转换的 Document 对象序列。

类型: Sequence[Document]

返回 描述
Sequence[Document]

已转换的 Document 对象序列。

create_documents

create_documents(
    texts: list[str], metadatas: list[dict[Any, Any]] | None = None
) -> list[Document]

从文本列表创建 Document 对象列表。

split_documents

split_documents(documents: Iterable[Document]) -> list[Document]

分割文档。

from_huggingface_tokenizer classmethod

from_huggingface_tokenizer(
    tokenizer: PreTrainedTokenizerBase, **kwargs: Any
) -> TextSplitter

使用 Hugging Face 分词器计算长度的文本分割器。

from_tiktoken_encoder classmethod

from_tiktoken_encoder(
    encoding_name: str = "gpt2",
    model_name: str | None = None,
    allowed_special: Literal["all"] | Set[str] = set(),
    disallowed_special: Literal["all"] | Collection[str] = "all",
    **kwargs: Any,
) -> Self

使用 tiktoken 编码器计算长度的文本分割器。

__init__

__init__(
    separator: str = "\n\n",
    pipeline: str = "en_core_web_sm",
    max_length: int = 1000000,
    *,
    strip_whitespace: bool = True,
    **kwargs: Any,
) -> None

初始化 Spacy 文本分割器。

split_text

split_text(text: str) -> list[str]

分割传入文本并返回块。

split_text_on_tokens

split_text_on_tokens(*, text: str, tokenizer: Tokenizer) -> list[str]

使用分词器分割输入文本并返回块。

© . This site is unofficial and not affiliated with LangChain, Inc.