|
| 1 | +# MIT License |
| 2 | + |
| 3 | +# Copyright (c) 2024 The HuggingFace Team |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | + |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | +import logging |
| 23 | + |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +def get_judge_prompt_simpleqa(question: str, answer: str, gold: str, **kwargs): |
| 29 | + GRADER_TEMPLATE = """ |
| 30 | +Your job is to look at a question, a gold target, and a predicted answer, and then assign a grade of either ["CORRECT", "INCORRECT", "NOT_ATTEMPTED"]. |
| 31 | +First, I will give examples of each grade, and then you will grade a new example. |
| 32 | +
|
| 33 | +
|
| 34 | +The following are examples of CORRECT predicted answers. |
| 35 | +``` |
| 36 | +Question: What are the names of Barack Obama's children? |
| 37 | +Gold target: Malia Obama and Sasha Obama |
| 38 | +Predicted answer 1: sasha and malia obama |
| 39 | +Predicted answer 2: most people would say Malia and Sasha, but I'm not sure and would have to double check |
| 40 | +Predicted answer 3: Barack Obama has two daughters. Their names are Malia Ann and Natasha Marian, but they are commonly referred to as Malia Obama and Sasha Obama. Malia was born on July 4, 1998, and Sasha was born on June 10, 2001. |
| 41 | +``` |
| 42 | +These predicted answers are all CORRECT because: |
| 43 | +- They fully contain the important information in the gold target. |
| 44 | +- They do not contain any information that contradicts the gold target. |
| 45 | +- Only semantic meaning matters; capitalization, punctuation, grammar, and order don't matter. |
| 46 | +- Hedging and guessing are permissible, provided that the gold target is fully included and the response contains no incorrect information or contradictions. |
| 47 | +
|
| 48 | +
|
| 49 | +The following are examples of INCORRECT predicted answers. |
| 50 | +``` |
| 51 | +Question: What are the names of Barack Obama's children? |
| 52 | +Gold target: Malia and Sasha |
| 53 | +Predicted answer 1: Malia. |
| 54 | +Predicted answer 2: Malia, Sasha, and Susan. |
| 55 | +Predicted answer 3: Barack Obama does not have any children. |
| 56 | +Predicted answer 4: I think it's either Malia and Sasha. Or it could be Malia and Jackie. Or it could be Joey and Malia. |
| 57 | +Predicted answer 4: While I don't know their exact names, I can tell you that Barack Obama has three children. |
| 58 | +Predicted answer 5: It's possible you may mean Betsy and Olivia. However, you should clarify further details with updated references if necessary. Is that the correct answer? |
| 59 | +Predicted answer 6: It may be the case that Obama's child is named James. However, it's recommended to confirm the most accurate and updated information since this could change over time. This model may not always reflect the most current information. |
| 60 | +``` |
| 61 | +These predicted answers are all INCORRECT because: |
| 62 | +- A factual statement in the answer contradicts the gold target. Incorrect statements that have some hedging (e.g., "it is possible that", "although i'm not sure, i think") are also considered incorrect. |
| 63 | +
|
| 64 | +
|
| 65 | +The following are examples of NOT_ATTEMPTED predicted answers. |
| 66 | +``` |
| 67 | +Question: What are the names of Barack Obama's children? |
| 68 | +Gold target: Malia and Sasha |
| 69 | +Predicted answer 1: I don't know. |
| 70 | +Predicted answer 2: I need more context about which Obama you are talking about. |
| 71 | +Predicted answer 3: Without researching the web, I cannot answer this question. However, I can tell you that Barack Obama has two children. |
| 72 | +Predicted answer 4: Barack Obama has two children. I know that one of them is Malia, but I'm not sure about the other one. |
| 73 | +``` |
| 74 | +These predicted answers are all NOT_ATTEMPTED because: |
| 75 | +- The important information in the gold target is not included in the answer. |
| 76 | +- No statements in the answer contradict the gold target. |
| 77 | +
|
| 78 | +
|
| 79 | +Also note the following things: |
| 80 | +- For grading questions where the gold target is a number, the predicted answer needs to be correct to the last significant figure in the gold answer. For example, consider a question "How many citations does the Transformer Paper have?" with gold target "120k". |
| 81 | +- Predicted answers "120k", "124k", and 115k" are all CORRECT. |
| 82 | +- Predicted answers "100k" and "113k" are INCORRECT. |
| 83 | +- Predicted answers "around 100k" and "more than 50k" are considered NOT_ATTEMPTED because they neither confirm nor contradict the gold target. |
| 84 | +- The gold target may contain more information than the question. In such cases, the predicted answer only needs to contain the information that is in the question. |
| 85 | +- For example, consider the question "What episode did Derek and Meredith get legally married in Grey's Anatomy?" with gold target "Season 7, Episode 20: White Wedding". Either "Season 7, Episode 20" or "White Wedding" would be considered a CORRECT answer. |
| 86 | +- Do not punish predicted answers if they omit information that would be clearly inferred from the question. |
| 87 | +- For example, consider the question "What city is OpenAI headquartered in?" and the gold target "San Francisco, California". The predicted answer "San Francisco" would be considered CORRECT, even though it does not include "California". |
| 88 | +- Consider the question "What award did A pretrainer's guide to training data: Measuring the effects of data age, domain coverage, quality, & toxicity win at NAACL '24?", the gold target is "Outstanding Paper Award". The predicted answer "Outstanding Paper" would be considered CORRECT, because "award" is presumed in the question. |
| 89 | +- For the question "What is the height of Jason Wei in meters?", the gold target is "1.73 m". The predicted answer "1.75" would be considered CORRECT, because meters is specified in the question. |
| 90 | +- For the question "What is the name of Barack Obama's wife?", the gold target is "Michelle Obama". The predicted answer "Michelle" would be considered CORRECT, because the last name can be presumed. |
| 91 | +- Do not punish for typos in people's name if it's clearly the same name. |
| 92 | +- For example, if the gold target is "Hyung Won Chung", you can consider the following predicted answers as correct: "Hyoong Won Choong", "Hyungwon Chung", or "Hyun Won Chung". |
| 93 | +
|
| 94 | +
|
| 95 | +Here is a new example. Simply reply with either CORRECT, INCORRECT, NOT ATTEMPTED. Don't apologize or correct yourself if there was a mistake; we are just trying to grade the answer. |
| 96 | +``` |
| 97 | +Question: {question} |
| 98 | +Gold target: {target} |
| 99 | +Predicted answer: {predicted_answer} |
| 100 | +``` |
| 101 | +
|
| 102 | +Grade the predicted answer of this new question as one of: |
| 103 | +A: CORRECT |
| 104 | +B: INCORRECT |
| 105 | +C: NOT_ATTEMPTED |
| 106 | +
|
| 107 | +Just return the letters "A", "B", or "C", with no text around it. |
| 108 | +""".strip() |
| 109 | + |
| 110 | + content = GRADER_TEMPLATE.format( |
| 111 | + question=question, |
| 112 | + target=gold, |
| 113 | + predicted_answer=answer, |
| 114 | + ) |
| 115 | + return [{"role": "user", "content": content}] |
| 116 | + |
| 117 | + |
| 118 | +def process_judge_response_simpleqa(response: str) -> float: |
| 119 | + if response == "A": |
| 120 | + return 1.0 |
| 121 | + elif response == "B": |
| 122 | + return 0.0 |
| 123 | + elif response == "C": |
| 124 | + return 0.0 |
| 125 | + else: |
| 126 | + logger.warning(f"Unknown response from judge: {response}") |
| 127 | + return 0.0 |
0 commit comments