Repository navigation
Expand file tree
/
Copy pathprompt_utils.py
More file actions
715 lines (555 loc) · 21 KB
/
Copy pathprompt_utils.py
File metadata and controls
715 lines (555 loc) · 21 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
from typing import Iterable
import jinja2
from ast_utils import ErrorCheckVisitor, get_parser
from model_utils import ModelWrapper
def truncate_to_first_line(code):
lines = code.splitlines()
for line in lines:
if line.strip():
return line
return ""
COMPLETION_PLACEHOLDER = {
"python": "# TODO: Your code here",
"java": "/* TODO: Your code here */",
"cpp": "/* TODO: Your code here */",
"csharp": "/* TODO: Your code here */",
}
FEW_SHOT_PROMPTS, FEW_SHOT_ANSWERS = {}, {}
FEW_SHOT_PROMPTS[("statement", "python")] = """\
Complete the code in python to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
t = int(input())
for _ in range(t):
a, b = map(int, input().split())
# TODO: Your code here
print(c)
"""
FEW_SHOT_ANSWERS[("statement", "python")] = "c = a + b"
FEW_SHOT_PROMPTS[("statement", "java")] = """\
Complete the code in java to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
/* TODO: Your code here */
System.out.println(c);
}
sc.close();
}
}
"""
FEW_SHOT_ANSWERS[("statement", "java")] = "int c = a + b;"
FEW_SHOT_PROMPTS[("statement", "cpp")] = """\
Complete the code in cpp to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
#include <iostream>
using namespace std;
int main() {
int t, a, b, c;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> a >> b;
/* TODO: Your code here */
cout << c << endl;
}
return 0;
}
"""
FEW_SHOT_ANSWERS[("statement", "cpp")] = "c = a + b;"
FEW_SHOT_PROMPTS[("statement", "csharp")] = """\
Complete the code in csharp to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
using System;
class Program {
static void Main() {
int t = int.Parse(Console.ReadLine());
for (int i = 0; i < t; i++) {
string[] inputs = Console.ReadLine().Split(' ');
int a = int.Parse(inputs[0]);
int b = int.Parse(inputs[1]);
/* TODO: Your code here */
Console.WriteLine(c);
}
}
}
"""
FEW_SHOT_ANSWERS[("statement", "csharp")] = "int c = a + b;"
FEW_SHOT_PROMPTS[("block", "python")] = """\
Complete the code in python to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
t = int(input())
for _ in range(t):
# TODO: Your code here
"""
FEW_SHOT_ANSWERS[("block", "python")] = """\
a, b = map(int, input().split())
c = a + b
print(c)
"""
FEW_SHOT_PROMPTS[("block", "java")] = """\
Complete the code in java to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
/* TODO: Your code here */
}
sc.close();
}
}
"""
FEW_SHOT_ANSWERS[("block", "java")] = """\
int a = sc.nextInt();
int b = sc.nextInt();
int c = a + b;
System.out.println(c);
"""
FEW_SHOT_PROMPTS[("block", "cpp")] = """\
Complete the code in cpp to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
#include <iostream>
using namespace std;
int main() {
int t, a, b, c;
cin >> t;
for (int i = 0; i < t; i++) {
/* TODO: Your code here */
}
return 0;
}
"""
FEW_SHOT_ANSWERS[("block", "cpp")] = """\
cin >> a >> b;
c = a + b;
cout << c << endl;
"""
FEW_SHOT_PROMPTS[("block", "csharp")] = """\
Complete the code in csharp to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
using System;
class Program {
static void Main() {
int t = int.Parse(Console.ReadLine());
for (int i = 0; i < t; i++) {
/* TODO: Your code here */
}
}
}
"""
FEW_SHOT_ANSWERS[("block", "csharp")] = """\
string[] inputs = Console.ReadLine().Split(' ');
int a = int.Parse(inputs[0]);
int b = int.Parse(inputs[1]);
Console.WriteLine(c);
"""
FEW_SHOT_PROMPTS[("control", "python")] = """\
Complete the code in python to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
t = int(input())
for # TODO: Your code here
a, b = map(int, input().split())
c = a + b
print(c)
"""
FEW_SHOT_ANSWERS[("control", "python")] = """\
_ in range(t):
"""
FEW_SHOT_PROMPTS[("control", "java")] = """\
Complete the code in java to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (/* TODO: Your code here */) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = a + b;
System.out.println(c);
}
sc.close();
}
}
"""
FEW_SHOT_ANSWERS[("control", "java")] = """\
int i = 0; i < t; i++
"""
FEW_SHOT_PROMPTS[("control", "cpp")] = """\
Complete the code in cpp to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
#include <iostream>
using namespace std;
int main() {
int t, a, b, c;
cin >> t;
for (/* TODO: Your code here */) {
cin >> a >> b;
c = a + b;
cout << c << endl;
}
return 0;
}
"""
FEW_SHOT_ANSWERS[("control", "cpp")] = """\
int i = 0; i < t; i++
"""
FEW_SHOT_PROMPTS[("control", "csharp")] = """\
Complete the code in csharp to solve this programming problem:
Description: You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$.
Input Specification: The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 \le a, b \le 1000$$$).
Output Specification: Print $$$t$$$ integers — the required numbers $$$a+b$$$.
Code:
using System;
class Program {
static void Main() {
int t = int.Parse(Console.ReadLine());
for (/* TODO: Your code here */) {
string[] inputs = Console.ReadLine().Split(' ');
int a = int.Parse(inputs[0]);
int b = int.Parse(inputs[1]);
Console.WriteLine(c);
}
}
}
"""
FEW_SHOT_ANSWERS[("control", "csharp")] = """\
int i = 0; i < t; i++
"""
FEW_SHOT_PROMPTS[("api", "python")] = """\
Complete the code in python:
import numpy as np
n = 10
# Create an n by n array of ones
a = # TODO: Your code here
"""
FEW_SHOT_ANSWERS[("api", "python")] = """\
np.ones((n, n))
"""
FEW_SHOT_PROMPTS[("api", "java")] = """\
Complete the code in java:
import org.json.JSONObject;
public class Example {
public static void main(String[] args) {
String jsonData = "{\"name\":\"John\", \"age\":30}";
JSONObject obj = /* TODO: Your code here */;
String name = obj.getString("name");
int age = obj.getInt("age");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
"""
FEW_SHOT_ANSWERS[("api", "java")] = """\
new JSONObject(jsonData)
"""
FEW_SHOT_PROMPTS[("api", "cpp")] = """\
Complete the code in cpp:
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// JSON data
std::string jsonData = "{\"name\":\"John\", \"age\":30}";
// Parse JSON data
json j = /* TODO: Your code here */;
// Extract data
std::string name = j["name"];
int age =
8000
j["age"];
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
return 0;
}
"""
FEW_SHOT_ANSWERS[("api", "cpp")] = """\
json::parse(jsonData)
"""
FEW_SHOT_PROMPTS[("api", "csharp")] = """\
using System;
using Newtonsoft.Json.Linq;
class Program
{
static void Main()
{
string jsonData = "{\"name\":\"John\", \"age\":30}";
JObject jObject = /* TODO: Your code here */;
string name = (string)jObject["name"];
int age = (int)jObject["age"];
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
}
}
"""
FEW_SHOT_ANSWERS[("api", "csharp")] = """\
JObject.Parse(jsonData)
"""
INSTRUCTION_TEMPLATE = jinja2.Template(
'Replace the "{{placeholder}}" in the code above with the appropriate {{completion_type}}. Provide only the replaced {{completion_type}}.'
)
PREFIX_FEEDING_TEMPLATE = jinja2.Template(
'Replace the "{{placeholder}}" in the code above with the appropriate {{completion_type}}.'
)
COMPLETION_TYPE_REPR = {
"block": "block",
"control": "control expression",
"api": "API function call"
}
def get_infilling_parts(sample):
parts = sample["prompt"].split(COMPLETION_PLACEHOLDER[sample["lang"]])
assert len(parts) == 2
return parts
def convert_to_l2r_prompt(sample):
idx = sample["prompt"].find(COMPLETION_PLACEHOLDER[sample["lang"]])
assert idx != -1
return sample["prompt"][:idx]
def add_prefix_feeding(sample, completion_type):
idx = sample["eval_prompt"].find("{{completion}}")
prefix_feeding_instruction = PREFIX_FEEDING_TEMPLATE.render(
placeholder=COMPLETION_PLACEHOLDER[sample["lang"]],
completion_type=COMPLETION_TYPE_REPR[completion_type]
)
return (
sample["prompt"] + "\n"
+ prefix_feeding_instruction + "\n\n"
+ sample["eval_prompt"][:idx]
)
def add_instruct(sample, completion_type):
instruction = INSTRUCTION_TEMPLATE.render(
placeholder=COMPLETION_PLACEHOLDER[sample["lang"]],
completion_type=COMPLETION_TYPE_REPR[completion_type]
)
return sample["prompt"] + "\n" + instruction + "\n\n"
def add_instruct_with_fewshot(sample, completion_type):
instruction = INSTRUCTION_TEMPLATE.render(
placeholder=COMPLETION_PLACEHOLDER[sample["lang"]],
completion_type=COMPLETION_TYPE_REPR[completion_type]
)
return (
FEW_SHOT_PROMPTS[(completion_type, sample["lang"])] + "\n"
+ instruction + "\n\n"
+ FEW_SHOT_ANSWERS[(completion_type, sample["lang"])] + "\n\n"
+ add_instruct(sample, completion_type)
)
def extract_code_from_chat_model(code, lang, completion_type):
# Extract all code blocks
in_code = False
res = []
res_inline = []
cur_code_block = []
for line in code.splitlines(keepends=True):
if line.startswith("```"):
if in_code:
in_code = False
res.append("".join(cur_code_block).rstrip())
cur_code_block = []
else:
in_code = True
else:
if in_code:
cur_code_block.append(line)
else:
if "`" in line:
start = line.find('`') + 1
end = line.find('`', start)
if end != -1:
res_inline.append(line[start:end])
if not res:
if not res_inline:
return code.rstrip()
else:
res = res_inline
# Resolve conflicts if there are multiple
new_res = []
for i, code in enumerate(res):
if COMPLETION_PLACEHOLDER[lang].strip() in code:
new_res.append((-2, code))
elif FEW_SHOT_ANSWERS[(completion_type, lang)].strip() in code:
new_res.append((-1, code))
else:
new_res.append((len(res) - i, code))
return list(sorted(new_res, reverse=True))[0][1]
def truncate_for_fewshot(code):
idx = code.find("\nComplete the code in ")
if idx != -1:
code = code[:idx]
if code.endswith("\n"):
code = code[:-1]
return code
def truncate_line_until_parsable(sample, code):
parser = get_parser(sample["lang"])
lines = code.splitlines(keepends=True)
while lines:
full_code = sample["eval_prompt"].replace("{{completion}}", "".join(lines))
code_bytes = full_code.encode("utf-8")
tree = parser.parse(code_bytes)
error_check = ErrorCheckVisitor()
error_check(tree)
if error_check.error_cnt > 0:
lines.pop()
else:
break
return "".join(lines)
def match_prefix_and_suffix(l1, l2):
p = 0
while p < len(l1) and p < len(l2):
if l1[p] == l2[p]:
p += 1
else:
break
q = 0
while -q < len(l1) and -q < len(l2):
if l1[q - 1] == l2[q - 1]:
q -= 1
else:
break
return p, q
def truncate_line_until_block(sample, code):
parser = get_parser(sample["lang"])
lines = code.splitlines(keepends=True)
while lines:
eval_prefix, eval_suffix = sample['eval_prompt'].split("{{completion}}")
eval_prefix = eval_prefix.encode("utf-8")
eval_suffix = eval_suffix.encode("utf-8")
completion = "".join(lines).encode("utf-8")
if sample["lang"] == "python":
code_bytes_0 = eval_prefix + b"pass" + eval_suffix
else:
code_bytes_0 = eval_prefix + eval_suffix
code_bytes_1 = eval_prefix + completion + eval_suffix
visitor = ErrorCheckVisitor(with_ndtypes=True)
tree = parser.parse(code_bytes_1)
visitor(tree)
if visitor.error_cnt > 0:
lines.pop()
continue
visitor_trace_1 = [(x, y) for _, x, y in visitor.ndtypes]
visitor = ErrorCheckVisitor(with_ndtypes=True)
tree = parser.parse(code_bytes_0)
visitor(tree)
assert visitor.error_cnt == 0
visitor_trace_0 = [(x, y) for _, x, y in visitor.ndtypes]
if len(visitor_trace_0) > len(visitor_trace_1):
lines.pop()
continue
prefix_matched, suffix_matched = match_prefix_and_suffix(visitor_trace_0, visitor_trace_1)
matched_diff = len(visitor_trace_0) - (prefix_matched - suffix_matched)
if sample["lang"] == "python":
matched_diff -= 4
if matched_diff == 0:
break
else:
lines.pop()
return "".join(lines)
def truncate_control(sample, completion):
if sample["lang"] == "python":
return truncate_to_first_line(completion)
else:
depth = 0
for i, ch in enumerate(completion):
if ch == "(":
depth += 1
elif ch == ")":
depth -= 1
if depth == -1:
return completion[:i]
return completion
def truncate_api_call(completion):
depth = 0
for i, ch in enumerate(completion):
if ch == "(":
depth += 1
elif ch == ")":
depth -= 1
if depth <= 0:
return completion[:i + 1]
return completion
def apply_prompt(
sample: dict,
completion_type: str,
mode: str,
model_wrapper: ModelWrapper,
) -> str:
if mode == "infilling":
prefix, suffix = get_infilling_parts(sample)
prompt = model_wrapper.assemble_infilling_prompt(prefix, suffix, reverse=False)
elif mode == "reverse_infilling":
prefix, suffix = get_infilling_parts(sample)
prompt = model_wrapper.assemble_infilling_prompt(prefix, suffix, reverse=True)
elif mode == "left_to_right":
prompt = convert_to_l2r_prompt(sample)
elif mode == "prefix_feeding":
prompt = add_prefix_feeding(sample, completion_type)
elif mode == "instructed":
prompt = add_instruct(sample, completion_type)
elif mode == "fewshot&qu
4F06
ot;:
prompt = add_instruct_with_fewshot(sample, completion_type)
else:
raise ValueError(mode)
return prompt
def apply_postprocessors(
completion: str,
sample: dict,
completion_type: str,
post_processors: Iterable[str]
) -> str:
if post_processors is None:
post_processors = []
for post_processor in post_processors:
if post_processor == "extract_code":
completion = extract_code_from_chat_model(completion, sample["lang"], completion_type)
elif post_processor == "truncate_line":
completion = truncate_to_first_line(completion)
elif post_processor == "truncate_fewshot":
completion = truncate_for_fewshot(completion)
elif post_processor == "truncate_line_until_parsable":
completion = truncate_line_until_parsable(sample, completion)
elif post_processor == "truncate_line_until_block":
completion = truncate_line_until_block(sample, completion)
elif post_processor == "truncate_control":
completion = truncate_control(sample, completion)
elif post_processor == "truncate_api_call":
completion = truncate_api_call(completion)
else:
raise ValueError(post_processor)
return completion