-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
246 lines (202 loc) · 7.69 KB
/
Copy patheval.py
File metadata and controls
246 lines (202 loc) · 7.69 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langsmith.evaluation import evaluate, LangChainStringEvaluator
from langsmith.schemas import Run, Example
from openai import OpenAI
import json
import time
from dotenv import load_dotenv
load_dotenv()
from langsmith.wrappers import wrap_openai
from langsmith import traceable
client = wrap_openai(OpenAI())
model_name = "gpt-3.5-turbo"
# todo: clean up evaluators with helper functions to DRY code
@traceable
def conciseness_evaluator(run: Run, example: Example)-> dict:
# schema for chat examples
inputs = example.inputs['input']
outputs = example.outputs['output']
# Extract system prompt
system_prompt = next((msg['data']['content'] for msg in inputs if msg['type'] == 'system'), "")
# Extract message history
message_history = []
for msg in inputs:
if msg['type'] in ['human', 'ai']:
message_history.append({
"role": "user" if msg['type'] == 'human' else "assistant",
"content": msg['data']['content']
})
# Extract latest user message and model output
latest_message = message_history[-1]['content'] if message_history else ""
model_output = outputs['data']['content']
# eval prompt
evaluation_prompt = f"""
System Prompt: {system_prompt}
Message History:
{json.dumps(message_history, indent=2)}
Latest User Message: {latest_message}
Model Output: {model_output}
Based on the above information, evaluate the model's output for conciseness of summary of the input.
Provide a score from 0 to 10, where 0 is larger than the input and 10 is perfectly concise.
Also provide a brief explanation for your score.
Respond in the following JSON format:
{{
"score": <int>,
"explanation": "<string>"
}}
"""
# temp prevent rate limit excess
time.sleep(3)
response = client.chat.completions.create(
model=model_name,
messages=[
{"role": "system", "content": "You are an AI assistant tasked with evaluating the compliance of model outputs to given prompts and conversation context."},
{"role": "user", "content": evaluation_prompt}
],
temperature=0.2
)
try:
result = json.loads(response.choices[0].message.content)
return {
"key": "conciseness_score",
"score": result["score"] / 10, # Normalize to 0-1 range
"reason": result["explanation"]
}
except json.JSONDecodeError:
return {
"key": "conciseness_score",
"score": 0,
"reason": "Failed to parse evaluator response"
}
@traceable
def content_comparison(run: Run, example: Example) -> dict:
# schema for chat examples
inputs = example.inputs['input']
outputs = example.outputs['output']
# Extract system prompt
system_prompt = next((msg['data']['content'] for msg in inputs if msg['type'] == 'system'), "")
# Extract message history
message_history = []
for msg in inputs:
if msg['type'] in ['human', 'ai']:
message_history.append({
"role": "user" if msg['type'] == 'human' else "assistant",
"content": msg['data']['content']
})
# Extract latest user message and model output
latest_message = message_history[-1]['content'] if message_history else ""
model_output = outputs['data']['content']
# eval prompt
evaluation_prompt = f"""
System Prompt: {system_prompt}
Message History:
{json.dumps(message_history, indent=2)}
Latest User Message: {latest_message}
Model Output: {model_output}
Based on the above information, evaluate the model's output for similarity of content compared to the input.
Provide a score from 0 to 10, where 0 is completely different content and 10 means the output contains all the main points of the input.
Also provide a brief explanation for your score.
Respond in the following JSON format:
{{
"score": <int>,
"explanation": "<string>"
}}
"""
# temp prevent rate limit excess
time.sleep(3)
response = client.chat.completions.create(
model=model_name,
messages=[
{"role": "system", "content": "You are an AI assistant tasked with evaluating the compliance of model outputs to given prompts and conversation context."},
{"role": "user", "content": evaluation_prompt}
],
temperature=0.2
)
try:
result = json.loads(response.choices[0].message.content)
return {
"key": "comparison_score",
"score": result["score"] / 10, # Normalize to 0-1 range
"reason": result["explanation"]
}
except json.JSONDecodeError:
return {
"key": "comparison_score",
"score": 0,
"reason": "Failed to parse evaluator response"
}
@traceable
def prompt_compliance_evaluator(run: Run, example: Example) -> dict:
# schema for chat examples
inputs = example.inputs['input']
outputs = example.outputs['output']
# Extract system prompt
system_prompt = next((msg['data']['content'] for msg in inputs if msg['type'] == 'system'), "")
# Extract message history
message_history = []
for msg in inputs:
if msg['type'] in ['human', 'ai']:
message_history.append({
"role": "user" if msg['type'] == 'human' else "assistant",
"content": msg['data']['content']
})
# Extract latest user message and model output
latest_message = message_history[-1]['content'] if message_history else ""
model_output = outputs['data']['content']
evaluation_prompt = f"""
System Prompt: {system_prompt}
Message History:
{json.dumps(message_history, indent=2)}
Latest User Message: {latest_message}
Model Output: {model_output}
Based on the above information, evaluate the model's output for compliance with the system prompt and context of the conversation.
Provide a score from 0 to 10, where 0 is completely non-compliant and 10 is perfectly compliant.
Also provide a brief explanation for your score.
Respond in the following JSON format:
{{
"score": <int>,
"explanation": "<string>"
}}
"""
# temp prevent rate limit excess
time.sleep(3)
response = client.chat.completions.create(
model=model_name,
messages=[
{"role": "system", "content": "You are an AI assistant tasked with evaluating the compliance of model outputs to given prompts and conversation context."},
{"role": "user", "content": evaluation_prompt}
],
temperature=0.2
)
try:
result = json.loads(response.choices[0].message.content)
return {
"key": "prompt_compliance",
"score": result["score"] / 10, # Normalize to 0-1 range
"reason": result["explanation"]
}
except json.JSONDecodeError:
return {
"key": "prompt_compliance",
"score": 0,
"reason": "Failed to parse evaluator response"
}
# The name or UUID of the LangSmith dataset to evaluate on.
data = "DocumentSummarizer Homework1"
# A string to prefix the experiment name with.
experiment_prefix = "summaryEvaluations"
# List of evaluators to score the outputs of target task
evaluators = [
prompt_compliance_evaluator,
conciseness_evaluator,
content_comparison,
]
# Evaluate the target task
results = evaluate(
lambda inputs: inputs,
data=data,
evaluators=evaluators,
experiment_prefix=experiment_prefix,
)