judgeval 0.0.32__py3-none-any.whl → 0.0.33__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 (48) hide show
  1. judgeval/common/s3_storage.py +93 -0
  2. judgeval/common/tracer.py +612 -123
  3. judgeval/data/sequence.py +4 -10
  4. judgeval/judgment_client.py +25 -86
  5. judgeval/rules.py +4 -7
  6. judgeval/run_evaluation.py +1 -1
  7. judgeval/scorers/__init__.py +4 -4
  8. judgeval/scorers/judgeval_scorers/__init__.py +0 -176
  9. {judgeval-0.0.32.dist-info → judgeval-0.0.33.dist-info}/METADATA +15 -2
  10. judgeval-0.0.33.dist-info/RECORD +63 -0
  11. judgeval/scorers/base_scorer.py +0 -58
  12. judgeval/scorers/judgeval_scorers/local_implementations/__init__.py +0 -27
  13. judgeval/scorers/judgeval_scorers/local_implementations/answer_correctness/__init__.py +0 -4
  14. judgeval/scorers/judgeval_scorers/local_implementations/answer_correctness/answer_correctness_scorer.py +0 -276
  15. judgeval/scorers/judgeval_scorers/local_implementations/answer_correctness/prompts.py +0 -169
  16. judgeval/scorers/judgeval_scorers/local_implementations/answer_relevancy/__init__.py +0 -4
  17. judgeval/scorers/judgeval_scorers/local_implementations/answer_relevancy/answer_relevancy_scorer.py +0 -298
  18. judgeval/scorers/judgeval_scorers/local_implementations/answer_relevancy/prompts.py +0 -174
  19. judgeval/scorers/judgeval_scorers/local_implementations/comparison/__init__.py +0 -0
  20. judgeval/scorers/judgeval_scorers/local_implementations/comparison/comparison_scorer.py +0 -161
  21. judgeval/scorers/judgeval_scorers/local_implementations/comparison/prompts.py +0 -222
  22. judgeval/scorers/judgeval_scorers/local_implementations/contextual_precision/__init__.py +0 -3
  23. judgeval/scorers/judgeval_scorers/local_implementations/contextual_precision/contextual_precision_scorer.py +0 -264
  24. judgeval/scorers/judgeval_scorers/local_implementations/contextual_precision/prompts.py +0 -106
  25. judgeval/scorers/judgeval_scorers/local_implementations/contextual_recall/__init__.py +0 -3
  26. judgeval/scorers/judgeval_scorers/local_implementations/contextual_recall/contextual_recall_scorer.py +0 -254
  27. judgeval/scorers/judgeval_scorers/local_implementations/contextual_recall/prompts.py +0 -142
  28. judgeval/scorers/judgeval_scorers/local_implementations/contextual_relevancy/__init__.py +0 -3
  29. judgeval/scorers/judgeval_scorers/local_implementations/contextual_relevancy/contextual_relevancy_scorer.py +0 -245
  30. judgeval/scorers/judgeval_scorers/local_implementations/contextual_relevancy/prompts.py +0 -121
  31. judgeval/scorers/judgeval_scorers/local_implementations/execution_order/__init__.py +0 -3
  32. judgeval/scorers/judgeval_scorers/local_implementations/execution_order/execution_order.py +0 -156
  33. judgeval/scorers/judgeval_scorers/local_implementations/faithfulness/__init__.py +0 -3
  34. judgeval/scorers/judgeval_scorers/local_implementations/faithfulness/faithfulness_scorer.py +0 -318
  35. judgeval/scorers/judgeval_scorers/local_implementations/faithfulness/prompts.py +0 -268
  36. judgeval/scorers/judgeval_scorers/local_implementations/hallucination/__init__.py +0 -3
  37. judgeval/scorers/judgeval_scorers/local_implementations/hallucination/hallucination_scorer.py +0 -264
  38. judgeval/scorers/judgeval_scorers/local_implementations/hallucination/prompts.py +0 -104
  39. judgeval/scorers/judgeval_scorers/local_implementations/instruction_adherence/instruction_adherence.py +0 -232
  40. judgeval/scorers/judgeval_scorers/local_implementations/instruction_adherence/prompt.py +0 -102
  41. judgeval/scorers/judgeval_scorers/local_implementations/json_correctness/__init__.py +0 -5
  42. judgeval/scorers/judgeval_scorers/local_implementations/json_correctness/json_correctness_scorer.py +0 -134
  43. judgeval/scorers/judgeval_scorers/local_implementations/summarization/__init__.py +0 -3
  44. judgeval/scorers/judgeval_scorers/local_implementations/summarization/prompts.py +0 -247
  45. judgeval/scorers/judgeval_scorers/local_implementations/summarization/summarization_scorer.py +0 -551
  46. judgeval-0.0.32.dist-info/RECORD +0 -97
  47. {judgeval-0.0.32.dist-info → judgeval-0.0.33.dist-info}/WHEEL +0 -0
  48. {judgeval-0.0.32.dist-info → judgeval-0.0.33.dist-info}/licenses/LICENSE.md +0 -0
@@ -1,27 +0,0 @@
1
- from judgeval.scorers.judgeval_scorers.local_implementations.answer_relevancy.answer_relevancy_scorer import AnswerRelevancyScorer
2
- from judgeval.scorers.judgeval_scorers.local_implementations.contextual_precision.contextual_precision_scorer import ContextualPrecisionScorer
3
- from judgeval.scorers.judgeval_scorers.local_implementations.contextual_recall.contextual_recall_scorer import ContextualRecallScorer
4
- from judgeval.scorers.judgeval_scorers.local_implementations.contextual_relevancy.contextual_relevancy_scorer import ContextualRelevancyScorer
5
- from judgeval.scorers.judgeval_scorers.local_implementations.faithfulness.faithfulness_scorer import FaithfulnessScorer
6
- from judgeval.scorers.judgeval_scorers.local_implementations.json_correctness.json_correctness_scorer import JsonCorrectnessScorer
7
- from judgeval.scorers.judgeval_scorers.local_implementations.execution_order.execution_order import ExecutionOrderScorer
8
- from judgeval.scorers.judgeval_scorers.local_implementations.hallucination.hallucination_scorer import HallucinationScorer
9
- from judgeval.scorers.judgeval_scorers.local_implementations.summarization.summarization_scorer import SummarizationScorer
10
- from judgeval.scorers.judgeval_scorers.local_implementations.answer_correctness.answer_correctness_scorer import AnswerCorrectnessScorer
11
- from judgeval.scorers.judgeval_scorers.local_implementations.comparison.comparison_scorer import ComparisonScorer
12
- from judgeval.scorers.judgeval_scorers.local_implementations.instruction_adherence.instruction_adherence import InstructionAdherenceScorer
13
-
14
- __all__ = [
15
- "AnswerCorrectnessScorer",
16
- "AnswerRelevancyScorer",
17
- "ComparisonScorer",
18
- "ContextualPrecisionScorer",
19
- "ContextualRecallScorer",
20
- "ContextualRelevancyScorer",
21
- "FaithfulnessScorer",
22
- "JsonCorrectnessScorer",
23
- "ExecutionOrderScorer",
24
- "HallucinationScorer",
25
- "SummarizationScorer",
26
- "InstructionAdherenceScorer",
27
- ]
@@ -1,4 +0,0 @@
1
- from judgeval.scorers.judgeval_scorers.local_implementations.answer_correctness.answer_correctness_scorer import AnswerCorrectnessScorer
2
-
3
-
4
- __all__ = ["AnswerCorrectnessScorer"]
@@ -1,276 +0,0 @@
1
- from typing import Optional, List, Union, Tuple
2
-
3
- from judgeval.constants import APIScorer
4
- from judgeval.judges import JudgevalJudge
5
- from judgeval.judges.utils import create_judge
6
- from judgeval.data import Example, ExampleParams
7
- from judgeval.scorers import JudgevalScorer
8
- from judgeval.scorers.utils import (
9
- get_or_create_event_loop,
10
- parse_response_json,
11
- scorer_progress_meter,
12
- create_verbose_logs,
13
- check_example_params,
14
- )
15
- from .prompts import (
16
- ACVerdict,
17
- AnswerCorrectnessTemplate,
18
- Statements,
19
- Verdicts,
20
- Reason,
21
- )
22
-
23
-
24
- required_params = [
25
- ExampleParams.INPUT,
26
- ExampleParams.ACTUAL_OUTPUT,
27
- ExampleParams.EXPECTED_OUTPUT,
28
- ]
29
-
30
-
31
- class AnswerCorrectnessScorer(JudgevalScorer):
32
- def __init__(
33
- self,
34
- threshold: float = 0.5,
35
- model: Optional[Union[str, JudgevalJudge]] = None,
36
- include_reason: bool = True,
37
- async_mode: bool = True,
38
- strict_mode: bool = False,
39
- verbose_mode: bool = False
40
- ):
41
- super().__init__(
42
- score_type=APIScorer.ANSWER_CORRECTNESS,
43
- threshold=1 if strict_mode else threshold,
44
- evaluation_model=None,
45
- include_reason=include_reason,
46
- async_mode=async_mode,
47
- strict_mode=strict_mode,
48
- verbose_mode=verbose_mode
49
- )
50
- self.model, self.using_native_model = create_judge(model)
51
- self.evaluation_model = self.model.get_model_name()
52
-
53
- async def _a_get_statements(self, expected_output: str) -> List[str]:
54
- prompt = AnswerCorrectnessTemplate.deduce_statements(
55
- expected_output=expected_output,
56
- )
57
- if self.using_native_model:
58
- res = await self.model.a_generate(prompt)
59
- data = parse_response_json(res, self)
60
- return data["statements"]
61
- else:
62
- try:
63
- res: Statements = await self.model.a_generate(
64
- prompt, schema=Statements
65
- )
66
- return res.statements
67
- except TypeError:
68
- res = await self.model.a_generate(prompt)
69
- data = parse_response_json(res, self)
70
- return data["statements"]
71
-
72
- def _get_statements(self, expected_output: str) -> List[str]:
73
- prompt = AnswerCorrectnessTemplate.deduce_statements(
74
- expected_output=expected_output,
75
- )
76
- if self.using_native_model:
77
- res = self.model.generate(prompt)
78
- data = parse_response_json(res, self)
79
- return data["statements"]
80
- else:
81
- try:
82
- res: Statements = self.model.generate(
83
- prompt, schema=Statements
84
- )
85
- return res.statements
86
- except TypeError:
87
- res = self.model.generate(prompt)
88
- data = parse_response_json(res, self)
89
- return data["statements"]
90
-
91
- async def _a_get_verdicts(self, actual_output: str) -> List[ACVerdict]:
92
- if len(self.statements) == 0:
93
- return []
94
-
95
- prompt = AnswerCorrectnessTemplate.generate_verdicts(
96
- actual_output=actual_output,
97
- statements=self.statements,
98
- )
99
-
100
- if self.using_native_model:
101
- res = await self.model.a_generate(prompt)
102
- data = parse_response_json(res, self)
103
- return [ACVerdict(**item) for item in data["verdicts"]]
104
- else:
105
- try:
106
- res: Verdicts = await self.model.a_generate(prompt, schema=Verdicts)
107
- return [item for item in res.verdicts]
108
- except TypeError:
109
- res = await self.model.a_generate(prompt)
110
- data = parse_response_json(res, self)
111
- return [ACVerdict(**item) for item in data["verdicts"]]
112
-
113
- def _get_verdicts(self, actual_output: str) -> List[ACVerdict]:
114
- if len(self.statements) == 0:
115
- return []
116
-
117
- prompt = AnswerCorrectnessTemplate.generate_verdicts(
118
- actual_output=actual_output,
119
- statements=self.statements,
120
- )
121
-
122
- if self.using_native_model:
123
- res = self.model.generate(prompt)
124
- data = parse_response_json(res, self)
125
- return [ACVerdict(**item) for item in data["verdicts"]]
126
- else:
127
- try:
128
- res: Verdicts = self.model.generate(prompt, schema=Verdicts)
129
- return [item for item in res.verdicts]
130
- except TypeError:
131
- res = self.model.generate(prompt)
132
- data = parse_response_json(res, self)
133
- return [ACVerdict(**item) for item in data["verdicts"]]
134
-
135
- async def _a_get_reason(self) -> str:
136
- if self.include_reason is False:
137
- return None
138
-
139
- incorrect_statements: List[Tuple[str, str]] = []
140
- for idx, verdict in enumerate(self.verdicts):
141
- if verdict.verdict.strip().lower() == "no":
142
- incorrect_statements.append((self.statements[idx], verdict.reason))
143
-
144
- prompt = AnswerCorrectnessTemplate.generate_reason(
145
- incorrect_statements=incorrect_statements,
146
- score=format(self.score, ".2f"),
147
- )
148
- if self.using_native_model:
149
- res = await self.model.a_generate(prompt)
150
- data = parse_response_json(res, self)
151
- return data["reason"]
152
- else:
153
- try:
154
- res: Reason = await self.model.a_generate(
155
- prompt=prompt, schema=Reason
156
- )
157
- return res.reason
158
- except TypeError:
159
- res = await self.model.a_generate(prompt)
160
- data = parse_response_json(res, self)
161
- return data["reason"]
162
-
163
- def _get_reason(self) -> str:
164
- if self.include_reason is False:
165
- return None
166
-
167
- incorrect_statements: List[Tuple[str, str]] = []
168
- for idx, verdict in enumerate(self.verdicts):
169
- if verdict.verdict.strip().lower() == "no":
170
- incorrect_statements.append((self.statements[idx], verdict.reason))
171
-
172
- prompt = AnswerCorrectnessTemplate.generate_reason(
173
- incorrect_statements=incorrect_statements,
174
- score=format(self.score, ".2f"),
175
- )
176
- if self.using_native_model:
177
- res = self.model.generate(prompt)
178
- data = parse_response_json(res, self)
179
- return data["reason"]
180
- else:
181
- try:
182
- res: Reason = self.model.generate(
183
- prompt=prompt, schema=Reason
184
- )
185
- return res.reason
186
- except TypeError:
187
- res = self.model.generate(prompt)
188
- data = parse_response_json(res, self)
189
- return data["reason"]
190
-
191
- def _compute_score(self) -> float:
192
- number_of_verdicts = len(self.verdicts)
193
- if number_of_verdicts == 0:
194
- return 1
195
-
196
- correct_count = 0
197
- for verdict in self.verdicts:
198
- if verdict.verdict.strip().lower() == "yes":
199
- correct_count += 1
200
-
201
- score = correct_count / number_of_verdicts
202
- return 0 if self.strict_mode and score < self.threshold else score
203
-
204
- def score_example(
205
- self,
206
- example: Example,
207
- _show_indicator: bool = True,
208
- ) -> float:
209
- check_example_params(example, required_params, self)
210
-
211
- with scorer_progress_meter(self, display_meter=_show_indicator):
212
- try:
213
- if self.async_mode:
214
- loop = get_or_create_event_loop()
215
- loop.run_until_complete(
216
- self.a_score_example(example, _show_indicator=False)
217
- )
218
- else:
219
- self.statements = self._get_statements(example.expected_output)
220
- self.verdicts = self._get_verdicts(example.actual_output)
221
- self.score = self._compute_score()
222
- self.reason = self._get_reason()
223
- self.success = self.score >= self.threshold
224
- self.verbose_logs = create_verbose_logs(
225
- self,
226
- steps=[
227
- f"Statements:\n{self.statements}",
228
- f"Verdicts:\n{[v.model_dump() for v in self.verdicts]}",
229
- f"Score: {self.score}\nReason: {self.reason}",
230
- ],
231
- )
232
- return self.score
233
- except Exception as e:
234
- print(f"Error in score_example for AnswerCorrectnessScorer: {e}")
235
- raise
236
-
237
- async def a_score_example(
238
- self,
239
- example: Example,
240
- _show_indicator: bool = True,
241
- ) -> float:
242
- check_example_params(example, required_params, self)
243
-
244
- with scorer_progress_meter(self, async_mode=True, display_meter=_show_indicator):
245
- try:
246
- self.statements: List[str] = await self._a_get_statements(example.expected_output)
247
- self.verdicts: List[ACVerdict] = await self._a_get_verdicts(example.actual_output)
248
- self.score = self._compute_score()
249
- self.reason = await self._a_get_reason()
250
- self.success = self.score >= self.threshold
251
- self.verbose_logs = create_verbose_logs(
252
- self,
253
- steps=[
254
- f"Statements:\n{self.statements}",
255
- f"Verdicts:\n{[v.model_dump() for v in self.verdicts]}",
256
- f"Score: {self.score}\nReason: {self.reason}",
257
- ],
258
- )
259
- return self.score
260
- except Exception as e:
261
- print(f"Error in a_score_example for AnswerCorrectnessScorer: {e}")
262
- raise
263
-
264
- def _success_check(self) -> bool:
265
- if self.error is not None:
266
- self.success = False
267
- else:
268
- try:
269
- self.success = self.score >= self.threshold
270
- except:
271
- self.success = False
272
- return self.success
273
-
274
- @property
275
- def __name__(self):
276
- return "Answer Correctness"
@@ -1,169 +0,0 @@
1
- """
2
- Util prompts for AnswerCorrectnessScorer
3
- """
4
-
5
- from typing import List, Tuple
6
- from pydantic import BaseModel
7
-
8
-
9
- # BaseModels to enforce formatting in LLM JSON response
10
- class Statements(BaseModel):
11
- statements: List[str]
12
-
13
-
14
- class ACVerdict(BaseModel):
15
- verdict: str
16
- reason: str
17
-
18
-
19
- class Verdicts(BaseModel):
20
- verdicts: List[ACVerdict]
21
-
22
-
23
- class Reason(BaseModel):
24
- reason: str
25
-
26
-
27
- class AnswerCorrectnessTemplate:
28
- @staticmethod
29
- def deduce_statements(expected_output):
30
- return f"""You will be presented with a piece of text. Your task is to break down the text and generate a list of statements contained within the text. Single words and ambiguous phrases should be considered statements.
31
-
32
- ===== START OF EXAMPLES =====
33
- Example 1:
34
- Example text: The weather is sunny today. Temperature is 75 degrees. Don't forget your sunscreen!
35
-
36
- Output:
37
- {{
38
- "statements": ["The weather is sunny today", "Temperature is 75 degrees", "Don't forget your sunscreen!"]
39
- }}
40
-
41
- Example 2:
42
- Example text: I love pizza. It has cheese and tomato sauce and the crust is crispy.
43
-
44
- Output:
45
- {{
46
- "statements": ["I love pizza", "It has cheese and tomato sauce", "The crust is crispy"]
47
- }}
48
- ===== END OF EXAMPLES =====
49
-
50
-
51
- **
52
- IMPORTANT: Please return your answer in valid JSON format, with the "statements" key mapping to a list of strings. No words or explanation is needed.
53
- **
54
-
55
- ==== START OF INPUT ====
56
- Text:
57
- {expected_output}
58
- ==== END OF INPUT ====
59
-
60
- ==== YOUR ANSWER ====
61
- JSON:
62
- """
63
-
64
- @staticmethod
65
- def generate_verdicts(statements, actual_output):
66
- return f"""You will be provided with:\n
67
- - a list of statements from a text that we will refer to as expected output
68
- - a text that we will refer to as actual output\n
69
-
70
- Your task is to determine whether each statement from the expected output is correct/consistent with the actual output text.
71
- More specifically, you should generate a JSON object with the key "verdicts". "verdicts" will map to a list of nested JSON objects with two keys: `verdict` and `reason`.
72
- The "reason" key should provide an explanation for your choice, regardless of which verdict you select. Try providing quotes from the text(s) to justify your answer where possible.
73
- The "verdict" key be EXACTLY EITHER "yes" or "no". You should select "yes" if the statement is correct/consistent based on the actual output and "no" otherwise.
74
-
75
- ==== OUTPUT FORMATTING ====
76
- IMPORTANT: Please make sure to only return in JSON format, with the "verdicts" key mapping to a list of JSON objects. Each JSON object should contain keys "verdict" (one of "yes" or "no") and "reason" (str).
77
-
78
- ==== START OF EXAMPLES ====
79
- Example input 1: What's the capital of France?
80
- Example expected output statements 1: ["Paris is the capital of France", "It is located in northern France", "The city has a population of over 2 million"]
81
- Example actual output 1: "Paris is the capital city of France. It is situated in the northern part of the country and has over 2 million residents."
82
- Example JSON 1:
83
- {{
84
- "verdicts": [
85
- {{
86
- "verdict": "yes",
87
- "reason": "The actual output directly states 'Paris is the capital city of France', which matches the statement"
88
- }},
89
- {{
90
- "verdict": "yes",
91
- "reason": "The actual output confirms this by saying it is 'situated in the northern part of the country'"
92
- }},
93
- {{
94
- "verdict": "yes",
95
- "reason": "The actual output mentions the city 'has over 2 million residents', matching the population statement"
96
- }}
97
- ]
98
- }}
99
-
100
- Example input 2: What is the largest planet in our solar system?
101
- Example expected output statements 2: ["Jupiter is the largest planet", "It is a gas giant", "Jupiter has 79 known moons", "The Great Red Spot is a storm on Jupiter"]
102
- Example actual output 2: "Jupiter is the biggest planet in the solar system. It is made mostly of gas. The planet has many moons orbiting it."
103
- Example JSON 2:
104
- {{
105
- "verdicts": [
106
- {{
107
- "verdict": "yes",
108
- "reason": "The actual output confirms 'Jupiter is the biggest planet', which is equivalent to it being the largest"
109
- }},
110
- {{
111
- "verdict": "yes",
112
- "reason": "The actual output states it is 'made mostly of gas', indicating it is a gas giant"
113
- }},
114
- {{
115
- "verdict": "no",
116
- "reason": "While the actual output mentions Jupiter has 'many moons', it does not specify the exact number of 79 known moons"
117
- }},
118
- {{
119
- "verdict": "no",
120
- "reason": "The actual output makes no mention of the Great Red Spot or any storms on Jupiter"
121
- }}
122
- ]
123
- }}
124
- ==== END OF EXAMPLES ====
125
-
126
- ** LASTLY **
127
- Since you are tasked to choose a verdict for each statement, the number of "verdicts" SHOULD BE EXACTLY EQUAL to the number of "statements".
128
-
129
-
130
- ==== YOUR TURN =====
131
-
132
- Statements:
133
- {statements}
134
-
135
- Actual output:
136
- {actual_output}
137
-
138
- JSON:
139
- """
140
-
141
- @staticmethod
142
- def generate_reason(incorrect_statements: List[Tuple[str, str]], score: float):
143
- incorrect_statements = "\n".join([f"statement: {statement}\nreason: {reason}\n------" for statement, reason in incorrect_statements])
144
- return f"""==== TASK INSTRUCTIONS ====\nYou will provided with two inputs: an answer correctness score and a list of inconsistent/incorrect statements made in a model's output (with the reason why it's irrelevant). Your task is to provide a CLEAR and CONCISE reason for the answer correctness score.
145
- For context, there were a list of statements generated from an expected output. The model's actual output was then compared to the expected output, and we collected a list of claims made in the expected output that were either incorrect or inconsistent with the actual output.
146
- The score represents how well the model's output matches the expected output.
147
- You should explain why the score is not higher, but also include why its current score is fair.
148
- The incorrect statements represent parts of the model output that are incorrect or inconsistent with the expected output. The incorrect statement will be paired with the reason why it's incorrect.
149
- If there are no incorrect statements, instead respond with a positive remark with an upbeat encouraging tone (but don't overblow the kind attitude).
150
-
151
-
152
- ==== FORMATTING YOUR ANSWER ====
153
- IMPORTANT: Please make sure to only return in JSON format, with the 'reason' key providing the reason.
154
- Example JSON:
155
- {{
156
- "reason": "The score is <answer_relevancy_score> because <your_reason>."
157
- }}
158
-
159
- ==== YOUR TURN ====
160
- ---- ANSWER CORRECTNESS SCORE ----
161
- {score}
162
-
163
- ---- INCORRECT STATEMENTS ----
164
- {incorrect_statements}
165
-
166
- ---- YOUR RESPONSE ----
167
- JSON:
168
- """
169
-
@@ -1,4 +0,0 @@
1
- from judgeval.scorers.judgeval_scorers.local_implementations.answer_relevancy.answer_relevancy_scorer import AnswerRelevancyScorer
2
-
3
-
4
- __all__ = ["AnswerRelevancyScorer"]