forked from huggingface/lighteval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgerman_rag_evals.py
232 lines (194 loc) · 6.71 KB
/
german_rag_evals.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
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
# MIT License
# Copyright (c) 2024 The HuggingFace Team
# Copyright (c) 2024 Philip May, Deutsche Telekom AG
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ruff: noqa: F405, F403, F401
"""
Custom evaluation tasks for lighteval.
This file generally creates just a TASKS_TABLE and TASKS_GROUPS which are then imported by LightEval.
This module implements the 4 tasks of deutsche-telekom/Ger-RAG-eval.
See: https://huggingface.co/datasets/deutsche-telekom/Ger-RAG-eval
"""
from lighteval.tasks.lighteval_task import LightevalTaskConfig
from lighteval.tasks.requests import Doc
# Task 1: Choose question by context.
# Given is a context and 4 questions.
# The task is to decide which question can be answered by the context.
task1 = LightevalTaskConfig(
name="german_rag_eval:choose_question_by_context",
prompt_function="prompt_fn_choose_question_by_context",
suite=["community"],
hf_repo="deutsche-telekom/Ger-RAG-eval",
hf_subset="task1",
hf_avail_splits=["test"],
evaluation_splits=["test"],
few_shots_split="test",
few_shots_select="sequential",
metric=["loglikelihood_acc"],
version=1,
)
# Task 2: Choose context by question.
# Given is a question and 4 contexts.
# The task is to decide which context can answer the question.
task2 = LightevalTaskConfig(
name="german_rag_eval:choose_context_by_question",
prompt_function="prompt_fn_choose_context_by_question",
suite=["community"],
hf_repo="deutsche-telekom/Ger-RAG-eval",
hf_subset="task2",
hf_avail_splits=["test"],
evaluation_splits=["test"],
few_shots_split="test",
few_shots_select="sequential",
metric=["loglikelihood_acc"],
version=1,
)
# Task 3: Question-answer match.
# Given is a question and an answer.
# The task is to decide whether the answer actualy answers the question.
task3 = LightevalTaskConfig(
name="german_rag_eval:question_answer_match",
prompt_function="prompt_fn_question_answer_match",
suite=["community"],
hf_repo="deutsche-telekom/Ger-RAG-eval",
hf_subset="task3",
hf_avail_splits=["test"],
evaluation_splits=["test"],
few_shots_split="test",
few_shots_select="sequential",
metric=["loglikelihood_acc"],
version=1,
)
# Task 4: Context-question match.
# Given is a context and a question.
# The task is to decide whether the question can be answered by the context or not.
task4 = LightevalTaskConfig(
name="german_rag_eval:context_question_match",
prompt_function="prompt_fn_context_question_match",
suite=["community"],
hf_repo="deutsche-telekom/Ger-RAG-eval",
hf_subset="task4",
hf_avail_splits=["test"],
evaluation_splits=["test"],
few_shots_split="test",
few_shots_select="sequential",
metric=["loglikelihood_acc"],
version=1,
)
def prompt_fn_choose_question_by_context(line, task_name: str = None):
instruction = "Welche der folgenden Fragen (A oder B oder C oder D) lässt sich anhand des Kontext beantworten?\n\n"
query_template = """\
Kontext:
{context}
Fragen:
A: {choice_a}
B: {choice_b}
C: {choice_c}
D: {choice_d}
Antwort:"""
query = instruction + query_template.format(
context=line["context"],
choice_a=line["choice_a"],
choice_b=line["choice_b"],
choice_c=line["choice_c"],
choice_d=line["choice_d"],
)
choices = ["A", "B", "C", "D"]
return Doc(
task_name=task_name,
instruction=instruction,
query=query,
choices=choices,
gold_index=choices.index(line["target"]),
)
def prompt_fn_choose_context_by_question(line, task_name: str = None):
instruction = (
"Auf Basis welcher der folgenden Kontexte (A oder B oder C oder D) lässt sich die Frage beantworten?\n\n"
)
query_template = """\
Frage: {question}
Kontexte:
A:
{choice_a}
B:
{choice_b}
C:
{choice_c}
D:
{choice_d}
Antwort:"""
query = instruction + query_template.format(
question=line["question"],
choice_a=line["choice_a"],
choice_b=line["choice_b"],
choice_c=line["choice_c"],
choice_d=line["choice_d"],
)
choices = ["A", "B", "C", "D"]
return Doc(
task_name=task_name,
instruction=instruction,
query=query,
choices=choices,
gold_index=choices.index(line["target"]),
)
def prompt_fn_question_answer_match(line, task_name: str = None):
instruction = "Beantwortet die Antwort wirklich die Frage? Antworte mit J für ja oder N für nein.\n\n"
query_template = """\
Die Frage: {question}
Die Antwort: {answer}
Auswahl (J/N):"""
query = instruction + query_template.format(
question=line["question"],
answer=line["answer"],
)
choices = ["J", "N"]
return Doc(
task_name=task_name,
instruction=instruction,
query=query,
choices=choices,
gold_index=choices.index(line["target"]),
)
def prompt_fn_context_question_match(line, task_name: str = None):
instruction = "Lässt sich die Frage mithilfe der Informationen aus dem Kontext beantworten? Antworte mit J für ja oder N für nein.\n\n"
query_template = """\
Kontext:
{context}
Die Frage: {question}
Auswahl (J/N):"""
query = instruction + query_template.format(
question=line["question"],
context=line["context"],
)
choices = ["J", "N"]
return Doc(
task_name=task_name,
instruction=instruction,
query=query,
choices=choices,
gold_index=choices.index(line["target"]),
)
# STORE YOUR EVALS
_TASKS = [task1, task2, task3, task4]
# MODULE LOGIC
# You should not need to touch this
# Convert to dict for lighteval
TASKS_TABLE = [task.as_dict() for task in _TASKS]
if __name__ == "__main__":
print(t["name"] for t in TASKS_TABLE)
print(len(TASKS_TABLE))