-
Notifications
You must be signed in to change notification settings - Fork 3
/
rag_eval.py
186 lines (154 loc) · 6.46 KB
/
rag_eval.py
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
import asyncio
import json
from pathlib import Path
import numpy as np
import typer
import httpx
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from prompt_templates import SCORING_PROMPT_A, SCORING_PROMPT_B
app = typer.Typer()
async def fetch_response(client, llm, query_str, api_url="http://localhost:8001"):
response = await client.post(
f"{api_url}/get_response",
json={"llm": llm, "query_str": query_str},
timeout=600,
)
response_json = response.json()
print(
f"\033[92mResponse from {llm} for question:\033[0m \033[95m'{query_str}'\033[0m\n\033[0m{response_json['response']}\033[0m"
)
return response_json
@app.command()
def generate_responses(
dataset_path: Path = typer.Argument(
Path("rag_questions.jsonl"), help="Evaluation Dataset Path", exists=True
),
output_path: Path = typer.Option(
"rag_with_responses.jsonl", help="Output Dataset Path"
),
api_url: str = typer.Option(
"http://localhost:8001", help="API URL for fetching responses"
),
):
# Read the JSONL file
with open(dataset_path, "r", encoding="utf-8") as file:
questions = [json.loads(line) for line in file]
async def process_questions():
async with httpx.AsyncClient() as client:
tasks = []
for item in questions:
tasks.append(
fetch_response(client, "granite", item["question"], api_url=api_url)
)
tasks.append(
fetch_response(client, "openai", item["question"], api_url=api_url)
)
return await asyncio.gather(*tasks)
# Run the async function
results = asyncio.run(process_questions())
# Process responses and update items
for i, item in enumerate(questions):
item["agent_rag_granite"] = results[i * 2]["response"]
item["agent_rag_openai"] = results[i * 2 + 1]["response"]
# Save results to the output file
with open(output_path, "w", encoding="utf-8") as file:
for item in questions:
json.dump(item, file)
file.write("\n")
print(f"Results saved to {output_path}")
def score(answer: str, model_1="System A"):
if answer not in ["System A", "System B", "EQUAL"]:
return float("NaN")
if answer == model_1:
return 1.0
elif answer == "EQUAL":
return 0.5
else:
return 0.0
@app.command()
def evaluate_responses(
input_file: Path = typer.Argument(
"rag_with_responses.jsonl", help="Input file with RAG responses", exists=True
),
output_file: Path = typer.Option(
"rag_with_responses_and_scores.jsonl", help="Output file with scores"
),
model_0_key: str = typer.Option(
"agent_rag_granite",
help="Key for the responses of the model that is preferred when the mean score is closer to 0 in the input file",
),
model_1_key: str = typer.Option(
"agent_rag_openai",
help="Key for the responses of the model that is preferred when the mean score is closer to 1 in the input file",
),
):
print("\033[92mStarting evaluation of responses.\033[0m")
print(f"\033[92mModel 0: {model_0_key}\033[0m")
print(f"\033[92mModel 1: {model_1_key}\033[0m")
# Read the JSONL file
with open(input_file, "r", encoding="utf-8") as file:
questions = [json.loads(line) for line in file]
# Update dictionary keys to match what is in the scoring prompts
for question in questions:
question["agent_rag_model_0"] = question.pop(model_0_key)
question["agent_rag_model_1"] = question.pop(model_1_key)
# Create the ChatOpenAI model
model = ChatOpenAI(
model="gpt-4",
temperature=0.0,
timeout=1200,
)
# Create the prompt templates
prompt_a = ChatPromptTemplate.from_template(SCORING_PROMPT_A) # model A is granite
prompt_b = ChatPromptTemplate.from_template(SCORING_PROMPT_B) # model B is granite
# Create the chains
chain_a = prompt_a | model
chain_b = prompt_b | model
# Define the async function to run tasks
async def run_tasks():
tasks = [chain_a.abatch(questions), chain_b.abatch(questions)]
return await asyncio.gather(*tasks)
# Run evaluations using asyncio.run
results_a, results_b = asyncio.run(run_tasks())
# Process results
for i, (result_a, result_b) in enumerate(zip(results_a, results_b)):
answer_a = result_a.content.split("<answer>")[-1].split("</answer>")[0].strip()
answer_b = result_b.content.split("<answer>")[-1].split("</answer>")[0].strip()
score_a = score(answer_a, model_1="System B")
score_b = score(answer_b, model_1="System A")
avg_score = (score_a + score_b) / 2
questions[i]["judgement_when_model_0_is_system_A"] = result_a.content
questions[i]["judgement_when_model_0_is_system_B"] = result_b.content
questions[i]["avg_gpt4_based_judgement"] = avg_score
# Save results
with open(output_file, "w", encoding="utf-8") as file:
for item in questions:
json.dump(item, file)
file.write("\n")
scores = np.array([q["avg_gpt4_based_judgement"] for q in questions])
mean_score = np.nanmean(scores) if scores.size else 0
nan_count = np.isnan(scores).sum()
if nan_count > 0:
print(f"\033[91mWarning: There were {nan_count} errors in the scoring.\033[0m")
print("\033[96mgpt4 as a judge scores (average between positions):\033[0m", scores)
print("\033[93mExplanation of scores:\033[0m")
print("\033[93m- Score 1: Both evaluations favored Model 1.\033[0m")
print(
"\033[93m- Score 0.75: One evaluation favored Model 1, the other found both models equal.\033[0m"
)
print("\033[93m- Score 0.5: Evaluations found models equal or disagreed.\033[0m")
print(
"\033[93m- Score 0.25: One evaluation favored Model 0, the other found both models equal.\033[0m"
)
print("\033[93m- Score 0: Both evaluations favored Model 0.\033[0m")
print(
"\033[93mNote: Evaluations are done twice to account for positional bias.\033[0m"
)
print("\033[96mMean of gpt4 judge scores:\033[0m", mean_score)
print("\033[93m- Mean score closer to 0: Preference for Model 0.\033[0m")
print("\033[93m- Mean score closer to 1: Preference for Model 1.\033[0m")
print(f"\033[92mJudgements and scores written to {output_file}.\033[0m")
return scores
if __name__ == "__main__":
app()