-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_metric.py
33 lines (24 loc) · 1.22 KB
/
response_metric.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
import dspy
gpt4T = dspy.OpenAI(model='gpt-4-1106-preview', max_tokens=1000, model_type='chat')
class MessageResponseAssess(dspy.Signature):
"""Assess the quality of a response along the specified dimension."""
chat_input = dspy.InputField()
assessment_dimension = dspy.InputField() # user state
example_response = dspy.InputField()
ai_response_label = dspy.OutputField(desc="yes or no")
def metric(example, pred, trace=None):
"""Assess the quality of a response along the specified dimension."""
chat_input = example.chat_input
assessment_dimension = f"The user is in the following state: {example.assessment_dimension}. Is the AI response appropriate for this state? Respond with Yes or No."
example_response = pred.response
with dspy.context(lm=gpt4T):
assessment_result = dspy.Predict(MessageResponseAssess)(
chat_input=chat_input,
assessment_dimension=assessment_dimension,
example_response=example_response
)
is_appropriate = assessment_result.ai_response_label.lower() == 'yes'
print("======== OPTIMIZER HISTORY ========")
gpt4T.inspect_history(n=5)
print("======== END OPTIMIZER HISTORY ========")
return is_appropriate