Commit 899a544a authored by Konrad's avatar Konrad
Browse files

multiturn alternation fix

parent c3706651
...@@ -293,6 +293,11 @@ def cli_evaluate(args: Union[argparse.Namespace, None] = None) -> None: ...@@ -293,6 +293,11 @@ def cli_evaluate(args: Union[argparse.Namespace, None] = None) -> None:
"If fewshot_as_multiturn is set, apply_chat_template must be set to True." "If fewshot_as_multiturn is set, apply_chat_template must be set to True."
) )
if args.num_fewshot is None or args.num_fewshot == 0 and args.fewshot_as_multiturn:
raise ValueError(
"If fewshot_as_multiturn is set, num_fewshot must be greater than 0."
)
if args.include_path is not None: if args.include_path is not None:
eval_logger.info(f"Including path: {args.include_path}") eval_logger.info(f"Including path: {args.include_path}")
task_manager = TaskManager(args.verbosity, include_path=args.include_path) task_manager = TaskManager(args.verbosity, include_path=args.include_path)
......
...@@ -965,6 +965,27 @@ class ConfigurableTask(Task): ...@@ -965,6 +965,27 @@ class ConfigurableTask(Task):
) )
return super().fewshot_docs() return super().fewshot_docs()
@staticmethod
def append_target_question(
labeled_examples: list[dict[str, str]],
question: str,
fewshot_as_multiturn: bool = False,
) -> None:
"""Adds a target question to the labeled examples list.
If fewshot_as_multiturn is True, or labeled_examples is empty, or the last entry is a system turn, appends the question as a new user entry.
Otherwise, it is appended to the last user entry, ensuring that the conversation alternates between the user and the assistant.
"""
if not fewshot_as_multiturn:
# if no messages or last message is system, append as new user entry
if len(labeled_examples) == 0 or labeled_examples[-1]["role"] == "system":
labeled_examples.append({"role": "user", "content": question})
# if last message is user, append to it to avoid two user messages in a row
else:
labeled_examples[-1]["content"] += question
else:
# if fewshot_as_multiturn is True, append as next user entry (last is always assistant)
labeled_examples.append({"role": "user", "content": question})
@utils.positional_deprecated @utils.positional_deprecated
def fewshot_context( def fewshot_context(
self, self,
...@@ -1035,29 +1056,33 @@ class ConfigurableTask(Task): ...@@ -1035,29 +1056,33 @@ class ConfigurableTask(Task):
example = self.doc_to_text(doc) example = self.doc_to_text(doc)
if apply_chat_template: if apply_chat_template:
if not self.multiple_input: if self.multiple_input:
if isinstance(example, str): return lm.apply_chat_template(labeled_examples)
labeled_examples.append({"role": "user", "content": example}) if isinstance(example, str):
# for loglikelihood create a list of questions with appended choices self.append_target_question(
elif isinstance(example, list): labeled_examples, example, fewshot_as_multiturn
labeled_examples_list = [] )
# copy chat history for each example and append the answer # for loglikelihood create a list of questions with appended choices
for ex in example: elif isinstance(example, list):
chat = deepcopy(labeled_examples) labeled_examples_list = []
chat.append({"role": "user", "content": ex}) # copy chat history for each example and append the answer
labeled_examples_list.append(lm.apply_chat_template(chat)) for ex in example:
return labeled_examples_list chat = deepcopy(labeled_examples)
# if example is an integer, append the choice or convert to string self.append_target_question(chat, ex, fewshot_as_multiturn)
elif isinstance(example, int): labeled_examples_list.append(lm.apply_chat_template(chat))
if self.config.doc_to_choice is not None: return labeled_examples_list
choices = self.doc_to_choice(doc) # if example is an integer, append the choice or convert to string
labeled_examples.append( elif isinstance(example, int):
{"role": "user", "content": choices[example]} if self.config.doc_to_choice is not None:
) choices = self.doc_to_choice(doc)
else: self.append_target_question(
labeled_examples.append( labeled_examples, choices[example], fewshot_as_multiturn
{"role": "user", "content": str(example)} )
) else:
self.append_target_question(
labeled_examples, str(example), fewshot_as_multiturn
)
# return lm.apply_chat_template(labeled_examples)
return lm.apply_chat_template(labeled_examples) return lm.apply_chat_template(labeled_examples)
else: else:
if self.multiple_input: if self.multiple_input:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment