judgeval 0.0.3__py3-none-any.whl → 0.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. judgeval/__init__.py +0 -71
  2. judgeval/common/tracer.py +57 -31
  3. judgeval/constants.py +1 -0
  4. judgeval/data/__init__.py +2 -1
  5. judgeval/data/scorer_data.py +2 -2
  6. judgeval/evaluation_run.py +16 -15
  7. judgeval/judges/__init__.py +2 -2
  8. judgeval/judges/base_judge.py +1 -1
  9. judgeval/judges/litellm_judge.py +2 -2
  10. judgeval/judges/mixture_of_judges.py +2 -2
  11. judgeval/judges/together_judge.py +2 -2
  12. judgeval/judges/utils.py +4 -4
  13. judgeval/judgment_client.py +67 -15
  14. judgeval/run_evaluation.py +79 -14
  15. judgeval/scorers/__init__.py +8 -4
  16. judgeval/scorers/api_scorer.py +64 -0
  17. judgeval/scorers/base_scorer.py +3 -2
  18. judgeval/scorers/exceptions.py +11 -0
  19. judgeval/scorers/{custom_scorer.py → judgeval_scorer.py} +9 -5
  20. judgeval/scorers/judgeval_scorers/__init__.py +132 -9
  21. judgeval/scorers/judgeval_scorers/api_scorers/__init__.py +23 -0
  22. judgeval/scorers/judgeval_scorers/api_scorers/answer_correctness.py +19 -0
  23. judgeval/scorers/judgeval_scorers/{answer_relevancy.py → api_scorers/answer_relevancy.py} +2 -2
  24. judgeval/scorers/judgeval_scorers/{contextual_precision.py → api_scorers/contextual_precision.py} +2 -2
  25. judgeval/scorers/judgeval_scorers/{contextual_recall.py → api_scorers/contextual_recall.py} +2 -2
  26. judgeval/scorers/judgeval_scorers/{contextual_relevancy.py → api_scorers/contextual_relevancy.py} +2 -2
  27. judgeval/scorers/judgeval_scorers/{faithfulness.py → api_scorers/faithfulness.py} +2 -2
  28. judgeval/scorers/judgeval_scorers/{hallucination.py → api_scorers/hallucination.py} +2 -2
  29. judgeval/scorers/judgeval_scorers/{json_correctness.py → api_scorers/json_correctness.py} +7 -7
  30. judgeval/scorers/judgeval_scorers/{summarization.py → api_scorers/summarization.py} +2 -2
  31. judgeval/scorers/judgeval_scorers/{tool_correctness.py → api_scorers/tool_correctness.py} +2 -2
  32. judgeval/scorers/judgeval_scorers/local_implementations/__init__.py +24 -0
  33. judgeval/scorers/judgeval_scorers/local_implementations/answer_correctness/__init__.py +4 -0
  34. judgeval/scorers/judgeval_scorers/local_implementations/answer_correctness/answer_correctness_scorer.py +272 -0
  35. judgeval/scorers/judgeval_scorers/local_implementations/answer_correctness/prompts.py +169 -0
  36. judgeval/scorers/judgeval_scorers/local_implementations/answer_relevancy/__init__.py +4 -0
  37. judgeval/scorers/judgeval_scorers/local_implementations/answer_relevancy/answer_relevancy_scorer.py +292 -0
  38. judgeval/scorers/judgeval_scorers/local_implementations/answer_relevancy/prompts.py +174 -0
  39. judgeval/scorers/judgeval_scorers/local_implementations/contextual_precision/__init__.py +3 -0
  40. judgeval/scorers/judgeval_scorers/local_implementations/contextual_precision/contextual_precision_scorer.py +259 -0
  41. judgeval/scorers/judgeval_scorers/local_implementations/contextual_precision/prompts.py +106 -0
  42. judgeval/scorers/judgeval_scorers/local_implementations/contextual_recall/__init__.py +3 -0
  43. judgeval/scorers/judgeval_scorers/local_implementations/contextual_recall/contextual_recall_scorer.py +249 -0
  44. judgeval/scorers/judgeval_scorers/local_implementations/contextual_recall/prompts.py +142 -0
  45. judgeval/scorers/judgeval_scorers/local_implementations/contextual_relevancy/__init__.py +3 -0
  46. judgeval/scorers/judgeval_scorers/local_implementations/contextual_relevancy/contextual_relevancy_scorer.py +240 -0
  47. judgeval/scorers/judgeval_scorers/local_implementations/contextual_relevancy/prompts.py +121 -0
  48. judgeval/scorers/judgeval_scorers/local_implementations/faithfulness/__init__.py +3 -0
  49. judgeval/scorers/judgeval_scorers/local_implementations/faithfulness/faithfulness_scorer.py +318 -0
  50. judgeval/scorers/judgeval_scorers/local_implementations/faithfulness/prompts.py +265 -0
  51. judgeval/scorers/judgeval_scorers/local_implementations/hallucination/__init__.py +3 -0
  52. judgeval/scorers/judgeval_scorers/local_implementations/hallucination/hallucination_scorer.py +258 -0
  53. judgeval/scorers/judgeval_scorers/local_implementations/hallucination/prompts.py +104 -0
  54. judgeval/scorers/judgeval_scorers/local_implementations/json_correctness/json_correctness_scorer.py +127 -0
  55. judgeval/scorers/judgeval_scorers/local_implementations/summarization/__init__.py +3 -0
  56. judgeval/scorers/judgeval_scorers/local_implementations/summarization/prompts.py +247 -0
  57. judgeval/scorers/judgeval_scorers/local_implementations/summarization/summarization_scorer.py +541 -0
  58. judgeval/scorers/judgeval_scorers/local_implementations/tool_correctness/__init__.py +3 -0
  59. judgeval/scorers/judgeval_scorers/local_implementations/tool_correctness/tool_correctness_scorer.py +151 -0
  60. judgeval/scorers/prompt_scorer.py +4 -4
  61. judgeval/scorers/score.py +14 -14
  62. judgeval/scorers/utils.py +40 -6
  63. {judgeval-0.0.3.dist-info → judgeval-0.0.4.dist-info}/METADATA +1 -1
  64. judgeval-0.0.4.dist-info/RECORD +78 -0
  65. judgeval-0.0.3.dist-info/RECORD +0 -46
  66. {judgeval-0.0.3.dist-info → judgeval-0.0.4.dist-info}/WHEEL +0 -0
  67. {judgeval-0.0.3.dist-info → judgeval-0.0.4.dist-info}/licenses/LICENSE.md +0 -0
@@ -0,0 +1,318 @@
1
+ """
2
+ Code for the local implementation of the Faithfulness metric.
3
+ """
4
+ from typing import List, Optional, Union
5
+
6
+
7
+ from judgeval.data import (
8
+ Example,
9
+ ExampleParams
10
+ )
11
+ from judgeval.scorers import JudgevalScorer
12
+ from judgeval.scorers.utils import (
13
+ get_or_create_event_loop,
14
+ check_example_params
15
+ )
16
+ from judgeval.judges.utils import create_judge
17
+ from judgeval.judges import JudgevalJudge
18
+ from judgeval.scorers.utils import (
19
+ scorer_progress_meter,
20
+ create_verbose_logs,
21
+ parse_response_json
22
+ )
23
+ from judgeval.scorers.judgeval_scorers.local_implementations.faithfulness.prompts import (
24
+ FaithfulnessTemplate,
25
+ FaithfulnessVerdict,
26
+ Verdicts,
27
+ Reason,
28
+ Claims,
29
+ )
30
+
31
+
32
+ required_params = [
33
+ ExampleParams.INPUT,
34
+ ExampleParams.ACTUAL_OUTPUT,
35
+ ExampleParams.RETRIEVAL_CONTEXT,
36
+ ]
37
+
38
+
39
+ class FaithfulnessScorer(JudgevalScorer):
40
+ def __init__(
41
+ self,
42
+ threshold: float = 0.5,
43
+ model: Optional[Union[str, JudgevalJudge]] = None,
44
+ include_reason: bool = True,
45
+ async_mode: bool = True,
46
+ strict_mode: bool = False,
47
+ verbose_mode: bool = False,
48
+ user: Optional[str] = None
49
+ ):
50
+ self.threshold = 1 if strict_mode else threshold
51
+ self.model, self.using_native_model = create_judge(model)
52
+ self.using_native_model = True # NOTE: SETTING THIS FOR LITELLM and TOGETHER usage
53
+ self.evaluation_model = self.model.get_model_name()
54
+ self.include_reason = include_reason
55
+ self.async_mode = async_mode
56
+ self.strict_mode = strict_mode
57
+ self.verbose_mode = verbose_mode
58
+
59
+ def score_example(
60
+ self,
61
+ example: Example,
62
+ all_claims: bool = False,
63
+ _show_indicator: bool = True,
64
+ ) -> float:
65
+ check_example_params(example, required_params, self)
66
+
67
+ with scorer_progress_meter(self, display_meter=_show_indicator):
68
+ if self.async_mode:
69
+ loop = get_or_create_event_loop()
70
+ loop.run_until_complete(
71
+ self.a_score_example(
72
+ example,
73
+ all_claims=all_claims,
74
+ _show_indicator=False
75
+ )
76
+ )
77
+ else:
78
+ self.claims = self._generate_claims(example.actual_output, all_claims=all_claims)
79
+ if self.additional_metadata is None:
80
+ self.additional_metadata = {}
81
+ self.additional_metadata["claims"] = self.claims # Add claims generated to metadata
82
+
83
+ self.verdicts = self._generate_verdicts(example.retrieval_context)
84
+ self.additional_metadata["verdicts"] = [v.model_dump() for v in self.verdicts] # Add verdicts generated to metadata
85
+
86
+ self.score = self._calculate_score()
87
+ self.reason = self._generate_reason()
88
+ self.success = self.score >= self.threshold
89
+ self.verbose_logs = create_verbose_logs(
90
+ self,
91
+ steps=[
92
+ f"Claims:\n{self.claims}",
93
+ f"Verdicts:\n{self.verdicts}",
94
+ f"Score: {self.score}\nReason: {self.reason}",
95
+ ],
96
+ )
97
+
98
+ return self.score
99
+
100
+ async def a_score_example(
101
+ self,
102
+ example: Example,
103
+ _show_indicator: bool = True
104
+ ) -> float:
105
+ check_example_params(example, required_params, self)
106
+
107
+ with scorer_progress_meter(
108
+ self, async_mode=True, display_meter=_show_indicator
109
+ ):
110
+ self.claims = await self._a_generate_claims(example.actual_output)
111
+
112
+ if self.additional_metadata is None:
113
+ self.additional_metadata = {}
114
+ self.additional_metadata["claims"] = self.claims
115
+
116
+ self.verdicts = await self._a_generate_verdicts(example.retrieval_context)
117
+ self.additional_metadata["verdicts"] = [v.model_dump() for v in self.verdicts] # Add verdicts generated to metadata
118
+
119
+ self.score = self._calculate_score()
120
+ self.reason = await self._a_generate_reason()
121
+ self.success = self.score >= self.threshold
122
+ self.verbose_logs = create_verbose_logs(
123
+ self,
124
+ steps=[
125
+ f"Claims:\n{self.claims}",
126
+ f"Verdicts:\n{self.verdicts}",
127
+ f"Score: {self.score}\nReason: {self.reason}",
128
+ ],
129
+ )
130
+
131
+ return self.score
132
+
133
+ async def _a_generate_reason(self) -> str:
134
+ if self.include_reason is False:
135
+ return None
136
+
137
+ contradictions = []
138
+ for verdict in self.verdicts:
139
+ if verdict.verdict.strip().lower() == "no":
140
+ contradictions.append(verdict.model_dump())
141
+
142
+ prompt: dict = FaithfulnessTemplate.justify_reason(
143
+ contradictions=contradictions,
144
+ score=format(self.score, ".2f"),
145
+ )
146
+ if self.using_native_model:
147
+ res = await self.model.a_generate(prompt)
148
+ data = parse_response_json(res, self)
149
+ return data["reason"]
150
+ else:
151
+ try:
152
+ res: Reason = await self.model.a_generate(prompt, schema=Reason)
153
+ return res.reason
154
+ except TypeError:
155
+ res = await self.model.a_generate(prompt)
156
+ data = parse_response_json(res, self)
157
+ return data["reason"]
158
+
159
+ def _generate_reason(self) -> str:
160
+ if self.include_reason is False:
161
+ return None
162
+
163
+ contradictions = []
164
+ for verdict in self.verdicts:
165
+ if verdict.verdict.strip().lower() == "no":
166
+ contradictions.append(verdict.reason)
167
+
168
+ prompt: dict = FaithfulnessTemplate.justify_reason(
169
+ contradictions=contradictions,
170
+ score=format(self.score, ".2f"),
171
+ )
172
+
173
+ if self.using_native_model:
174
+ res = self.model.generate(prompt)
175
+ data = parse_response_json(res, self)
176
+ return data["reason"]
177
+ else:
178
+ try:
179
+ res: Reason = self.model.generate(prompt, schema=Reason)
180
+ return res.reason
181
+ except TypeError:
182
+ res = self.model.generate(prompt)
183
+ data = parse_response_json(res, self)
184
+ return data["reason"]
185
+
186
+ async def _a_generate_verdicts(self, retrieval_context: str) -> List[FaithfulnessVerdict]:
187
+ if len(self.claims) == 0:
188
+ return []
189
+
190
+ verdicts: List[FaithfulnessVerdict] = []
191
+
192
+ claims = [
193
+ claim["claim"] for claim in self.claims
194
+ ] # We only need the claims, not the quotes involved
195
+
196
+ prompt = FaithfulnessTemplate.create_verdicts(
197
+ claims=claims,
198
+ retrieval_context=retrieval_context,
199
+ )
200
+ if self.using_native_model:
201
+ res = await self.model.a_generate(prompt)
202
+ data = parse_response_json(res, self)
203
+ verdicts = [
204
+ FaithfulnessVerdict(**item) for item in data["verdicts"]
205
+ ]
206
+ return verdicts
207
+ else:
208
+ try:
209
+ res: Verdicts = await self.model.generate(
210
+ prompt, schema=Verdicts
211
+ )
212
+ verdicts = [item for item in res.verdicts]
213
+ return verdicts
214
+ except TypeError:
215
+ res = await self.model.a_generate(prompt)
216
+ data = parse_response_json(res, self)
217
+ verdicts = [
218
+ FaithfulnessVerdict(**item) for item in data["verdicts"]
219
+ ]
220
+ return verdicts
221
+
222
+ def _generate_verdicts(self, retrieval_context: str) -> List[FaithfulnessVerdict]:
223
+ if len(self.claims) == 0:
224
+ return []
225
+
226
+ verdicts: List[FaithfulnessVerdict] = []
227
+
228
+ claims = [
229
+ claim["claim"] for claim in self.claims
230
+ ] # We only need the claims, not the quotes involved
231
+
232
+ prompt = FaithfulnessTemplate.create_verdicts(
233
+ claims=claims,
234
+ retrieval_context=retrieval_context,
235
+ )
236
+ if self.using_native_model:
237
+ res = self.model.generate(prompt)
238
+ data = parse_response_json(res, self)
239
+ verdicts = [
240
+ FaithfulnessVerdict(**item) for item in data["verdicts"]
241
+ ]
242
+ return verdicts
243
+ else:
244
+ try:
245
+ res: Verdicts = self.model.generate(prompt, schema=Verdicts)
246
+ verdicts = [item for item in res.verdicts]
247
+ return verdicts
248
+ except TypeError:
249
+ res = self.model.generate(prompt)
250
+ data = parse_response_json(res, self)
251
+ verdicts = [
252
+ FaithfulnessVerdict(**item) for item in data["verdicts"]
253
+ ]
254
+ return verdicts
255
+
256
+ async def _a_generate_claims(self, actual_output: str) -> List[str]:
257
+ prompt = FaithfulnessTemplate.find_claims(text=actual_output)
258
+ if self.using_native_model:
259
+ res = await self.model.a_generate(prompt)
260
+ data = parse_response_json(res, self)
261
+ return data["claims"]
262
+ else:
263
+ try:
264
+ res: Claims = await self.model.a_generate(prompt, schema=Claims)
265
+ return res.claims
266
+ except TypeError:
267
+ res = await self.model.a_generate(prompt)
268
+ data = parse_response_json(res, self)
269
+ return data["claims"]
270
+
271
+ def _generate_claims(self, actual_output: str, all_claims: bool = False) -> List[str]:
272
+ prompt = FaithfulnessTemplate.find_claims(text=actual_output)
273
+ if self.using_native_model:
274
+ res = self.model.generate(prompt)
275
+ data = parse_response_json(res, self)
276
+ return data["claims"]
277
+ else:
278
+ try:
279
+ res: Claims = self.model.generate(prompt, schema=Claims)
280
+ return res.claims
281
+ except TypeError:
282
+ res = self.model.generate(prompt)
283
+ data = parse_response_json(res, self)
284
+ return data["claims"]
285
+
286
+ def _calculate_score(self) -> float:
287
+ number_of_verdicts = len(self.verdicts)
288
+ if number_of_verdicts == 0:
289
+ return 1
290
+
291
+ faithfulness_count = 0
292
+ for verdict in self.verdicts:
293
+ if verdict.verdict.strip().lower() != "no":
294
+ faithfulness_count += 1
295
+
296
+ score = faithfulness_count / number_of_verdicts
297
+ return 0 if self.strict_mode and score < self.threshold else score
298
+
299
+ def _success_check(self) -> bool:
300
+ if self.error is not None:
301
+ self.success = False
302
+ else:
303
+ try:
304
+ self.success = self.score >= self.threshold
305
+ except:
306
+ self.success = False
307
+ return self.success
308
+
309
+ def get_claims(self):
310
+ return self.claims
311
+
312
+ def get_verdicts(self):
313
+ return self.verdicts
314
+
315
+ @property
316
+ def __name__(self):
317
+ return "Faithfulness"
318
+
@@ -0,0 +1,265 @@
1
+ from typing import List, Optional
2
+ from pydantic import BaseModel, Field
3
+
4
+
5
+ class FaithfulnessVerdict(BaseModel):
6
+ verdict: str
7
+ reason: Optional[str] = Field(default=None)
8
+
9
+
10
+ class Verdicts(BaseModel):
11
+ verdicts: List[FaithfulnessVerdict]
12
+
13
+
14
+ class Truths(BaseModel):
15
+ truths: List[str]
16
+
17
+
18
+ class Claims(BaseModel):
19
+ claims: List[str]
20
+
21
+
22
+ class Reason(BaseModel):
23
+ reason: str
24
+
25
+
26
+ class FaithfulnessTemplate:
27
+ @staticmethod
28
+ def find_claims(text):
29
+ return f"""==== TASK INSTRUCTIONS ====
30
+ You will be provided with a passage of text. Based on the text, your task is to generate a comprehensive list of ALL CLAIMS that can be inferred from the text.
31
+ For every claim that you derive from the text, provide the source of the claim via quoting the original text. Please try to extract EVERY CLAIM that is in the original text; priortize generating the most claims rather than being concise.
32
+ You should NOT include any prior knowledge, and take the text at face value when extracting claims.
33
+
34
+ ==== FORMATTING YOUR ANSWER ====
35
+ Please return your answer in JSON format, with the "claims" key as a list of JSON objects with keys "claim" and "quote". No words or explanation beyond the output JSON is needed.
36
+
37
+ ==== EXAMPLES ====
38
+
39
+ ---- START OF EXAMPLE 1 ----
40
+ Example Text:
41
+ "Einstein won the nobel prize in 1968 for his discovery of the photoelectric effect."
42
+
43
+ Example JSON:
44
+ {{
45
+ "claims": [
46
+ {{
47
+ "claim": "Einstein won the nobel prize for his discovery of the photoelectric effect.",
48
+ "quote": "Einstein won the nobel prize in 1968 for his discovery of the photoelectric effect."
49
+ }},
50
+ {{
51
+ "claim": "Einstein won the nobel prize in 1968.",
52
+ "quote": "Einstein won the nobel prize in 1968 for his discovery of the photoelectric effect."
53
+ }}
54
+ ]
55
+ }}
56
+ ---- END OF EXAMPLE 1 ----
57
+
58
+ ---- START OF EXAMPLE 2 ----
59
+ Example Text: "The Wright brothers successfully flew the first powered airplane on December 17, 1903, in Kitty Hawk, North Carolina."
60
+
61
+ {{
62
+ "claims": [
63
+ {{
64
+ "claim": "The Wright brothers flew the first powered airplane.",
65
+ "quote": "The Wright brothers successfully flew the first powered airplane on December 17, 1903, in Kitty Hawk, North Carolina."
66
+ }},
67
+ {{
68
+ "claim": "The Wright brothers made their flight in Kitty Hawk, North Carolina.",
69
+ "quote": "The Wright brothers successfully flew the first powered airplane on December 17, 1903, in Kitty Hawk, North Carolina."
70
+ }},
71
+ {{
72
+ "claim": "The first powered airplane flight occurred on December 17, 1903.",
73
+ "quote": "The Wright brothers successfully flew the first powered airplane on December 17, 1903, in Kitty Hawk, North Carolina."
74
+ }}
75
+ ]
76
+ }}
77
+ ---- END OF EXAMPLE 2 ----
78
+
79
+ ---- START OF EXAMPLE 3 ----
80
+ Example Text:
81
+ "The Great Wall of China was built over many centuries by different Chinese dynasties. Construction began more than 2,000 years ago during the Warring States period. The most famous sections were built during the Ming Dynasty. The wall stretches for thousands of miles across northern China and was primarily built for military defense."
82
+
83
+ Example JSON:
84
+ {{
85
+ "claims": [
86
+ {{
87
+ "claim": "The Great Wall of China was built by multiple Chinese dynasties",
88
+ "quote": "The Great Wall of China was built over many centuries by different Chinese dynasties."
89
+ }},
90
+ {{
91
+ "claim": "Construction of the Great Wall began over 2,000 years ago",
92
+ "quote": "Construction began more than 2,000 years ago during the Warring States period."
93
+ }},
94
+ {{
95
+ "claim": "Construction started during the Warring States period",
96
+ "quote": "Construction began more than 2,000 years ago during the Warring States period."
97
+ }},
98
+ {{
99
+ "claim": "The most well-known parts of the wall were constructed during the Ming Dynasty",
100
+ "quote": "The most famous sections were built during the Ming Dynasty."
101
+ }},
102
+ {{
103
+ "claim": "The Great Wall extends for thousands of miles",
104
+ "quote": "The wall stretches for thousands of miles across northern China"
105
+ }},
106
+ {{
107
+ "claim": "The wall is located in northern China",
108
+ "quote": "The wall stretches for thousands of miles across northern China"
109
+ }},
110
+ {{
111
+ "claim": "The Great Wall was constructed for defensive military purposes",
112
+ "quote": "was primarily built for military defense."
113
+ }}
114
+ ]
115
+ }}
116
+ ---- END OF EXAMPLE 3 ----
117
+
118
+ ==== END OF EXAMPLES ====
119
+
120
+ ==== YOUR TURN ====
121
+
122
+ Example Text:
123
+ {text}
124
+
125
+ JSON:
126
+ """
127
+
128
+ @staticmethod
129
+ def create_verdicts(claims, retrieval_context):
130
+ return f"""==== TASK INSTRUCTIONS ====
131
+ You will be provided with a list of claims from an LLM's output text, accompanied by the retrieval documents that the LLM used to generate the output.
132
+ Your task is to determine whether EACH claim is factually consistent with the retrieval context ("yes", "no", or "idk").
133
+ ONLY choose 'no' if the retrieval context DIRECTLY CONTRADICTS the claims. YOU SHOULD NEVER USE YOUR PRIOR KNOWLEDGE IN YOUR JUDGMENT.
134
+ Claims made using vague, suggestive, or speculative language such as 'may have', 'possibility due to', do NOT count as a contradiction.
135
+ Claims that are not justified by the retrieval context due to a lack of information MUST BE ANSWERED with 'idk'.
136
+
137
+ ==== FORMATTING YOUR ANSWER ====
138
+ Please return your answer in JSON format, with the 'verdicts' key as a list of JSON objects. Each JSON object should have 2 fields: 'verdict' and 'reason'.
139
+ The 'verdict' key should be either 'yes', 'no', or 'idk', which states WHETHER THE CLAIM AGREES with the context.
140
+ The 'reason' key should be a string explaining why the claim is 'yes', 'no', or 'idk'. Make specific reference to the retrieval context to justify your verdict.
141
+
142
+ ==== EXAMPLES ====
143
+ ---- START OF EXAMPLE 1 ----
144
+ Example retrieval contexts: "Einstein won the Nobel Prize for his discovery of the photoelectric effect. Einstein won the Nobel Prize in 1968. Einstein is a German Scientist."
145
+ Example claims: ["Barack Obama is a caucasian male.", "Zurich is a city in London", "Einstein won the Nobel Prize for the discovery of the photoelectric effect which may have contributed to his fame.", "Einstein won the Nobel Prize in 1969 for his discovery of the photoelectric effect.", "Einstein was a Germen chef."]
146
+
147
+ Example JSON:
148
+ {{
149
+ "verdicts": [
150
+ {{
151
+ "verdict": "idk",
152
+ "reason": "The claim about Barack Obama's ethnicity cannot be verified from the given retrieval context as it contains no information about Barack Obama."
153
+ }},
154
+ {{
155
+ "verdict": "idk",
156
+ "reason": "The claim about Zurich being a city in London cannot be verified from the given retrieval context as it contains no information about Zurich or London."
157
+ }},
158
+ {{
159
+ "verdict": "yes",
160
+ "reason": "The retrieval context confirms that Einstein won the Nobel Prize for discovering the photoelectric effect."
161
+ }},
162
+ {{
163
+ "verdict": "no",
164
+ "reason": "The actual output claims Einstein won the Nobel Prize in 1969, which is untrue as the retrieval context states it is 1968 instead."
165
+ }},
166
+ {{
167
+ "verdict": "no",
168
+ "reason": "The actual output claims Einstein is a Germen chef, which is not correct as the retrieval context states he was a German scientist instead."
169
+ }}
170
+ ]
171
+ }}
172
+ ---- END OF EXAMPLE 1 ----
173
+ ---- START OF EXAMPLE 2 ----
174
+ Example retrieval contexts: "The Great Wall of China was built over many centuries by different Chinese dynasties. Construction began more than 2,000 years ago. The wall stretches for thousands of miles across China's northern borders. Most of the existing wall was built during the Ming Dynasty."
175
+ Example claims: ["The Great Wall was built in a single year.", "The Great Wall may have taken centuries to complete.", "The Great Wall was built by the Romans.", "The Great Wall is located in China's northern region.", "The Great Wall is 100 meters long."]
176
+
177
+ Example JSON:
178
+ {{
179
+ "verdicts": [
180
+ {{
181
+ "verdict": "no",
182
+ "reason": "The claim that the Great Wall was built in a single year directly contradicts the retrieval context, which states it was built over many centuries."
183
+ }},
184
+ {{
185
+ "verdict": "yes",
186
+ "reason": "The retrieval context confirms that the Great Wall was built over many centuries by different Chinese dynasties."
187
+ }},
188
+ {{
189
+ "verdict": "no",
190
+ "reason": "The claim states the Romans built the wall, which contradicts the retrieval context that specifies it was built by Chinese dynasties."
191
+ }},
192
+ {{
193
+ "verdict": "yes",
194
+ "reason": "The retrieval context explicitly states that the wall stretches across China's northern borders."
195
+ }},
196
+ {{
197
+ "verdict": "no",
198
+ "reason": "The claim that the wall is 100 meters long contradicts the retrieval context which states it stretches for thousands of miles."
199
+ }}
200
+ ]
201
+ }}
202
+ ---- END OF EXAMPLE 2 ----
203
+ ==== END OF EXAMPLES ====
204
+
205
+ ==== YOUR TURN ====
206
+ Retrieval Contexts:
207
+ {retrieval_context}
208
+
209
+ Claims:
210
+ {claims}
211
+
212
+ JSON:
213
+ """
214
+
215
+ @staticmethod
216
+ def justify_reason(score, contradictions):
217
+ return f"""==== TASK INSTRUCTIONS ====
218
+ You will be provided with a list of contradictions and a faithfulness score.
219
+ The list of contradictions will be references to statements made by a RAG generator that contradicted one or more document(s) from the retrieval context.
220
+ - To clarify, the LLM produced an `actual output` that contained claims that contradicted the `retrieval context` used to produce the output.
221
+ The faithfulness score is a 0 - 1 float (1 is highest) indicating how factually consistent the RAG generator's output is to the retrieval context.
222
+
223
+ Your task is to CLEARLY and CONCISELY summarize the contradictions to justify the score.
224
+ If there are no contradictions, just say something positive with an upbeat encouraging tone (but don't overdo it otherwise it gets annoying).
225
+ Your reason MUST use information from the contradictions in your reason.
226
+
227
+
228
+ ==== FORMATTING YOUR ANSWER ====
229
+ Please make sure to only return in JSON format, with the 'reason' key providing the reason.
230
+
231
+ Example JSON:
232
+ {{
233
+ "reason": "The score is <faithfulness_score> because <your_reason>."
234
+ }}
235
+
236
+ ==== EXAMPLE ====
237
+ Example Contradictions:
238
+ [
239
+ {{
240
+ "verdict": "no",
241
+ "reason": "The output claims Marie Curie was born in Russia, but the context clearly states she was born in Warsaw, Poland."
242
+ }},
243
+ {{
244
+ "verdict": "no",
245
+ "reason": "The output states Marie Curie discovered uranium, but the context indicates she discovered radium and polonium."
246
+ }}
247
+ ]
248
+
249
+ Example Faithfulness Score:
250
+ 0.60
251
+
252
+ Example Response:
253
+ {{
254
+ "reason": "The score is 0.60 because the output made two significant factual errors: incorrectly stating Marie Curie was born in Russia instead of Poland, and wrongly attributing the discovery of uranium to her instead of radium and polonium."
255
+ }}
256
+
257
+ ==== YOUR TURN ====
258
+ Faithfulness Score:
259
+ {score}
260
+
261
+ Contradictions:
262
+ {contradictions}
263
+
264
+ JSON:
265
+ """
@@ -0,0 +1,3 @@
1
+ from judgeval.scorers.judgeval_scorers.local_implementations.hallucination.hallucination_scorer import HallucinationScorer
2
+
3
+ __all__ = ["HallucinationScorer"]