/
evaluators.py
180 lines (161 loc) Β· 5.42 KB
/
evaluators.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
from langchain.smith.evaluation.config import RunEvalConfig, SingleKeyEvalConfig
from langsmith.evaluation.evaluator import (
EvaluationResult,
run_evaluator,
)
from langsmith.schemas import Example, Run
from langchain_benchmarks.extraction.tasks.chat_extraction.schema import GenerateTicket
@run_evaluator
def json_schema(run: Run, example: Example) -> EvaluationResult:
"""Evaluate the json schema of the generated ticket."""
score, comment = None, None
try:
GenerateTicket.parse_obj(run.outputs["output"])
score = 1
except Exception as e:
comment = repr(e)
score = 0
return EvaluationResult(
key="json_schema",
score=score,
comment=comment,
)
@run_evaluator
def evaluate_toxicity_similarity(run: Run, example: Example) -> EvaluationResult:
"""Evaluate the toxicity of the generated ticket."""
gt = example.outputs["output"]["question"]["toxicity"]
score, comment = None, None
# Toxicity should be a on scale from 0 to 5
try:
pred = run.outputs["output"]["question"]["toxicity"]
score = 1 - abs(gt - float(pred)) / 5
except Exception as e:
comment = repr(e)
# Forgot to predict / mis-structured
score = 0
return EvaluationResult(
key="toxicity_similarity",
score=score,
comment=comment,
)
@run_evaluator
def evaluate_sentiment_similarity(run: Run, example: Example) -> EvaluationResult:
"""Evaluate the sentiment of the generated ticket."""
gt = example.outputs["output"]["question"]["sentiment"]
ordinal_map = {
"negative": 0,
"neutral": 1,
"positive": 2,
}
gt_score = ordinal_map.get(str(gt).lower())
score, comment = None, None
# Sentiment is an enum, "Negative", "Neutral", "Positive"
try:
pred = run.outputs["output"]["question"]["sentiment"]
pred_score = ordinal_map.get(str(pred).lower())
score = 1 - (abs(gt_score - float(pred_score)) / 2)
except Exception as e:
comment = repr(e)
# Forgot to predict / mis-structured
score = 0
return EvaluationResult(
key="sentiment_similarity",
score=score,
comment=comment,
)
@run_evaluator
def evaluate_confidence_level_similarity(
run: Run, example: Example
) -> EvaluationResult:
"""Evaluate the confidence level of the generated ticket.
This is a binary T/F question."""
gt = example.outputs["output"]["response"]["confidence_level"]
score, comment = None, None
try:
pred = run.outputs["output"]["response"]["confidence_level"]
score = 1 - (abs(gt - float(pred)) / 5)
except Exception as e:
comment = repr(e)
score = 0
return EvaluationResult(
key="confidence_level_similarity",
score=score,
comment=comment,
)
@run_evaluator
def evaluate_question_category_similarity(
run: Run, example: Example
) -> EvaluationResult:
"""Evaluate the question category of the generated ticket.
This is a binary T/F question."""
gt = example.outputs["output"]["question"]["question_category"]
score, comment = None, None
try:
pred = run.outputs["output"]["question"]["question_category"]
score = int(gt == pred)
except Exception as e:
comment = repr(e)
# Forgot to predict / mis-structured
score = 0
return EvaluationResult(
key="question_category",
score=score,
comment=comment,
)
@run_evaluator
def evaluate_off_topic(run: Run, example: Example) -> EvaluationResult:
"""Evaluate the off topic of the generated ticket.
This is a binary T/F question."""
gt = example.outputs["output"]["question"]["is_off_topic"]
score, comment = None, None
try:
pred = run.outputs["output"]["question"].get("is_off_topic")
score = int(gt == pred)
except Exception as e:
comment = repr(e)
# Forgot to predict / mis-structured
score = 0
return EvaluationResult(
key="off_topic_similarity",
score=score,
comment=comment,
)
@run_evaluator
def evaluate_programming_language(run: Run, example: Example) -> EvaluationResult:
"""Evaluate the programming language of the generated ticket.
This is a binary T/F question."""
gt = example.outputs["output"]["question"]["programming_language"]
score, comment = None, None
try:
pred = run.outputs["output"]["question"]["programming_language"]
score = int(gt == pred)
except Exception as e:
comment = repr(e)
# Forgot to predict / mis-structured
score = 0
return EvaluationResult(
key="programming_language_similarity",
score=score,
comment=comment,
)
def get_eval_config() -> RunEvalConfig:
"""Get the evaluation configuration for the chat extraction task."""
return RunEvalConfig(
evaluators=[
# General aggregate score
SingleKeyEvalConfig(
# input key is ignored.
evaluator_type="json_edit_distance",
input_key="question",
)
],
custom_evaluators=[
json_schema,
evaluate_toxicity_similarity,
evaluate_sentiment_similarity,
evaluate_confidence_level_similarity,
evaluate_question_category_similarity,
evaluate_off_topic,
evaluate_programming_language,
],
)