-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathparse.py
346 lines (286 loc) · 11.3 KB
/
parse.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import logging
import re
import string
from collections import defaultdict
from enum import Enum
from typing import (
Annotated,
Any,
DefaultDict,
Dict,
Generator,
Iterable,
List,
Literal,
Mapping,
Optional,
TypedDict,
Union,
)
from pydantic import BaseModel, Field, validator
logger = logging.getLogger(__name__)
class PartialStringFormatter(string.Formatter):
def __init__(self):
super().__init__()
self._current_field_name = None
def get_field(self, field_name, args, kwargs):
self._current_field_name = field_name
try:
return super().get_field(field_name, args, kwargs)
except (KeyError, AttributeError):
# For unprovided variables, preserve the entire field name including format spec
return "{" + field_name + "}", field_name
def format_field(self, value, format_spec):
if isinstance(value, str) and value.startswith("{"):
# This is a preserved placeholder, return as is
if format_spec:
return value[:-1] + ":" + format_spec + "}"
else:
return value
try:
return super().format_field(value, format_spec)
except (ValueError, TypeError):
# If format spec is invalid, preserve the original field name and format spec
if format_spec:
return "{" + self._current_field_name + ":" + format_spec + "}"
return str(value)
PartialStringFormat = PartialStringFormatter()
def partial_str_format(format_string: str, **kwargs) -> str:
"""
Format a string with the provided variables while preserving any unprovided placeholders.
Preserves format specifiers for both provided and unprovided variables.
Args:
format_string: The string to format
**kwargs: The variables to use for formatting
Returns:
The formatted string with preserved unprovided placeholders and format specifiers
Examples:
>>> partial_str_format("Hello {name}!", name="World")
'Hello World!'
>>> partial_str_format("Hello {name} {unknown}!", name="World")
'Hello World {unknown}!'
>>> partial_str_format("Value: {x:.2f}", x="not_a_float")
'Value: {x:.2f}'
"""
if not format_string:
return ""
# Temporarily replace valid format strings to protect them from escaping
format_pattern = re.compile(r"\{[^{}]+\}")
markers = {
f"__MARKER_{i}__": m.group(0)
for i, m in enumerate(format_pattern.finditer(format_string))
}
processed = format_string
for marker, format_str in markers.items():
processed = processed.replace(format_str, marker)
# Escape remaining brackets and restore format strings
processed = processed.replace("{", "{{").replace("}", "}}")
for marker, format_str in markers.items():
processed = processed.replace(marker, format_str)
return PartialStringFormat.format(processed, **kwargs)
class TemplateChunks(TypedDict):
text: str
start: int
end: int
type: str
match_fields_regex = re.compile(r"(?<!\{)\{([a-zA-Z0-9_]+)\}(?!})")
class MessageChunkType(Enum):
TEXT = "text"
IMAGE_URL = "image_url"
IMAGE_URLS = "image_urls"
def parse_template(
string,
include_texts=True,
payload: Optional[Dict[str, Any]] = None,
input_field_types: Optional[Dict[str, MessageChunkType]] = None,
) -> List[TemplateChunks]:
"""
Parses a template string to extract output fields and the text between them.
Args:
string (str): The template string to parse.
include_texts (bool): Whether to include the text between the fields in the output.
Returns:
List[Element]: A list of dictionaries with the keys 'text', 'start', 'end', and 'type'.
Example:
>>> parse_template("some text {field1} some more text {field2}")
[{"text": "some text ", "start": 0, "end": 10, "type": "text"},
{"text": "field1", "start": 11, "end": 17, "type": "var"},
{"text": " some more text ", "start": 18, "end": 35, "type": "text"},
{"text": "field2", "start": 36, "end": 42, "type": "var"}]
"""
chunks: List[TemplateChunks] = []
last_index = 0
payload = payload or {}
input_field_types = input_field_types or {}
for match in match_fields_regex.finditer(string):
# for match in re.finditer(r'\{(.*?)\}', string):
# Text before field
start = match.start()
if last_index < start and include_texts:
text = string[last_index:start]
chunks.append(
{
"text": text,
"start": last_index,
"end": start,
"type": "text",
"data": None,
"field_type": None,
}
)
# Field itself
end = match.end()
field = string[start:end].strip("{}")
# Extract the field name by removing the brackets
data = payload.get(field)
field_type = input_field_types.get(field, MessageChunkType.TEXT)
chunks.append(
{
"text": field,
"start": start,
"end": end,
"type": "var",
"data": data,
"field_type": field_type,
}
)
last_index = end
# Text after the last field
if last_index < len(string) and include_texts:
text = string[last_index:]
chunks.append(
{
"text": text,
"start": last_index,
"end": len(string),
"type": "text",
"data": None,
"field_type": None,
}
)
return chunks
# TODO: consolidate these data models and unify our preprocessing for LLM input into one step RawInputModel -> PreparedInputModel
class TextMessageChunk(TypedDict):
type: Literal["text"]
text: str
class ImageMessageChunk(TypedDict):
type: Literal["image"]
image_url: Dict[str, str]
MessageChunk = Union[TextMessageChunk, ImageMessageChunk]
Message = Union[str, List[MessageChunk]]
def split_message_into_chunks(
input_template: str, input_field_types: Dict[str, MessageChunkType], **payload
) -> List[MessageChunk]:
"""Split a template string into message chunks based on field types.
Args:
input_template: Template string with placeholders like '{field_name}'
input_field_types: Mapping of field names to their chunk types
payload: Dictionary with values to substitute into the template instead of placeholders
Returns:
List of message chunks with appropriate type and content:
- Text chunks: {'type': 'text', 'text': str}
- Image chunks: {'type': 'image_url', 'image_url': {'url': str}}
Example:
>>> split_message_into_chunks(
... 'Look at {image} and describe {text}',
... {'image': MessageChunkType.IMAGE_URL, 'text': MessageChunkType.TEXT},
... {'image': 'http://example.com/img.jpg', 'text': 'this content'}
... )
[
{'type': 'text', 'text': 'Look at '},
{'type': 'image_url', 'image_url': {'url': 'http://example.com/img.jpg'}},
{'type': 'text', 'text': ' and describe this content'}
]
"""
# Parse template to get chunks with field positions and types
parsed = parse_template(
input_template,
include_texts=True,
payload=payload,
input_field_types=input_field_types,
)
logger.debug(f"Parsed template: {parsed}")
result = []
current_text = ""
def _add_current_text_as_chunk():
# this function is used to flush `current_text` buffer into a text chunk, and start over
nonlocal current_text
if current_text:
result.append({"type": "text", "text": current_text})
current_text = ""
for part in parsed:
# iterate over parsed chunks - they already contains field types and placeholder values
if part["type"] == "text":
# each text chunk without placeholders is added to the current buffer and we continue
current_text += part["text"]
elif part["type"] == "var":
field_type = part["field_type"]
field_value = part["data"]
if field_value is None:
# if field value is not provided, it is assumed to be a text field
current_text += part["text"]
else:
match field_type:
case MessageChunkType.TEXT:
# For text fields, we don't break chunks and add text fields to current buffer
current_text += (
str(field_value)
if field_value is not None
else part["text"]
)
case MessageChunkType.IMAGE_URL:
# Add remaining text as text chunk
_add_current_text_as_chunk()
# Add image URL as new image chunk
result.append(
{"type": "image_url", "image_url": {"url": field_value}}
)
case MessageChunkType.IMAGE_URLS:
assert isinstance(
field_value, List
), "Image URLs must be a list"
# Add remaining text as text chunk
_add_current_text_as_chunk()
# Add image URLs as new image chunks
for url in field_value:
result.append(
{"type": "image_url", "image_url": {"url": url}}
)
case _:
# Handle unknown field types as text
current_text += part["text"]
# Add any remaining text
_add_current_text_as_chunk()
logger.debug(f"Result: {result}")
return result
class MessagesBuilder(BaseModel):
user_prompt_template: str
system_prompt: Optional[str] = None
instruction_first: bool = True
extra_fields: Dict[str, Any] = Field(default_factory=dict)
split_into_chunks: bool = False
input_field_types: DefaultDict[
str,
Annotated[
MessageChunkType, Field(default_factory=lambda: MessageChunkType.TEXT)
],
] = Field(default_factory=lambda: defaultdict(lambda: MessageChunkType.TEXT))
def get_messages(self, payload: Dict[str, Any]):
if self.split_into_chunks:
user_prompt = split_message_into_chunks(
input_template=self.user_prompt_template,
input_field_types=self.input_field_types,
**payload,
**self.extra_fields,
)
else:
user_prompt = partial_str_format(
self.user_prompt_template, **payload, **self.extra_fields
)
messages = [{"role": "user", "content": user_prompt}]
if self.system_prompt:
if self.instruction_first:
messages.insert(0, {"role": "system", "content": self.system_prompt})
else:
messages[0]["content"] += self.system_prompt
return messages