Upload formats.py with huggingface_hub
Browse files- formats.py +48 -0
formats.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .artifact import Artifact
|
2 |
+
|
3 |
+
|
4 |
+
class Format(Artifact):
|
5 |
+
pass
|
6 |
+
|
7 |
+
|
8 |
+
class SizeLimitingFormat(Format):
|
9 |
+
size_limiter: Artifact = None
|
10 |
+
|
11 |
+
|
12 |
+
class ICLFormat(SizeLimitingFormat):
|
13 |
+
input_prefix: str = ""
|
14 |
+
output_prefix: str = ""
|
15 |
+
target_prefix: str = " "
|
16 |
+
instruction_prefix: str = ""
|
17 |
+
input_output_separator: str = "\n"
|
18 |
+
demo_separator: str = "\n\n"
|
19 |
+
|
20 |
+
def single_source_str(self, source):
|
21 |
+
return self.input_prefix + source + self.input_output_separator + self.output_prefix
|
22 |
+
|
23 |
+
def format(self, instance, demos_instances=[]):
|
24 |
+
source = ""
|
25 |
+
|
26 |
+
query_str = self.single_source_str(instance["source"])
|
27 |
+
|
28 |
+
if "instruction" in instance:
|
29 |
+
instruction = instance.pop("instruction")
|
30 |
+
source += self.instruction_prefix + instruction + self.demo_separator
|
31 |
+
|
32 |
+
for demo_instance in demos_instances:
|
33 |
+
demo_str = (
|
34 |
+
self.single_source_str(demo_instance["source"])
|
35 |
+
+ self.target_prefix
|
36 |
+
+ demo_instance["target"]
|
37 |
+
+ self.demo_separator
|
38 |
+
)
|
39 |
+
|
40 |
+
if self.size_limiter is not None:
|
41 |
+
if not self.size_limiter.check(source + demo_str + query_str + instance["target"]):
|
42 |
+
continue
|
43 |
+
|
44 |
+
source += demo_str
|
45 |
+
|
46 |
+
source += query_str
|
47 |
+
|
48 |
+
return source
|