File size: 10,006 Bytes
f5776d3 |
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 |
from collections import namedtuple
import re
from typing import Union, Any
import dsp
from dsp.primitives.demonstrate import Example
from .utils import passages2text, format_answers
Field = namedtuple("Field", "name separator input_variable output_variable description")
# TODO: de-duplicate with dsp/templates/template.py
class TemplateV2:
def __init__(
self,
template,
format_handlers={
"passages": passages2text,
"contexte": passages2text,
"reponse": format_answers,
"reponses": format_answers,
},
):
self.format_handlers = format_handlers
template = template.strip()
self.instructions = re.search("(.*)\n", template).group(1)
template = template[len(self.instructions) :].strip()
self.fields = []
while len(template) > 0:
match = re.search("(.*)(\s){(.*)}\s(.*\${.*})", template)
if match is not None:
name = match.group(1)
separator = match.group(2)
variable = match.group(3)
description = match.group(4)
else:
match = re.search("(.*)(\s){(.*)}", template)
if match is not None:
name = match.group(1)
separator = match.group(2)
variable = match.group(3)
description = None
else:
raise ValueError(f"Could not parse template")
var_match = re.match("(.*) -> (.*)", variable)
if var_match is not None:
input_variable = var_match.group(1)
output_variable = var_match.group(2)
else:
input_variable = variable
output_variable = variable
self.fields.append(
Field(
name=name,
separator=separator,
input_variable=input_variable,
output_variable=output_variable,
description=description,
)
)
template = template[len(match.group(0)) :].strip()
def query(self, example: Example, is_demo: bool = False) -> str:
"""Retrieves the input variables from the example and formats them into a query string."""
result: list[str] = []
if not is_demo:
has_value = [
field.input_variable in example
and example[field.input_variable] is not None
and example[field.input_variable] != ""
for field in self.fields
]
for i in range(1, len(has_value)):
if has_value[i - 1] and not any(has_value[i:]):
example[self.fields[i].input_variable] = ""
break
for field in self.fields:
if (
field.input_variable in example
and example[field.input_variable] is not None
):
if field.input_variable in self.format_handlers:
format_handler = self.format_handlers[field.input_variable]
else:
def format_handler(x):
return " ".join(x.split())
formatted_value = format_handler(example[field.input_variable])
separator = '\n' if field.separator == ' ' and '\n' in formatted_value else field.separator
result.append(
f"{field.name}{separator}{formatted_value}"
)
if self._has_augmented_guidelines() and ("augmented" in example and example.augmented):
return "\n\n".join([r for r in result if r])
return "\n".join([r for r in result if r])
def guidelines(self, show_guidelines=True) -> str:
"""Returns the task guidelines as described in the lm prompt"""
if (not show_guidelines) or (
hasattr(dsp.settings, "show_guidelines")
and not dsp.settings.show_guidelines
):
return ""
result = "Respecte le format suivant.\n\n"
example = dsp.Example()
for field in self.fields:
example[field.input_variable] = field.description
example.augmented = self._has_augmented_guidelines()
result += self.query(example)
return result
def _has_augmented_guidelines(self):
return len(self.fields) > 3 or any(
("\n" in field.separator) or ('\n' in field.description) for field in self.fields
)
def extract(
self, example: Union[Example, dict[str, Any]], raw_pred: str
) -> Example:
"""Extracts the answer from the LM raw prediction using the template structure
Args:
example (Union[Example, dict[str, Any]]): Contains the input variables that raw_pred was completed on.
raw_pred (str): LM generated string
Returns:
Example: The example with the output variables filled in
"""
example = dsp.Example(example)
raw_pred = raw_pred.strip()
idx = 0
while idx < len(self.fields):
if (
self.fields[idx].input_variable not in example
or example[self.fields[idx].input_variable] is None
):
break
idx += 1
import dspy
idx = min(idx, len(self.fields) - 1)
while raw_pred != "" and idx < len(self.fields):
if idx < len(self.fields) - 1:
next_field_name = "\n" + self.fields[idx + 1].name
offset = raw_pred.find(next_field_name)
if offset >= 0:
if dspy.settings.release >= 20231003:
example[self.fields[idx].output_variable] = raw_pred[:offset].strip().rstrip('---').strip()
raw_pred = raw_pred[offset + len(next_field_name) :].strip().rstrip('---').strip()
else:
example[self.fields[idx].output_variable] = raw_pred[:offset].strip()
raw_pred = raw_pred[offset + len(next_field_name) :].strip()
idx += 1
else:
if dspy.settings.release >= 20231003:
example[self.fields[idx].output_variable] = raw_pred.strip().rstrip('---').strip()
else:
example[self.fields[idx].output_variable] = raw_pred.strip()
raw_pred = ""
idx += 1
break
else:
assert idx == len(self.fields) - 1, (idx, len(self.fields))
if dspy.settings.release >= 20231003:
example[self.fields[idx].output_variable] = raw_pred.strip().rstrip('---').strip()
else:
example[self.fields[idx].output_variable] = raw_pred.strip()
break
return example
def __call__(self, example, show_guidelines=True) -> str:
example = dsp.Example(example)
if hasattr(dsp.settings, 'query_only') and dsp.settings.query_only:
return self.query(example)
# The training data should not contain the output variable
if self.fields[-1].input_variable in example:
del example[self.fields[-1].input_variable]
rdemos = [
self.query(demo, is_demo=True)
for demo in example.demos
if (
("augmented" not in demo or not demo.augmented)
and ( # validate that the training example has the same primitive input var as the template
self.fields[-1].input_variable in demo
and demo[self.fields[-1].input_variable] is not None
)
)
]
ademos = [
self.query(demo, is_demo=True)
for demo in example.demos
if "augmented" in demo and demo.augmented
]
# Move the rdemos to ademos if rdemo has all the fields filled in
rdemos_ = []
new_ademos = []
for rdemo in rdemos:
if all(
(field.name in rdemo)
for field in self.fields
if field.input_variable in example
):
import dspy
if dspy.settings.release >= 20230928:
new_ademos.append(rdemo)
else:
ademos.append(rdemo)
else:
rdemos_.append(rdemo)
ademos = new_ademos + ademos
rdemos = rdemos_
long_query = self._has_augmented_guidelines()
if long_query:
example["augmented"] = True
query = self.query(example)
# if it has more lines than fields
if len(query.split('\n')) > len(self.fields):
long_query = True
if "augmented" not in example or not example.augmented:
example["augmented"] = True
query = self.query(example)
rdemos = "\n\n".join(rdemos)
if len(rdemos) >= 1 and len(ademos) == 0 and not long_query:
rdemos_and_query = "\n\n".join([rdemos, query])
parts = [
self.instructions,
self.guidelines(show_guidelines),
rdemos_and_query,
]
elif len(rdemos) == 0:
parts = [
self.instructions,
self.guidelines(show_guidelines),
*ademos,
query,
]
else:
parts = [
self.instructions,
rdemos,
self.guidelines(show_guidelines),
*ademos,
query,
]
prompt = "\n\n---\n\n".join([p.strip() for p in parts if p])
return prompt.strip()
|