text
stringlengths
0
15.3k
return self.config.fewshot_config['samples']()
else:
raise Exception("`fewshot_config['samples']` was incorrectly defined in the configuration. It should be either a list of samples as a dict, or function returning this list.")
else:
if self.config.num_fewshot is not None and self.config.num_fewshot > 0:
eval_logger.warning(f'[Task: {self.config.task}] num_fewshot > 0 but fewshot_split is None. using preconfigured rule.')
return super().fewshot_docs()
@staticmethod
def append_target_question(labeled_examples: List[Dict[str, str]], question: str, fewshot_as_multiturn: bool=False) -> None:
if not fewshot_as_multiturn:
if len(labeled_examples) == 0 or labeled_examples[-1]['role'] == 'system':
labeled_examples.append({'role': 'user', 'content': question})
else:
labeled_examples[-1]['content'] += question
else:
labeled_examples.append({'role': 'user', 'content': question})
@utils.positional_deprecated
def fewshot_context(self, doc: str, num_fewshot: int, system_instruction: Optional[str]=None, apply_chat_template: bool=False, fewshot_as_multiturn: bool=False, chat_template: Optional[Callable]=None) -> str:
if apply_chat_template:
labeled_examples = []
else:
labeled_examples = ''
if (description := self.config.description):
description = utils.apply_template(self.config.description, doc)
if system_instruction is not None and description:
system_prompt = f'{system_instruction}{self.sampler.fewshot_delimiter}{description}'
elif system_instruction is not None:
system_prompt = system_instruction
elif description:
system_prompt = description
else:
system_prompt = ''
if system_prompt:
if apply_chat_template:
labeled_examples.append({'role': 'system', 'content': system_prompt})
else:
labeled_examples = system_prompt
if num_fewshot > 0:
if apply_chat_template:
labeled_examples.extend(self.sampler.get_chat_context(doc, num_fewshot, fewshot_as_multiturn))
else:
labeled_examples += self.sampler.get_context(doc, num_fewshot)
example = self.doc_to_text(doc)
if apply_chat_template:
if self.multiple_input:
return chat_template(labeled_examples)
if isinstance(example, str):
self.append_target_question(labeled_examples, example, fewshot_as_multiturn)
elif isinstance(example, list):
labeled_examples_list = []
for ex in example:
chat = deepcopy(labeled_examples)
self.append_target_question(chat, ex, fewshot_as_multiturn)
labeled_examples_list.append(chat_template(chat))
return labeled_examples_list
elif isinstance(example, int):
if self.config.doc_to_choice is not None:
choices = self.doc_to_choice(doc)
self.append_target_question(labeled_examples, choices[example], fewshot_as_multiturn)
else:
self.append_target_question(labeled_examples, str(example), fewshot_as_multiturn)
return chat_template(labeled_examples)
else:
if self.multiple_input:
return labeled_examples
if isinstance(example, str):
return labeled_examples + example
elif isinstance(example, list):
return [labeled_examples + ex for ex in example]
elif isinstance(example, int):
if self.config.doc_to_choice is not None:
choices = self.doc_to_choice(doc)
return labeled_examples + choices[example]
else:
return labeled_examples + str(example)
def apply_filters(self):
if hasattr(self, '_filters'):
for f in self._filters:
f.apply(self._instances)
else:
eval_logger.warning('No filter defined, passing through instances')
return self._instances
def should_decontaminate(self):
return self.config.should_decontaminate
def doc_to_decontamination_query(self, doc):
if self.config.should_decontaminate:
if self.config.doc_to_decontamination_query is None:
return self.doc_to_text(doc)
else:
doc_to_decontamination_query = self.config.doc_to_decontamination_query
if doc_to_decontamination_query in self.features:
return doc[doc_to_decontamination_query]
elif callable(doc_to_decontamination_query):
return doc_to_decontamination_query(doc)
else: