File size: 12,256 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
import json
import math
import random
import re
import time
from typing import List
import numpy as np
import requests
from opencompass.models import OpenAI
from .icl_base_evaluator import BaseEvaluator
DEFAULT_FAIL_WORDS = ('sorry', 'apologize', 'apology', 'unfortunately',
"couldn't")
CHECK_SOLVE_QUERY_PROMPT = '''\
Please check whether the answer solve the query or not.
Query:
{query}
Answer:
{answer}
Now give your judgment of JSON to `{func_name}`, remember do not be too strict.
'''
SELECT_BEST_ANSWER_PROMPT = '''\
For query {query}, you have the following answers in JSON format:
{answers}
I want you to select the best answer from the above answers and give the index of the answer of JSON to `{func_name}`. Now select the best answer.''' # noqa: E501
def extract_answer(result: dict):
"""Extract answer from toolbench format."""
final_answer = result['final_answer']
try:
final_answer = json.loads(final_answer)['final_answer']
except Exception:
pass
next_step = result['answer_details']
steps = []
while len(next_step) > 0:
step = next_step[-1]
next_step = step['next']
if step['role'] == 'tool':
tool_type = re.findall(r"'name': '(.*?)'", step['message'])
error = re.findall(r"{\"error\": \"([^\"]+)", step['message'])
if len(tool_type) > 0:
tool_type = tool_type[0]
valid = 0
else:
tool_type = None
valid = -2
if tool_type == 'Finish':
valid = 1
if len(error) > 0:
valid = -2
elif step['role'] == 'assistant':
tool_type = None
valid = -2
else:
continue
steps.append(
dict(
type=tool_type,
args=None,
result=None,
thought=None,
state=0,
valid=valid,
))
return final_answer, steps
class PassRateEvaluator(BaseEvaluator):
"""This Evaluator can determine whether pred refuses to execute the
task."""
def __init__(self, fail_words=DEFAULT_FAIL_WORDS) -> None:
super().__init__()
self.fail_words = fail_words
def score(self, predictions: List, references: List = None) -> dict:
results = []
for pred in predictions:
if pred and self.check_real_valid(pred):
results.append(1)
else:
results.append(0)
pass_rate = sum(results) / len(results) * 100
return dict(pass_rate=pass_rate)
def check_real_valid(self, answer):
"""Exclude response without real answer."""
return not any(word in answer.lower() for word in self.fail_words)
class WinRateEvaluator(BaseEvaluator):
# https://github.com/OpenBMB/ToolBench/blob/e18a30ed8f9afc131a7e313d0522c4371f030f31/toolbench/tooleval/evaluators/registered_cls/tooleval.py#L50
"""Follow `OpenAINormalizedEvaluator` in the `ToolBench`.
The Evaluator will compare which call-tool process between `pred` and
`reference` is better.
1. Compare whether an answer can be extracted. The one that can extract an
answer wins.
2. If both can, then compare whether the answer is correct. The correct one
wins.
3. If both answers are correct, then compare the number of tool calls; the
one with fewer calls wins. If the number of steps is the same, the one
with the better-looking final answer wins.
4. If both answers are incorrect, then consider factors such as whether the
tool was successfully called and the variety of tools used.
"""
def __init__(self,
model='gpt-3.5-turbo-16k',
temperature=0,
**kwargs) -> None:
super().__init__()
self.openai = OpenAI(path=model, temperature=temperature, **kwargs)
def score(self, predictions: List, references: List, origin_prompt: List,
steps: List):
compare_list = []
for query, ref, pred_answer, pred_steps in zip(origin_prompt,
references, predictions,
steps):
ref_answer, ref_steps = extract_answer(ref)
if bool(pred_answer) ^ bool(ref_answer):
# Empty vs non-empty
win = int(bool(pred_answer))
else:
pred_valid = bool(pred_answer) and self.check_solve_query(
query, pred_answer)
ref_valid = bool(ref_answer) and self.check_solve_query(
query, ref_answer)
if pred_valid and ref_valid:
# both answer success
if len(pred_steps) != len(ref_steps):
win = 1 if len(pred_steps) < len(ref_steps) else 0
else:
win = self.select_best_final_answer(
query, [ref_answer, pred_answer])
elif not pred_valid and not ref_valid:
# both answer failed
win = self.compare_steps([ref_steps, pred_steps])
else:
win = int(pred_valid)
compare_list.append(win)
pred_answer = pred_answer.replace('\n', '')
ref_answer = ref_answer.replace('\n', '')
return {'win_rate': sum(compare_list) / len(compare_list) * 100.}
def check_solve_query(self, query: str, answer: str) -> bool:
"""Check whether the answer solved the query."""
func_name = 'check_solve_query'
return_key = 'is_solved'
prompt = CHECK_SOLVE_QUERY_PROMPT.format(query=query,
answer=answer,
func_name=func_name)
function = dict(
name=func_name,
description=('Check whether the given answer solve the given '
'query, return true or false'),
parameters={
'type': 'object',
'properties': {
return_key: {
'type': 'boolean',
'description': 'true if solved and false if not'
}
},
'required': [return_key]
})
result = self._openai_function(
prompt,
max_out_len=100,
functions=[function],
function_call={'name': function['name']},
)
return bool(result[return_key])
def select_best_final_answer(self, query: str, answers: list) -> int:
"""Select the best final answer from candidates."""
func_name = 'select_best_final_answer'
return_key = 'best_answer_index'
is_reversed = random.random() > 0.5
if is_reversed:
answers = list(reversed(answers))
prompt = SELECT_BEST_ANSWER_PROMPT.format(query=query,
answers=answers,
func_name=func_name)
function = dict(
name=func_name,
description=('For given query, select the best answer in answers '
'list and return the index of the best answer'),
parameters={
'type': 'object',
'properties': {
return_key: {
'type':
'number',
'description': ('The index of the best answer in the '
'answer list, start from 0')
}
},
'required': [return_key]
})
result = self._openai_function(
prompt,
max_out_len=100,
functions=[function],
function_call={'name': function['name']},
)
if not is_reversed:
return int(result[return_key])
else:
return len(answers) - int(result[return_key]) - 1
def compare_steps(self, steps_list: list) -> int:
"""Compare results according to score when both answers are failed."""
# calculate socre and return one with highest score
scores = []
for steps in steps_list:
succeed_tool_calling = sum(step['valid'] == 0 for step in steps)
used_tool_types = len(set(step['type'] for step in steps))
score = succeed_tool_calling * 10 + used_tool_types * 5
if len(steps) <= 0:
score -= int(1e5)
else:
score += -5 * math.log(len(steps))
scores.append(score)
# return index of highest score
scores = np.array(scores)
highest_idx = np.where(scores == scores.max())[0].tolist()
return random.choice(highest_idx)
def _openai_function(self, msg: str, max_out_len: int, functions: dict,
function_call: dict, **kwargs) -> dict:
openai = self.openai
messages = [{'role': 'user', 'content': msg}]
max_num_retries = 0
while max_num_retries < openai.retry:
openai.wait()
if len(openai.invalid_keys) == len(openai.keys):
raise RuntimeError('All keys have insufficient quota.')
# find the next valid key
while True:
openai.key_ctr += 1
if openai.key_ctr == len(openai.keys):
openai.key_ctr = 0
if openai.keys[openai.key_ctr] not in openai.invalid_keys:
break
key = openai.keys[openai.key_ctr]
header = {
'Authorization': f'Bearer {key}',
'content-type': 'application/json',
}
if openai.orgs:
openai.org_ctr += 1
if openai.org_ctr == len(openai.orgs):
openai.org_ctr = 0
header['OpenAI-Organization'] = openai.orgs[openai.org_ctr]
try:
data = dict(model=openai.path,
messages=messages,
max_tokens=max_out_len,
n=1,
stop=None,
temperature=openai.temperature,
functions=functions,
function_call=function_call,
**kwargs)
raw_response = requests.post(openai.url,
headers=header,
data=json.dumps(data))
except requests.ConnectionError:
openai.logger.error('Got connection error, retrying...')
continue
try:
response = raw_response.json()
except requests.JSONDecodeError:
openai.logger.error('JsonDecode error, got',
str(raw_response.content))
continue
try:
result = response['choices'][0]['message']['function_call'][
'arguments']
return json.loads(result)
except KeyError:
if 'error' in response:
if response['error']['code'] == 'rate_limit_exceeded':
time.sleep(1)
continue
elif response['error']['code'] == 'insufficient_quota':
openai.invalid_keys.add(key)
openai.logger.warn(f'insufficient_quota key: {key}')
continue
openai.logger.error('Find error message in response: ',
str(response['error']))
max_num_retries += 1
raise RuntimeError('Calling OpenAI failed after retrying for '
f'{max_num_retries} times. Check the logs for '
'details.')
|