| 1234567891011121314151617181920212223242526272829303132333435 |
- from pathlib import Path
- import sys
- sys.path.append(str(Path(__file__).resolve().parents[1]))
- from _chapter2_config import print_dependency_hint
- try:
- from langchain_text_splitters import RecursiveCharacterTextSplitter
- except ModuleNotFoundError as exc:
- print_dependency_hint(exc)
- raise SystemExit(1)
- def main() -> None:
- text_splitter = RecursiveCharacterTextSplitter(
- chunk_size=20,
- chunk_overlap=6,
- length_function=len,
- separators=["\n\n", "\n", " ", ""],
- )
- text = """
- 人工智能正在快速发展,尤其是大语言模型的应用,正在改变人类的工作方式。
- 它们可以帮助人们进行写作、代码生成、甚至是科研探索。
- 相比之下,新能源的发展同样重要。
- 电动车和太阳能正在逐渐替代传统能源,减少碳排放,对全球环境保护至关重要。
- """
- docs = text_splitter.split_text(text)
- print(docs)
- if __name__ == "__main__":
- main()
|