Elron commited on
Commit
1247c04
1 Parent(s): 78663de

Upload formats.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. formats.py +37 -6
formats.py CHANGED
@@ -18,19 +18,49 @@ class ICLFormat(SizeLimitingFormat):
18
  input_output_separator: str = "\n"
19
  demo_separator: str = "\n\n"
20
  suffix: str = ""
 
 
21
 
22
  def single_source_str(self, source):
23
- return self.input_prefix + source + self.input_output_separator + self.output_prefix
 
 
 
 
 
24
 
25
- def format(self, instance, demos_instances=[]):
26
- source = self.prefix
 
 
 
 
 
 
 
27
 
28
- query_str = self.single_source_str(instance["source"])
 
 
 
29
 
 
30
  if "instruction" in instance:
31
  instruction = instance.pop("instruction")
 
 
 
 
 
32
  source += self.instruction_prefix + instruction + self.demo_separator
33
 
 
 
 
 
 
 
 
34
  for demo_instance in demos_instances:
35
  demo_str = (
36
  self.single_source_str(demo_instance["source"])
@@ -40,12 +70,13 @@ class ICLFormat(SizeLimitingFormat):
40
  )
41
 
42
  if self.size_limiter is not None:
43
- if not self.size_limiter.check(source + demo_str + query_str + instance["target"]):
 
 
44
  continue
45
 
46
  source += demo_str
47
 
48
  source += query_str
49
  source += self.suffix
50
-
51
  return source
 
18
  input_output_separator: str = "\n"
19
  demo_separator: str = "\n\n"
20
  suffix: str = ""
21
+ add_instruction_at_start: bool = True
22
+ add_instruction_after_demos: bool = False
23
 
24
  def single_source_str(self, source):
25
+ return (
26
+ self.input_prefix
27
+ + source
28
+ + self.input_output_separator
29
+ + self.output_prefix
30
+ )
31
 
32
+ def single_source_str_with_instruction(self, source, instruction):
33
+ return (
34
+ self.input_prefix
35
+ + instruction
36
+ + self.demo_separator
37
+ + source
38
+ + self.input_output_separator
39
+ + self.output_prefix
40
+ )
41
 
42
+ def format(self, instance, demos_instances=None):
43
+ if demos_instances is None:
44
+ demos_instances = []
45
+ source = self.prefix
46
 
47
+ instruction = ""
48
  if "instruction" in instance:
49
  instruction = instance.pop("instruction")
50
+ assert (
51
+ "instruction" != None
52
+ ), f"instruction field can not be none : {instance}"
53
+
54
+ if self.add_instruction_at_start and instruction != "":
55
  source += self.instruction_prefix + instruction + self.demo_separator
56
 
57
+ if self.add_instruction_after_demos and instruction != "":
58
+ query_str = self.single_source_str_with_instruction(
59
+ instance["source"], instruction
60
+ )
61
+ else:
62
+ query_str = self.single_source_str(instance["source"])
63
+
64
  for demo_instance in demos_instances:
65
  demo_str = (
66
  self.single_source_str(demo_instance["source"])
 
70
  )
71
 
72
  if self.size_limiter is not None:
73
+ if not self.size_limiter.check(
74
+ source + demo_str + query_str + instance["target"]
75
+ ):
76
  continue
77
 
78
  source += demo_str
79
 
80
  source += query_str
81
  source += self.suffix
 
82
  return source