|
| 1 | +import json |
| 2 | +import re |
| 3 | +import os |
| 4 | +import requests |
| 5 | +import numpy as np |
| 6 | +import time |
| 7 | +import yaml |
| 8 | +from pathlib import Path |
| 9 | +from copy import deepcopy |
| 10 | +from io import BytesIO |
| 11 | +import base64 |
| 12 | + |
| 13 | +from loguru import logger as eval_logger |
| 14 | + |
| 15 | +NUM_SECONDS_TO_SLEEP = 5 |
| 16 | + |
| 17 | + |
| 18 | +with open(Path(__file__).parent / "_default_template_yaml", "r") as f: |
| 19 | + raw_data = f.readlines() |
| 20 | + safe_data = [] |
| 21 | + for i, line in enumerate(raw_data): |
| 22 | + # remove function definition since yaml load cannot handle it |
| 23 | + if "!function" not in line: |
| 24 | + safe_data.append(line) |
| 25 | + |
| 26 | + config = yaml.safe_load("".join(safe_data)) |
| 27 | + |
| 28 | +GPT_EVAL_MODEL_NAME = config["metadata"]["judge_model"] |
| 29 | +BASELINE_MODEL_NAME = config["metadata"]["baseline_model"] |
| 30 | + |
| 31 | +API_TYPE = os.getenv("API_TYPE", "openai") |
| 32 | + |
| 33 | +if API_TYPE == "openai": |
| 34 | + API_URL = os.getenv("OPENAI_API_URL", "https://api.openai.com/v1/chat/completions") |
| 35 | + API_KEY = os.getenv("OPENAI_API_KEY", "YOUR_API_KEY") |
| 36 | + headers = { |
| 37 | + "Authorization": f"Bearer {API_KEY}", |
| 38 | + "Content-Type": "application/json", |
| 39 | + } |
| 40 | +elif API_TYPE == "azure": |
| 41 | + API_URL = os.getenv("AZURE_ENDPOINT", "https://api.cognitive.microsoft.com/sts/v1.0/issueToken") |
| 42 | + API_KEY = os.getenv("AZURE_API_KEY", "YOUR_API_KEY") |
| 43 | + headers = { |
| 44 | + "api-key": API_KEY, |
| 45 | + "Content-Type": "application/json", |
| 46 | + } |
| 47 | + |
| 48 | +system_prompt = """\ |
| 49 | +Please act as an impartial judge and evaluate the quality of the responses provided by two AI assistants to the user prompt displayed below. You will be given assistant A's answer and assistant B's answer. Your job is to evaluate which assistant's answer is better. |
| 50 | +
|
| 51 | +Begin your evaluation by generating your own answer to the prompt. You must provide your answers before judging any answers. |
| 52 | +
|
| 53 | +When evaluating the assistants' answers, compare both assistants' answers with your answer. You must identify and correct any mistakes or inaccurate information. |
| 54 | +
|
| 55 | +Then consider if the assistant's answers are helpful, relevant, and concise. Helpful means the answer correctly responds to the prompt or follows the instructions. Note when user prompt has any ambiguity or more than one interpretation, it is more helpful and appropriate to ask for clarifications or more information from the user than providing an answer based on assumptions. Relevant means all parts of the response closely connect or are appropriate to what is being asked. Concise means the response is clear and not verbose or excessive. |
| 56 | +
|
| 57 | +Then consider the creativity and novelty of the assistant's answers when needed. Finally, identify any missing important information in the assistants' answers that would be beneficial to include when responding to the user prompt. |
| 58 | +
|
| 59 | +After providing your explanation, you must output only one of the following choices as your final verdict with a label: |
| 60 | +
|
| 61 | +1. Assistant A is significantly better: [[A>>B]] |
| 62 | +2. Assistant A is slightly better: [[A>B]] |
| 63 | +3. Tie, relatively the same: [[A=B]] |
| 64 | +4. Assistant B is slightly better: [[B>A]] |
| 65 | +5. Assistant B is significantly better: [[B>>A]] |
| 66 | +
|
| 67 | +Example output: "My final verdict is tie: [[A=B]]".\ |
| 68 | +""" |
| 69 | + |
| 70 | +prompt_template = "<|User Prompt|>\n{question_1}\n\n<|The Start of Assistant A's Answer|>\n{answer_1}\n<|The End of Assistant A's Answer|>\n\n<|The Start of Assistant B's Answer|>\n{answer_2}\n<|The End of Assistant B's Answer|>" |
| 71 | + |
| 72 | +def get_chat_response(base64_image, prompt, max_retries=5, wait_time=10): |
| 73 | + headers = { |
| 74 | + "Authorization": f"Bearer {API_KEY}", |
| 75 | + "Content-Type": "application/json", |
| 76 | + } |
| 77 | + |
| 78 | + payload = { |
| 79 | + "model": GPT_EVAL_MODEL_NAME, |
| 80 | + "messages": [ |
| 81 | + {"role": "system", "content": [{"type": "text", "text": system_prompt}]}, |
| 82 | + { |
| 83 | + "role": "user", |
| 84 | + "content": [ |
| 85 | + {"type": "text", "text": prompt}, |
| 86 | + {"type": "image_url", |
| 87 | + "image_url" : { |
| 88 | + "url" : f"data:image/jpeg;base64, {base64_image}" |
| 89 | + } |
| 90 | + }, |
| 91 | + ], |
| 92 | + } |
| 93 | + ], |
| 94 | + "max_tokens": 1024, |
| 95 | + "temperature": 0.0, |
| 96 | + } |
| 97 | + |
| 98 | + for attempt in range(max_retries): |
| 99 | + try: |
| 100 | + response = requests.post(API_URL, headers=headers, json=payload, timeout=60) |
| 101 | + response.raise_for_status() |
| 102 | + response_data = response.json() |
| 103 | + return response_data["choices"][0]["message"]["content"], GPT_EVAL_MODEL_NAME |
| 104 | + except requests.exceptions.RequestException as e: |
| 105 | + print(f"Request failed on attempt {attempt+1}: {e}") |
| 106 | + if attempt == max_retries - 1: |
| 107 | + print(f"Failed to get response after {max_retries} attempts") |
| 108 | + return "", GPT_EVAL_MODEL_NAME |
| 109 | + except Exception as e: |
| 110 | + print(f"Error on attempt {attempt+1}: {e}") |
| 111 | + return "", GPT_EVAL_MODEL_NAME |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +def image_to_base64(pil_image): |
| 116 | + buffered = BytesIO() |
| 117 | + pil_image.save(buffered, format="PNG") |
| 118 | + return base64.b64encode(buffered.getvalue()).decode("utf-8") |
| 119 | + |
| 120 | +def get_score(judgement, pattern, pairwise=True): |
| 121 | + matches = pattern.findall(judgement) |
| 122 | + matches = [m for m in matches if m != ""] |
| 123 | + if len(set(matches)) == 0: |
| 124 | + return None, True |
| 125 | + elif len(set(matches)) == 1: |
| 126 | + if pairwise: |
| 127 | + return matches[0].strip("\n"), False |
| 128 | + return int(matches[0]) |
| 129 | + else: |
| 130 | + return None, False |
| 131 | + |
| 132 | +def wild_vision_doc_to_visual(doc): |
| 133 | + return [doc["image"].convert('RGB')] |
| 134 | + |
| 135 | + |
| 136 | +def wild_vision_doc_to_text(doc, model_specific_prompt_kwargs=None): |
| 137 | + question = doc["instruction"].strip() |
| 138 | + if "pre_prompt" in model_specific_prompt_kwargs and model_specific_prompt_kwargs["pre_prompt"] != "": |
| 139 | + question = f"{model_specific_prompt_kwargs['pre_prompt']}{question}" |
| 140 | + if "post_prompt" in model_specific_prompt_kwargs and model_specific_prompt_kwargs["post_prompt"] != "": |
| 141 | + question = f"{question}{model_specific_prompt_kwargs['post_prompt']}" |
| 142 | + return question |
| 143 | + |
| 144 | +def wild_vision_doc_to_target(doc): |
| 145 | + return doc[BASELINE_MODEL_NAME] |
| 146 | + |
| 147 | + |
| 148 | +def wild_vision_process_results(doc, results): |
| 149 | + pred = results[0] |
| 150 | + user_prompt = prompt_template.format(question_1=doc["instruction"], answer_1=doc[BASELINE_MODEL_NAME], answer_2=pred) |
| 151 | + base64_image = image_to_base64(doc["image"]) |
| 152 | + resps, gpt_name = get_chat_response(base64_image, user_prompt) |
| 153 | + score, _ = get_score(resps, pattern=re.compile("\[\[([AB<>=]+)\]\]")) |
| 154 | + |
| 155 | + if score is None: |
| 156 | + score = resps |
| 157 | + |
| 158 | + if "A>B" in score: |
| 159 | + final_score = -1 |
| 160 | + judgement = "Worse" #Baseline better |
| 161 | + elif "A>>B" in score: |
| 162 | + final_score = -2 |
| 163 | + judgement = "Worse++" |
| 164 | + elif "A=B" in score: |
| 165 | + final_score = 0 |
| 166 | + judgement = "Tie" |
| 167 | + elif "B>A" in score: |
| 168 | + final_score = 1 |
| 169 | + judgement = "Better" |
| 170 | + elif "B>>A" in score: |
| 171 | + final_score = 2 |
| 172 | + judgement = "Better++" |
| 173 | + else: |
| 174 | + final_score = 0 |
| 175 | + judgement = "Unclear" |
| 176 | + |
| 177 | + |
| 178 | + return {"gpt_eval_score" : {"question" : doc["instruction"], "score" : final_score, "gpt_resps" : resps, "ans_1" : doc[BASELINE_MODEL_NAME], "ans_2" : pred, "filtered_resps" : score, "judgement" : judgement}} |
| 179 | + |
| 180 | + |
| 181 | +def wild_vision_aggregation(results): |
| 182 | + score = 0 |
| 183 | + for res in results: |
| 184 | + score += res["score"] |
| 185 | + |
| 186 | + return score / len(results) |
| 187 | + |
| 188 | + |
0 commit comments