forked from langchain-ai/text-split-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_snippets.py
57 lines (45 loc) · 1.45 KB
/
code_snippets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
CHARACTER = """```python
from langchain.text_splitter import CharacterTextSplitter
{length_function}
splitter = CharacterTextSplitter(
separator = "\\n\\n", # Split character (default \\n\\n)
chunk_size={chunk_size},
chunk_overlap={chunk_overlap},
length_function=length_function,
)
text = "foo bar"
splits = splitter.split_text(text)
"""
RECURSIVE_CHARACTER = """```python
from langchain.text_splitter import RecursiveCharacterTextSplitter
{length_function}
# The default list of split characters is [\\n\\n, \\n, " ", ""]
# Tries to split on them in order until the chunks are small enough
# Keep paragraphs, sentences, words together as long as possible
splitter = RecursiveCharacterTextSplitter(
separators=["\\n\\n", "\\n", " ", ""],
chunk_size={chunk_size},
chunk_overlap={chunk_overlap},
length_function=length_function,
)
text = "foo bar"
splits = splitter.split_text(text)
"""
LANGUAGE = """```python
from langchain.text_splitter import RecursiveCharacterTextSplitter, Language
{length_function}
splitter = RecursiveCharacterTextSplitter.from_language(
{language},
chunk_size={chunk_size},
chunk_overlap={chunk_overlap},
length_function=length_function,
)
text = "foo bar"
splits = splitter.split_text(text)
"""
CHARACTER_LENGTH = "length_function = len"
TOKEN_LENGTH = """import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
def length_function(text: str) -> int:
return len(enc.encode(text))
"""