跳到主要内容
本指南提供了 WRITER 文本分割器的快速入门概述。 WRITER 的上下文感知分割端点为长文档(最长 4000 字)提供了智能文本分割功能。与简单的基于字符的分割不同,它保留了块之间的语义意义和上下文,使其成为处理长篇内容同时保持连贯性的理想选择。在 langchain-writer 中,我们提供了 WRITER 的上下文感知分割端点作为 LangChain 文本分割器的用法。

概览

集成详情

类别本地可序列化JS 支持下载量版本
WriterTextSplitterlangchain-writerPyPI - DownloadsPyPI - Version

设置

WriterTextSplitterlangchain-writer 包中可用
pip install --quiet -U langchain-writer

凭据

注册 WRITER AI Studio 以生成 API 密钥(您可以遵循此快速入门)。然后,设置 WRITER_API_KEY 环境变量
import getpass
import os

if not os.getenv("WRITER_API_KEY"):
    os.environ["WRITER_API_KEY"] = getpass.getpass("Enter your WRITER API key: ")
设置 LangSmith 以实现一流的可观察性也很有帮助(但不是必需的)。如果您希望这样做,可以设置 LANGSMITH_TRACINGLANGSMITH_API_KEY 环境变量
os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()

实例化

使用 strategy 参数实例化 WriterTextSplitter 实例,该参数设置为以下之一
  • llm_split: 使用语言模型进行精确的语义分割
  • fast_split: 使用基于启发式的方法进行快速分割
  • hybrid_split: 结合两种方法
from langchain_writer.text_splitter import WriterTextSplitter

splitter = WriterTextSplitter(strategy="fast_split")

用法

WriterTextSplitter 可以同步或异步使用。

同步用法

要同步使用 WriterTextSplitter,请使用要分割的文本调用 split_text 方法
text = """Reeeeeeeeeeeeeeeeeeeeeaally long text you want to divide into smaller chunks. For example you can add a poem multiple times:
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;

Then took the other, as just as fair,
And having perhaps the better claim,
Because it was grassy and wanted wear;
Though as for that the passing there
Had worn them really about the same,

And both that morning equally lay
In leaves no step had trodden black.
Oh, I kept the first for another day!
Yet knowing how way leads on to way,
I doubted if I should ever come back.

I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I—
I took the one less traveled by,
And that has made all the difference.

Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;

Then took the other, as just as fair,
And having perhaps the better claim,
Because it was grassy and wanted wear;
Though as for that the passing there
Had worn them really about the same,

And both that morning equally lay
In leaves no step had trodden black.
Oh, I kept the first for another day!
Yet knowing how way leads on to way,
I doubted if I should ever come back.

I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I—
I took the one less traveled by,
And that has made all the difference.

Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;

Then took the other, as just as fair,
And having perhaps the better claim,
Because it was grassy and wanted wear;
Though as for that the passing there
Had worn them really about the same,

And both that morning equally lay
In leaves no step had trodden black.
Oh, I kept the first for another day!
Yet knowing how way leads on to way,
I doubted if I should ever come back.

I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I—
I took the one less traveled by,
And that has made all the difference.
"""

chunks = splitter.split_text(text)
chunks
您可以打印块的长度以查看创建了多少块
print(len(chunks))

异步使用

要异步使用 WriterTextSplitter,请使用要分割的文本调用 asplit_text 方法
async_chunks = await splitter.asplit_text(text)
async_chunks
打印块的长度以查看创建了多少块
print(len(async_chunks))

API 参考

有关所有 WriterTextSplitter 功能和配置的详细文档,请参阅API 参考

附加资源

您可以在 WRITER 文档中找到有关 WRITER 模型(包括成本、上下文窗口和支持的输入类型)和工具的信息。
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.