File size: 16,510 Bytes
256a159 |
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 |
# flake8: noqa: E501
import json
import os.path as osp
import re
from typing import Optional
from datasets import Dataset, DatasetDict
from opencompass.registry import LOAD_DATASET
from ..base import BaseDataset
NEED_REF_CATS = ['math', 'reasoning', 'coding', 'arena-hard-200']
pair_v2 = {
'type': 'pairwise',
'system_prompt':
"Please act as an impartial judge and evaluate the quality of the responses provided by two AI assistants to the user question displayed below. You should choose the assistant that follows the user's instructions and answers the user's question better. Your evaluation should consider factors such as the helpfulness, relevance, accuracy, depth, creativity, and level of detail of their responses. Begin your evaluation by comparing the two responses and provide a short explanation. Avoid any position biases and ensure that the order in which the responses were presented does not influence your decision. Do not allow the length of the responses to influence your evaluation. Do not favor certain names of the assistants. Be as objective as possible. After providing your explanation, output your final verdict by strictly following this format: \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better, and \"[[C]]\" for a tie.",
'prompt_template':
"[User Question]\n{question}\n\n[The Start of Assistant A's Answer]\n{prediction_r1}\n[The End of Assistant A's Answer]\n\n[The Start of Assistant B's Answer]\n{prediction1_r1}\n[The End of Assistant B's Answer]",
'description': 'Prompt for general questions',
'category': 'general',
'output_format': '[[A]]'
}
pair_v2_multi_turn = {
'type': 'pairwise',
'system_prompt':
"Please act as an impartial judge and evaluate the quality of the responses provided by two AI assistants to the user questions. You should choose the assistant that follows the user's instructions and answers the user's questions better. Your evaluation should consider factors such as the helpfulness, relevance, accuracy, depth, creativity, and level of detail of their responses. You should focus on who provides a better answer to the second user question. Begin your evaluation by comparing the responses of the two assistants and provide a short explanation. Avoid any position biases and ensure that the order in which the responses were presented does not influence your decision. Do not allow the length of the responses to influence your evaluation. Do not favor certain names of the assistants. Be as objective as possible. After providing your explanation, output your final verdict by strictly following this format: \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better, and \"[[C]]\" for a tie.",
'prompt_template':
"<|The Start of Assistant A's Conversation with User|>\n\n### User:\n{question_1}\n\n### Assistant A:\n{prediction_r1}\n\n### User:\n{question_2}\n\n### Assistant A:\n{prediction_r2}\n\n<|The End of Assistant A's Conversation with User|>\n\n\n<|The Start of Assistant B's Conversation with User|>\n\n### User:\n{question_1}\n\n### Assistant B:\n{prediction1_r1}\n\n### User:\n{question_2}\n\n### Assistant B:\n{prediction1_r2}\n\n<|The End of Assistant B's Conversation with User|>",
'description': 'Prompt for multi-turn general questions',
'category': 'general',
'output_format': '[[A]]'
}
pair_math_v1 = {
'type': 'pairwise',
'system_prompt':
"Please act as an impartial judge and evaluate the quality of the responses provided by two AI assistants to the user question displayed below. Your evaluation should consider correctness and helpfulness. You will be given a reference answer, assistant A's answer, and assistant B's answer. Your job is to evaluate which assistant's answer is better. Begin your evaluation by comparing both assistants' answers with the reference answer. Identify and correct any mistakes. Avoid any position biases and ensure that the order in which the responses were presented does not influence your decision. Do not allow the length of the responses to influence your evaluation. Do not favor certain names of the assistants. Be as objective as possible. After providing your explanation, output your final verdict by strictly following this format: \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better, and \"[[C]]\" for a tie.",
'prompt_template':
"[User Question]\n{question}\n\n[The Start of Reference Answer]\n{ref_answer_1}\n[The End of Reference Answer]\n\n[The Start of Assistant A's Answer]\n{prediction_r1}\n[The End of Assistant A's Answer]\n\n[The Start of Assistant B's Answer]\n{prediction1_r1}\n[The End of Assistant B's Answer]",
'description': 'Prompt for math questions',
'category': 'math',
'output_format': '[[A]]'
}
pair_math_v1_multi_turn = {
'type': 'pairwise',
'system_prompt':
"Please act as an impartial judge and evaluate the quality of the responses provided by two AI assistants to the user questions. Your evaluation should consider correctness and helpfulness. You will be given reference answers, the assistant A's answers, the assistant B's answers. Your job is to determine which assistant provides correct and helpful answers to the second user question. Begin your evaluation by comparing both assistants' answers with the reference answers. Identify and correct any mistakes. Avoid any position biases and ensure that the order in which the responses were presented does not influence your decision. Do not allow the length of the responses to influence your evaluation. Do not favor certain names of the assistants. Be as objective as possible. After providing your explanation, output your final verdict by strictly following this format: \"[[A]]\" if assistant A is better, \"[[B]]\" if assistant B is better, and \"[[C]]\" for a tie.",
'prompt_template':
"<|The Start of Reference Answer|>\n\n### User:\n{question_1}\n\n### Reference answer:\n{ref_answer_1}\n\n### User:\n{question_2}\n\n### Reference answer:\n{ref_answer_2}\n\n<|The End of Reference Answer|>\n\n\n<|The Start of Assistant A's Conversation with User|>\n\n### User:\n{question_1}\n\n### Assistant A:\n{prediction_r1}\n\n### User:\n{question_2}\n\n### Assistant A:\n{prediction_r2}\n\n<|The End of Assistant A's Conversation with User|>\n\n\n<|The Start of Assistant B's Conversation with User|>\n\n### User:\n{question_1}\n\n### Assistant B:\n{prediction1_r1}\n\n### User:\n{question_2}\n\n### Assistant B:\n{prediction1_r2}\n\n<|The End of Assistant B's Conversation with User|>",
'description': 'Prompt for multi-turn general questions',
'category': 'general',
'output_format': '[[A]]'
}
single_v1 = {
'type': 'single',
'system_prompt': 'You are a helpful assistant.',
'prompt_template':
"[Instruction]\nPlease act as an impartial judge and evaluate the quality of the response provided by an AI assistant to the user question displayed below. Your evaluation should consider factors such as the helpfulness, relevance, accuracy, depth, creativity, and level of detail of the response. Begin your evaluation by providing a short explanation. Be as objective as possible. After providing your explanation, you must rate the response on a scale of 1 to 10 by strictly following this format: \"[[rating]]\", for example: \"Rating: [[5]]\".\n\n[Question]\n{question}\n\n[The Start of Assistant's Answer]\n{prediction_r1}\n[The End of Assistant's Answer]",
'description': 'Prompt for general questions',
'category': 'general',
'output_format': '[[rating]]'
}
single_math_v1 = {
'type': 'single',
'system_prompt': 'You are a helpful assistant.',
'prompt_template':
"[Instruction]\nPlease act as an impartial judge and evaluate the quality of the response provided by an AI assistant to the user question displayed below. Your evaluation should consider correctness and helpfulness. You will be given a reference answer and the assistant's answer. Begin your evaluation by comparing the assistant's answer with the reference answer. Identify and correct any mistakes. Be as objective as possible. After providing your explanation, you must rate the response on a scale of 1 to 10 by strictly following this format: \"[[rating]]\", for example: \"Rating: [[5]]\".\n\n[Question]\n{question}\n\n[The Start of Reference Answer]\n{ref_answer_1}\n[The End of Reference Answer]\n\n[The Start of Assistant's Answer]\n{prediction_r1}\n[The End of Assistant's Answer]",
'description': 'Prompt for general questions',
'category': 'math',
'output_format': '[[rating]]'
}
single_v1_multi_turn = {
'type': 'single',
'system_prompt':
"Please act as an impartial judge and evaluate the quality of the response provided by an AI assistant to the user question displayed below. Your evaluation should consider factors such as the helpfulness, relevance, accuracy, depth, creativity, and level of detail of the response. You evaluation should focus on the assistant's answer to the second user question. Begin your evaluation by providing a short explanation. Be as objective as possible. After providing your explanation, you must rate the response on a scale of 1 to 10 by strictly following this format: \"[[rating]]\", for example: \"Rating: [[5]]\".\n\n",
'prompt_template':
"<|The Start of Assistant A's Conversation with User|>\n\n### User:\n{question_1}\n\n### Assistant A:\n{prediction_r1}\n\n### User:\n{question_2}\n\n### Assistant A:\n{prediction_r2}\n\n<|The End of Assistant A's Conversation with User|>",
'description': 'Prompt for general questions',
'category': 'general',
'output_format': '[[rating]]'
}
single_math_v1_multi_turn = {
'type': 'single',
'system_prompt':
"Please act as an impartial judge and evaluate the quality of the response provided by an AI assistant to the user question. Your evaluation should consider correctness and helpfulness. You will be given a reference answer and the assistant's answer. You evaluation should focus on the assistant's answer to the second question. Begin your evaluation by comparing the assistant's answer with the reference answer. Identify and correct any mistakes. Be as objective as possible. After providing your explanation, you must rate the response on a scale of 1 to 10 by strictly following this format: \"[[rating]]\", for example: \"Rating: [[5]]\".\n\n",
'prompt_template':
"<|The Start of Reference Answer|>\n\n### User:\n{question_1}\n\n### Reference answer:\n{ref_answer_1}\n\n### User:\n{question_2}\n\n### Reference answer:\n{ref_answer_2}\n\n<|The End of Reference Answer|>\n\n\n<|The Start of Assistant A's Conversation with User|>\n\n### User:\n{question_1}\n\n### Assistant A:\n{prediction_r1}\n\n### User:\n{question_2}\n\n### Assistant A:\n{prediction_r2}\n\n<|The End of Assistant A's Conversation with User|>",
'description': 'Prompt for general questions',
'category': 'math',
'output_format': '[[rating]]'
}
def prompt_construct(problem, multi_turn=False, judge_type='single'):
"""Return the correct pairwise judge."""
question_1 = problem['dialogue'][0]['content']
if multi_turn:
question_2 = problem['dialogue'][2]['content']
if problem['capability'] in NEED_REF_CATS:
ref_answer_1 = problem['others']['reference'][0]
ref_answer_2 = problem['others']['reference'][1]
if judge_type == 'pair':
return pair_math_v1_multi_turn[
'system_prompt'], pair_math_v1_multi_turn[
'prompt_template'].format(
question_1=question_1,
question_2=question_2,
ref_answer_1=ref_answer_1,
ref_answer_2=ref_answer_2,
prediction_r1='{prediction_r1}',
prediction_r2='{prediction_r2}',
prediction1_r1='{prediction1_r1}',
prediction1_r2='{prediction1_r2}')
elif judge_type == 'single':
return single_math_v1_multi_turn[
'system_prompt'], single_math_v1_multi_turn[
'prompt_template'].format(
question_1=question_1,
question_2=question_2,
ref_answer_1=ref_answer_1,
ref_answer_2=ref_answer_2,
prediction_r1='{prediction_r1}',
prediction_r2='{prediction_r2}')
if judge_type == 'pair':
return pair_v2_multi_turn['system_prompt'], pair_v2_multi_turn[
'prompt_template'].format(question_1=question_1,
question_2=question_2,
prediction_r1='{prediction_r1}',
prediction_r2='{prediction_r2}',
prediction1_r1='{prediction1_r1}',
prediction1_r2='{prediction1_r2}')
elif judge_type == 'single':
return single_v1_multi_turn['system_prompt'], single_v1_multi_turn[
'prompt_template'].format(question_1=question_1,
question_2=question_2,
answer_1='{answer_1}',
prediction_r1='{prediction_r1}',
prediction_r2='{prediction_r2}')
if problem['capability'] in NEED_REF_CATS:
ref_answer_1 = problem['others']['reference'][0]
if judge_type == 'pair':
return pair_math_v1['system_prompt'], pair_math_v1[
'prompt_template'].format(question=question_1,
ref_answer_1=ref_answer_1,
prediction_r1='{prediction_r1}',
prediction1_r1='{prediction1_r1}')
elif judge_type == 'single':
return single_math_v1['system_prompt'], single_math_v1[
'prompt_template'].format(question=question_1,
ref_answer_1=ref_answer_1,
prediction_r1='{prediction_r1}')
else:
if judge_type == 'pair':
return pair_v2['system_prompt'], pair_v2['prompt_template'].format(
question=question_1,
prediction_r1='{prediction_r1}',
prediction1_r1='{prediction1_r1}')
elif judge_type == 'single':
return single_v1['system_prompt'], single_v1[
'prompt_template'].format(question=question_1,
prediction_r1='{prediction_r1}')
@LOAD_DATASET.register_module()
class MTBenchDataset(BaseDataset):
def load(self, path: str, name: str, multi_turn=True, judge_type='single'):
filename = osp.join(path, f'{name}.json')
dataset = DatasetDict()
raw_data = []
with open(filename, 'r', encoding='utf-8') as f:
json_data = json.load(f)
for problem in json_data:
if 'dialogue' in problem:
system_prompt, prompt_template = prompt_construct(
problem, multi_turn, judge_type)
dialogue = problem['dialogue']
capability = problem['capability']
others = problem['others']
others['round'] = int(len(dialogue) / 2)
user_contents = [
item['content'] for item in dialogue
if item['role'] == 'user'
]
question = ' '.join(user_contents)
others['question'] = question
raw_data.append({
'dialogue': dialogue,
'capability': capability,
'system_prompt': system_prompt,
'prompt_template': prompt_template,
'others': others,
'judge': {
'capability': capability,
'others': others,
}
})
dataset = Dataset.from_list(raw_data)
return dataset
|