Segments API
pretok.segment.Segment
dataclass
A segment of text within a prompt.
Segments represent distinct parts of a prompt that may need different handling during translation.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
SegmentType
|
The type of segment |
content |
str
|
The text content of the segment |
start |
int
|
Starting character index in original text |
end |
int
|
Ending character index in original text |
language |
str | None
|
Detected language code (if applicable) |
metadata |
dict[str, Any]
|
Additional segment metadata |
translatable |
bool
|
Whether this segment should be translated |
Source code in src/pretok/segment/types.py
span
property
Return (start, end) tuple.
__len__()
__post_init__()
Set default translatability based on type.
Source code in src/pretok/segment/types.py
with_content(new_content)
Create new segment with different content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_content
|
str
|
New content string |
required |
Returns:
| Type | Description |
|---|---|
Segment
|
New Segment with updated content |
Source code in src/pretok/segment/types.py
with_language(language)
Create new segment with detected language.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language
|
str
|
ISO 639-1 language code |
required |
Returns:
| Type | Description |
|---|---|
Segment
|
New Segment with language set |
Source code in src/pretok/segment/types.py
pretok.segment.SegmentType
Bases: Enum
Types of segments in a prompt.
Different segment types have different handling rules:
- TEXT: Regular text content that should be translated
- CODE: Code blocks that should NOT be translated (except comments)
- JSON: JSON content where only string values may be translated
- ROLE_MARKER: Chat format markers (e.g., <|user|>, [INST])
- CONTROL_TOKEN: Special tokens (e.g., <|endoftext|>, )
- DELIMITER: Format delimiters (e.g., ###, ---)
- WHITESPACE: Significant whitespace that should be preserved
- COMMENT: Code comments (may be translated based on config)
Source code in src/pretok/segment/types.py
pretok.segment.PromptLexer
Lexer for parsing prompts into segments.
The lexer identifies different types of content in a prompt:
- Role markers (e.g., <|user|>, [INST])
- Control tokens (e.g., <s>, </s>)
- Code blocks
- JSON content
- Regular text
Example
lexer = PromptLexer() segments = lexer.lex("<|im_start|>user\nHello!<|im_end|>") [s.type for s in segments] [
SegmentType.ROLE_MARKER,SegmentType.TEXT,SegmentType.ROLE_MARKER]
Source code in src/pretok/segment/lexer.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
__init__(config=None, format_hint=None)
Initialize the lexer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
SegmentConfig | None
|
Optional segment configuration |
None
|
format_hint
|
str | None
|
Hint for prompt format ('chatml', 'llama', etc.) |
None
|
Source code in src/pretok/segment/lexer.py
lex(text)
Parse text into segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text to parse |
required |
Returns:
| Type | Description |
|---|---|
list[Segment]
|
List of Segment objects |
Source code in src/pretok/segment/lexer.py
pretok.segment.lex_prompt(text, *, format_hint=None, config=None)
Convenience function to lex a prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text to parse |
required |
format_hint
|
str | None
|
Hint for prompt format |
None
|
config
|
SegmentConfig | None
|
Optional segment configuration |
None
|
Returns:
| Type | Description |
|---|---|
list[Segment]
|
List of Segment objects |