-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_types.py
310 lines (240 loc) · 9.22 KB
/
common_types.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
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Standard type definitions shared by modules."""
import dataclasses
import enum
import re
from typing import Any
import immutabledict
# ==== Parsing Types and Helper Functions ====
UNKNOWN_ANSWER_DEFAULT_STRING = "UNKNOWN_ANSWER_DEFAULT_STRING"
class ParsingFunctionEnum(enum.StrEnum):
STRIP_ONLY = "STRIP_ONLY"
REGEX_NAIVE1 = "REGEX_NAIVE1"
CLEAN_MODEL_RESPONSE = "CLEAN_MODEL_RESPONSE"
BASE = "BASE"
SYLLOGISM_CLEAN_MODEL_RESPONSE = "SYLLOGISM_CLEAN_MODEL_RESPONSE"
PARSE_PYTHON_CODE_LIST = "PARSE_PYTHON_CODE_LIST"
def _regex_naive1(answer):
"""Uses regex search to parse for the first answer.
Args:
answer: The raw llm prediction.
Returns:
The parsed answer.
"""
# Note that newline search must occur before the no newline search.
# This is so Answer:\n<answer> is matched before checking Answer:<answer>.
# The second one will trigger if the first one does, but the second one will
# only return an empty string.
answer_search1 = re.search(r"Answer:\n(.*)", answer)
answer_search2 = re.search(r"\*\*Answer:\*\*\n(.*)", answer)
answer_search3 = re.search(r"Answer:(.*)", answer)
if answer_search1:
answer = answer_search1.group(1)
elif answer_search2:
answer = answer_search2.group(1)
elif answer_search3:
answer = answer_search3.group(1)
else:
answer = UNKNOWN_ANSWER_DEFAULT_STRING
return answer.replace("\n", "").replace("*", "")
def _strip_only(answer):
return answer.strip()
def _clean_model_response(x):
x = x.strip().split("\n")[-1]
if "**Answer:**" in x:
x = x.split("**Answer:**")[1]
elif "Answer:" in x:
x = x.split("Answer:")[1]
x = x.replace("*", "").replace(".", "")
x = x.strip()
return x
def _parse_python_code_list_answer(llm_answer):
"""Parses llm_answer for python list."""
# turn '```python' to '```'
processed_answer = llm_answer.replace("```python", "```")
code_block_start = processed_answer.find("```")
if code_block_start == -1:
return UNKNOWN_ANSWER_DEFAULT_STRING
code_block_end = processed_answer.find("```", code_block_start + 3)
if code_block_end == -1:
return UNKNOWN_ANSWER_DEFAULT_STRING
list_block_start = processed_answer.find(
"[", code_block_start, code_block_end)
if list_block_start == -1:
return UNKNOWN_ANSWER_DEFAULT_STRING
list_block_end = processed_answer.find(
"]", list_block_start, code_block_end)
if list_block_end == -1:
return UNKNOWN_ANSWER_DEFAULT_STRING
return processed_answer[list_block_start : list_block_end + 1]
def _syllogism_clean_model_response(answer):
"""Syllogism parsing implementation.
Args:
answer: The string of the raw LLM answer.
Returns:
The parsed answer.
"""
out = answer.strip().split("\n")[-1]
if "**Answer:**" in out:
out = out.split("**Answer:**")[1]
elif "Answer:" in out:
out = out.split("Answer:")[1]
out = out.replace("*", "").replace(".", "")
out = out.strip()
if not out:
out = answer.strip()
if "**Answer:**" in out:
out = out.split("**Answer:**")[0]
elif "Answer:" in out:
out = out.split("Answer:")[0]
out = out.replace("*", "").replace(".", "")
out = out.strip()
return out
PARSING_FN = immutabledict.immutabledict({
ParsingFunctionEnum.STRIP_ONLY: _strip_only,
ParsingFunctionEnum.REGEX_NAIVE1: _regex_naive1,
ParsingFunctionEnum.CLEAN_MODEL_RESPONSE: _clean_model_response,
ParsingFunctionEnum.BASE: lambda x: x,
ParsingFunctionEnum.SYLLOGISM_CLEAN_MODEL_RESPONSE: (
_syllogism_clean_model_response
),
ParsingFunctionEnum.PARSE_PYTHON_CODE_LIST: _parse_python_code_list_answer,
})
# ==== Prommpting Types ====
ALL_STR_PROMPT_STRING = "ALL"
# If *_ALL is requested, then all prompts in the same StrEnum will be requested.
class CommonPromptEnum(enum.StrEnum):
"""Common prompts for all modules."""
DEFAULT_ALL = ALL_STR_PROMPT_STRING
PROMPT_1 = "{question}"
PROMPT_2 = "{question}\nAnswer in only one word."
PROMPT_3 = (
"{question}\nThink through your answer then respond at the end "
"with a newline and 'Answer:' with your answer. "
"Use only one word for the answer."
)
PROMPT_3_MULTI = (
"{question}\nThink through your answer then respond at the end "
"with a newline and 'Answer:' with your answer."
)
PROMPT_4 = "{question}\nLet's think step by step"
class SocialNetworkPromptEnum(enum.StrEnum):
SOCIAL_DEFAULT_ALL = ALL_STR_PROMPT_STRING
SOCIAL_PROMPT_1 = (
"You are a language model with advanced cognitive abilities. "
"Your task is to understand and reason about the following "
"social scenario, much like a human would. "
"Read the story carefully and answer the questions that follow\n"
"{question}"
)
class ComparisonPromptEnum(enum.StrEnum):
"""Enum for comparison prompts."""
COMPARISON_DEFAULT_ALL = ALL_STR_PROMPT_STRING
COMPARISON_PROMPT_1 = (
"{question}\nAnswer the above relational reasoning question with Yes or"
" No. Use only one word for the answer."
)
COMPARISON_PROMPT_3 = (
"You are a language model being probed for your reasoning abilities."
" Your task is to carefully think about the following information"
" and answer the question.\n{question}\n. Make sure to respond at the"
" end with 'Answer:'"
)
COMPARISON_PROMPT_5 = (
"{question}\nAnswer the above relational reasoning question with Yes or"
" No with a newline and 'Answer:' with your answer. "
"Give your best guess if uncertain. Use only one word for the answer."
)
COMPARISON_PROMPT_6 = (
"{question}\nAnswer the above relational reasoning question with only "
" Yes or No with a newline and 'Answer:'. "
"Give your best guess if uncertain. Use only one word for the answer."
)
class ComparisonIndeterminatePromptEnum(enum.StrEnum):
"""Comparison prompts that probe for uncertainty."""
COMPARISON_INDETERMINATE_DEFAULT_ALL = ALL_STR_PROMPT_STRING
COMPARISON_INDETERMINATE_1 = (
"{question}\nAnswer the above relational reasoning question with Yes, No,"
" or Unknown. Use Unknown if the question cannot be answered with "
"the information given. Use only one word for the answer."
)
class ComparisonConsistentPromptEnum(enum.StrEnum):
"""Comparison prompts that probe for detecting inconsistency."""
COMPARISON_CONSISTENT_DEFAULT_ALL = ALL_STR_PROMPT_STRING
COMPARISON_CONSISTENT_1 = ComparisonPromptEnum.COMPARISON_PROMPT_3.value
class SyllogismPrompt(enum.StrEnum):
"""Syllogism Prompts."""
SYLLOGISM_DEFAULT_ALL = ALL_STR_PROMPT_STRING
SYLLOGISM_1 = (
"{question}\nThink through your answer then respond at the end "
"with a newline and 'Answer:' with your answer."
)
StrPrompt = enum.StrEnum
# ==== Generic Classes ====
class DatasetSplit(enum.StrEnum):
TRAIN = "train"
VAL = "val"
TEST = "test"
@dataclasses.dataclass(frozen=True)
class Entity:
"""Represents an entity in any module/example.
text: The text information of the Entity like name or type
image: Any, not implemented yet.
"""
text: str
image: Any = dataclasses.field(hash=False)
@dataclasses.dataclass(frozen=True)
class CongruentObjectEntity(Entity):
"""An entity with a specific size and weight that can be compared.
weight: How heavy an entity is in kilograms.
size: How large an entity is in meters.
"""
weight: float = dataclasses.field(default=-1.0, hash=False)
size: float = dataclasses.field(default=-1.0, hash=False)
# Class for invalid data generation (usually generate an invalid graph)
class DataGenerationError(RuntimeError):
"""Exception for invalid data generation."""
# ==== Configs ====
class Ordering(enum.StrEnum):
"""Ordering for the entities in the prompt."""
INORDER = "inorder"
REVERSE = "reverse"
RANDOM = "random"
class EntityMode(enum.StrEnum):
"""Mode for how to generate entities."""
PRESET = "preset"
INPUT = "input"
CUSTOM = "custom"
CONGRUENT = "congruent"
INCONGRUENT = "incongruent"
# Comparison relation types and congruency modes.
class RelationType(enum.StrEnum):
SIZE_NATURALTEXT = "size"
WEIGHT_NATURALTEXT = "weight"
AGE_NATURALTEXT = "age"
class CongruencyMode(enum.StrEnum):
ALL_CONGRUENT = "all_congruent" # all relationships will be congruent.
ALL_INCONGRUENT = "all_incongruent" # all relationships will be incongruent.
RANDOM_NAME = "random_name" # object names will be randomly generated.
RANDOM = "random" # all objects will be shuffled to construct an order.
@dataclasses.dataclass
class ModelRunSpec:
model_label: str = ""
human_readable_label: str = ""
model_class: Any = None
num_workers: int = -1
optimal_prompt: Any = None
optimal_parse: Any = None