(self, doc: dict)
| 1047 | raise TypeError |
| 1048 | |
| 1049 | def doc_to_target(self, doc: dict) -> Union[int, str, list]: |
| 1050 | if self.prompt is not None: |
| 1051 | doc_to_target = self.prompt |
| 1052 | else: |
| 1053 | doc_to_target = self.config.doc_to_target |
| 1054 | |
| 1055 | if isinstance(doc_to_target, int): |
| 1056 | return doc_to_target |
| 1057 | elif isinstance(doc_to_target, str): |
| 1058 | if doc_to_target in self.features: |
| 1059 | # if self.config.doc_to_choice is not None: |
| 1060 | # return self.doc_to_choice(doc)[doc[doc_to_target]] |
| 1061 | # else: |
| 1062 | return doc[doc_to_target] |
| 1063 | else: |
| 1064 | target_string = utils.apply_template(doc_to_target, doc) |
| 1065 | if target_string.isdigit() and self._config.doc_to_choice is not None: |
| 1066 | return ast.literal_eval(target_string) |
| 1067 | elif ( |
| 1068 | len(target_string) >= 2 |
| 1069 | and (target_string[0] == "[") |
| 1070 | and (target_string[-1] == "]") |
| 1071 | ): |
| 1072 | try: |
| 1073 | return ast.literal_eval(target_string) |
| 1074 | except (SyntaxError, ValueError): |
| 1075 | return target_string |
| 1076 | else: |
| 1077 | return target_string |
| 1078 | elif isinstance(doc_to_target, list): |
| 1079 | return doc_to_target |
| 1080 | elif callable(doc_to_target): |
| 1081 | return doc_to_target(doc) |
| 1082 | # Used when applying a Promptsource template |
| 1083 | elif hasattr(doc_to_target, "apply"): |
| 1084 | applied_prompt = doc_to_target.apply(doc) |
| 1085 | if len(applied_prompt) == 2: |
| 1086 | return applied_prompt[1] |
| 1087 | else: |
| 1088 | eval_logger.warning("Applied prompt returns empty string") |
| 1089 | return self.config.fewshot_delimiter |
| 1090 | else: |
| 1091 | raise TypeError |
| 1092 | |
| 1093 | def doc_to_choice(self, doc: Any) -> List[str]: |
| 1094 | if self.prompt is not None: |
no test coverage detected