-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
521 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from typing import List | ||
|
||
from helm.benchmark.adaptation.request_state import RequestState | ||
from helm.benchmark.scenarios.scenario import Instance | ||
from helm.common.request import Request | ||
from helm.benchmark.adaptation.adapters.in_context_learning_adapter import InContextLearningAdapter | ||
|
||
|
||
class ChatAdapter(InContextLearningAdapter): | ||
""" | ||
Each `Instance` in a `Scenario` has a history of the format: | ||
[ | ||
{"role": "user", "content": <user-content>}, | ||
{"role": "assistant", "content": <assistant-content>}, | ||
{"role": "user", "content": <user-content>}, | ||
... | ||
] | ||
""" | ||
|
||
def generate_requests( | ||
self, eval_instance: Instance, train_trial_index: int, training_instances: List[Instance] | ||
) -> List[RequestState]: | ||
assert eval_instance.extra_data | ||
messages = [ | ||
{"role": message["role"], "content": message["content"]} | ||
for message in eval_instance.extra_data["conversation"] | ||
] | ||
request = Request( | ||
model=self.adapter_spec.model, | ||
model_deployment=self.adapter_spec.model_deployment, | ||
messages=messages, | ||
num_completions=self.adapter_spec.num_outputs, | ||
temperature=self.adapter_spec.temperature, | ||
max_tokens=self.adapter_spec.max_tokens, | ||
stop_sequences=self.adapter_spec.stop_sequences, | ||
random=self.adapter_spec.random, | ||
image_generation_parameters=self.adapter_spec.image_generation_parameters, | ||
) | ||
request_state = RequestState( | ||
instance=eval_instance, | ||
reference_index=None, | ||
request_mode=None, | ||
train_trial_index=train_trial_index, | ||
output_mapping=None, | ||
request=request, | ||
result=None, | ||
num_train_instances=0, | ||
prompt_truncated=False, | ||
) | ||
return [request_state] |
75 changes: 75 additions & 0 deletions
75
src/helm/benchmark/annotation/wildbench/eval_template.pairwise.v2.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Instruction | ||
|
||
You are an expert evaluator. Your task is to evaluate the quality of the responses generated by two AI models. | ||
We will provide you with the user query and a pair of AI-generated responses (Response A and Response B). | ||
You should first read the user query and the conversation history carefully for analyzing the task, and then evaluate the quality of the responses based on and rules provided below. | ||
|
||
# Conversation between User and AI | ||
|
||
## History | ||
<|begin_of_history|> | ||
|
||
{$history} | ||
|
||
<|end_of_history|> | ||
|
||
## Current User Query | ||
<|begin_of_query|> | ||
|
||
{$user_query} | ||
|
||
<|end_of_query|> | ||
|
||
## Response A | ||
<|begin_of_response_A|> | ||
|
||
{$candidate_A} | ||
|
||
<|end_of_response_A|> | ||
|
||
## Response B | ||
<|begin_of_response_B|> | ||
|
||
{$candidate_B} | ||
|
||
<|end_of_response_B|> | ||
|
||
# Evaluation | ||
|
||
## Checklist | ||
|
||
<|begin_of_checklist|> | ||
|
||
{$checklist} | ||
|
||
<|end_of_checklist|> | ||
|
||
Please use this checklist to guide your evaluation, but do not limit your assessment to the checklist. | ||
|
||
## Rules | ||
|
||
You should compare the above two responses based on your analysis of the user queries and the conversation history. | ||
You should first write down your analysis and the checklist that you used for the evaluation, and then provide your assessment according to the checklist. | ||
There are five choices to give your final assessment: ["A++", "A+", "A=B", "B+", "B++"], which correspond to the following meanings: | ||
|
||
- `A++`: Response A is much better than Response B. | ||
- `A+`: Response A is only slightly better than Response B. | ||
- `A=B`: Response A and B are of the same quality. Please use this choice sparingly. | ||
- `B+`: Response B is only slightly better than Response A. | ||
- `B++`: Response B is much better than Response A. | ||
|
||
|
||
## Output Format | ||
First, please output your analysis for each model response, and then summarize your assessment to three aspects: "reason A=B", "reason A>B", and "reason B>A", and finally make your choice for the final assessment. | ||
|
||
Please provide your evaluation results in the following json format by filling in the placeholders in []: | ||
``` | ||
{ | ||
"analysis of A": "[analysis of Response A]", | ||
"analysis of B": "[analysis of Response B]", | ||
"reason of A=B": "[where Response A and B perform equally well]", | ||
"reason of A>B": "[where Response A is better than Response B]", | ||
"reason of B>A": "[where Response B is better than Response A]", | ||
"choice": "[A++ or A+ or A=B or B+ or B++]", | ||
} | ||
``` |
66 changes: 66 additions & 0 deletions
66
src/helm/benchmark/annotation/wildbench/eval_template.score.v2.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Instruction | ||
|
||
You are an expert evaluator. Your task is to evaluate the quality of the responses generated by AI models. | ||
We will provide you with the user query and an AI-generated responses. | ||
You should first read the user query and the conversation history carefully for analyzing the task, and then evaluate the quality of the responses based on and rules provided below. | ||
|
||
# Conversation between User and AI | ||
|
||
## History | ||
<|begin_of_history|> | ||
|
||
{$history} | ||
|
||
<|end_of_history|> | ||
|
||
## Current User Query | ||
<|begin_of_query|> | ||
|
||
{$user_query} | ||
|
||
<|end_of_query|> | ||
|
||
## AI Response | ||
<|begin_of_response|> | ||
|
||
{$model_output} | ||
|
||
<|end_of_response|> | ||
|
||
|
||
# Evaluation | ||
|
||
## Checklist | ||
|
||
<|begin_of_checklist|> | ||
|
||
{$checklist} | ||
|
||
<|end_of_checklist|> | ||
|
||
Please use this checklist to guide your evaluation, but do not limit your assessment to the checklist. | ||
|
||
## Rules | ||
|
||
You should compare the above response based on your analysis of the user queries and the conversation history. | ||
You should first write down your analysis and the checklist that you used for the evaluation, and then provide your assessment according to the checklist. | ||
The scores are in the range of 1~10, where 1 means the response is very poor and 10 means the response is perfect. | ||
Here are more detailed criteria for the scores: | ||
|
||
- Score 1~2: The response is very poor and does not make sense at all. | ||
- Score 3~4: The response is poor and does help user solve the problem in a meaningful way. | ||
- Score 5~6: The response is fair but has some issues (e.g., factual errors, hallucinations, missing key information). | ||
- Score 7~8: The response is good enough but could be improved in some ways. | ||
- Score 9~10: The response is perfect and provides helpful information that can help user solve the problem. | ||
|
||
## Output Format | ||
First, please output your analysis for the model response, and then summarize your assessment to two aspects: "strengths" and "weaknesses"; Finally, please write down your rating for the assessment. | ||
|
||
Please provide your evaluation results in the following json format by filling in the placeholders in []: | ||
``` | ||
{ | ||
"strengths": "[analysis for the strengths of the response]", | ||
"weaknesses": "[analysis for the weaknesses of the response]", | ||
"score": "[1~10]" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import re | ||
from typing import Any | ||
|
||
from helm.benchmark.adaptation.request_state import RequestState | ||
from helm.benchmark.annotation.annotator import Annotator | ||
from helm.clients.auto_client import AutoClient | ||
from helm.common.request import Request | ||
|
||
|
||
class WildBenchAnnotator(Annotator): | ||
"""The WildBench autograder.""" | ||
|
||
name = "wildbench" | ||
|
||
def __init__(self, auto_client: AutoClient): | ||
self._auto_client = auto_client | ||
with open("src/helm/benchmark/annotation/wildbench/eval_template.score.v2.md") as f: | ||
self._score_template = f.read() | ||
self._pattern = re.compile( | ||
r'"strengths"\s*:\s*"(.*?)"\s*,\s*"weaknesses"\s*:\s*"(.*?)"\s*,\s*"score"\s*:\s*(".*?"|\d+)', re.DOTALL | ||
) | ||
|
||
def annotate(self, request_state: RequestState) -> Any: | ||
assert request_state.result | ||
assert len(request_state.result.completions) == 1 | ||
assert request_state.instance.extra_data | ||
model_output_text = request_state.result.completions[0].text | ||
if not model_output_text.strip(): | ||
# Following https://github.com/allenai/WildBench/blob/d6b8dcaf377d173d031980f97c16e1a82618c03d/src/eval.py | ||
return {"prompt_text": "", "strengths": "N/A", "weaknesses": "The model output is empty.", "score": 1.0} | ||
prompt_template = self._score_template | ||
|
||
annotator_prompt = ( | ||
prompt_template.replace("{$history}", request_state.instance.extra_data["history"]) | ||
.replace("{$user_query}", request_state.instance.extra_data["user_query"]) | ||
.replace("{$model_output}", model_output_text) | ||
.replace("{$checklist}", "\n".join(request_state.instance.extra_data["checklist"])) | ||
) | ||
annotator_request = Request( | ||
model="openai/gpt-4o-2024-05-13", | ||
model_deployment="openai/gpt-4o-2024-05-13", | ||
prompt=annotator_prompt, | ||
temperature=0.0, | ||
max_tokens=2000, | ||
) | ||
annotator_response = self._auto_client.make_request(annotator_request) | ||
if not annotator_response.success: | ||
raise Exception(f"Annotation request failed: {annotator_response.error}") | ||
assert len(annotator_response.completions) == 1 | ||
annotator_response_text = annotator_response.completions[0].text | ||
annotator_response_parts = self._pattern.search(annotator_response_text) | ||
if not annotator_response_parts: | ||
raise ValueError(f"Malformed annotator response: {annotator_response_text}") | ||
|
||
strengths = annotator_response_parts[1].strip() | ||
weaknesses = annotator_response_parts[2].strip() | ||
score_text = annotator_response_parts[3].strip().strip('"') | ||
try: | ||
score = float(score_text) | ||
except ValueError: | ||
raise ValueError(f"Malformed score '{score_text}' in annotator response: {annotator_response_text}") | ||
|
||
return {"prompt_text": annotator_prompt, "strengths": strengths, "weaknesses": weaknesses, "score": score} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import List | ||
|
||
from helm.benchmark.adaptation.adapter_spec import AdapterSpec | ||
from helm.benchmark.adaptation.request_state import RequestState | ||
from helm.benchmark.metrics.metric import Metric | ||
from helm.benchmark.metrics.metric_name import MetricName | ||
from helm.benchmark.metrics.metric_service import MetricService | ||
from helm.benchmark.metrics.statistic import Stat | ||
|
||
|
||
class WildBenchScoreMetric(Metric): | ||
"""Score metrics for WildBench.""" | ||
|
||
def evaluate_generation( | ||
self, | ||
adapter_spec: AdapterSpec, | ||
request_state: RequestState, | ||
metric_service: MetricService, | ||
eval_cache_path: str, | ||
) -> List[Stat]: | ||
assert request_state.annotations | ||
score = request_state.annotations["wildbench"]["score"] | ||
return [ | ||
Stat(MetricName("wildbench_score")).add(score), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.