chatterer 0.1.6__py3-none-any.whl → 0.1.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chatterer/__init__.py +39 -27
- chatterer/language_model.py +371 -327
- chatterer/messages.py +8 -0
- chatterer/strategies/__init__.py +13 -13
- chatterer/strategies/atom_of_thoughts.py +975 -976
- chatterer/strategies/base.py +14 -14
- chatterer/tools/__init__.py +17 -15
- chatterer/tools/citation_chunking/__init__.py +3 -0
- chatterer/tools/citation_chunking/chunks.py +53 -0
- chatterer/tools/citation_chunking/citation_chunker.py +118 -0
- chatterer/tools/citation_chunking/citations.py +285 -0
- chatterer/tools/citation_chunking/prompt.py +157 -0
- chatterer/tools/citation_chunking/reference.py +26 -0
- chatterer/tools/citation_chunking/utils.py +138 -0
- chatterer/tools/convert_to_text.py +466 -464
- chatterer/tools/webpage_to_markdown/__init__.py +4 -4
- chatterer/tools/webpage_to_markdown/playwright_bot.py +649 -631
- chatterer/tools/webpage_to_markdown/utils.py +329 -556
- chatterer/utils/image.py +284 -0
- {chatterer-0.1.6.dist-info → chatterer-0.1.7.dist-info}/METADATA +166 -166
- chatterer-0.1.7.dist-info/RECORD +24 -0
- {chatterer-0.1.6.dist-info → chatterer-0.1.7.dist-info}/WHEEL +1 -1
- chatterer-0.1.6.dist-info/RECORD +0 -15
- {chatterer-0.1.6.dist-info → chatterer-0.1.7.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
from typing import Literal, TypeAlias
|
2
|
+
|
3
|
+
from pydantic import BaseModel, Field
|
4
|
+
|
5
|
+
|
6
|
+
class MultiMatchRegex(BaseModel):
|
7
|
+
type: Literal["multi_match_regex"] = Field(
|
8
|
+
description="A regex pattern that should match multiple instances of the subject in the document."
|
9
|
+
)
|
10
|
+
regular_expression: str = Field(
|
11
|
+
description="The regex pattern that should match multiple instances of the subject in the document."
|
12
|
+
)
|
13
|
+
|
14
|
+
def __hash__(self) -> int:
|
15
|
+
return hash((self.type, self.regular_expression))
|
16
|
+
|
17
|
+
|
18
|
+
class SingleMatchCitation(BaseModel):
|
19
|
+
start_from: str = Field(description="A snippet of text at the beginning of the cited section.")
|
20
|
+
end_at: str = Field(description="A snippet of text at the end of the cited section.")
|
21
|
+
|
22
|
+
def __hash__(self) -> int:
|
23
|
+
return hash((self.start_from, self.end_at))
|
24
|
+
|
25
|
+
|
26
|
+
Reference: TypeAlias = SingleMatchCitation | MultiMatchRegex
|
@@ -0,0 +1,138 @@
|
|
1
|
+
from typing import Callable, NamedTuple, Self, TypeVar
|
2
|
+
|
3
|
+
from pydantic import BaseModel
|
4
|
+
|
5
|
+
T = TypeVar("T", bound=BaseModel)
|
6
|
+
|
7
|
+
|
8
|
+
class MatchedText(NamedTuple):
|
9
|
+
text: str
|
10
|
+
start_idx: int
|
11
|
+
end_idx: int
|
12
|
+
|
13
|
+
@classmethod
|
14
|
+
def from_text(
|
15
|
+
cls,
|
16
|
+
full_text: str,
|
17
|
+
len_func: Callable[[str], int],
|
18
|
+
chunk_size: int = 2048,
|
19
|
+
token_overlap: int = 0,
|
20
|
+
separator: str = "\n",
|
21
|
+
) -> list[Self]:
|
22
|
+
"""
|
23
|
+
토큰 수 제한과 선택적 오버랩을 기준으로 텍스트를 청크로 분할합니다.
|
24
|
+
각 청크는 원본 텍스트 내의 위치 정보 (start_idx, end_idx)와 함께 반환됩니다.
|
25
|
+
텍스트는 separator 문자열로 분할하며, 토큰 수는 len_func 함수를 통해 계산합니다.
|
26
|
+
|
27
|
+
Args:
|
28
|
+
full_text: 분할할 전체 텍스트.
|
29
|
+
len_func: 주어진 텍스트의 토큰 수를 반환하는 함수.
|
30
|
+
chunk_size: 각 청크의 최대 토큰 수. 기본값은 2048.
|
31
|
+
token_overlap: 청크 간 중첩할 토큰 수. 기본값은 0.
|
32
|
+
separator: 텍스트를 분할할 구분자 문자열. 기본값은 "\n".
|
33
|
+
|
34
|
+
Returns:
|
35
|
+
각 요소가 (chunk_text, start_idx, end_idx)인 튜플의 리스트.
|
36
|
+
chunk_text는 whole_text 내에서 whole_text[start_idx:end_idx]와 동일한 부분 문자열입니다.
|
37
|
+
"""
|
38
|
+
text_chunks: list[Self] = []
|
39
|
+
sep_token_count: int = len_func(separator)
|
40
|
+
sep_len = len(separator)
|
41
|
+
|
42
|
+
# 먼저, separator를 기준으로 원본 텍스트를 분할하되 각 조각의 시작/종료 인덱스를 기록합니다.
|
43
|
+
piece_infos: list[Self] = [] # 각 튜플: (piece_text, start_index, end_index)
|
44
|
+
start_idx = 0
|
45
|
+
while True:
|
46
|
+
idx = full_text.find(separator, start_idx)
|
47
|
+
if idx == -1:
|
48
|
+
# 마지막 조각: separator가 더 이상 없으므로 전체 남은 부분을 추가합니다.
|
49
|
+
piece_infos.append(
|
50
|
+
cls(
|
51
|
+
text=full_text[start_idx:],
|
52
|
+
start_idx=start_idx,
|
53
|
+
end_idx=len(full_text),
|
54
|
+
)
|
55
|
+
)
|
56
|
+
break
|
57
|
+
else:
|
58
|
+
piece_infos.append(
|
59
|
+
cls(
|
60
|
+
text=full_text[start_idx:idx],
|
61
|
+
start_idx=start_idx,
|
62
|
+
end_idx=idx,
|
63
|
+
)
|
64
|
+
)
|
65
|
+
start_idx = idx + sep_len
|
66
|
+
|
67
|
+
current_chunk: list[Self] = []
|
68
|
+
current_token_count: int = 0
|
69
|
+
i = 0
|
70
|
+
while i < len(piece_infos):
|
71
|
+
piece_info = piece_infos[i]
|
72
|
+
piece = piece_info.text
|
73
|
+
piece_start = piece_info.start_idx
|
74
|
+
piece_end = piece_info.end_idx
|
75
|
+
# 원래 코드는 각 조각에 separator의 토큰 수도 포함합니다.
|
76
|
+
piece_token_count: int = len_func(piece) + sep_token_count
|
77
|
+
|
78
|
+
# 현재 청크에 추가하면 chunk_size를 초과하는 경우
|
79
|
+
if current_token_count + piece_token_count > chunk_size:
|
80
|
+
# 단일 조각이 chunk_size보다 큰 경우엔 어쩔 수 없이 추가합니다.
|
81
|
+
if not current_chunk:
|
82
|
+
current_chunk.append(
|
83
|
+
cls(
|
84
|
+
text=piece,
|
85
|
+
start_idx=piece_start,
|
86
|
+
end_idx=piece_end,
|
87
|
+
)
|
88
|
+
)
|
89
|
+
current_token_count += piece_token_count
|
90
|
+
i += 1
|
91
|
+
# 현재 청크 완성 → 청크에 추가
|
92
|
+
chunk_start = current_chunk[0].start_idx
|
93
|
+
# current_chunk에 담긴 조각들은 원본 텍스트상 연속되어 있으므로,
|
94
|
+
# 청크의 종료 인덱스는 마지막 조각의 end_index가 됩니다.
|
95
|
+
chunk_end = current_chunk[-1].end_idx
|
96
|
+
# 원본 텍스트의 해당 구간을 그대로 추출하면 separator가 포함됩니다.
|
97
|
+
chunk_text = full_text[chunk_start:chunk_end]
|
98
|
+
text_chunks.append(
|
99
|
+
cls(
|
100
|
+
text=chunk_text,
|
101
|
+
start_idx=chunk_start,
|
102
|
+
end_idx=chunk_end,
|
103
|
+
)
|
104
|
+
)
|
105
|
+
|
106
|
+
# token_overlap이 적용되는 경우: 청크 끝부분 일부를 다음 청크에 오버랩합니다.
|
107
|
+
if token_overlap > 0:
|
108
|
+
overlap_chunk: list[Self] = []
|
109
|
+
overlap_count: int = 0
|
110
|
+
# 뒤에서부터 역순으로 오버랩할 조각들을 선택합니다.
|
111
|
+
for j in range(len(current_chunk) - 1, -1, -1):
|
112
|
+
p_text = current_chunk[j].text
|
113
|
+
p_token_count = len_func(p_text) + sep_token_count
|
114
|
+
# 최소 한 조각은 포함하고, 오버랩 토큰 수가 token_overlap 이하라면 계속 추가
|
115
|
+
if overlap_count + p_token_count <= token_overlap or not overlap_chunk:
|
116
|
+
overlap_chunk.insert(0, current_chunk[j])
|
117
|
+
overlap_count += p_token_count
|
118
|
+
else:
|
119
|
+
break
|
120
|
+
current_chunk = overlap_chunk.copy()
|
121
|
+
current_token_count = overlap_count
|
122
|
+
else:
|
123
|
+
current_chunk.clear()
|
124
|
+
current_token_count = 0
|
125
|
+
else:
|
126
|
+
# 청크에 추가 후 다음 조각 진행
|
127
|
+
current_chunk.append(cls(text=piece, start_idx=piece_start, end_idx=piece_end))
|
128
|
+
current_token_count += piece_token_count
|
129
|
+
i += 1
|
130
|
+
|
131
|
+
# 남은 조각이 있다면 마지막 청크로 추가합니다.
|
132
|
+
if current_chunk:
|
133
|
+
chunk_start = current_chunk[0].start_idx
|
134
|
+
chunk_end = current_chunk[-1].end_idx
|
135
|
+
chunk_text = full_text[chunk_start:chunk_end]
|
136
|
+
text_chunks.append(cls(text=chunk_text, start_idx=chunk_start, end_idx=chunk_end))
|
137
|
+
|
138
|
+
return text_chunks
|